1
/******************************************************************************
2
* $Id: dbfopen.c,v 1.89 2011-07-24 05:59:25 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.89 2011-07-24 05:59:25 fwarmerdam
38
* minimize use of CPLError in favor of SAHooks.Error()
40
* Revision 1.88 2011-05-13 17:35:17 fwarmerdam
41
* added DBFReorderFields() and DBFAlterFields() functions (from Even)
43
* Revision 1.87 2011-05-07 22:41:02 fwarmerdam
44
* ensure pending record is flushed when adding a native field (GDAL #4073)
46
* Revision 1.86 2011-04-17 15:15:29 fwarmerdam
47
* Removed unused variable.
49
* Revision 1.85 2010-12-06 16:09:34 fwarmerdam
50
* fix buffer read overrun fetching code page (bug 2276)
52
* Revision 1.84 2009-10-29 19:59:48 fwarmerdam
53
* avoid crash on truncated header (gdal #3093)
55
* Revision 1.83 2008/11/12 14:28:15 fwarmerdam
56
* DBFCreateField() now works on files with records
58
* Revision 1.82 2008/11/11 17:47:09 fwarmerdam
59
* added DBFDeleteField() function
61
* Revision 1.81 2008/01/03 17:48:13 bram
62
* in DBFCreate, use default code page LDID/87 (= 0x57, ANSI)
63
* instead of LDID/3. This seems to be the same as what ESRI
64
* would be doing by default.
66
* Revision 1.80 2007/12/30 14:36:39 fwarmerdam
67
* avoid syntax issue with last comment.
69
* Revision 1.79 2007/12/30 14:35:48 fwarmerdam
70
* Avoid char* / unsigned char* warnings.
72
* Revision 1.78 2007/12/18 18:28:07 bram
73
* - create hook for client specific atof (bugzilla ticket 1615)
74
* - check for NULL handle before closing cpCPG file, and close after reading.
76
* Revision 1.77 2007/12/15 20:25:21 bram
77
* dbfopen.c now reads the Code Page information from the DBF file, and exports
78
* this information as a string through the DBFGetCodePage function. This is
79
* either the number from the LDID header field ("LDID/<number>") or as the
80
* content of an accompanying .CPG file. When creating a DBF file, the code can
81
* be set using DBFCreateEx.
83
* Revision 1.76 2007/12/12 22:21:32 bram
84
* DBFClose: check for NULL psDBF handle before trying to close it.
86
* Revision 1.75 2007/12/06 13:58:19 fwarmerdam
87
* make sure file offset calculations are done in as SAOffset
89
* Revision 1.74 2007/12/06 07:00:25 fwarmerdam
90
* dbfopen now using SAHooks for fileio
92
* Revision 1.73 2007/09/03 19:48:11 fwarmerdam
93
* move DBFReadAttribute() static dDoubleField into dbfinfo
95
* Revision 1.72 2007/09/03 19:34:06 fwarmerdam
96
* Avoid use of static tuple buffer in DBFReadTuple()
98
* Revision 1.71 2006/06/22 14:37:18 fwarmerdam
99
* avoid memory leak if dbfopen fread fails
101
* Revision 1.70 2006/06/17 17:47:05 fwarmerdam
102
* use calloc() for dbfinfo in DBFCreate
104
* Revision 1.69 2006/06/17 15:34:32 fwarmerdam
105
* disallow creating fields wider than 255
107
* Revision 1.68 2006/06/17 15:12:40 fwarmerdam
108
* Fixed C++ style comments.
110
* Revision 1.67 2006/06/17 00:24:53 fwarmerdam
111
* Don't treat non-zero decimals values as high order byte for length
112
* for strings. It causes serious corruption for some files.
113
* http://bugzilla.remotesensing.org/show_bug.cgi?id=1202
115
* Revision 1.66 2006/03/29 18:26:20 fwarmerdam
116
* fixed bug with size of pachfieldtype in dbfcloneempty
118
* Revision 1.65 2006/02/15 01:14:30 fwarmerdam
119
* added DBFAddNativeFieldType
121
* Revision 1.64 2006/02/09 00:29:04 fwarmerdam
122
* Changed to put spaces into string fields that are NULL as
123
* per http://bugzilla.maptools.org/show_bug.cgi?id=316.
125
* Revision 1.63 2006/01/25 15:35:43 fwarmerdam
126
* check success on DBFFlushRecord
128
* Revision 1.62 2006/01/10 16:28:03 fwarmerdam
129
* Fixed typo in CPLError.
131
* Revision 1.61 2006/01/10 16:26:29 fwarmerdam
132
* Push loading record buffer into DBFLoadRecord.
133
* Implement CPL error reporting if USE_CPL defined.
135
* Revision 1.60 2006/01/05 01:27:27 fwarmerdam
136
* added dbf deletion mark/fetch
138
* Revision 1.59 2005/03/14 15:20:28 fwarmerdam
141
* Revision 1.58 2005/03/14 15:18:54 fwarmerdam
142
* Treat very wide fields with no decimals as double. This is
143
* more than 32bit integer fields.
145
* Revision 1.57 2005/02/10 20:16:54 fwarmerdam
146
* Make the pszStringField buffer for DBFReadAttribute() static char [256]
149
* Revision 1.56 2005/02/10 20:07:56 fwarmerdam
150
* Fixed bug 305 in DBFCloneEmpty() - header length problem.
152
* Revision 1.55 2004/09/26 20:23:46 fwarmerdam
153
* avoid warnings with rcsid and signed/unsigned stuff
155
* Revision 1.54 2004/09/15 16:26:10 fwarmerdam
156
* Treat all blank numeric fields as null too.
159
#include "shapefil.h"
166
SHP_CVSID("$Id: dbfopen.c,v 1.89 2011-07-24 05:59:25 fwarmerdam Exp $")
173
/************************************************************************/
176
/* A realloc cover function that will access a NULL pointer as */
178
/************************************************************************/
180
static void * SfRealloc( void * pMem, int nNewSize )
184
return( (void *) malloc(nNewSize) );
186
return( (void *) realloc(pMem,nNewSize) );
189
/************************************************************************/
190
/* DBFWriteHeader() */
192
/* This is called to write out the file header, and field */
193
/* descriptions before writing any actual data records. This */
194
/* also computes all the DBFDataSet field offset/size/decimals */
195
/* and so forth values. */
196
/************************************************************************/
198
static void DBFWriteHeader(DBFHandle psDBF)
201
unsigned char abyHeader[XBASE_FLDHDR_SZ];
204
if( !psDBF->bNoHeader )
207
psDBF->bNoHeader = FALSE;
209
/* -------------------------------------------------------------------- */
210
/* Initialize the file header information. */
211
/* -------------------------------------------------------------------- */
212
for( i = 0; i < XBASE_FLDHDR_SZ; i++ )
215
abyHeader[0] = 0x03; /* memo field? - just copying */
217
/* write out a dummy date */
218
abyHeader[1] = 95; /* YY */
219
abyHeader[2] = 7; /* MM */
220
abyHeader[3] = 26; /* DD */
222
/* record count preset at zero */
224
abyHeader[8] = (unsigned char) (psDBF->nHeaderLength % 256);
225
abyHeader[9] = (unsigned char) (psDBF->nHeaderLength / 256);
227
abyHeader[10] = (unsigned char) (psDBF->nRecordLength % 256);
228
abyHeader[11] = (unsigned char) (psDBF->nRecordLength / 256);
230
abyHeader[29] = (unsigned char) (psDBF->iLanguageDriver);
232
/* -------------------------------------------------------------------- */
233
/* Write the initial 32 byte file header, and all the field */
235
/* -------------------------------------------------------------------- */
236
psDBF->sHooks.FSeek( psDBF->fp, 0, 0 );
237
psDBF->sHooks.FWrite( abyHeader, XBASE_FLDHDR_SZ, 1, psDBF->fp );
238
psDBF->sHooks.FWrite( psDBF->pszHeader, XBASE_FLDHDR_SZ, psDBF->nFields,
241
/* -------------------------------------------------------------------- */
242
/* Write out the newline character if there is room for it. */
243
/* -------------------------------------------------------------------- */
244
if( psDBF->nHeaderLength > 32*psDBF->nFields + 32 )
249
psDBF->sHooks.FWrite( &cNewline, 1, 1, psDBF->fp );
253
/************************************************************************/
254
/* DBFFlushRecord() */
256
/* Write out the current record if there is one. */
257
/************************************************************************/
259
static int DBFFlushRecord( DBFHandle psDBF )
262
SAOffset nRecordOffset;
264
if( psDBF->bCurrentRecordModified && psDBF->nCurrentRecord > -1 )
266
psDBF->bCurrentRecordModified = FALSE;
269
psDBF->nRecordLength * (SAOffset) psDBF->nCurrentRecord
270
+ psDBF->nHeaderLength;
272
if( psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 ) != 0
273
|| psDBF->sHooks.FWrite( psDBF->pszCurrentRecord,
274
psDBF->nRecordLength,
275
1, psDBF->fp ) != 1 )
278
sprintf( szMessage, "Failure writing DBF record %d.",
279
psDBF->nCurrentRecord );
280
psDBF->sHooks.Error( szMessage );
288
/************************************************************************/
289
/* DBFLoadRecord() */
290
/************************************************************************/
292
static int DBFLoadRecord( DBFHandle psDBF, int iRecord )
295
if( psDBF->nCurrentRecord != iRecord )
297
SAOffset nRecordOffset;
299
if( !DBFFlushRecord( psDBF ) )
303
psDBF->nRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
305
if( psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, SEEK_SET ) != 0 )
308
sprintf( szMessage, "fseek(%ld) failed on DBF file.\n",
309
(long) nRecordOffset );
310
psDBF->sHooks.Error( szMessage );
314
if( psDBF->sHooks.FRead( psDBF->pszCurrentRecord,
315
psDBF->nRecordLength, 1, psDBF->fp ) != 1 )
318
sprintf( szMessage, "fread(%d) failed on DBF file.\n",
319
psDBF->nRecordLength );
320
psDBF->sHooks.Error( szMessage );
324
psDBF->nCurrentRecord = iRecord;
330
/************************************************************************/
331
/* DBFUpdateHeader() */
332
/************************************************************************/
335
DBFUpdateHeader( DBFHandle psDBF )
338
unsigned char abyFileHeader[32];
340
if( psDBF->bNoHeader )
341
DBFWriteHeader( psDBF );
343
DBFFlushRecord( psDBF );
345
psDBF->sHooks.FSeek( psDBF->fp, 0, 0 );
346
psDBF->sHooks.FRead( abyFileHeader, 32, 1, psDBF->fp );
348
abyFileHeader[4] = (unsigned char) (psDBF->nRecords % 256);
349
abyFileHeader[5] = (unsigned char) ((psDBF->nRecords/256) % 256);
350
abyFileHeader[6] = (unsigned char) ((psDBF->nRecords/(256*256)) % 256);
351
abyFileHeader[7] = (unsigned char) ((psDBF->nRecords/(256*256*256)) % 256);
353
psDBF->sHooks.FSeek( psDBF->fp, 0, 0 );
354
psDBF->sHooks.FWrite( abyFileHeader, 32, 1, psDBF->fp );
356
psDBF->sHooks.FFlush( psDBF->fp );
359
/************************************************************************/
362
/* Open a .dbf file. */
363
/************************************************************************/
365
DBFHandle SHPAPI_CALL
366
DBFOpen( const char * pszFilename, const char * pszAccess )
371
SASetupDefaultHooks( &sHooks );
373
return DBFOpenLL( pszFilename, pszAccess, &sHooks );
376
/************************************************************************/
379
/* Open a .dbf file. */
380
/************************************************************************/
382
DBFHandle SHPAPI_CALL
383
DBFOpenLL( const char * pszFilename, const char * pszAccess, SAHooks *psHooks )
388
unsigned char *pabyBuf;
389
int nFields, nHeadLen, iField, i;
390
char *pszBasename, *pszFullname;
393
/* -------------------------------------------------------------------- */
394
/* We only allow the access strings "rb" and "r+". */
395
/* -------------------------------------------------------------------- */
396
if( strcmp(pszAccess,"r") != 0 && strcmp(pszAccess,"r+") != 0
397
&& strcmp(pszAccess,"rb") != 0 && strcmp(pszAccess,"rb+") != 0
398
&& strcmp(pszAccess,"r+b") != 0 )
401
if( strcmp(pszAccess,"r") == 0 )
404
if( strcmp(pszAccess,"r+") == 0 )
407
/* -------------------------------------------------------------------- */
408
/* Compute the base (layer) name. If there is any extension */
409
/* on the passed in filename we will strip it off. */
410
/* -------------------------------------------------------------------- */
411
pszBasename = (char *) malloc(strlen(pszFilename)+5);
412
strcpy( pszBasename, pszFilename );
413
for( i = strlen(pszBasename)-1;
414
i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
415
&& pszBasename[i] != '\\';
418
if( pszBasename[i] == '.' )
419
pszBasename[i] = '\0';
421
pszFullname = (char *) malloc(strlen(pszBasename) + 5);
422
sprintf( pszFullname, "%s.dbf", pszBasename );
424
psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );
425
psDBF->fp = psHooks->FOpen( pszFullname, pszAccess );
426
memcpy( &(psDBF->sHooks), psHooks, sizeof(SAHooks) );
428
if( psDBF->fp == NULL )
430
sprintf( pszFullname, "%s.DBF", pszBasename );
431
psDBF->fp = psDBF->sHooks.FOpen(pszFullname, pszAccess );
434
sprintf( pszFullname, "%s.cpg", pszBasename );
435
pfCPG = psHooks->FOpen( pszFullname, "r" );
438
sprintf( pszFullname, "%s.CPG", pszBasename );
439
pfCPG = psHooks->FOpen( pszFullname, "r" );
445
if( psDBF->fp == NULL )
448
if( pfCPG ) psHooks->FClose( pfCPG );
452
psDBF->bNoHeader = FALSE;
453
psDBF->nCurrentRecord = -1;
454
psDBF->bCurrentRecordModified = FALSE;
456
/* -------------------------------------------------------------------- */
457
/* Read Table Header info */
458
/* -------------------------------------------------------------------- */
459
pabyBuf = (unsigned char *) malloc(nBufSize);
460
if( psDBF->sHooks.FRead( pabyBuf, 32, 1, psDBF->fp ) != 1 )
462
psDBF->sHooks.FClose( psDBF->fp );
463
if( pfCPG ) psDBF->sHooks.FClose( pfCPG );
470
pabyBuf[4] + pabyBuf[5]*256 + pabyBuf[6]*256*256 + pabyBuf[7]*256*256*256;
472
psDBF->nHeaderLength = nHeadLen = pabyBuf[8] + pabyBuf[9]*256;
473
psDBF->nRecordLength = pabyBuf[10] + pabyBuf[11]*256;
474
psDBF->iLanguageDriver = pabyBuf[29];
478
psDBF->sHooks.FClose( psDBF->fp );
479
if( pfCPG ) psDBF->sHooks.FClose( pfCPG );
485
psDBF->nFields = nFields = (nHeadLen - 32) / 32;
487
psDBF->pszCurrentRecord = (char *) malloc(psDBF->nRecordLength);
489
/* -------------------------------------------------------------------- */
490
/* Figure out the code page from the LDID and CPG */
491
/* -------------------------------------------------------------------- */
493
psDBF->pszCodePage = NULL;
497
memset( pabyBuf, 0, nBufSize);
498
psDBF->sHooks.FRead( pabyBuf, nBufSize - 1, 1, pfCPG );
499
n = strcspn( (char *) pabyBuf, "\n\r" );
503
psDBF->pszCodePage = (char *) malloc(n + 1);
504
memcpy( psDBF->pszCodePage, pabyBuf, n + 1 );
506
psDBF->sHooks.FClose( pfCPG );
508
if( psDBF->pszCodePage == NULL && pabyBuf[29] != 0 )
510
sprintf( (char *) pabyBuf, "LDID/%d", psDBF->iLanguageDriver );
511
psDBF->pszCodePage = (char *) malloc(strlen((char*)pabyBuf) + 1);
512
strcpy( psDBF->pszCodePage, (char *) pabyBuf );
515
/* -------------------------------------------------------------------- */
516
/* Read in Field Definitions */
517
/* -------------------------------------------------------------------- */
519
pabyBuf = (unsigned char *) SfRealloc(pabyBuf,nHeadLen);
520
psDBF->pszHeader = (char *) pabyBuf;
522
psDBF->sHooks.FSeek( psDBF->fp, 32, 0 );
523
if( psDBF->sHooks.FRead( pabyBuf, nHeadLen-32, 1, psDBF->fp ) != 1 )
525
psDBF->sHooks.FClose( psDBF->fp );
527
free( psDBF->pszCurrentRecord );
532
psDBF->panFieldOffset = (int *) malloc(sizeof(int) * nFields);
533
psDBF->panFieldSize = (int *) malloc(sizeof(int) * nFields);
534
psDBF->panFieldDecimals = (int *) malloc(sizeof(int) * nFields);
535
psDBF->pachFieldType = (char *) malloc(sizeof(char) * nFields);
537
for( iField = 0; iField < nFields; iField++ )
539
unsigned char *pabyFInfo;
541
pabyFInfo = pabyBuf+iField*32;
543
if( pabyFInfo[11] == 'N' || pabyFInfo[11] == 'F' )
545
psDBF->panFieldSize[iField] = pabyFInfo[16];
546
psDBF->panFieldDecimals[iField] = pabyFInfo[17];
550
psDBF->panFieldSize[iField] = pabyFInfo[16];
551
psDBF->panFieldDecimals[iField] = 0;
554
** The following seemed to be used sometimes to handle files with long
555
** string fields, but in other cases (such as bug 1202) the decimals field
556
** just seems to indicate some sort of preferred formatting, not very
557
** wide fields. So I have disabled this code. FrankW.
558
psDBF->panFieldSize[iField] = pabyFInfo[16] + pabyFInfo[17]*256;
559
psDBF->panFieldDecimals[iField] = 0;
563
psDBF->pachFieldType[iField] = (char) pabyFInfo[11];
565
psDBF->panFieldOffset[iField] = 1;
567
psDBF->panFieldOffset[iField] =
568
psDBF->panFieldOffset[iField-1] + psDBF->panFieldSize[iField-1];
574
/************************************************************************/
576
/************************************************************************/
579
DBFClose(DBFHandle psDBF)
584
/* -------------------------------------------------------------------- */
585
/* Write out header if not already written. */
586
/* -------------------------------------------------------------------- */
587
if( psDBF->bNoHeader )
588
DBFWriteHeader( psDBF );
590
DBFFlushRecord( psDBF );
592
/* -------------------------------------------------------------------- */
593
/* Update last access date, and number of records if we have */
595
/* -------------------------------------------------------------------- */
596
if( psDBF->bUpdated )
597
DBFUpdateHeader( psDBF );
599
/* -------------------------------------------------------------------- */
600
/* Close, and free resources. */
601
/* -------------------------------------------------------------------- */
602
psDBF->sHooks.FClose( psDBF->fp );
604
if( psDBF->panFieldOffset != NULL )
606
free( psDBF->panFieldOffset );
607
free( psDBF->panFieldSize );
608
free( psDBF->panFieldDecimals );
609
free( psDBF->pachFieldType );
612
if( psDBF->pszWorkField != NULL )
613
free( psDBF->pszWorkField );
615
free( psDBF->pszHeader );
616
free( psDBF->pszCurrentRecord );
617
free( psDBF->pszCodePage );
622
/************************************************************************/
625
/* Create a new .dbf file with default code page LDID/87 (0x57) */
626
/************************************************************************/
628
DBFHandle SHPAPI_CALL
629
DBFCreate( const char * pszFilename )
632
return DBFCreateEx( pszFilename, "LDID/87" ); // 0x57
635
/************************************************************************/
638
/* Create a new .dbf file. */
639
/************************************************************************/
641
DBFHandle SHPAPI_CALL
642
DBFCreateEx( const char * pszFilename, const char* pszCodePage )
647
SASetupDefaultHooks( &sHooks );
649
return DBFCreateLL( pszFilename, pszCodePage , &sHooks );
652
/************************************************************************/
655
/* Create a new .dbf file. */
656
/************************************************************************/
658
DBFHandle SHPAPI_CALL
659
DBFCreateLL( const char * pszFilename, const char * pszCodePage, SAHooks *psHooks )
664
char *pszFullname, *pszBasename;
668
/* -------------------------------------------------------------------- */
669
/* Compute the base (layer) name. If there is any extension */
670
/* on the passed in filename we will strip it off. */
671
/* -------------------------------------------------------------------- */
672
pszBasename = (char *) malloc(strlen(pszFilename)+5);
673
strcpy( pszBasename, pszFilename );
674
for( i = strlen(pszBasename)-1;
675
i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
676
&& pszBasename[i] != '\\';
679
if( pszBasename[i] == '.' )
680
pszBasename[i] = '\0';
682
pszFullname = (char *) malloc(strlen(pszBasename) + 5);
683
sprintf( pszFullname, "%s.dbf", pszBasename );
685
/* -------------------------------------------------------------------- */
686
/* Create the file. */
687
/* -------------------------------------------------------------------- */
688
fp = psHooks->FOpen( pszFullname, "wb" );
692
psHooks->FWrite( &chZero, 1, 1, fp );
693
psHooks->FClose( fp );
695
fp = psHooks->FOpen( pszFullname, "rb+" );
700
sprintf( pszFullname, "%s.cpg", pszBasename );
701
if( pszCodePage != NULL )
703
if( strncmp( pszCodePage, "LDID/", 5 ) == 0 )
705
ldid = atoi( pszCodePage + 5 );
707
ldid = -1; // don't use 0 to indicate out of range as LDID/0 is a valid one
711
SAFile fpCPG = psHooks->FOpen( pszFullname, "w" );
712
psHooks->FWrite( (char*) pszCodePage, strlen(pszCodePage), 1, fpCPG );
713
psHooks->FClose( fpCPG );
716
if( pszCodePage == NULL || ldid >= 0 )
718
psHooks->Remove( pszFullname );
724
/* -------------------------------------------------------------------- */
725
/* Create the info structure. */
726
/* -------------------------------------------------------------------- */
727
psDBF = (DBFHandle) calloc(1,sizeof(DBFInfo));
729
memcpy( &(psDBF->sHooks), psHooks, sizeof(SAHooks) );
733
psDBF->nRecordLength = 1;
734
psDBF->nHeaderLength = 33;
736
psDBF->panFieldOffset = NULL;
737
psDBF->panFieldSize = NULL;
738
psDBF->panFieldDecimals = NULL;
739
psDBF->pachFieldType = NULL;
740
psDBF->pszHeader = NULL;
742
psDBF->nCurrentRecord = -1;
743
psDBF->bCurrentRecordModified = FALSE;
744
psDBF->pszCurrentRecord = NULL;
746
psDBF->bNoHeader = TRUE;
748
psDBF->iLanguageDriver = ldid > 0 ? ldid : 0;
749
psDBF->pszCodePage = NULL;
752
psDBF->pszCodePage = (char * ) malloc( strlen(pszCodePage) + 1 );
753
strcpy( psDBF->pszCodePage, pszCodePage );
759
/************************************************************************/
762
/* Add a field to a newly created .dbf or to an existing one */
763
/************************************************************************/
766
DBFAddField(DBFHandle psDBF, const char * pszFieldName,
767
DBFFieldType eType, int nWidth, int nDecimals )
770
char chNativeType = 'C';
772
if( eType == FTLogical )
774
else if( eType == FTString )
779
return DBFAddNativeFieldType( psDBF, pszFieldName, chNativeType,
783
/************************************************************************/
784
/* DBFGetNullCharacter() */
785
/************************************************************************/
787
static char DBFGetNullCharacter(char chType)
803
/************************************************************************/
806
/* Add a field to a newly created .dbf file before any records */
808
/************************************************************************/
811
DBFAddNativeFieldType(DBFHandle psDBF, const char * pszFieldName,
812
char chType, int nWidth, int nDecimals )
817
int nOldRecordLength, nOldHeaderLength;
820
SAOffset nRecordOffset;
822
/* make sure that everything is written in .dbf */
823
if( !DBFFlushRecord( psDBF ) )
826
/* -------------------------------------------------------------------- */
827
/* Do some checking to ensure we can add records to this file. */
828
/* -------------------------------------------------------------------- */
835
nOldRecordLength = psDBF->nRecordLength;
836
nOldHeaderLength = psDBF->nHeaderLength;
838
/* -------------------------------------------------------------------- */
839
/* SfRealloc all the arrays larger to hold the additional field */
841
/* -------------------------------------------------------------------- */
844
psDBF->panFieldOffset = (int *)
845
SfRealloc( psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
847
psDBF->panFieldSize = (int *)
848
SfRealloc( psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
850
psDBF->panFieldDecimals = (int *)
851
SfRealloc( psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
853
psDBF->pachFieldType = (char *)
854
SfRealloc( psDBF->pachFieldType, sizeof(char) * psDBF->nFields );
856
/* -------------------------------------------------------------------- */
857
/* Assign the new field information fields. */
858
/* -------------------------------------------------------------------- */
859
psDBF->panFieldOffset[psDBF->nFields-1] = psDBF->nRecordLength;
860
psDBF->nRecordLength += nWidth;
861
psDBF->panFieldSize[psDBF->nFields-1] = nWidth;
862
psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;
863
psDBF->pachFieldType[psDBF->nFields-1] = chType;
865
/* -------------------------------------------------------------------- */
866
/* Extend the required header information. */
867
/* -------------------------------------------------------------------- */
868
psDBF->nHeaderLength += 32;
869
psDBF->bUpdated = FALSE;
871
psDBF->pszHeader = (char *) SfRealloc(psDBF->pszHeader,psDBF->nFields*32);
873
pszFInfo = psDBF->pszHeader + 32 * (psDBF->nFields-1);
875
for( i = 0; i < 32; i++ )
878
if( (int) strlen(pszFieldName) < 10 )
879
strncpy( pszFInfo, pszFieldName, strlen(pszFieldName));
881
strncpy( pszFInfo, pszFieldName, 10);
883
pszFInfo[11] = psDBF->pachFieldType[psDBF->nFields-1];
887
pszFInfo[16] = (unsigned char) (nWidth % 256);
888
pszFInfo[17] = (unsigned char) (nWidth / 256);
892
pszFInfo[16] = (unsigned char) nWidth;
893
pszFInfo[17] = (unsigned char) nDecimals;
896
/* -------------------------------------------------------------------- */
897
/* Make the current record buffer appropriately larger. */
898
/* -------------------------------------------------------------------- */
899
psDBF->pszCurrentRecord = (char *) SfRealloc(psDBF->pszCurrentRecord,
900
psDBF->nRecordLength);
902
/* we're done if dealing with new .dbf */
903
if( psDBF->bNoHeader )
904
return( psDBF->nFields - 1 );
906
/* -------------------------------------------------------------------- */
907
/* For existing .dbf file, shift records */
908
/* -------------------------------------------------------------------- */
911
pszRecord = (char *) malloc(sizeof(char) * psDBF->nRecordLength);
913
chFieldFill = DBFGetNullCharacter(chType);
915
for (i = psDBF->nRecords-1; i >= 0; --i)
917
nRecordOffset = nOldRecordLength * (SAOffset) i + nOldHeaderLength;
920
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
921
psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
923
/* set new field's value to NULL */
924
memset(pszRecord + nOldRecordLength, chFieldFill, nWidth);
926
nRecordOffset = psDBF->nRecordLength * (SAOffset) i + psDBF->nHeaderLength;
928
/* move record to the new place*/
929
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
930
psDBF->sHooks.FWrite( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
936
/* force update of header with new header, record length and new field */
937
psDBF->bNoHeader = TRUE;
938
DBFUpdateHeader( psDBF );
940
psDBF->nCurrentRecord = -1;
941
psDBF->bCurrentRecordModified = FALSE;
943
return( psDBF->nFields-1 );
946
/************************************************************************/
947
/* DBFReadAttribute() */
949
/* Read one of the attribute fields of a record. */
950
/************************************************************************/
952
static void *DBFReadAttribute(DBFHandle psDBF, int hEntity, int iField,
956
unsigned char *pabyRec;
957
void *pReturnField = NULL;
959
/* -------------------------------------------------------------------- */
960
/* Verify selection. */
961
/* -------------------------------------------------------------------- */
962
if( hEntity < 0 || hEntity >= psDBF->nRecords )
965
if( iField < 0 || iField >= psDBF->nFields )
968
/* -------------------------------------------------------------------- */
969
/* Have we read the record? */
970
/* -------------------------------------------------------------------- */
971
if( !DBFLoadRecord( psDBF, hEntity ) )
974
pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
976
/* -------------------------------------------------------------------- */
977
/* Ensure we have room to extract the target field. */
978
/* -------------------------------------------------------------------- */
979
if( psDBF->panFieldSize[iField] >= psDBF->nWorkFieldLength )
981
psDBF->nWorkFieldLength = psDBF->panFieldSize[iField] + 100;
982
if( psDBF->pszWorkField == NULL )
983
psDBF->pszWorkField = (char *) malloc(psDBF->nWorkFieldLength);
985
psDBF->pszWorkField = (char *) realloc(psDBF->pszWorkField,
986
psDBF->nWorkFieldLength);
989
/* -------------------------------------------------------------------- */
990
/* Extract the requested field. */
991
/* -------------------------------------------------------------------- */
992
strncpy( psDBF->pszWorkField,
993
((const char *) pabyRec) + psDBF->panFieldOffset[iField],
994
psDBF->panFieldSize[iField] );
995
psDBF->pszWorkField[psDBF->panFieldSize[iField]] = '\0';
997
pReturnField = psDBF->pszWorkField;
999
/* -------------------------------------------------------------------- */
1000
/* Decode the field. */
1001
/* -------------------------------------------------------------------- */
1002
if( chReqType == 'N' )
1004
psDBF->dfDoubleField = psDBF->sHooks.Atof(psDBF->pszWorkField);
1006
pReturnField = &(psDBF->dfDoubleField);
1009
/* -------------------------------------------------------------------- */
1010
/* Should we trim white space off the string attribute value? */
1011
/* -------------------------------------------------------------------- */
1012
#ifdef TRIM_DBF_WHITESPACE
1015
char *pchSrc, *pchDst;
1017
pchDst = pchSrc = psDBF->pszWorkField;
1018
while( *pchSrc == ' ' )
1021
while( *pchSrc != '\0' )
1022
*(pchDst++) = *(pchSrc++);
1025
while( pchDst != psDBF->pszWorkField && *(--pchDst) == ' ' )
1030
return( pReturnField );
1033
/************************************************************************/
1034
/* DBFReadIntAttribute() */
1036
/* Read an integer attribute. */
1037
/************************************************************************/
1040
DBFReadIntegerAttribute( DBFHandle psDBF, int iRecord, int iField )
1045
pdValue = (double *) DBFReadAttribute( psDBF, iRecord, iField, 'N' );
1047
if( pdValue == NULL )
1050
return( (int) *pdValue );
1053
/************************************************************************/
1054
/* DBFReadDoubleAttribute() */
1056
/* Read a double attribute. */
1057
/************************************************************************/
1060
DBFReadDoubleAttribute( DBFHandle psDBF, int iRecord, int iField )
1065
pdValue = (double *) DBFReadAttribute( psDBF, iRecord, iField, 'N' );
1067
if( pdValue == NULL )
1073
/************************************************************************/
1074
/* DBFReadStringAttribute() */
1076
/* Read a string attribute. */
1077
/************************************************************************/
1079
const char SHPAPI_CALL1(*)
1080
DBFReadStringAttribute( DBFHandle psDBF, int iRecord, int iField )
1083
return( (const char *) DBFReadAttribute( psDBF, iRecord, iField, 'C' ) );
1086
/************************************************************************/
1087
/* DBFReadLogicalAttribute() */
1089
/* Read a logical attribute. */
1090
/************************************************************************/
1092
const char SHPAPI_CALL1(*)
1093
DBFReadLogicalAttribute( DBFHandle psDBF, int iRecord, int iField )
1096
return( (const char *) DBFReadAttribute( psDBF, iRecord, iField, 'L' ) );
1100
/************************************************************************/
1101
/* DBFIsValueNULL() */
1103
/* Return TRUE if the passed string is NULL. */
1104
/************************************************************************/
1106
static int DBFIsValueNULL( char chType, const char* pszValue )
1110
if( pszValue == NULL )
1118
** We accept all asterisks or all blanks as NULL
1119
** though according to the spec I think it should be all
1122
if( pszValue[0] == '*' )
1125
for( i = 0; pszValue[i] != '\0'; i++ )
1127
if( pszValue[i] != ' ' )
1133
/* NULL date fields have value "00000000" */
1134
return strncmp(pszValue,"00000000",8) == 0;
1137
/* NULL boolean fields have value "?" */
1138
return pszValue[0] == '?';
1141
/* empty string fields are considered NULL */
1142
return strlen(pszValue) == 0;
1146
/************************************************************************/
1147
/* DBFIsAttributeNULL() */
1149
/* Return TRUE if value for field is NULL. */
1151
/* Contributed by Jim Matthews. */
1152
/************************************************************************/
1155
DBFIsAttributeNULL( DBFHandle psDBF, int iRecord, int iField )
1158
const char *pszValue;
1160
pszValue = DBFReadStringAttribute( psDBF, iRecord, iField );
1162
if( pszValue == NULL )
1165
return DBFIsValueNULL( psDBF->pachFieldType[iField], pszValue );
1168
/************************************************************************/
1169
/* DBFGetFieldCount() */
1171
/* Return the number of fields in this table. */
1172
/************************************************************************/
1175
DBFGetFieldCount( DBFHandle psDBF )
1178
return( psDBF->nFields );
1181
/************************************************************************/
1182
/* DBFGetRecordCount() */
1184
/* Return the number of records in this table. */
1185
/************************************************************************/
1188
DBFGetRecordCount( DBFHandle psDBF )
1191
return( psDBF->nRecords );
1194
/************************************************************************/
1195
/* DBFGetFieldInfo() */
1197
/* Return any requested information about the field. */
1198
/************************************************************************/
1200
DBFFieldType SHPAPI_CALL
1201
DBFGetFieldInfo( DBFHandle psDBF, int iField, char * pszFieldName,
1202
int * pnWidth, int * pnDecimals )
1205
if( iField < 0 || iField >= psDBF->nFields )
1206
return( FTInvalid );
1208
if( pnWidth != NULL )
1209
*pnWidth = psDBF->panFieldSize[iField];
1211
if( pnDecimals != NULL )
1212
*pnDecimals = psDBF->panFieldDecimals[iField];
1214
if( pszFieldName != NULL )
1218
strncpy( pszFieldName, (char *) psDBF->pszHeader+iField*32, 11 );
1219
pszFieldName[11] = '\0';
1220
for( i = 10; i > 0 && pszFieldName[i] == ' '; i-- )
1221
pszFieldName[i] = '\0';
1224
if ( psDBF->pachFieldType[iField] == 'L' )
1227
else if( psDBF->pachFieldType[iField] == 'N'
1228
|| psDBF->pachFieldType[iField] == 'F' )
1230
if( psDBF->panFieldDecimals[iField] > 0
1231
|| psDBF->panFieldSize[iField] > 10 )
1234
return( FTInteger );
1242
/************************************************************************/
1243
/* DBFWriteAttribute() */
1245
/* Write an attribute record to the file. */
1246
/************************************************************************/
1248
static int DBFWriteAttribute(DBFHandle psDBF, int hEntity, int iField,
1252
int i, j, nRetResult = TRUE;
1253
unsigned char *pabyRec;
1254
char szSField[400], szFormat[20];
1256
/* -------------------------------------------------------------------- */
1257
/* Is this a valid record? */
1258
/* -------------------------------------------------------------------- */
1259
if( hEntity < 0 || hEntity > psDBF->nRecords )
1262
if( psDBF->bNoHeader )
1263
DBFWriteHeader(psDBF);
1265
/* -------------------------------------------------------------------- */
1266
/* Is this a brand new record? */
1267
/* -------------------------------------------------------------------- */
1268
if( hEntity == psDBF->nRecords )
1270
if( !DBFFlushRecord( psDBF ) )
1274
for( i = 0; i < psDBF->nRecordLength; i++ )
1275
psDBF->pszCurrentRecord[i] = ' ';
1277
psDBF->nCurrentRecord = hEntity;
1280
/* -------------------------------------------------------------------- */
1281
/* Is this an existing record, but different than the last one */
1283
/* -------------------------------------------------------------------- */
1284
if( !DBFLoadRecord( psDBF, hEntity ) )
1287
pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
1289
psDBF->bCurrentRecordModified = TRUE;
1290
psDBF->bUpdated = TRUE;
1292
/* -------------------------------------------------------------------- */
1293
/* Translate NULL value to valid DBF file representation. */
1295
/* Contributed by Jim Matthews. */
1296
/* -------------------------------------------------------------------- */
1297
if( pValue == NULL )
1299
memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]),
1300
DBFGetNullCharacter(psDBF->pachFieldType[iField]),
1301
psDBF->panFieldSize[iField] );
1305
/* -------------------------------------------------------------------- */
1306
/* Assign all the record fields. */
1307
/* -------------------------------------------------------------------- */
1308
switch( psDBF->pachFieldType[iField] )
1313
if( psDBF->panFieldDecimals[iField] == 0 )
1315
int nWidth = psDBF->panFieldSize[iField];
1317
if( (int) sizeof(szSField)-2 < nWidth )
1318
nWidth = sizeof(szSField)-2;
1320
sprintf( szFormat, "%%%dd", nWidth );
1321
sprintf(szSField, szFormat, (int) *((double *) pValue) );
1322
if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )
1324
szSField[psDBF->panFieldSize[iField]] = '\0';
1328
strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1329
szSField, strlen(szSField) );
1333
int nWidth = psDBF->panFieldSize[iField];
1335
if( (int) sizeof(szSField)-2 < nWidth )
1336
nWidth = sizeof(szSField)-2;
1338
sprintf( szFormat, "%%%d.%df",
1339
nWidth, psDBF->panFieldDecimals[iField] );
1340
sprintf(szSField, szFormat, *((double *) pValue) );
1341
if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )
1343
szSField[psDBF->panFieldSize[iField]] = '\0';
1346
strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1347
szSField, strlen(szSField) );
1352
if (psDBF->panFieldSize[iField] >= 1 &&
1353
(*(char*)pValue == 'F' || *(char*)pValue == 'T'))
1354
*(pabyRec+psDBF->panFieldOffset[iField]) = *(char*)pValue;
1358
if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1360
j = psDBF->panFieldSize[iField];
1365
memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
1366
psDBF->panFieldSize[iField] );
1367
j = strlen((char *) pValue);
1370
strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1371
(char *) pValue, j );
1375
return( nRetResult );
1378
/************************************************************************/
1379
/* DBFWriteAttributeDirectly() */
1381
/* Write an attribute record to the file, but without any */
1382
/* reformatting based on type. The provided buffer is written */
1383
/* as is to the field position in the record. */
1384
/************************************************************************/
1387
DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
1392
unsigned char *pabyRec;
1394
/* -------------------------------------------------------------------- */
1395
/* Is this a valid record? */
1396
/* -------------------------------------------------------------------- */
1397
if( hEntity < 0 || hEntity > psDBF->nRecords )
1400
if( psDBF->bNoHeader )
1401
DBFWriteHeader(psDBF);
1403
/* -------------------------------------------------------------------- */
1404
/* Is this a brand new record? */
1405
/* -------------------------------------------------------------------- */
1406
if( hEntity == psDBF->nRecords )
1408
if( !DBFFlushRecord( psDBF ) )
1412
for( i = 0; i < psDBF->nRecordLength; i++ )
1413
psDBF->pszCurrentRecord[i] = ' ';
1415
psDBF->nCurrentRecord = hEntity;
1418
/* -------------------------------------------------------------------- */
1419
/* Is this an existing record, but different than the last one */
1421
/* -------------------------------------------------------------------- */
1422
if( !DBFLoadRecord( psDBF, hEntity ) )
1425
pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
1427
/* -------------------------------------------------------------------- */
1428
/* Assign all the record fields. */
1429
/* -------------------------------------------------------------------- */
1430
if( (int)strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1431
j = psDBF->panFieldSize[iField];
1434
memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
1435
psDBF->panFieldSize[iField] );
1436
j = strlen((char *) pValue);
1439
strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1440
(char *) pValue, j );
1442
psDBF->bCurrentRecordModified = TRUE;
1443
psDBF->bUpdated = TRUE;
1448
/************************************************************************/
1449
/* DBFWriteDoubleAttribute() */
1451
/* Write a double attribute. */
1452
/************************************************************************/
1455
DBFWriteDoubleAttribute( DBFHandle psDBF, int iRecord, int iField,
1459
return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );
1462
/************************************************************************/
1463
/* DBFWriteIntegerAttribute() */
1465
/* Write a integer attribute. */
1466
/************************************************************************/
1469
DBFWriteIntegerAttribute( DBFHandle psDBF, int iRecord, int iField,
1473
double dValue = nValue;
1475
return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );
1478
/************************************************************************/
1479
/* DBFWriteStringAttribute() */
1481
/* Write a string attribute. */
1482
/************************************************************************/
1485
DBFWriteStringAttribute( DBFHandle psDBF, int iRecord, int iField,
1486
const char * pszValue )
1489
return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) pszValue ) );
1492
/************************************************************************/
1493
/* DBFWriteNULLAttribute() */
1495
/* Write a string attribute. */
1496
/************************************************************************/
1499
DBFWriteNULLAttribute( DBFHandle psDBF, int iRecord, int iField )
1502
return( DBFWriteAttribute( psDBF, iRecord, iField, NULL ) );
1505
/************************************************************************/
1506
/* DBFWriteLogicalAttribute() */
1508
/* Write a logical attribute. */
1509
/************************************************************************/
1512
DBFWriteLogicalAttribute( DBFHandle psDBF, int iRecord, int iField,
1516
return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) (&lValue) ) );
1519
/************************************************************************/
1520
/* DBFWriteTuple() */
1522
/* Write an attribute record to the file. */
1523
/************************************************************************/
1526
DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple )
1530
unsigned char *pabyRec;
1532
/* -------------------------------------------------------------------- */
1533
/* Is this a valid record? */
1534
/* -------------------------------------------------------------------- */
1535
if( hEntity < 0 || hEntity > psDBF->nRecords )
1538
if( psDBF->bNoHeader )
1539
DBFWriteHeader(psDBF);
1541
/* -------------------------------------------------------------------- */
1542
/* Is this a brand new record? */
1543
/* -------------------------------------------------------------------- */
1544
if( hEntity == psDBF->nRecords )
1546
if( !DBFFlushRecord( psDBF ) )
1550
for( i = 0; i < psDBF->nRecordLength; i++ )
1551
psDBF->pszCurrentRecord[i] = ' ';
1553
psDBF->nCurrentRecord = hEntity;
1556
/* -------------------------------------------------------------------- */
1557
/* Is this an existing record, but different than the last one */
1559
/* -------------------------------------------------------------------- */
1560
if( !DBFLoadRecord( psDBF, hEntity ) )
1563
pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
1565
memcpy ( pabyRec, pRawTuple, psDBF->nRecordLength );
1567
psDBF->bCurrentRecordModified = TRUE;
1568
psDBF->bUpdated = TRUE;
1573
/************************************************************************/
1574
/* DBFReadTuple() */
1576
/* Read a complete record. Note that the result is only valid */
1577
/* till the next record read for any reason. */
1578
/************************************************************************/
1580
const char SHPAPI_CALL1(*)
1581
DBFReadTuple(DBFHandle psDBF, int hEntity )
1584
if( hEntity < 0 || hEntity >= psDBF->nRecords )
1587
if( !DBFLoadRecord( psDBF, hEntity ) )
1590
return (const char *) psDBF->pszCurrentRecord;
1593
/************************************************************************/
1594
/* DBFCloneEmpty() */
1596
/* Read one of the attribute fields of a record. */
1597
/************************************************************************/
1599
DBFHandle SHPAPI_CALL
1600
DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename )
1604
newDBF = DBFCreateEx ( pszFilename, psDBF->pszCodePage );
1605
if ( newDBF == NULL ) return ( NULL );
1607
newDBF->nFields = psDBF->nFields;
1608
newDBF->nRecordLength = psDBF->nRecordLength;
1609
newDBF->nHeaderLength = psDBF->nHeaderLength;
1611
newDBF->pszHeader = (char *) malloc ( newDBF->nHeaderLength );
1612
memcpy ( newDBF->pszHeader, psDBF->pszHeader, newDBF->nHeaderLength );
1614
newDBF->panFieldOffset = (int *) malloc ( sizeof(int) * psDBF->nFields );
1615
memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
1616
newDBF->panFieldSize = (int *) malloc ( sizeof(int) * psDBF->nFields );
1617
memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
1618
newDBF->panFieldDecimals = (int *) malloc ( sizeof(int) * psDBF->nFields );
1619
memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
1620
newDBF->pachFieldType = (char *) malloc ( sizeof(char) * psDBF->nFields );
1621
memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(char)*psDBF->nFields );
1623
newDBF->bNoHeader = TRUE;
1624
newDBF->bUpdated = TRUE;
1626
DBFWriteHeader ( newDBF );
1627
DBFClose ( newDBF );
1629
newDBF = DBFOpen ( pszFilename, "rb+" );
1634
/************************************************************************/
1635
/* DBFGetNativeFieldType() */
1637
/* Return the DBase field type for the specified field. */
1639
/* Value can be one of: 'C' (String), 'D' (Date), 'F' (Float), */
1640
/* 'N' (Numeric, with or without decimal), */
1641
/* 'L' (Logical), */
1642
/* 'M' (Memo: 10 digits .DBT block ptr) */
1643
/************************************************************************/
1646
DBFGetNativeFieldType( DBFHandle psDBF, int iField )
1649
if( iField >=0 && iField < psDBF->nFields )
1650
return psDBF->pachFieldType[iField];
1655
/************************************************************************/
1656
/* str_to_upper() */
1657
/************************************************************************/
1659
static void str_to_upper (char *string)
1664
len = strlen (string);
1667
if (isalpha(string[i]) && islower(string[i]))
1668
string[i] = (char) toupper ((int)string[i]);
1671
/************************************************************************/
1672
/* DBFGetFieldIndex() */
1674
/* Get the index number for a field in a .dbf file. */
1676
/* Contributed by Jim Matthews. */
1677
/************************************************************************/
1680
DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName)
1683
char name[12], name1[12], name2[12];
1686
strncpy(name1, pszFieldName,11);
1688
str_to_upper(name1);
1690
for( i = 0; i < DBFGetFieldCount(psDBF); i++ )
1692
DBFGetFieldInfo( psDBF, i, name, NULL, NULL );
1693
strncpy(name2,name,11);
1694
str_to_upper(name2);
1696
if(!strncmp(name1,name2,10))
1702
/************************************************************************/
1703
/* DBFIsRecordDeleted() */
1705
/* Returns TRUE if the indicated record is deleted, otherwise */
1706
/* it returns FALSE. */
1707
/************************************************************************/
1709
int SHPAPI_CALL DBFIsRecordDeleted( DBFHandle psDBF, int iShape )
1712
/* -------------------------------------------------------------------- */
1713
/* Verify selection. */
1714
/* -------------------------------------------------------------------- */
1715
if( iShape < 0 || iShape >= psDBF->nRecords )
1718
/* -------------------------------------------------------------------- */
1719
/* Have we read the record? */
1720
/* -------------------------------------------------------------------- */
1721
if( !DBFLoadRecord( psDBF, iShape ) )
1724
/* -------------------------------------------------------------------- */
1725
/* '*' means deleted. */
1726
/* -------------------------------------------------------------------- */
1727
return psDBF->pszCurrentRecord[0] == '*';
1730
/************************************************************************/
1731
/* DBFMarkRecordDeleted() */
1732
/************************************************************************/
1734
int SHPAPI_CALL DBFMarkRecordDeleted( DBFHandle psDBF, int iShape,
1740
/* -------------------------------------------------------------------- */
1741
/* Verify selection. */
1742
/* -------------------------------------------------------------------- */
1743
if( iShape < 0 || iShape >= psDBF->nRecords )
1746
/* -------------------------------------------------------------------- */
1747
/* Is this an existing record, but different than the last one */
1749
/* -------------------------------------------------------------------- */
1750
if( !DBFLoadRecord( psDBF, iShape ) )
1753
/* -------------------------------------------------------------------- */
1754
/* Assign value, marking record as dirty if it changes. */
1755
/* -------------------------------------------------------------------- */
1761
if( psDBF->pszCurrentRecord[0] != chNewFlag )
1763
psDBF->bCurrentRecordModified = TRUE;
1764
psDBF->bUpdated = TRUE;
1765
psDBF->pszCurrentRecord[0] = chNewFlag;
1771
/************************************************************************/
1772
/* DBFGetCodePage */
1773
/************************************************************************/
1775
const char SHPAPI_CALL1(*)
1776
DBFGetCodePage(DBFHandle psDBF )
1780
return psDBF->pszCodePage;
1783
/************************************************************************/
1784
/* DBFDeleteField() */
1786
/* Remove a field from a .dbf file */
1787
/************************************************************************/
1790
DBFDeleteField(DBFHandle psDBF, int iField)
1792
int nOldRecordLength, nOldHeaderLength;
1793
int nDeletedFieldOffset, nDeletedFieldSize;
1794
SAOffset nRecordOffset;
1798
if (iField < 0 || iField >= psDBF->nFields)
1801
/* make sure that everything is written in .dbf */
1802
if( !DBFFlushRecord( psDBF ) )
1805
/* get information about field to be deleted */
1806
nOldRecordLength = psDBF->nRecordLength;
1807
nOldHeaderLength = psDBF->nHeaderLength;
1808
nDeletedFieldOffset = psDBF->panFieldOffset[iField];
1809
nDeletedFieldSize = psDBF->panFieldSize[iField];
1811
/* update fields info */
1812
for (i = iField + 1; i < psDBF->nFields; i++)
1814
psDBF->panFieldOffset[i-1] = psDBF->panFieldOffset[i] - nDeletedFieldSize;
1815
psDBF->panFieldSize[i-1] = psDBF->panFieldSize[i];
1816
psDBF->panFieldDecimals[i-1] = psDBF->panFieldDecimals[i];
1817
psDBF->pachFieldType[i-1] = psDBF->pachFieldType[i];
1820
/* resize fields arrays */
1823
psDBF->panFieldOffset = (int *)
1824
SfRealloc( psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
1826
psDBF->panFieldSize = (int *)
1827
SfRealloc( psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
1829
psDBF->panFieldDecimals = (int *)
1830
SfRealloc( psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
1832
psDBF->pachFieldType = (char *)
1833
SfRealloc( psDBF->pachFieldType, sizeof(char) * psDBF->nFields );
1835
/* update header information */
1836
psDBF->nHeaderLength -= 32;
1837
psDBF->nRecordLength -= nDeletedFieldSize;
1839
/* overwrite field information in header */
1840
memmove(psDBF->pszHeader + iField*32,
1841
psDBF->pszHeader + (iField+1)*32,
1842
sizeof(char) * (psDBF->nFields - iField)*32);
1844
psDBF->pszHeader = (char *) SfRealloc(psDBF->pszHeader,psDBF->nFields*32);
1846
/* update size of current record appropriately */
1847
psDBF->pszCurrentRecord = (char *) SfRealloc(psDBF->pszCurrentRecord,
1848
psDBF->nRecordLength);
1850
/* we're done if we're dealing with not yet created .dbf */
1851
if ( psDBF->bNoHeader && psDBF->nRecords == 0 )
1854
/* force update of header with new header and record length */
1855
psDBF->bNoHeader = TRUE;
1856
DBFUpdateHeader( psDBF );
1859
pszRecord = (char *) malloc(sizeof(char) * nOldRecordLength);
1861
/* shift records to their new positions */
1862
for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
1865
nOldRecordLength * (SAOffset) iRecord + nOldHeaderLength;
1868
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1869
psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
1872
psDBF->nRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
1874
/* move record in two steps */
1875
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1876
psDBF->sHooks.FWrite( pszRecord, nDeletedFieldOffset, 1, psDBF->fp );
1877
psDBF->sHooks.FWrite( pszRecord + nDeletedFieldOffset + nDeletedFieldSize,
1878
nOldRecordLength - nDeletedFieldOffset - nDeletedFieldSize,
1883
/* TODO: truncate file */
1888
psDBF->nCurrentRecord = -1;
1889
psDBF->bCurrentRecordModified = FALSE;
1894
/************************************************************************/
1895
/* DBFReorderFields() */
1897
/* Reorder the fields of a .dbf file */
1899
/* panMap must be exactly psDBF->nFields long and be a permutation */
1900
/* of [0, psDBF->nFields-1]. This assumption will not be asserted in the*/
1901
/* code of DBFReorderFields. */
1902
/************************************************************************/
1905
DBFReorderFields( DBFHandle psDBF, int* panMap )
1907
SAOffset nRecordOffset;
1909
int *panFieldOffsetNew;
1910
int *panFieldSizeNew;
1911
int *panFieldDecimalsNew;
1912
char *pachFieldTypeNew;
1917
if ( psDBF->nFields == 0 )
1920
/* make sure that everything is written in .dbf */
1921
if( !DBFFlushRecord( psDBF ) )
1924
panFieldOffsetNew = (int *) malloc(sizeof(int) * psDBF->nFields);
1925
panFieldSizeNew = (int *) malloc(sizeof(int) * psDBF->nFields);
1926
panFieldDecimalsNew = (int *) malloc(sizeof(int) * psDBF->nFields);
1927
pachFieldTypeNew = (char *) malloc(sizeof(char) * psDBF->nFields);
1928
pszHeaderNew = (char*) malloc(sizeof(char) * 32 * psDBF->nFields);
1930
/* shuffle fields definitions */
1931
for(i=0; i < psDBF->nFields; i++)
1933
panFieldSizeNew[i] = psDBF->panFieldSize[panMap[i]];
1934
panFieldDecimalsNew[i] = psDBF->panFieldDecimals[panMap[i]];
1935
pachFieldTypeNew[i] = psDBF->pachFieldType[panMap[i]];
1936
memcpy(pszHeaderNew + i * 32,
1937
psDBF->pszHeader + panMap[i] * 32, 32);
1939
panFieldOffsetNew[0] = 1;
1940
for(i=1; i < psDBF->nFields; i++)
1942
panFieldOffsetNew[i] = panFieldOffsetNew[i - 1] + panFieldSizeNew[i - 1];
1945
free(psDBF->pszHeader);
1946
psDBF->pszHeader = pszHeaderNew;
1948
/* we're done if we're dealing with not yet created .dbf */
1949
if ( !(psDBF->bNoHeader && psDBF->nRecords == 0) )
1951
/* force update of header with new header and record length */
1952
psDBF->bNoHeader = TRUE;
1953
DBFUpdateHeader( psDBF );
1956
pszRecord = (char *) malloc(sizeof(char) * psDBF->nRecordLength);
1957
pszRecordNew = (char *) malloc(sizeof(char) * psDBF->nRecordLength);
1959
/* shuffle fields in records */
1960
for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
1963
psDBF->nRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
1966
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1967
psDBF->sHooks.FRead( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
1969
pszRecordNew[0] = pszRecord[0];
1971
for(i=0; i < psDBF->nFields; i++)
1973
memcpy(pszRecordNew + panFieldOffsetNew[i],
1974
pszRecord + psDBF->panFieldOffset[panMap[i]],
1975
psDBF->panFieldSize[panMap[i]]);
1979
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1980
psDBF->sHooks.FWrite( pszRecordNew, psDBF->nRecordLength, 1, psDBF->fp );
1988
free(psDBF->panFieldOffset);
1989
free(psDBF->panFieldSize);
1990
free(psDBF->panFieldDecimals);
1991
free(psDBF->pachFieldType);
1993
psDBF->panFieldOffset = panFieldOffsetNew;
1994
psDBF->panFieldSize = panFieldSizeNew;
1995
psDBF->panFieldDecimals =panFieldDecimalsNew;
1996
psDBF->pachFieldType = pachFieldTypeNew;
1998
psDBF->nCurrentRecord = -1;
1999
psDBF->bCurrentRecordModified = FALSE;
2005
/************************************************************************/
2006
/* DBFAlterFieldDefn() */
2008
/* Alter a field definition in a .dbf file */
2009
/************************************************************************/
2012
DBFAlterFieldDefn( DBFHandle psDBF, int iField, const char * pszFieldName,
2013
char chType, int nWidth, int nDecimals )
2019
int nOldRecordLength;
2026
if (iField < 0 || iField >= psDBF->nFields)
2029
/* make sure that everything is written in .dbf */
2030
if( !DBFFlushRecord( psDBF ) )
2033
chFieldFill = DBFGetNullCharacter(chType);
2035
chOldType = psDBF->pachFieldType[iField];
2036
nOffset = psDBF->panFieldOffset[iField];
2037
nOldWidth = psDBF->panFieldSize[iField];
2038
nOldRecordLength = psDBF->nRecordLength;
2040
/* -------------------------------------------------------------------- */
2041
/* Do some checking to ensure we can add records to this file. */
2042
/* -------------------------------------------------------------------- */
2049
/* -------------------------------------------------------------------- */
2050
/* Assign the new field information fields. */
2051
/* -------------------------------------------------------------------- */
2052
psDBF->panFieldSize[iField] = nWidth;
2053
psDBF->panFieldDecimals[iField] = nDecimals;
2054
psDBF->pachFieldType[iField] = chType;
2056
/* -------------------------------------------------------------------- */
2057
/* Update the header information. */
2058
/* -------------------------------------------------------------------- */
2059
pszFInfo = psDBF->pszHeader + 32 * iField;
2061
for( i = 0; i < 32; i++ )
2064
if( (int) strlen(pszFieldName) < 10 )
2065
strncpy( pszFInfo, pszFieldName, strlen(pszFieldName));
2067
strncpy( pszFInfo, pszFieldName, 10);
2069
pszFInfo[11] = psDBF->pachFieldType[iField];
2073
pszFInfo[16] = (unsigned char) (nWidth % 256);
2074
pszFInfo[17] = (unsigned char) (nWidth / 256);
2078
pszFInfo[16] = (unsigned char) nWidth;
2079
pszFInfo[17] = (unsigned char) nDecimals;
2082
/* -------------------------------------------------------------------- */
2083
/* Update offsets */
2084
/* -------------------------------------------------------------------- */
2085
if (nWidth != nOldWidth)
2087
for (i = iField + 1; i < psDBF->nFields; i++)
2088
psDBF->panFieldOffset[i] += nWidth - nOldWidth;
2089
psDBF->nRecordLength += nWidth - nOldWidth;
2091
psDBF->pszCurrentRecord = (char *) SfRealloc(psDBF->pszCurrentRecord,
2092
psDBF->nRecordLength);
2095
/* we're done if we're dealing with not yet created .dbf */
2096
if ( psDBF->bNoHeader && psDBF->nRecords == 0 )
2099
/* force update of header with new header and record length */
2100
psDBF->bNoHeader = TRUE;
2101
DBFUpdateHeader( psDBF );
2103
if (nWidth < nOldWidth || (nWidth == nOldWidth && chType != chOldType))
2105
char* pszRecord = (char *) malloc(sizeof(char) * nOldRecordLength);
2106
char* pszOldField = (char *) malloc(sizeof(char) * (nOldWidth + 1));
2108
pszOldField[nOldWidth] = 0;
2110
/* move records to their new positions */
2111
for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
2114
nOldRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
2117
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
2118
psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
2120
memcpy(pszOldField, pszRecord + nOffset, nOldWidth);
2121
bIsNULL = DBFIsValueNULL( chOldType, pszOldField );
2123
if (nWidth != nOldWidth)
2125
if ((chOldType == 'N' || chOldType == 'F') && pszOldField[0] == ' ')
2127
/* Strip leading spaces when truncating a numeric field */
2128
memmove( pszRecord + nOffset,
2129
pszRecord + nOffset + nOldWidth - nWidth,
2132
if (nOffset + nOldWidth < nOldRecordLength)
2134
memmove( pszRecord + nOffset + nWidth,
2135
pszRecord + nOffset + nOldWidth,
2136
nOldRecordLength - (nOffset + nOldWidth));
2140
/* Convert null value to the appropriate value of the new type */
2143
memset( pszRecord + nOffset, chFieldFill, nWidth);
2147
psDBF->nRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
2150
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
2151
psDBF->sHooks.FWrite( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
2157
else if (nWidth > nOldWidth)
2159
char* pszRecord = (char *) malloc(sizeof(char) * psDBF->nRecordLength);
2160
char* pszOldField = (char *) malloc(sizeof(char) * (nOldWidth + 1));
2162
pszOldField[nOldWidth] = 0;
2164
/* move records to their new positions */
2165
for (iRecord = psDBF->nRecords - 1; iRecord >= 0; iRecord--)
2168
nOldRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
2171
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
2172
psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
2174
memcpy(pszOldField, pszRecord + nOffset, nOldWidth);
2175
bIsNULL = DBFIsValueNULL( chOldType, pszOldField );
2177
if (nOffset + nOldWidth < nOldRecordLength)
2179
memmove( pszRecord + nOffset + nWidth,
2180
pszRecord + nOffset + nOldWidth,
2181
nOldRecordLength - (nOffset + nOldWidth));
2184
/* Convert null value to the appropriate value of the new type */
2187
memset( pszRecord + nOffset, chFieldFill, nWidth);
2191
if ((chOldType == 'N' || chOldType == 'F'))
2193
/* Add leading spaces when expanding a numeric field */
2194
memmove( pszRecord + nOffset + nWidth - nOldWidth,
2195
pszRecord + nOffset, nOldWidth );
2196
memset( pszRecord + nOffset, ' ', nWidth - nOldWidth );
2200
/* Add trailing spaces */
2201
memset(pszRecord + nOffset + nOldWidth, ' ', nWidth - nOldWidth);
2206
psDBF->nRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
2209
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
2210
psDBF->sHooks.FWrite( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
2217
psDBF->nCurrentRecord = -1;
2218
psDBF->bCurrentRecordModified = FALSE;