1 /******************************************************************************
2 * $Id: dbfopen.c,v 1.83 2008/11/12 14:28:15 fwarmerdam Exp $
5 * Purpose: Implementation of .dbf access API documented in dbf_api.html.
6 * Author: Frank Warmerdam, warmerdam@pobox.com
8 ******************************************************************************
9 * Copyright (c) 1999, Frank Warmerdam
11 * This software is available under the following "MIT Style" license,
12 * or at the option of the licensee under the LGPL (see LICENSE.LGPL). This
13 * option is discussed in more detail in shapelib.html.
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
24 * The above copyright notice and this permission notice shall be included
25 * in all copies or substantial portions of the Software.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33 * DEALINGS IN THE SOFTWARE.
34 ******************************************************************************
37 * Revision 1.83 2008/11/12 14:28:15 fwarmerdam
38 * DBFCreateField() now works on files with records
40 * Revision 1.82 2008/11/11 17:47:09 fwarmerdam
41 * added DBFDeleteField() function
43 * Revision 1.81 2008/01/03 17:48:13 bram
44 * in DBFCreate, use default code page LDID/87 (= 0x57, ANSI)
45 * instead of LDID/3. This seems to be the same as what ESRI
46 * would be doing by default.
48 * Revision 1.80 2007/12/30 14:36:39 fwarmerdam
49 * avoid syntax issue with last comment.
51 * Revision 1.79 2007/12/30 14:35:48 fwarmerdam
52 * Avoid char* / unsigned char* warnings.
54 * Revision 1.78 2007/12/18 18:28:07 bram
55 * - create hook for client specific atof (bugzilla ticket 1615)
56 * - check for NULL handle before closing cpCPG file, and close after reading.
58 * Revision 1.77 2007/12/15 20:25:21 bram
59 * dbfopen.c now reads the Code Page information from the DBF file, and exports
60 * this information as a string through the DBFGetCodePage function. This is
61 * either the number from the LDID header field ("LDID/<number>") or as the
62 * content of an accompanying .CPG file. When creating a DBF file, the code can
63 * be set using DBFCreateEx.
65 * Revision 1.76 2007/12/12 22:21:32 bram
66 * DBFClose: check for NULL psDBF handle before trying to close it.
68 * Revision 1.75 2007/12/06 13:58:19 fwarmerdam
69 * make sure file offset calculations are done in as SAOffset
71 * Revision 1.74 2007/12/06 07:00:25 fwarmerdam
72 * dbfopen now using SAHooks for fileio
74 * Revision 1.73 2007/09/03 19:48:11 fwarmerdam
75 * move DBFReadAttribute() static dDoubleField into dbfinfo
77 * Revision 1.72 2007/09/03 19:34:06 fwarmerdam
78 * Avoid use of static tuple buffer in DBFReadTuple()
80 * Revision 1.71 2006/06/22 14:37:18 fwarmerdam
81 * avoid memory leak if dbfopen fread fails
83 * Revision 1.70 2006/06/17 17:47:05 fwarmerdam
84 * use calloc() for dbfinfo in DBFCreate
86 * Revision 1.69 2006/06/17 15:34:32 fwarmerdam
87 * disallow creating fields wider than 255
89 * Revision 1.68 2006/06/17 15:12:40 fwarmerdam
90 * Fixed C++ style comments.
92 * Revision 1.67 2006/06/17 00:24:53 fwarmerdam
93 * Don't treat non-zero decimals values as high order byte for length
94 * for strings. It causes serious corruption for some files.
95 * http://bugzilla.remotesensing.org/show_bug.cgi?id=1202
97 * Revision 1.66 2006/03/29 18:26:20 fwarmerdam
98 * fixed bug with size of pachfieldtype in dbfcloneempty
100 * Revision 1.65 2006/02/15 01:14:30 fwarmerdam
101 * added DBFAddNativeFieldType
103 * Revision 1.64 2006/02/09 00:29:04 fwarmerdam
104 * Changed to put spaces into string fields that are NULL as
105 * per http://bugzilla.maptools.org/show_bug.cgi?id=316.
107 * Revision 1.63 2006/01/25 15:35:43 fwarmerdam
108 * check success on DBFFlushRecord
110 * Revision 1.62 2006/01/10 16:28:03 fwarmerdam
111 * Fixed typo in CPLError.
113 * Revision 1.61 2006/01/10 16:26:29 fwarmerdam
114 * Push loading record buffer into DBFLoadRecord.
115 * Implement CPL error reporting if USE_CPL defined.
117 * Revision 1.60 2006/01/05 01:27:27 fwarmerdam
118 * added dbf deletion mark/fetch
120 * Revision 1.59 2005/03/14 15:20:28 fwarmerdam
123 * Revision 1.58 2005/03/14 15:18:54 fwarmerdam
124 * Treat very wide fields with no decimals as double. This is
125 * more than 32bit integer fields.
127 * Revision 1.57 2005/02/10 20:16:54 fwarmerdam
128 * Make the pszStringField buffer for DBFReadAttribute() static char [256]
131 * Revision 1.56 2005/02/10 20:07:56 fwarmerdam
132 * Fixed bug 305 in DBFCloneEmpty() - header length problem.
134 * Revision 1.55 2004/09/26 20:23:46 fwarmerdam
135 * avoid warnings with rcsid and signed/unsigned stuff
137 * Revision 1.54 2004/09/15 16:26:10 fwarmerdam
138 * Treat all blank numeric fields as null too.
141 #include "shapefil.h"
153 /************************************************************************/
156 /* A realloc cover function that will access a NULL pointer as */
158 /************************************************************************/
160 static void * SfRealloc( void * pMem, int nNewSize )
164 return( (void *) malloc(nNewSize) );
166 return( (void *) realloc(pMem,nNewSize) );
169 /************************************************************************/
170 /* DBFWriteHeader() */
172 /* This is called to write out the file header, and field */
173 /* descriptions before writing any actual data records. This */
174 /* also computes all the DBFDataSet field offset/size/decimals */
175 /* and so forth values. */
176 /************************************************************************/
178 static void DBFWriteHeader(DBFHandle psDBF)
181 unsigned char abyHeader[XBASE_FLDHDR_SZ];
184 if( !psDBF->bNoHeader )
187 psDBF->bNoHeader = FALSE;
189 /* -------------------------------------------------------------------- */
190 /* Initialize the file header information. */
191 /* -------------------------------------------------------------------- */
192 for( i = 0; i < XBASE_FLDHDR_SZ; i++ )
195 abyHeader[0] = 0x03; /* memo field? - just copying */
197 /* write out a dummy date */
198 abyHeader[1] = 95; /* YY */
199 abyHeader[2] = 7; /* MM */
200 abyHeader[3] = 26; /* DD */
202 /* record count preset at zero */
204 abyHeader[8] = (unsigned char) (psDBF->nHeaderLength % 256);
205 abyHeader[9] = (unsigned char) (psDBF->nHeaderLength / 256);
207 abyHeader[10] = (unsigned char) (psDBF->nRecordLength % 256);
208 abyHeader[11] = (unsigned char) (psDBF->nRecordLength / 256);
210 abyHeader[29] = (unsigned char) (psDBF->iLanguageDriver);
212 /* -------------------------------------------------------------------- */
213 /* Write the initial 32 byte file header, and all the field */
215 /* -------------------------------------------------------------------- */
216 psDBF->sHooks.FSeek( psDBF->fp, 0, 0 );
217 psDBF->sHooks.FWrite( abyHeader, XBASE_FLDHDR_SZ, 1, psDBF->fp );
218 psDBF->sHooks.FWrite( psDBF->pszHeader, XBASE_FLDHDR_SZ, psDBF->nFields,
221 /* -------------------------------------------------------------------- */
222 /* Write out the newline character if there is room for it. */
223 /* -------------------------------------------------------------------- */
224 if( psDBF->nHeaderLength > 32*psDBF->nFields + 32 )
229 psDBF->sHooks.FWrite( &cNewline, 1, 1, psDBF->fp );
233 /************************************************************************/
234 /* DBFFlushRecord() */
236 /* Write out the current record if there is one. */
237 /************************************************************************/
239 static int DBFFlushRecord( DBFHandle psDBF )
242 SAOffset nRecordOffset;
244 if( psDBF->bCurrentRecordModified && psDBF->nCurrentRecord > -1 )
246 psDBF->bCurrentRecordModified = FALSE;
249 psDBF->nRecordLength * (SAOffset) psDBF->nCurrentRecord
250 + psDBF->nHeaderLength;
252 if( psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 ) != 0
253 || psDBF->sHooks.FWrite( psDBF->pszCurrentRecord,
254 psDBF->nRecordLength,
255 1, psDBF->fp ) != 1 )
258 CPLError( CE_Failure, CPLE_FileIO,
259 "Failure writing DBF record %d.",
260 psDBF->nCurrentRecord );
262 fprintf( stderr, "Failure writing DBF record %d.",
263 psDBF->nCurrentRecord );
272 /************************************************************************/
273 /* DBFLoadRecord() */
274 /************************************************************************/
276 static int DBFLoadRecord( DBFHandle psDBF, int iRecord )
279 if( psDBF->nCurrentRecord != iRecord )
281 SAOffset nRecordOffset;
283 if( !DBFFlushRecord( psDBF ) )
287 psDBF->nRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
289 if( psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, SEEK_SET ) != 0 )
292 CPLError( CE_Failure, CPLE_FileIO,
293 "fseek(%ld) failed on DBF file.\n",
294 (long) nRecordOffset );
296 fprintf( stderr, "fseek(%ld) failed on DBF file.\n",
297 (long) nRecordOffset );
302 if( psDBF->sHooks.FRead( psDBF->pszCurrentRecord,
303 psDBF->nRecordLength, 1, psDBF->fp ) != 1 )
306 CPLError( CE_Failure, CPLE_FileIO,
307 "fread(%d) failed on DBF file.\n",
308 psDBF->nRecordLength );
310 fprintf( stderr, "fread(%d) failed on DBF file.\n",
311 psDBF->nRecordLength );
316 psDBF->nCurrentRecord = iRecord;
322 /************************************************************************/
323 /* DBFUpdateHeader() */
324 /************************************************************************/
327 DBFUpdateHeader( DBFHandle psDBF )
330 unsigned char abyFileHeader[32];
332 if( psDBF->bNoHeader )
333 DBFWriteHeader( psDBF );
335 DBFFlushRecord( psDBF );
337 psDBF->sHooks.FSeek( psDBF->fp, 0, 0 );
338 psDBF->sHooks.FRead( abyFileHeader, 32, 1, psDBF->fp );
340 abyFileHeader[4] = (unsigned char) (psDBF->nRecords % 256);
341 abyFileHeader[5] = (unsigned char) ((psDBF->nRecords/256) % 256);
342 abyFileHeader[6] = (unsigned char) ((psDBF->nRecords/(256*256)) % 256);
343 abyFileHeader[7] = (unsigned char) ((psDBF->nRecords/(256*256*256)) % 256);
345 psDBF->sHooks.FSeek( psDBF->fp, 0, 0 );
346 psDBF->sHooks.FWrite( abyFileHeader, 32, 1, psDBF->fp );
348 psDBF->sHooks.FFlush( psDBF->fp );
351 /************************************************************************/
354 /* Open a .dbf file. */
355 /************************************************************************/
357 DBFHandle SHPAPI_CALL
358 DBFOpen( const char * pszFilename, const char * pszAccess )
363 SASetupDefaultHooks( &sHooks );
365 return DBFOpenLL( pszFilename, pszAccess, &sHooks );
368 /************************************************************************/
371 /* Open a .dbf file. */
372 /************************************************************************/
374 DBFHandle SHPAPI_CALL
375 DBFOpenLL( const char * pszFilename, const char * pszAccess, SAHooks *psHooks )
380 unsigned char *pabyBuf;
381 int nFields, nHeadLen, iField, i;
382 char *pszBasename, *pszFullname;
385 /* -------------------------------------------------------------------- */
386 /* We only allow the access strings "rb" and "r+". */
387 /* -------------------------------------------------------------------- */
388 if( strcmp(pszAccess,"r") != 0 && strcmp(pszAccess,"r+") != 0
389 && strcmp(pszAccess,"rb") != 0 && strcmp(pszAccess,"rb+") != 0
390 && strcmp(pszAccess,"r+b") != 0 )
393 if( strcmp(pszAccess,"r") == 0 )
396 if( strcmp(pszAccess,"r+") == 0 )
399 /* -------------------------------------------------------------------- */
400 /* Compute the base (layer) name. If there is any extension */
401 /* on the passed in filename we will strip it off. */
402 /* -------------------------------------------------------------------- */
403 pszBasename = (char *) malloc(strlen(pszFilename)+5);
404 strcpy( pszBasename, pszFilename );
405 for( i = strlen(pszBasename)-1;
406 i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
407 && pszBasename[i] != '\\';
410 if( pszBasename[i] == '.' )
411 pszBasename[i] = '\0';
413 pszFullname = (char *) malloc(strlen(pszBasename) + 5);
414 sprintf( pszFullname, "%s.dbf", pszBasename );
416 psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );
417 psDBF->fp = psHooks->FOpen( pszFullname, pszAccess );
418 memcpy( &(psDBF->sHooks), psHooks, sizeof(SAHooks) );
420 if( psDBF->fp == NULL )
422 sprintf( pszFullname, "%s.DBF", pszBasename );
423 psDBF->fp = psDBF->sHooks.FOpen(pszFullname, pszAccess );
426 sprintf( pszFullname, "%s.cpg", pszBasename );
427 pfCPG = psHooks->FOpen( pszFullname, "r" );
430 sprintf( pszFullname, "%s.CPG", pszBasename );
431 pfCPG = psHooks->FOpen( pszFullname, "r" );
437 if( psDBF->fp == NULL )
440 if( pfCPG ) psHooks->FClose( pfCPG );
444 psDBF->bNoHeader = FALSE;
445 psDBF->nCurrentRecord = -1;
446 psDBF->bCurrentRecordModified = FALSE;
448 /* -------------------------------------------------------------------- */
449 /* Read Table Header info */
450 /* -------------------------------------------------------------------- */
451 pabyBuf = (unsigned char *) malloc(nBufSize);
452 if( psDBF->sHooks.FRead( pabyBuf, 32, 1, psDBF->fp ) != 1 )
454 psDBF->sHooks.FClose( psDBF->fp );
455 if( pfCPG ) psDBF->sHooks.FClose( pfCPG );
462 pabyBuf[4] + pabyBuf[5]*256 + pabyBuf[6]*256*256 + pabyBuf[7]*256*256*256;
464 psDBF->nHeaderLength = nHeadLen = pabyBuf[8] + pabyBuf[9]*256;
465 psDBF->nRecordLength = pabyBuf[10] + pabyBuf[11]*256;
466 psDBF->iLanguageDriver = pabyBuf[29];
468 psDBF->nFields = nFields = (nHeadLen - 32) / 32;
470 psDBF->pszCurrentRecord = (char *) malloc(psDBF->nRecordLength);
472 /* -------------------------------------------------------------------- */
473 /* Figure out the code page from the LDID and CPG */
474 /* -------------------------------------------------------------------- */
476 psDBF->pszCodePage = NULL;
480 char *buffer = (char *) pabyBuf;
482 psDBF->sHooks.FRead( pabyBuf, nBufSize - 1, 1, pfCPG );
483 n = strcspn( (char *) pabyBuf, "\n\r" );
487 psDBF->pszCodePage = (char *) malloc(n + 1);
488 memcpy( psDBF->pszCodePage, pabyBuf, n + 1 );
490 psDBF->sHooks.FClose( pfCPG );
492 if( psDBF->pszCodePage == NULL && pabyBuf[29] != 0 )
494 sprintf( (char *) pabyBuf, "LDID/%d", psDBF->iLanguageDriver );
495 psDBF->pszCodePage = (char *) malloc(strlen((char*)pabyBuf) + 1);
496 strcpy( psDBF->pszCodePage, (char *) pabyBuf );
499 /* -------------------------------------------------------------------- */
500 /* Read in Field Definitions */
501 /* -------------------------------------------------------------------- */
503 pabyBuf = (unsigned char *) SfRealloc(pabyBuf,nHeadLen);
504 psDBF->pszHeader = (char *) pabyBuf;
506 psDBF->sHooks.FSeek( psDBF->fp, 32, 0 );
507 if( psDBF->sHooks.FRead( pabyBuf, nHeadLen-32, 1, psDBF->fp ) != 1 )
509 psDBF->sHooks.FClose( psDBF->fp );
511 free( psDBF->pszCurrentRecord );
516 psDBF->panFieldOffset = (int *) malloc(sizeof(int) * nFields);
517 psDBF->panFieldSize = (int *) malloc(sizeof(int) * nFields);
518 psDBF->panFieldDecimals = (int *) malloc(sizeof(int) * nFields);
519 psDBF->pachFieldType = (char *) malloc(sizeof(char) * nFields);
521 for( iField = 0; iField < nFields; iField++ )
523 unsigned char *pabyFInfo;
525 pabyFInfo = pabyBuf+iField*32;
527 if( pabyFInfo[11] == 'N' || pabyFInfo[11] == 'F' )
529 psDBF->panFieldSize[iField] = pabyFInfo[16];
530 psDBF->panFieldDecimals[iField] = pabyFInfo[17];
534 psDBF->panFieldSize[iField] = pabyFInfo[16];
535 psDBF->panFieldDecimals[iField] = 0;
538 ** The following seemed to be used sometimes to handle files with long
539 ** string fields, but in other cases (such as bug 1202) the decimals field
540 ** just seems to indicate some sort of preferred formatting, not very
541 ** wide fields. So I have disabled this code. FrankW.
542 psDBF->panFieldSize[iField] = pabyFInfo[16] + pabyFInfo[17]*256;
543 psDBF->panFieldDecimals[iField] = 0;
547 psDBF->pachFieldType[iField] = (char) pabyFInfo[11];
549 psDBF->panFieldOffset[iField] = 1;
551 psDBF->panFieldOffset[iField] =
552 psDBF->panFieldOffset[iField-1] + psDBF->panFieldSize[iField-1];
558 /************************************************************************/
560 /************************************************************************/
563 DBFClose(DBFHandle psDBF)
568 /* -------------------------------------------------------------------- */
569 /* Write out header if not already written. */
570 /* -------------------------------------------------------------------- */
571 if( psDBF->bNoHeader )
572 DBFWriteHeader( psDBF );
574 DBFFlushRecord( psDBF );
576 /* -------------------------------------------------------------------- */
577 /* Update last access date, and number of records if we have */
579 /* -------------------------------------------------------------------- */
580 if( psDBF->bUpdated )
581 DBFUpdateHeader( psDBF );
583 /* -------------------------------------------------------------------- */
584 /* Close, and free resources. */
585 /* -------------------------------------------------------------------- */
586 psDBF->sHooks.FClose( psDBF->fp );
588 if( psDBF->panFieldOffset != NULL )
590 free( psDBF->panFieldOffset );
591 free( psDBF->panFieldSize );
592 free( psDBF->panFieldDecimals );
593 free( psDBF->pachFieldType );
596 if( psDBF->pszWorkField != NULL )
597 free( psDBF->pszWorkField );
599 free( psDBF->pszHeader );
600 free( psDBF->pszCurrentRecord );
601 free( psDBF->pszCodePage );
606 /************************************************************************/
609 /* Create a new .dbf file with default code page LDID/87 (0x57) */
610 /************************************************************************/
612 DBFHandle SHPAPI_CALL
613 DBFCreate( const char * pszFilename )
616 return DBFCreateEx( pszFilename, "LDID/87" ); // 0x57
619 /************************************************************************/
622 /* Create a new .dbf file. */
623 /************************************************************************/
625 DBFHandle SHPAPI_CALL
626 DBFCreateEx( const char * pszFilename, const char* pszCodePage )
631 SASetupDefaultHooks( &sHooks );
633 return DBFCreateLL( pszFilename, pszCodePage , &sHooks );
636 /************************************************************************/
639 /* Create a new .dbf file. */
640 /************************************************************************/
642 DBFHandle SHPAPI_CALL
643 DBFCreateLL( const char * pszFilename, const char * pszCodePage, SAHooks *psHooks )
648 char *pszFullname, *pszBasename;
652 /* -------------------------------------------------------------------- */
653 /* Compute the base (layer) name. If there is any extension */
654 /* on the passed in filename we will strip it off. */
655 /* -------------------------------------------------------------------- */
656 pszBasename = (char *) malloc(strlen(pszFilename)+5);
657 strcpy( pszBasename, pszFilename );
658 for( i = strlen(pszBasename)-1;
659 i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
660 && pszBasename[i] != '\\';
663 if( pszBasename[i] == '.' )
664 pszBasename[i] = '\0';
666 pszFullname = (char *) malloc(strlen(pszBasename) + 5);
667 sprintf( pszFullname, "%s.dbf", pszBasename );
669 /* -------------------------------------------------------------------- */
670 /* Create the file. */
671 /* -------------------------------------------------------------------- */
672 fp = psHooks->FOpen( pszFullname, "wb" );
676 psHooks->FWrite( &chZero, 1, 1, fp );
677 psHooks->FClose( fp );
679 fp = psHooks->FOpen( pszFullname, "rb+" );
684 sprintf( pszFullname, "%s.cpg", pszBasename );
685 if( pszCodePage != NULL )
687 if( strncmp( pszCodePage, "LDID/", 5 ) == 0 )
689 ldid = atoi( pszCodePage + 5 );
691 ldid = -1; // don't use 0 to indicate out of range as LDID/0 is a valid one
695 SAFile fpCPG = psHooks->FOpen( pszFullname, "w" );
696 psHooks->FWrite( (char*) pszCodePage, strlen(pszCodePage), 1, fpCPG );
697 psHooks->FClose( fpCPG );
700 if( pszCodePage == NULL || ldid >= 0 )
702 psHooks->Remove( pszFullname );
708 /* -------------------------------------------------------------------- */
709 /* Create the info structure. */
710 /* -------------------------------------------------------------------- */
711 psDBF = (DBFHandle) calloc(1,sizeof(DBFInfo));
713 memcpy( &(psDBF->sHooks), psHooks, sizeof(SAHooks) );
717 psDBF->nRecordLength = 1;
718 psDBF->nHeaderLength = 33;
720 psDBF->panFieldOffset = NULL;
721 psDBF->panFieldSize = NULL;
722 psDBF->panFieldDecimals = NULL;
723 psDBF->pachFieldType = NULL;
724 psDBF->pszHeader = NULL;
726 psDBF->nCurrentRecord = -1;
727 psDBF->bCurrentRecordModified = FALSE;
728 psDBF->pszCurrentRecord = NULL;
730 psDBF->bNoHeader = TRUE;
732 psDBF->iLanguageDriver = ldid > 0 ? ldid : 0;
733 psDBF->pszCodePage = NULL;
736 psDBF->pszCodePage = (char * ) malloc( strlen(pszCodePage) + 1 );
737 strcpy( psDBF->pszCodePage, pszCodePage );
743 /************************************************************************/
746 /* Add a field to a newly created .dbf or to an existing one */
747 /************************************************************************/
750 DBFAddField(DBFHandle psDBF, const char * pszFieldName,
751 DBFFieldType eType, int nWidth, int nDecimals )
754 char chNativeType = 'C';
756 if( eType == FTLogical )
758 else if( eType == FTString )
763 return DBFAddNativeFieldType( psDBF, pszFieldName, chNativeType,
767 /************************************************************************/
770 /* Add a field to a newly created .dbf file before any records */
772 /************************************************************************/
775 DBFAddNativeFieldType(DBFHandle psDBF, const char * pszFieldName,
776 char chType, int nWidth, int nDecimals )
781 int nOldRecordLength, nOldHeaderLength;
784 SAOffset nRecordOffset;
786 /* -------------------------------------------------------------------- */
787 /* Do some checking to ensure we can add records to this file. */
788 /* -------------------------------------------------------------------- */
795 nOldRecordLength = psDBF->nRecordLength;
796 nOldHeaderLength = psDBF->nHeaderLength;
798 /* -------------------------------------------------------------------- */
799 /* SfRealloc all the arrays larger to hold the additional field */
801 /* -------------------------------------------------------------------- */
804 psDBF->panFieldOffset = (int *)
805 SfRealloc( psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
807 psDBF->panFieldSize = (int *)
808 SfRealloc( psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
810 psDBF->panFieldDecimals = (int *)
811 SfRealloc( psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
813 psDBF->pachFieldType = (char *)
814 SfRealloc( psDBF->pachFieldType, sizeof(char) * psDBF->nFields );
816 /* -------------------------------------------------------------------- */
817 /* Assign the new field information fields. */
818 /* -------------------------------------------------------------------- */
819 psDBF->panFieldOffset[psDBF->nFields-1] = psDBF->nRecordLength;
820 psDBF->nRecordLength += nWidth;
821 psDBF->panFieldSize[psDBF->nFields-1] = nWidth;
822 psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;
823 psDBF->pachFieldType[psDBF->nFields-1] = chType;
825 /* -------------------------------------------------------------------- */
826 /* Extend the required header information. */
827 /* -------------------------------------------------------------------- */
828 psDBF->nHeaderLength += 32;
829 psDBF->bUpdated = FALSE;
831 psDBF->pszHeader = (char *) SfRealloc(psDBF->pszHeader,psDBF->nFields*32);
833 pszFInfo = psDBF->pszHeader + 32 * (psDBF->nFields-1);
835 for( i = 0; i < 32; i++ )
838 if( (int) strlen(pszFieldName) < 10 )
839 strncpy( pszFInfo, pszFieldName, strlen(pszFieldName));
841 strncpy( pszFInfo, pszFieldName, 10);
843 pszFInfo[11] = psDBF->pachFieldType[psDBF->nFields-1];
847 pszFInfo[16] = (unsigned char) (nWidth % 256);
848 pszFInfo[17] = (unsigned char) (nWidth / 256);
852 pszFInfo[16] = (unsigned char) nWidth;
853 pszFInfo[17] = (unsigned char) nDecimals;
856 /* -------------------------------------------------------------------- */
857 /* Make the current record buffer appropriately larger. */
858 /* -------------------------------------------------------------------- */
859 psDBF->pszCurrentRecord = (char *) SfRealloc(psDBF->pszCurrentRecord,
860 psDBF->nRecordLength);
862 /* we're done if dealing with new .dbf */
863 if( psDBF->bNoHeader )
864 return( psDBF->nFields - 1 );
866 /* -------------------------------------------------------------------- */
867 /* For existing .dbf file, shift records */
868 /* -------------------------------------------------------------------- */
871 pszRecord = (char *) malloc(sizeof(char) * psDBF->nRecordLength);
890 for (i = psDBF->nRecords-1; i >= 0; --i)
892 nRecordOffset = nOldRecordLength * (SAOffset) i + nOldHeaderLength;
895 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
896 psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
898 /* set new field's value to NULL */
899 memset(pszRecord + nOldRecordLength, chFieldFill, nWidth);
901 nRecordOffset = psDBF->nRecordLength * (SAOffset) i + psDBF->nHeaderLength;
903 /* move record to the new place*/
904 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
905 psDBF->sHooks.FWrite( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
911 /* force update of header with new header, record length and new field */
912 psDBF->bNoHeader = TRUE;
913 DBFUpdateHeader( psDBF );
915 return( psDBF->nFields-1 );
918 /************************************************************************/
919 /* DBFReadAttribute() */
921 /* Read one of the attribute fields of a record. */
922 /************************************************************************/
924 static void *DBFReadAttribute(DBFHandle psDBF, int hEntity, int iField,
928 unsigned char *pabyRec;
929 void *pReturnField = NULL;
931 /* -------------------------------------------------------------------- */
932 /* Verify selection. */
933 /* -------------------------------------------------------------------- */
934 if( hEntity < 0 || hEntity >= psDBF->nRecords )
937 if( iField < 0 || iField >= psDBF->nFields )
940 /* -------------------------------------------------------------------- */
941 /* Have we read the record? */
942 /* -------------------------------------------------------------------- */
943 if( !DBFLoadRecord( psDBF, hEntity ) )
946 pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
948 /* -------------------------------------------------------------------- */
949 /* Ensure we have room to extract the target field. */
950 /* -------------------------------------------------------------------- */
951 if( psDBF->panFieldSize[iField] >= psDBF->nWorkFieldLength )
953 psDBF->nWorkFieldLength = psDBF->panFieldSize[iField] + 100;
954 if( psDBF->pszWorkField == NULL )
955 psDBF->pszWorkField = (char *) malloc(psDBF->nWorkFieldLength);
957 psDBF->pszWorkField = (char *) realloc(psDBF->pszWorkField,
958 psDBF->nWorkFieldLength);
961 /* -------------------------------------------------------------------- */
962 /* Extract the requested field. */
963 /* -------------------------------------------------------------------- */
964 strncpy( psDBF->pszWorkField,
965 ((const char *) pabyRec) + psDBF->panFieldOffset[iField],
966 psDBF->panFieldSize[iField] );
967 psDBF->pszWorkField[psDBF->panFieldSize[iField]] = '\0';
969 pReturnField = psDBF->pszWorkField;
971 /* -------------------------------------------------------------------- */
972 /* Decode the field. */
973 /* -------------------------------------------------------------------- */
974 if( chReqType == 'N' )
976 psDBF->dfDoubleField = psDBF->sHooks.Atof(psDBF->pszWorkField);
978 pReturnField = &(psDBF->dfDoubleField);
981 /* -------------------------------------------------------------------- */
982 /* Should we trim white space off the string attribute value? */
983 /* -------------------------------------------------------------------- */
984 #ifdef TRIM_DBF_WHITESPACE
987 char *pchSrc, *pchDst;
989 pchDst = pchSrc = psDBF->pszWorkField;
990 while( *pchSrc == ' ' )
993 while( *pchSrc != '\0' )
994 *(pchDst++) = *(pchSrc++);
997 while( pchDst != psDBF->pszWorkField && *(--pchDst) == ' ' )
1002 return( pReturnField );
1005 /************************************************************************/
1006 /* DBFReadIntAttribute() */
1008 /* Read an integer attribute. */
1009 /************************************************************************/
1012 DBFReadIntegerAttribute( DBFHandle psDBF, int iRecord, int iField )
1017 pdValue = (double *) DBFReadAttribute( psDBF, iRecord, iField, 'N' );
1019 if( pdValue == NULL )
1022 return( (int) *pdValue );
1025 /************************************************************************/
1026 /* DBFReadDoubleAttribute() */
1028 /* Read a double attribute. */
1029 /************************************************************************/
1032 DBFReadDoubleAttribute( DBFHandle psDBF, int iRecord, int iField )
1037 pdValue = (double *) DBFReadAttribute( psDBF, iRecord, iField, 'N' );
1039 if( pdValue == NULL )
1045 /************************************************************************/
1046 /* DBFReadStringAttribute() */
1048 /* Read a string attribute. */
1049 /************************************************************************/
1051 const char SHPAPI_CALL1(*)
1052 DBFReadStringAttribute( DBFHandle psDBF, int iRecord, int iField )
1055 return( (const char *) DBFReadAttribute( psDBF, iRecord, iField, 'C' ) );
1058 /************************************************************************/
1059 /* DBFReadLogicalAttribute() */
1061 /* Read a logical attribute. */
1062 /************************************************************************/
1064 const char SHPAPI_CALL1(*)
1065 DBFReadLogicalAttribute( DBFHandle psDBF, int iRecord, int iField )
1068 return( (const char *) DBFReadAttribute( psDBF, iRecord, iField, 'L' ) );
1071 /************************************************************************/
1072 /* DBFIsAttributeNULL() */
1074 /* Return TRUE if value for field is NULL. */
1076 /* Contributed by Jim Matthews. */
1077 /************************************************************************/
1080 DBFIsAttributeNULL( DBFHandle psDBF, int iRecord, int iField )
1083 const char *pszValue;
1086 pszValue = DBFReadStringAttribute( psDBF, iRecord, iField );
1088 if( pszValue == NULL )
1091 switch(psDBF->pachFieldType[iField])
1096 ** We accept all asterisks or all blanks as NULL
1097 ** though according to the spec I think it should be all
1100 if( pszValue[0] == '*' )
1103 for( i = 0; pszValue[i] != '\0'; i++ )
1105 if( pszValue[i] != ' ' )
1111 /* NULL date fields have value "00000000" */
1112 return strncmp(pszValue,"00000000",8) == 0;
1115 /* NULL boolean fields have value "?" */
1116 return pszValue[0] == '?';
1119 /* empty string fields are considered NULL */
1120 return strlen(pszValue) == 0;
1124 /************************************************************************/
1125 /* DBFGetFieldCount() */
1127 /* Return the number of fields in this table. */
1128 /************************************************************************/
1131 DBFGetFieldCount( DBFHandle psDBF )
1134 return( psDBF->nFields );
1137 /************************************************************************/
1138 /* DBFGetRecordCount() */
1140 /* Return the number of records in this table. */
1141 /************************************************************************/
1144 DBFGetRecordCount( DBFHandle psDBF )
1147 return( psDBF->nRecords );
1150 /************************************************************************/
1151 /* DBFGetFieldInfo() */
1153 /* Return any requested information about the field. */
1154 /************************************************************************/
1156 DBFFieldType SHPAPI_CALL
1157 DBFGetFieldInfo( DBFHandle psDBF, int iField, char * pszFieldName,
1158 int * pnWidth, int * pnDecimals )
1161 if( iField < 0 || iField >= psDBF->nFields )
1162 return( FTInvalid );
1164 if( pnWidth != NULL )
1165 *pnWidth = psDBF->panFieldSize[iField];
1167 if( pnDecimals != NULL )
1168 *pnDecimals = psDBF->panFieldDecimals[iField];
1170 if( pszFieldName != NULL )
1174 strncpy( pszFieldName, (char *) psDBF->pszHeader+iField*32, 11 );
1175 pszFieldName[11] = '\0';
1176 for( i = 10; i > 0 && pszFieldName[i] == ' '; i-- )
1177 pszFieldName[i] = '\0';
1180 if ( psDBF->pachFieldType[iField] == 'L' )
1183 else if( psDBF->pachFieldType[iField] == 'N'
1184 || psDBF->pachFieldType[iField] == 'F' )
1186 if( psDBF->panFieldDecimals[iField] > 0
1187 || psDBF->panFieldSize[iField] > 10 )
1190 return( FTInteger );
1198 /************************************************************************/
1199 /* DBFWriteAttribute() */
1201 /* Write an attribute record to the file. */
1202 /************************************************************************/
1204 static int DBFWriteAttribute(DBFHandle psDBF, int hEntity, int iField,
1208 int i, j, nRetResult = TRUE;
1209 unsigned char *pabyRec;
1210 char szSField[400], szFormat[20];
1212 /* -------------------------------------------------------------------- */
1213 /* Is this a valid record? */
1214 /* -------------------------------------------------------------------- */
1215 if( hEntity < 0 || hEntity > psDBF->nRecords )
1218 if( psDBF->bNoHeader )
1219 DBFWriteHeader(psDBF);
1221 /* -------------------------------------------------------------------- */
1222 /* Is this a brand new record? */
1223 /* -------------------------------------------------------------------- */
1224 if( hEntity == psDBF->nRecords )
1226 if( !DBFFlushRecord( psDBF ) )
1230 for( i = 0; i < psDBF->nRecordLength; i++ )
1231 psDBF->pszCurrentRecord[i] = ' ';
1233 psDBF->nCurrentRecord = hEntity;
1236 /* -------------------------------------------------------------------- */
1237 /* Is this an existing record, but different than the last one */
1239 /* -------------------------------------------------------------------- */
1240 if( !DBFLoadRecord( psDBF, hEntity ) )
1243 pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
1245 psDBF->bCurrentRecordModified = TRUE;
1246 psDBF->bUpdated = TRUE;
1248 /* -------------------------------------------------------------------- */
1249 /* Translate NULL value to valid DBF file representation. */
1251 /* Contributed by Jim Matthews. */
1252 /* -------------------------------------------------------------------- */
1253 if( pValue == NULL )
1255 switch(psDBF->pachFieldType[iField])
1259 /* NULL numeric fields have value "****************" */
1260 memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '*',
1261 psDBF->panFieldSize[iField] );
1265 /* NULL date fields have value "00000000" */
1266 memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '0',
1267 psDBF->panFieldSize[iField] );
1271 /* NULL boolean fields have value "?" */
1272 memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '?',
1273 psDBF->panFieldSize[iField] );
1277 /* empty string fields are considered NULL */
1278 memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), ' ',
1279 psDBF->panFieldSize[iField] );
1285 /* -------------------------------------------------------------------- */
1286 /* Assign all the record fields. */
1287 /* -------------------------------------------------------------------- */
1288 switch( psDBF->pachFieldType[iField] )
1293 if( psDBF->panFieldDecimals[iField] == 0 )
1295 int nWidth = psDBF->panFieldSize[iField];
1297 if( (int) sizeof(szSField)-2 < nWidth )
1298 nWidth = sizeof(szSField)-2;
1300 sprintf( szFormat, "%%%dd", nWidth );
1301 sprintf(szSField, szFormat, (int) *((double *) pValue) );
1302 if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )
1304 szSField[psDBF->panFieldSize[iField]] = '\0';
1308 strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1309 szSField, strlen(szSField) );
1313 int nWidth = psDBF->panFieldSize[iField];
1315 if( (int) sizeof(szSField)-2 < nWidth )
1316 nWidth = sizeof(szSField)-2;
1318 sprintf( szFormat, "%%%d.%df",
1319 nWidth, psDBF->panFieldDecimals[iField] );
1320 sprintf(szSField, szFormat, *((double *) pValue) );
1321 if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )
1323 szSField[psDBF->panFieldSize[iField]] = '\0';
1326 strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1327 szSField, strlen(szSField) );
1332 if (psDBF->panFieldSize[iField] >= 1 &&
1333 (*(char*)pValue == 'F' || *(char*)pValue == 'T'))
1334 *(pabyRec+psDBF->panFieldOffset[iField]) = *(char*)pValue;
1338 if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1340 j = psDBF->panFieldSize[iField];
1345 memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
1346 psDBF->panFieldSize[iField] );
1347 j = strlen((char *) pValue);
1350 strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1351 (char *) pValue, j );
1355 return( nRetResult );
1358 /************************************************************************/
1359 /* DBFWriteAttributeDirectly() */
1361 /* Write an attribute record to the file, but without any */
1362 /* reformatting based on type. The provided buffer is written */
1363 /* as is to the field position in the record. */
1364 /************************************************************************/
1367 DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
1372 unsigned char *pabyRec;
1374 /* -------------------------------------------------------------------- */
1375 /* Is this a valid record? */
1376 /* -------------------------------------------------------------------- */
1377 if( hEntity < 0 || hEntity > psDBF->nRecords )
1380 if( psDBF->bNoHeader )
1381 DBFWriteHeader(psDBF);
1383 /* -------------------------------------------------------------------- */
1384 /* Is this a brand new record? */
1385 /* -------------------------------------------------------------------- */
1386 if( hEntity == psDBF->nRecords )
1388 if( !DBFFlushRecord( psDBF ) )
1392 for( i = 0; i < psDBF->nRecordLength; i++ )
1393 psDBF->pszCurrentRecord[i] = ' ';
1395 psDBF->nCurrentRecord = hEntity;
1398 /* -------------------------------------------------------------------- */
1399 /* Is this an existing record, but different than the last one */
1401 /* -------------------------------------------------------------------- */
1402 if( !DBFLoadRecord( psDBF, hEntity ) )
1405 pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
1407 /* -------------------------------------------------------------------- */
1408 /* Assign all the record fields. */
1409 /* -------------------------------------------------------------------- */
1410 if( (int)strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1411 j = psDBF->panFieldSize[iField];
1414 memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
1415 psDBF->panFieldSize[iField] );
1416 j = strlen((char *) pValue);
1419 strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1420 (char *) pValue, j );
1422 psDBF->bCurrentRecordModified = TRUE;
1423 psDBF->bUpdated = TRUE;
1428 /************************************************************************/
1429 /* DBFWriteDoubleAttribute() */
1431 /* Write a double attribute. */
1432 /************************************************************************/
1435 DBFWriteDoubleAttribute( DBFHandle psDBF, int iRecord, int iField,
1439 return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );
1442 /************************************************************************/
1443 /* DBFWriteIntegerAttribute() */
1445 /* Write a integer attribute. */
1446 /************************************************************************/
1449 DBFWriteIntegerAttribute( DBFHandle psDBF, int iRecord, int iField,
1453 double dValue = nValue;
1455 return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );
1458 /************************************************************************/
1459 /* DBFWriteStringAttribute() */
1461 /* Write a string attribute. */
1462 /************************************************************************/
1465 DBFWriteStringAttribute( DBFHandle psDBF, int iRecord, int iField,
1466 const char * pszValue )
1469 return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) pszValue ) );
1472 /************************************************************************/
1473 /* DBFWriteNULLAttribute() */
1475 /* Write a string attribute. */
1476 /************************************************************************/
1479 DBFWriteNULLAttribute( DBFHandle psDBF, int iRecord, int iField )
1482 return( DBFWriteAttribute( psDBF, iRecord, iField, NULL ) );
1485 /************************************************************************/
1486 /* DBFWriteLogicalAttribute() */
1488 /* Write a logical attribute. */
1489 /************************************************************************/
1492 DBFWriteLogicalAttribute( DBFHandle psDBF, int iRecord, int iField,
1496 return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) (&lValue) ) );
1499 /************************************************************************/
1500 /* DBFWriteTuple() */
1502 /* Write an attribute record to the file. */
1503 /************************************************************************/
1506 DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple )
1510 unsigned char *pabyRec;
1512 /* -------------------------------------------------------------------- */
1513 /* Is this a valid record? */
1514 /* -------------------------------------------------------------------- */
1515 if( hEntity < 0 || hEntity > psDBF->nRecords )
1518 if( psDBF->bNoHeader )
1519 DBFWriteHeader(psDBF);
1521 /* -------------------------------------------------------------------- */
1522 /* Is this a brand new record? */
1523 /* -------------------------------------------------------------------- */
1524 if( hEntity == psDBF->nRecords )
1526 if( !DBFFlushRecord( psDBF ) )
1530 for( i = 0; i < psDBF->nRecordLength; i++ )
1531 psDBF->pszCurrentRecord[i] = ' ';
1533 psDBF->nCurrentRecord = hEntity;
1536 /* -------------------------------------------------------------------- */
1537 /* Is this an existing record, but different than the last one */
1539 /* -------------------------------------------------------------------- */
1540 if( !DBFLoadRecord( psDBF, hEntity ) )
1543 pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
1545 memcpy ( pabyRec, pRawTuple, psDBF->nRecordLength );
1547 psDBF->bCurrentRecordModified = TRUE;
1548 psDBF->bUpdated = TRUE;
1553 /************************************************************************/
1554 /* DBFReadTuple() */
1556 /* Read a complete record. Note that the result is only valid */
1557 /* till the next record read for any reason. */
1558 /************************************************************************/
1560 const char SHPAPI_CALL1(*)
1561 DBFReadTuple(DBFHandle psDBF, int hEntity )
1564 if( hEntity < 0 || hEntity >= psDBF->nRecords )
1567 if( !DBFLoadRecord( psDBF, hEntity ) )
1570 return (const char *) psDBF->pszCurrentRecord;
1573 /************************************************************************/
1574 /* DBFCloneEmpty() */
1576 /* Read one of the attribute fields of a record. */
1577 /************************************************************************/
1579 DBFHandle SHPAPI_CALL
1580 DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename )
1584 newDBF = DBFCreateEx ( pszFilename, psDBF->pszCodePage );
1585 if ( newDBF == NULL ) return ( NULL );
1587 newDBF->nFields = psDBF->nFields;
1588 newDBF->nRecordLength = psDBF->nRecordLength;
1589 newDBF->nHeaderLength = psDBF->nHeaderLength;
1591 newDBF->pszHeader = (char *) malloc ( newDBF->nHeaderLength );
1592 memcpy ( newDBF->pszHeader, psDBF->pszHeader, newDBF->nHeaderLength );
1594 newDBF->panFieldOffset = (int *) malloc ( sizeof(int) * psDBF->nFields );
1595 memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
1596 newDBF->panFieldSize = (int *) malloc ( sizeof(int) * psDBF->nFields );
1597 memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
1598 newDBF->panFieldDecimals = (int *) malloc ( sizeof(int) * psDBF->nFields );
1599 memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
1600 newDBF->pachFieldType = (char *) malloc ( sizeof(char) * psDBF->nFields );
1601 memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(char)*psDBF->nFields );
1603 newDBF->bNoHeader = TRUE;
1604 newDBF->bUpdated = TRUE;
1606 DBFWriteHeader ( newDBF );
1607 DBFClose ( newDBF );
1609 newDBF = DBFOpen ( pszFilename, "rb+" );
1614 /************************************************************************/
1615 /* DBFGetNativeFieldType() */
1617 /* Return the DBase field type for the specified field. */
1619 /* Value can be one of: 'C' (String), 'D' (Date), 'F' (Float), */
1620 /* 'N' (Numeric, with or without decimal), */
1621 /* 'L' (Logical), */
1622 /* 'M' (Memo: 10 digits .DBT block ptr) */
1623 /************************************************************************/
1626 DBFGetNativeFieldType( DBFHandle psDBF, int iField )
1629 if( iField >=0 && iField < psDBF->nFields )
1630 return psDBF->pachFieldType[iField];
1635 /************************************************************************/
1636 /* str_to_upper() */
1637 /************************************************************************/
1639 static void str_to_upper (char *string)
1644 len = strlen (string);
1647 if (isalpha(string[i]) && islower(string[i]))
1648 string[i] = (char) toupper ((int)string[i]);
1651 /************************************************************************/
1652 /* DBFGetFieldIndex() */
1654 /* Get the index number for a field in a .dbf file. */
1656 /* Contributed by Jim Matthews. */
1657 /************************************************************************/
1660 DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName)
1663 char name[12], name1[12], name2[12];
1666 strncpy(name1, pszFieldName,11);
1668 str_to_upper(name1);
1670 for( i = 0; i < DBFGetFieldCount(psDBF); i++ )
1672 DBFGetFieldInfo( psDBF, i, name, NULL, NULL );
1673 strncpy(name2,name,11);
1674 str_to_upper(name2);
1676 if(!strncmp(name1,name2,10))
1682 /************************************************************************/
1683 /* DBFIsRecordDeleted() */
1685 /* Returns TRUE if the indicated record is deleted, otherwise */
1686 /* it returns FALSE. */
1687 /************************************************************************/
1689 int SHPAPI_CALL DBFIsRecordDeleted( DBFHandle psDBF, int iShape )
1692 /* -------------------------------------------------------------------- */
1693 /* Verify selection. */
1694 /* -------------------------------------------------------------------- */
1695 if( iShape < 0 || iShape >= psDBF->nRecords )
1698 /* -------------------------------------------------------------------- */
1699 /* Have we read the record? */
1700 /* -------------------------------------------------------------------- */
1701 if( !DBFLoadRecord( psDBF, iShape ) )
1704 /* -------------------------------------------------------------------- */
1705 /* '*' means deleted. */
1706 /* -------------------------------------------------------------------- */
1707 return psDBF->pszCurrentRecord[0] == '*';
1710 /************************************************************************/
1711 /* DBFMarkRecordDeleted() */
1712 /************************************************************************/
1714 int SHPAPI_CALL DBFMarkRecordDeleted( DBFHandle psDBF, int iShape,
1720 /* -------------------------------------------------------------------- */
1721 /* Verify selection. */
1722 /* -------------------------------------------------------------------- */
1723 if( iShape < 0 || iShape >= psDBF->nRecords )
1726 /* -------------------------------------------------------------------- */
1727 /* Is this an existing record, but different than the last one */
1729 /* -------------------------------------------------------------------- */
1730 if( !DBFLoadRecord( psDBF, iShape ) )
1733 /* -------------------------------------------------------------------- */
1734 /* Assign value, marking record as dirty if it changes. */
1735 /* -------------------------------------------------------------------- */
1741 if( psDBF->pszCurrentRecord[0] != chNewFlag )
1743 psDBF->bCurrentRecordModified = TRUE;
1744 psDBF->bUpdated = TRUE;
1745 psDBF->pszCurrentRecord[0] = chNewFlag;
1751 /************************************************************************/
1752 /* DBFGetCodePage */
1753 /************************************************************************/
1755 const char SHPAPI_CALL1(*)
1756 DBFGetCodePage(DBFHandle psDBF )
1760 return psDBF->pszCodePage;
1763 /************************************************************************/
1764 /* DBFDeleteField() */
1766 /* Remove a field from a .dbf file */
1767 /************************************************************************/
1770 DBFDeleteField(DBFHandle psDBF, int iField)
1772 int nOldRecordLength, nOldHeaderLength;
1773 int nDeletedFieldOffset, nDeletedFieldSize;
1774 SAOffset nRecordOffset;
1778 if (iField < 0 || iField >= psDBF->nFields)
1781 /* make sure that everything is written in .dbf */
1782 if( !DBFFlushRecord( psDBF ) )
1785 /* get information about field to be deleted */
1786 nOldRecordLength = psDBF->nRecordLength;
1787 nOldHeaderLength = psDBF->nHeaderLength;
1788 nDeletedFieldOffset = psDBF->panFieldOffset[iField];
1789 nDeletedFieldSize = psDBF->panFieldSize[iField];
1791 /* update fields info */
1792 for (i = iField + 1; i < psDBF->nFields; i++)
1794 psDBF->panFieldOffset[i-1] = psDBF->panFieldOffset[i] - nDeletedFieldSize;
1795 psDBF->panFieldSize[i-1] = psDBF->panFieldSize[i];
1796 psDBF->panFieldDecimals[i-1] = psDBF->panFieldDecimals[i];
1797 psDBF->pachFieldType[i-1] = psDBF->pachFieldType[i];
1800 /* resize fields arrays */
1803 psDBF->panFieldOffset = (int *)
1804 SfRealloc( psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
1806 psDBF->panFieldSize = (int *)
1807 SfRealloc( psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
1809 psDBF->panFieldDecimals = (int *)
1810 SfRealloc( psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
1812 psDBF->pachFieldType = (char *)
1813 SfRealloc( psDBF->pachFieldType, sizeof(char) * psDBF->nFields );
1815 /* update header information */
1816 psDBF->nHeaderLength -= 32;
1817 psDBF->nRecordLength -= nDeletedFieldSize;
1819 /* overwrite field information in header */
1820 memcpy(psDBF->pszHeader + iField*32,
1821 psDBF->pszHeader + (iField+1)*32,
1822 sizeof(char) * (psDBF->nFields - iField)*32);
1824 psDBF->pszHeader = (char *) SfRealloc(psDBF->pszHeader,psDBF->nFields*32);
1826 /* update size of current record appropriately */
1827 psDBF->pszCurrentRecord = (char *) SfRealloc(psDBF->pszCurrentRecord,
1828 psDBF->nRecordLength);
1830 /* we're done if we're dealing with not yet created .dbf */
1831 if ( psDBF->bNoHeader && psDBF->nRecords == 0 )
1834 /* force update of header with new header and record length */
1835 psDBF->bNoHeader = TRUE;
1836 DBFUpdateHeader( psDBF );
1839 pszRecord = (char *) malloc(sizeof(char) * nOldRecordLength);
1841 /* shift records to their new positions */
1842 for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
1845 nOldRecordLength * (SAOffset) iRecord + nOldHeaderLength;
1848 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1849 psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
1852 psDBF->nRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
1854 /* move record in two steps */
1855 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1856 psDBF->sHooks.FWrite( pszRecord, nDeletedFieldOffset, 1, psDBF->fp );
1857 psDBF->sHooks.FWrite( pszRecord + nDeletedFieldOffset + nDeletedFieldSize,
1858 nOldRecordLength - nDeletedFieldOffset - nDeletedFieldSize,
1863 /* TODO: truncate file */