forked from OSGeo/shapelib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbfopen.c
2300 lines (1853 loc) · 82.6 KB
/
dbfopen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* $Id$
*
* Project: Shapelib
* Purpose: Implementation of .dbf access API documented in dbf_api.html.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2012-2019, Even Rouault <even dot rouault at spatialys.com>
*
* This software is available under the following "MIT Style" license,
* or at the option of the licensee under the LGPL (see COPYING). This
* option is discussed in more detail in shapelib.html.
*
* --
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************/
#include "shapefil.h"
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#ifdef USE_CPL
#include "cpl_string.h"
#else
#if defined(WIN32) || defined(_WIN32)
# define STRCASECMP(a,b) (stricmp(a,b))
# else
#include <strings.h>
# define STRCASECMP(a,b) (strcasecmp(a,b))
#endif
#if defined(_MSC_VER)
# if _MSC_VER < 1900
# define snprintf _snprintf
# endif
#elif defined(WIN32) || defined(_WIN32)
# ifndef snprintf
# define snprintf _snprintf
# endif
#endif
#define CPLsprintf sprintf
#define CPLsnprintf snprintf
#endif
SHP_CVSID("$Id$")
#ifndef FALSE
# define FALSE 0
# define TRUE 1
#endif
/* File header size */
#define XBASE_FILEHDR_SZ 32
#define HEADER_RECORD_TERMINATOR 0x0D
/* See http://www.manmrk.net/tutorials/database/xbase/dbf.html */
#define END_OF_FILE_CHARACTER 0x1A
#ifdef USE_CPL
CPL_INLINE static void CPL_IGNORE_RET_VAL_INT(CPL_UNUSED int unused) {}
#else
#define CPL_IGNORE_RET_VAL_INT(x) x
#endif
#ifdef __cplusplus
#define STATIC_CAST(type,x) static_cast<type>(x)
#define REINTERPRET_CAST(type,x) reinterpret_cast<type>(x)
#define CONST_CAST(type,x) const_cast<type>(x)
#define SHPLIB_NULLPTR nullptr
#else
#define STATIC_CAST(type,x) ((type)(x))
#define REINTERPRET_CAST(type,x) ((type)(x))
#define CONST_CAST(type,x) ((type)(x))
#define SHPLIB_NULLPTR NULL
#endif
/************************************************************************/
/* SfRealloc() */
/* */
/* A realloc cover function that will access a NULL pointer as */
/* a valid input. */
/************************************************************************/
static void * SfRealloc( void * pMem, int nNewSize )
{
if( pMem == SHPLIB_NULLPTR )
return malloc(nNewSize);
else
return realloc(pMem,nNewSize);
}
/************************************************************************/
/* DBFWriteHeader() */
/* */
/* This is called to write out the file header, and field */
/* descriptions before writing any actual data records. This */
/* also computes all the DBFDataSet field offset/size/decimals */
/* and so forth values. */
/************************************************************************/
static void DBFWriteHeader(DBFHandle psDBF)
{
unsigned char abyHeader[XBASE_FILEHDR_SZ] = { 0 };
if( !psDBF->bNoHeader )
return;
psDBF->bNoHeader = FALSE;
/* -------------------------------------------------------------------- */
/* Initialize the file header information. */
/* -------------------------------------------------------------------- */
abyHeader[0] = 0x03; /* memo field? - just copying */
/* write out update date */
abyHeader[1] = STATIC_CAST(unsigned char, psDBF->nUpdateYearSince1900);
abyHeader[2] = STATIC_CAST(unsigned char, psDBF->nUpdateMonth);
abyHeader[3] = STATIC_CAST(unsigned char, psDBF->nUpdateDay);
/* record count preset at zero */
abyHeader[8] = STATIC_CAST(unsigned char, psDBF->nHeaderLength % 256);
abyHeader[9] = STATIC_CAST(unsigned char, psDBF->nHeaderLength / 256);
abyHeader[10] = STATIC_CAST(unsigned char, psDBF->nRecordLength % 256);
abyHeader[11] = STATIC_CAST(unsigned char, psDBF->nRecordLength / 256);
abyHeader[29] = STATIC_CAST(unsigned char, psDBF->iLanguageDriver);
/* -------------------------------------------------------------------- */
/* Write the initial 32 byte file header, and all the field */
/* descriptions. */
/* -------------------------------------------------------------------- */
psDBF->sHooks.FSeek( psDBF->fp, 0, 0 );
psDBF->sHooks.FWrite( abyHeader, XBASE_FILEHDR_SZ, 1, psDBF->fp );
psDBF->sHooks.FWrite( psDBF->pszHeader, XBASE_FLDHDR_SZ, psDBF->nFields,
psDBF->fp );
/* -------------------------------------------------------------------- */
/* Write out the newline character if there is room for it. */
/* -------------------------------------------------------------------- */
if( psDBF->nHeaderLength > XBASE_FLDHDR_SZ*psDBF->nFields +
XBASE_FLDHDR_SZ )
{
char cNewline;
cNewline = HEADER_RECORD_TERMINATOR;
psDBF->sHooks.FWrite( &cNewline, 1, 1, psDBF->fp );
}
/* -------------------------------------------------------------------- */
/* If the file is new, add a EOF character. */
/* -------------------------------------------------------------------- */
if( psDBF->nRecords == 0 && psDBF->bWriteEndOfFileChar )
{
char ch = END_OF_FILE_CHARACTER;
psDBF->sHooks.FWrite( &ch, 1, 1, psDBF->fp );
}
}
/************************************************************************/
/* DBFFlushRecord() */
/* */
/* Write out the current record if there is one. */
/************************************************************************/
static int DBFFlushRecord( DBFHandle psDBF )
{
SAOffset nRecordOffset;
if( psDBF->bCurrentRecordModified && psDBF->nCurrentRecord > -1 )
{
psDBF->bCurrentRecordModified = FALSE;
nRecordOffset =
psDBF->nRecordLength * STATIC_CAST(SAOffset, psDBF->nCurrentRecord)
+ psDBF->nHeaderLength;
/* -------------------------------------------------------------------- */
/* Guard FSeek with check for whether we're already at position; */
/* no-op FSeeks defeat network filesystems' write buffering. */
/* -------------------------------------------------------------------- */
if ( psDBF->bRequireNextWriteSeek ||
psDBF->sHooks.FTell( psDBF->fp ) != nRecordOffset ) {
if ( psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 ) != 0 ) {
char szMessage[128];
snprintf( szMessage, sizeof(szMessage),
"Failure seeking to position before writing DBF record %d.",
psDBF->nCurrentRecord );
psDBF->sHooks.Error( szMessage );
return FALSE;
}
}
if ( psDBF->sHooks.FWrite( psDBF->pszCurrentRecord,
psDBF->nRecordLength,
1, psDBF->fp ) != 1 )
{
char szMessage[128];
snprintf( szMessage, sizeof(szMessage), "Failure writing DBF record %d.",
psDBF->nCurrentRecord );
psDBF->sHooks.Error( szMessage );
return FALSE;
}
/* -------------------------------------------------------------------- */
/* If next op is also a write, allow possible skipping of FSeek. */
/* -------------------------------------------------------------------- */
psDBF->bRequireNextWriteSeek = FALSE;
if( psDBF->nCurrentRecord == psDBF->nRecords - 1 )
{
if( psDBF->bWriteEndOfFileChar )
{
char ch = END_OF_FILE_CHARACTER;
psDBF->sHooks.FWrite( &ch, 1, 1, psDBF->fp );
}
}
}
return TRUE;
}
/************************************************************************/
/* DBFLoadRecord() */
/************************************************************************/
static int DBFLoadRecord( DBFHandle psDBF, int iRecord )
{
if( psDBF->nCurrentRecord != iRecord )
{
SAOffset nRecordOffset;
if( !DBFFlushRecord( psDBF ) )
return FALSE;
nRecordOffset =
psDBF->nRecordLength * STATIC_CAST(SAOffset,iRecord) + psDBF->nHeaderLength;
if( psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, SEEK_SET ) != 0 )
{
char szMessage[128];
snprintf( szMessage, sizeof(szMessage), "fseek(%ld) failed on DBF file.",
STATIC_CAST(long, nRecordOffset) );
psDBF->sHooks.Error( szMessage );
return FALSE;
}
if( psDBF->sHooks.FRead( psDBF->pszCurrentRecord,
psDBF->nRecordLength, 1, psDBF->fp ) != 1 )
{
char szMessage[128];
snprintf( szMessage, sizeof(szMessage), "fread(%d) failed on DBF file.",
psDBF->nRecordLength );
psDBF->sHooks.Error( szMessage );
return FALSE;
}
psDBF->nCurrentRecord = iRecord;
/* -------------------------------------------------------------------- */
/* Require a seek for next write in case of mixed R/W operations. */
/* -------------------------------------------------------------------- */
psDBF->bRequireNextWriteSeek = TRUE;
}
return TRUE;
}
/************************************************************************/
/* DBFUpdateHeader() */
/************************************************************************/
void SHPAPI_CALL
DBFUpdateHeader( DBFHandle psDBF )
{
unsigned char abyFileHeader[XBASE_FILEHDR_SZ];
if( psDBF->bNoHeader )
DBFWriteHeader( psDBF );
if( !DBFFlushRecord( psDBF ) )
return;
psDBF->sHooks.FSeek( psDBF->fp, 0, 0 );
psDBF->sHooks.FRead( abyFileHeader, sizeof(abyFileHeader), 1, psDBF->fp );
abyFileHeader[1] = STATIC_CAST(unsigned char, psDBF->nUpdateYearSince1900);
abyFileHeader[2] = STATIC_CAST(unsigned char, psDBF->nUpdateMonth);
abyFileHeader[3] = STATIC_CAST(unsigned char, psDBF->nUpdateDay);
abyFileHeader[4] = STATIC_CAST(unsigned char, psDBF->nRecords & 0xFF);
abyFileHeader[5] = STATIC_CAST(unsigned char, (psDBF->nRecords>>8) & 0xFF);
abyFileHeader[6] = STATIC_CAST(unsigned char, (psDBF->nRecords>>16) & 0xFF);
abyFileHeader[7] = STATIC_CAST(unsigned char, (psDBF->nRecords>>24) & 0xFF);
psDBF->sHooks.FSeek( psDBF->fp, 0, 0 );
psDBF->sHooks.FWrite( abyFileHeader, sizeof(abyFileHeader), 1, psDBF->fp );
psDBF->sHooks.FFlush( psDBF->fp );
}
/************************************************************************/
/* DBFSetLastModifiedDate() */
/************************************************************************/
void SHPAPI_CALL
DBFSetLastModifiedDate( DBFHandle psDBF, int nYYSince1900, int nMM, int nDD )
{
psDBF->nUpdateYearSince1900 = nYYSince1900;
psDBF->nUpdateMonth = nMM;
psDBF->nUpdateDay = nDD;
}
/************************************************************************/
/* DBFOpen() */
/* */
/* Open a .dbf file. */
/************************************************************************/
DBFHandle SHPAPI_CALL
DBFOpen( const char * pszFilename, const char * pszAccess )
{
SAHooks sHooks;
SASetupDefaultHooks( &sHooks );
return DBFOpenLL( pszFilename, pszAccess, &sHooks );
}
/************************************************************************/
/* DBFGetLenWithoutExtension() */
/************************************************************************/
static int DBFGetLenWithoutExtension(const char* pszBasename)
{
int i;
int nLen = STATIC_CAST(int, strlen(pszBasename));
for( i = nLen-1;
i > 0 && pszBasename[i] != '/' && pszBasename[i] != '\\';
i-- )
{
if( pszBasename[i] == '.' )
{
return i;
}
}
return nLen;
}
/************************************************************************/
/* DBFOpen() */
/* */
/* Open a .dbf file. */
/************************************************************************/
DBFHandle SHPAPI_CALL
DBFOpenLL( const char * pszFilename, const char * pszAccess, SAHooks *psHooks )
{
DBFHandle psDBF;
SAFile pfCPG;
unsigned char *pabyBuf;
int nFields, nHeadLen, iField;
char *pszFullname;
int nBufSize = 500;
int nLenWithoutExtension;
/* -------------------------------------------------------------------- */
/* We only allow the access strings "rb" and "r+". */
/* -------------------------------------------------------------------- */
if( strcmp(pszAccess,"r") != 0 && strcmp(pszAccess,"r+") != 0
&& strcmp(pszAccess,"rb") != 0 && strcmp(pszAccess,"rb+") != 0
&& strcmp(pszAccess,"r+b") != 0 )
return SHPLIB_NULLPTR;
if( strcmp(pszAccess,"r") == 0 )
pszAccess = "rb";
if( strcmp(pszAccess,"r+") == 0 )
pszAccess = "rb+";
/* -------------------------------------------------------------------- */
/* Compute the base (layer) name. If there is any extension */
/* on the passed in filename we will strip it off. */
/* -------------------------------------------------------------------- */
nLenWithoutExtension = DBFGetLenWithoutExtension(pszFilename);
pszFullname = STATIC_CAST(char *, malloc(nLenWithoutExtension + 5));
memcpy(pszFullname, pszFilename, nLenWithoutExtension);
memcpy(pszFullname + nLenWithoutExtension, ".dbf", 5);
psDBF = STATIC_CAST(DBFHandle, calloc( 1, sizeof(DBFInfo) ));
psDBF->fp = psHooks->FOpen( pszFullname, pszAccess );
memcpy( &(psDBF->sHooks), psHooks, sizeof(SAHooks) );
if( psDBF->fp == SHPLIB_NULLPTR )
{
memcpy(pszFullname + nLenWithoutExtension, ".DBF", 5);
psDBF->fp = psDBF->sHooks.FOpen(pszFullname, pszAccess );
}
memcpy(pszFullname + nLenWithoutExtension, ".cpg", 5);
pfCPG = psHooks->FOpen( pszFullname, "r" );
if( pfCPG == SHPLIB_NULLPTR )
{
memcpy(pszFullname + nLenWithoutExtension, ".CPG", 5);
pfCPG = psHooks->FOpen( pszFullname, "r" );
}
free( pszFullname );
if( psDBF->fp == SHPLIB_NULLPTR )
{
free( psDBF );
if( pfCPG ) psHooks->FClose( pfCPG );
return SHPLIB_NULLPTR;
}
psDBF->bNoHeader = FALSE;
psDBF->nCurrentRecord = -1;
psDBF->bCurrentRecordModified = FALSE;
/* -------------------------------------------------------------------- */
/* Read Table Header info */
/* -------------------------------------------------------------------- */
pabyBuf = STATIC_CAST(unsigned char *, malloc(nBufSize));
if( psDBF->sHooks.FRead( pabyBuf, XBASE_FILEHDR_SZ, 1, psDBF->fp ) != 1 )
{
psDBF->sHooks.FClose( psDBF->fp );
if( pfCPG ) psDBF->sHooks.FClose( pfCPG );
free( pabyBuf );
free( psDBF );
return SHPLIB_NULLPTR;
}
DBFSetLastModifiedDate(psDBF, pabyBuf[1], pabyBuf[2], pabyBuf[3]);
psDBF->nRecords =
pabyBuf[4]|(pabyBuf[5]<<8)|(pabyBuf[6]<<16)|((pabyBuf[7]&0x7f)<<24);
psDBF->nHeaderLength = nHeadLen = pabyBuf[8]|(pabyBuf[9]<<8);
psDBF->nRecordLength = pabyBuf[10]|(pabyBuf[11]<<8);
psDBF->iLanguageDriver = pabyBuf[29];
if (psDBF->nRecordLength == 0 || nHeadLen < XBASE_FILEHDR_SZ)
{
psDBF->sHooks.FClose( psDBF->fp );
if( pfCPG ) psDBF->sHooks.FClose( pfCPG );
free( pabyBuf );
free( psDBF );
return SHPLIB_NULLPTR;
}
psDBF->nFields = nFields = (nHeadLen - XBASE_FILEHDR_SZ) / XBASE_FLDHDR_SZ;
/* coverity[tainted_data] */
psDBF->pszCurrentRecord = STATIC_CAST(char *, malloc(psDBF->nRecordLength));
/* -------------------------------------------------------------------- */
/* Figure out the code page from the LDID and CPG */
/* -------------------------------------------------------------------- */
psDBF->pszCodePage = SHPLIB_NULLPTR;
if( pfCPG )
{
size_t n;
memset( pabyBuf, 0, nBufSize);
psDBF->sHooks.FRead( pabyBuf, nBufSize - 1, 1, pfCPG );
n = strcspn( REINTERPRET_CAST(char *, pabyBuf), "\n\r" );
if( n > 0 )
{
pabyBuf[n] = '\0';
psDBF->pszCodePage = STATIC_CAST(char *, malloc(n + 1));
memcpy( psDBF->pszCodePage, pabyBuf, n + 1 );
}
psDBF->sHooks.FClose( pfCPG );
}
if( psDBF->pszCodePage == SHPLIB_NULLPTR && pabyBuf[29] != 0 )
{
snprintf( REINTERPRET_CAST(char *, pabyBuf), nBufSize, "LDID/%d", psDBF->iLanguageDriver );
psDBF->pszCodePage = STATIC_CAST(char *, malloc(strlen(REINTERPRET_CAST(char*, pabyBuf)) + 1));
strcpy( psDBF->pszCodePage, REINTERPRET_CAST(char *, pabyBuf) );
}
/* -------------------------------------------------------------------- */
/* Read in Field Definitions */
/* -------------------------------------------------------------------- */
pabyBuf = STATIC_CAST(unsigned char *, SfRealloc(pabyBuf,nHeadLen));
psDBF->pszHeader = REINTERPRET_CAST(char *, pabyBuf);
psDBF->sHooks.FSeek( psDBF->fp, XBASE_FILEHDR_SZ, 0 );
if( psDBF->sHooks.FRead( pabyBuf, nHeadLen-XBASE_FILEHDR_SZ, 1,
psDBF->fp ) != 1 )
{
psDBF->sHooks.FClose( psDBF->fp );
free( pabyBuf );
free( psDBF->pszCurrentRecord );
free( psDBF->pszCodePage );
free( psDBF );
return SHPLIB_NULLPTR;
}
psDBF->panFieldOffset = STATIC_CAST(int *, malloc(sizeof(int) * nFields));
psDBF->panFieldSize = STATIC_CAST(int *, malloc(sizeof(int) * nFields));
psDBF->panFieldDecimals = STATIC_CAST(int *, malloc(sizeof(int) * nFields));
psDBF->pachFieldType = STATIC_CAST(char *, malloc(sizeof(char) * nFields));
for( iField = 0; iField < nFields; iField++ )
{
unsigned char *pabyFInfo;
pabyFInfo = pabyBuf+iField*XBASE_FLDHDR_SZ;
if( pabyFInfo[0] == HEADER_RECORD_TERMINATOR )
{
psDBF->nFields = iField;
break;
}
if( pabyFInfo[11] == 'N' || pabyFInfo[11] == 'F' )
{
psDBF->panFieldSize[iField] = pabyFInfo[16];
psDBF->panFieldDecimals[iField] = pabyFInfo[17];
}
else
{
psDBF->panFieldSize[iField] = pabyFInfo[16];
psDBF->panFieldDecimals[iField] = 0;
/*
** The following seemed to be used sometimes to handle files with long
** string fields, but in other cases (such as bug 1202) the decimals field
** just seems to indicate some sort of preferred formatting, not very
** wide fields. So I have disabled this code. FrankW.
psDBF->panFieldSize[iField] = pabyFInfo[16] + pabyFInfo[17]*256;
psDBF->panFieldDecimals[iField] = 0;
*/
}
psDBF->pachFieldType[iField] = STATIC_CAST(char, pabyFInfo[11]);
if( iField == 0 )
psDBF->panFieldOffset[iField] = 1;
else
psDBF->panFieldOffset[iField] =
psDBF->panFieldOffset[iField-1] + psDBF->panFieldSize[iField-1];
}
/* Check that the total width of fields does not exceed the record width */
if( psDBF->nFields > 0 &&
psDBF->panFieldOffset[psDBF->nFields-1] +
psDBF->panFieldSize[psDBF->nFields-1] > psDBF->nRecordLength )
{
DBFClose( psDBF );
return SHPLIB_NULLPTR;
}
DBFSetWriteEndOfFileChar( psDBF, TRUE );
psDBF->bRequireNextWriteSeek = TRUE;
return( psDBF );
}
/************************************************************************/
/* DBFClose() */
/************************************************************************/
void SHPAPI_CALL
DBFClose(DBFHandle psDBF)
{
if( psDBF == SHPLIB_NULLPTR )
return;
/* -------------------------------------------------------------------- */
/* Write out header if not already written. */
/* -------------------------------------------------------------------- */
if( psDBF->bNoHeader )
DBFWriteHeader( psDBF );
CPL_IGNORE_RET_VAL_INT(DBFFlushRecord( psDBF ));
/* -------------------------------------------------------------------- */
/* Update last access date, and number of records if we have */
/* write access. */
/* -------------------------------------------------------------------- */
if( psDBF->bUpdated )
DBFUpdateHeader( psDBF );
/* -------------------------------------------------------------------- */
/* Close, and free resources. */
/* -------------------------------------------------------------------- */
psDBF->sHooks.FClose( psDBF->fp );
if( psDBF->panFieldOffset != SHPLIB_NULLPTR )
{
free( psDBF->panFieldOffset );
free( psDBF->panFieldSize );
free( psDBF->panFieldDecimals );
free( psDBF->pachFieldType );
}
if( psDBF->pszWorkField != SHPLIB_NULLPTR )
free( psDBF->pszWorkField );
free( psDBF->pszHeader );
free( psDBF->pszCurrentRecord );
free( psDBF->pszCodePage );
free( psDBF );
}
/************************************************************************/
/* DBFCreate() */
/* */
/* Create a new .dbf file with default code page LDID/87 (0x57) */
/************************************************************************/
DBFHandle SHPAPI_CALL
DBFCreate( const char * pszFilename )
{
return DBFCreateEx( pszFilename, "LDID/87" ); // 0x57
}
/************************************************************************/
/* DBFCreateEx() */
/* */
/* Create a new .dbf file. */
/************************************************************************/
DBFHandle SHPAPI_CALL
DBFCreateEx( const char * pszFilename, const char* pszCodePage )
{
SAHooks sHooks;
SASetupDefaultHooks( &sHooks );
return DBFCreateLL( pszFilename, pszCodePage , &sHooks );
}
/************************************************************************/
/* DBFCreate() */
/* */
/* Create a new .dbf file. */
/************************************************************************/
DBFHandle SHPAPI_CALL
DBFCreateLL( const char * pszFilename, const char * pszCodePage, SAHooks *psHooks )
{
DBFHandle psDBF;
SAFile fp;
char *pszFullname;
int ldid = -1;
char chZero = '\0';
int nLenWithoutExtension;
/* -------------------------------------------------------------------- */
/* Compute the base (layer) name. If there is any extension */
/* on the passed in filename we will strip it off. */
/* -------------------------------------------------------------------- */
nLenWithoutExtension = DBFGetLenWithoutExtension(pszFilename);
pszFullname = STATIC_CAST(char *, malloc(nLenWithoutExtension + 5));
memcpy(pszFullname, pszFilename, nLenWithoutExtension);
memcpy(pszFullname + nLenWithoutExtension, ".dbf", 5);
/* -------------------------------------------------------------------- */
/* Create the file. */
/* -------------------------------------------------------------------- */
fp = psHooks->FOpen( pszFullname, "wb" );
if( fp == SHPLIB_NULLPTR )
{
free( pszFullname );
return SHPLIB_NULLPTR;
}
psHooks->FWrite( &chZero, 1, 1, fp );
psHooks->FClose( fp );
fp = psHooks->FOpen( pszFullname, "rb+" );
if( fp == SHPLIB_NULLPTR )
{
free( pszFullname );
return SHPLIB_NULLPTR;
}
memcpy(pszFullname + nLenWithoutExtension, ".cpg", 5);
if( pszCodePage != SHPLIB_NULLPTR )
{
if( strncmp( pszCodePage, "LDID/", 5 ) == 0 )
{
ldid = atoi( pszCodePage + 5 );
if( ldid > 255 )
ldid = -1; // don't use 0 to indicate out of range as LDID/0 is a valid one
}
if( ldid < 0 )
{
SAFile fpCPG = psHooks->FOpen( pszFullname, "w" );
psHooks->FWrite( CONST_CAST(void*, STATIC_CAST(const void*, pszCodePage)), (SAOffset) strlen(pszCodePage), 1, fpCPG );
psHooks->FClose( fpCPG );
}
}
if( pszCodePage == SHPLIB_NULLPTR || ldid >= 0 )
{
psHooks->Remove( pszFullname );
}
free( pszFullname );
/* -------------------------------------------------------------------- */
/* Create the info structure. */
/* -------------------------------------------------------------------- */
psDBF = STATIC_CAST(DBFHandle, calloc(1,sizeof(DBFInfo)));
memcpy( &(psDBF->sHooks), psHooks, sizeof(SAHooks) );
psDBF->fp = fp;
psDBF->nRecords = 0;
psDBF->nFields = 0;
psDBF->nRecordLength = 1;
psDBF->nHeaderLength = XBASE_FILEHDR_SZ + 1; /* + 1 for HEADER_RECORD_TERMINATOR */
psDBF->panFieldOffset = SHPLIB_NULLPTR;
psDBF->panFieldSize = SHPLIB_NULLPTR;
psDBF->panFieldDecimals = SHPLIB_NULLPTR;
psDBF->pachFieldType = SHPLIB_NULLPTR;
psDBF->pszHeader = SHPLIB_NULLPTR;
psDBF->nCurrentRecord = -1;
psDBF->bCurrentRecordModified = FALSE;
psDBF->pszCurrentRecord = SHPLIB_NULLPTR;
psDBF->bNoHeader = TRUE;
psDBF->iLanguageDriver = ldid > 0 ? ldid : 0;
psDBF->pszCodePage = SHPLIB_NULLPTR;
if( pszCodePage )
{
psDBF->pszCodePage = STATIC_CAST(char *, malloc( strlen(pszCodePage) + 1 ));
strcpy( psDBF->pszCodePage, pszCodePage );
}
DBFSetLastModifiedDate(psDBF, 95, 7, 26); /* dummy date */
DBFSetWriteEndOfFileChar(psDBF, TRUE);
psDBF->bRequireNextWriteSeek = TRUE;
return( psDBF );
}
/************************************************************************/
/* DBFAddField() */
/* */
/* Add a field to a newly created .dbf or to an existing one */
/************************************************************************/
int SHPAPI_CALL
DBFAddField(DBFHandle psDBF, const char * pszFieldName,
DBFFieldType eType, int nWidth, int nDecimals )
{
char chNativeType = 'C';
if( eType == FTLogical )
chNativeType = 'L';
else if( eType == FTDate )
chNativeType = 'D';
else if( eType == FTString )
chNativeType = 'C';
else
chNativeType = 'N';
return DBFAddNativeFieldType( psDBF, pszFieldName, chNativeType,
nWidth, nDecimals );
}
/************************************************************************/
/* DBFGetNullCharacter() */
/************************************************************************/
static char DBFGetNullCharacter(char chType)
{
switch (chType)
{
case 'N':
case 'F':
return '*';
case 'D':
return '0';
case 'L':
return '?';
default:
return ' ';
}
}
/************************************************************************/
/* DBFAddField() */
/* */
/* Add a field to a newly created .dbf file before any records */
/* are written. */
/************************************************************************/
int SHPAPI_CALL
DBFAddNativeFieldType(DBFHandle psDBF, const char * pszFieldName,
char chType, int nWidth, int nDecimals )
{
char *pszFInfo;
int i;
int nOldRecordLength, nOldHeaderLength;
char *pszRecord;
char chFieldFill;
SAOffset nRecordOffset;
/* make sure that everything is written in .dbf */
if( !DBFFlushRecord( psDBF ) )
return -1;
if( psDBF->nHeaderLength + XBASE_FLDHDR_SZ > 65535 )
{
char szMessage[128];
snprintf( szMessage, sizeof(szMessage),
"Cannot add field %s. Header length limit reached "
"(max 65535 bytes, 2046 fields).",
pszFieldName );
psDBF->sHooks.Error( szMessage );
return -1;
}
/* -------------------------------------------------------------------- */
/* Do some checking to ensure we can add records to this file. */
/* -------------------------------------------------------------------- */
if( nWidth < 1 )
return -1;
if( nWidth > XBASE_FLD_MAX_WIDTH )
nWidth = XBASE_FLD_MAX_WIDTH;
if( psDBF->nRecordLength + nWidth > 65535 )
{
char szMessage[128];
snprintf( szMessage, sizeof(szMessage),
"Cannot add field %s. Record length limit reached "
"(max 65535 bytes).",
pszFieldName );
psDBF->sHooks.Error( szMessage );
return -1;
}
nOldRecordLength = psDBF->nRecordLength;
nOldHeaderLength = psDBF->nHeaderLength;
/* -------------------------------------------------------------------- */
/* SfRealloc all the arrays larger to hold the additional field */
/* information. */
/* -------------------------------------------------------------------- */
psDBF->nFields++;
psDBF->panFieldOffset = STATIC_CAST(int *,
SfRealloc( psDBF->panFieldOffset, sizeof(int) * psDBF->nFields ));
psDBF->panFieldSize = STATIC_CAST(int *,
SfRealloc( psDBF->panFieldSize, sizeof(int) * psDBF->nFields ));
psDBF->panFieldDecimals = STATIC_CAST(int *,
SfRealloc( psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields ));
psDBF->pachFieldType = STATIC_CAST(char *,
SfRealloc( psDBF->pachFieldType, sizeof(char) * psDBF->nFields ));
/* -------------------------------------------------------------------- */
/* Assign the new field information fields. */
/* -------------------------------------------------------------------- */
psDBF->panFieldOffset[psDBF->nFields-1] = psDBF->nRecordLength;
psDBF->nRecordLength += nWidth;
psDBF->panFieldSize[psDBF->nFields-1] = nWidth;
psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;
psDBF->pachFieldType[psDBF->nFields-1] = chType;
/* -------------------------------------------------------------------- */
/* Extend the required header information. */
/* -------------------------------------------------------------------- */
psDBF->nHeaderLength += XBASE_FLDHDR_SZ;
psDBF->bUpdated = FALSE;
psDBF->pszHeader = STATIC_CAST(char *, SfRealloc(psDBF->pszHeader,
psDBF->nFields*XBASE_FLDHDR_SZ));
pszFInfo = psDBF->pszHeader + XBASE_FLDHDR_SZ * (psDBF->nFields-1);
for( i = 0; i < XBASE_FLDHDR_SZ; i++ )
pszFInfo[i] = '\0';
strncpy( pszFInfo, pszFieldName, XBASE_FLDNAME_LEN_WRITE );
pszFInfo[11] = psDBF->pachFieldType[psDBF->nFields-1];
if( chType == 'C' )
{
pszFInfo[16] = STATIC_CAST(unsigned char, nWidth % 256);
pszFInfo[17] = STATIC_CAST(unsigned char, nWidth / 256);
}
else
{
pszFInfo[16] = STATIC_CAST(unsigned char, nWidth);
pszFInfo[17] = STATIC_CAST(unsigned char, nDecimals);
}
/* -------------------------------------------------------------------- */
/* Make the current record buffer appropriately larger. */
/* -------------------------------------------------------------------- */
psDBF->pszCurrentRecord = STATIC_CAST(char *, SfRealloc(psDBF->pszCurrentRecord,
psDBF->nRecordLength));
/* we're done if dealing with new .dbf */
if( psDBF->bNoHeader )
return( psDBF->nFields - 1 );
/* -------------------------------------------------------------------- */
/* For existing .dbf file, shift records */
/* -------------------------------------------------------------------- */
/* alloc record */
pszRecord = STATIC_CAST(char *, malloc(sizeof(char) * psDBF->nRecordLength));
chFieldFill = DBFGetNullCharacter(chType);
for (i = psDBF->nRecords-1; i >= 0; --i)
{
nRecordOffset = nOldRecordLength * STATIC_CAST(SAOffset, i) + nOldHeaderLength;
/* load record */
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
/* set new field's value to NULL */
memset(pszRecord + nOldRecordLength, chFieldFill, nWidth);
nRecordOffset = psDBF->nRecordLength * STATIC_CAST(SAOffset, i) + psDBF->nHeaderLength;
/* move record to the new place*/
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
psDBF->sHooks.FWrite( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
}
if( psDBF->bWriteEndOfFileChar )
{
char ch = END_OF_FILE_CHARACTER;
nRecordOffset =
psDBF->nRecordLength * STATIC_CAST(SAOffset,psDBF->nRecords) + psDBF->nHeaderLength;
psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
psDBF->sHooks.FWrite( &ch, 1, 1, psDBF->fp );
}
/* free record */
free(pszRecord);
/* force update of header with new header, record length and new field */
psDBF->bNoHeader = TRUE;
DBFUpdateHeader( psDBF );
psDBF->nCurrentRecord = -1;
psDBF->bCurrentRecordModified = FALSE;
psDBF->bUpdated = TRUE;
return( psDBF->nFields-1 );
}