1 #ifndef SHAPEFILE_H_INCLUDED
2 #define SHAPEFILE_H_INCLUDED
3 
4 /******************************************************************************
5  * $Id$
6  *
7  * Project:  Shapelib
8  * Purpose:  Primary include file for Shapelib.
9  * Author:   Frank Warmerdam, warmerdam@pobox.com
10  *
11  ******************************************************************************
12  * Copyright (c) 1999, Frank Warmerdam
13  * Copyright (c) 2012-2016, Even Rouault <even dot rouault at mines-paris dot org>
14  *
15  * This software is available under the following "MIT Style" license,
16  * or at the option of the licensee under the LGPL (see COPYING).  This
17  * option is discussed in more detail in shapelib.html.
18  *
19  * --
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining a
22  * copy of this software and associated documentation files (the "Software"),
23  * to deal in the Software without restriction, including without limitation
24  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
25  * and/or sell copies of the Software, and to permit persons to whom the
26  * Software is furnished to do so, subject to the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be included
29  * in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
32  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
34  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
36  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
37  * DEALINGS IN THE SOFTWARE.
38  ******************************************************************************
39  *
40  * $Log$
41  * Revision 1.56  2018-08-16 15:39:07  erouault
42  * * shpopen.c, dbfopen.c, shptree.c, sbnsearch.c: resyc with GDAL
43  * internal shapelib. Mostly to allow building those files as C++
44  * without warning. Also add FTDate entry in DBFFieldType
45  * (see https://github.com/OSGeo/gdal/pull/308). And some other
46  * code cleanups
47  *
48  * Revision 1.55  2016-12-05 18:44:08  erouault
49  * * dbfopen.c, shapefil.h: write DBF end-of-file character 0x1A by default.
50  * This behaviour can be controlled with the DBFSetWriteEndOfFileChar()
51  * function.
52  *
53  * Revision 1.54  2016-12-05 12:44:05  erouault
54  * * Major overhaul of Makefile build system to use autoconf/automake.
55  *
56  * * Warning fixes in contrib/
57  *
58  * Revision 1.53  2016-12-04 15:30:15  erouault
59  * * shpopen.c, dbfopen.c, shptree.c, shapefil.h: resync with
60  * GDAL Shapefile driver. Mostly cleanups. SHPObject and DBFInfo
61  * structures extended with new members. New functions:
62  * DBFSetLastModifiedDate, SHPOpenLLEx, SHPRestoreSHX,
63  * SHPSetFastModeReadObject
64  *
65  * * sbnsearch.c: new file to implement original ESRI .sbn spatial
66  * index reading. (no write support). New functions:
67  * SBNOpenDiskTree, SBNCloseDiskTree, SBNSearchDiskTree,
68  * SBNSearchDiskTreeInteger, SBNSearchFreeIds
69  *
70  * * Makefile, makefile.vc, CMakeLists.txt, shapelib.def: updates
71  * with new file and symbols.
72  *
73  * * commit: helper script to cvs commit
74  *
75  * Revision 1.52  2011-12-11 22:26:46  fwarmerdam
76  * upgrade .qix access code to use SAHooks (gdal #3365)
77  *
78  * Revision 1.51  2011-07-24 05:59:25  fwarmerdam
79  * minimize use of CPLError in favor of SAHooks.Error()
80  *
81  * Revision 1.50  2011-05-13 17:35:17  fwarmerdam
82  * added DBFReorderFields() and DBFAlterFields() functions (from Even)
83  *
84  * Revision 1.49  2011-04-16 14:38:21  fwarmerdam
85  * avoid warnings with gcc on SHP_CVSID
86  *
87  * Revision 1.48  2010-08-27 23:42:52  fwarmerdam
88  * add SHPAPI_CALL attribute in code
89  *
90  * Revision 1.47  2010-01-28 11:34:34  fwarmerdam
91  * handle the shape file length limits more gracefully (#3236)
92  *
93  * Revision 1.46  2008-11-12 14:28:15  fwarmerdam
94  * DBFCreateField() now works on files with records
95  *
96  * Revision 1.45  2008/11/11 17:47:10  fwarmerdam
97  * added DBFDeleteField() function
98  *
99  * Revision 1.44  2008/01/16 20:05:19  bram
100  * Add file hooks that accept UTF-8 encoded filenames on some platforms.  Use SASetupUtf8Hooks
101  *  tosetup the hooks and check SHPAPI_UTF8_HOOKS for its availability.  Currently, this
102  *  is only available on the Windows platform that decodes the UTF-8 filenames to wide
103  *  character strings and feeds them to _wfopen and _wremove.
104  *
105  * Revision 1.43  2008/01/10 16:35:30  fwarmerdam
106  * avoid _ prefix on #defined symbols (bug 1840)
107  *
108  * Revision 1.42  2007/12/18 18:28:14  bram
109  * - create hook for client specific atof (bugzilla ticket 1615)
110  * - check for NULL handle before closing cpCPG file, and close after reading.
111  *
112  * Revision 1.41  2007/12/15 20:25:32  bram
113  * dbfopen.c now reads the Code Page information from the DBF file, and exports
114  * this information as a string through the DBFGetCodePage function.  This is
115  * either the number from the LDID header field ("LDID/<number>") or as the
116  * content of an accompanying .CPG file.  When creating a DBF file, the code can
117  * be set using DBFCreateEx.
118  *
119  * Revision 1.40  2007/12/06 07:00:25  fwarmerdam
120  * dbfopen now using SAHooks for fileio
121  *
122  * Revision 1.39  2007/12/04 20:37:56  fwarmerdam
123  * preliminary implementation of hooks api for io and errors
124  *
125  * Revision 1.38  2007/11/21 22:39:56  fwarmerdam
126  * close shx file in readonly mode (GDAL #1956)
127  *
128  * Revision 1.37  2007/10/27 03:31:14  fwarmerdam
129  * limit default depth of tree to 12 levels (gdal ticket #1594)
130  *
131  * Revision 1.36  2007/09/10 23:33:15  fwarmerdam
132  * Upstreamed support for visibility flag in SHPAPI_CALL for the needs
133  * of GDAL (gdal ticket #1810).
134  *
135  * Revision 1.35  2007/09/03 19:48:10  fwarmerdam
136  * move DBFReadAttribute() static dDoubleField into dbfinfo
137  *
138  * Revision 1.34  2006/06/17 15:33:32  fwarmerdam
139  * added pszWorkField - bug 1202 (rso)
140  *
141  * Revision 1.33  2006/02/15 01:14:30  fwarmerdam
142  * added DBFAddNativeFieldType
143  *
144  * Revision 1.32  2006/01/26 15:07:32  fwarmerdam
145  * add bMeasureIsUsed flag from Craig Bruce: Bug 1249
146  *
147  * Revision 1.31  2006/01/05 01:27:27  fwarmerdam
148  * added dbf deletion mark/fetch
149  *
150  * Revision 1.30  2005/01/03 22:30:13  fwarmerdam
151  * added support for saved quadtrees
152  *
153  * Revision 1.29  2004/09/26 20:09:35  fwarmerdam
154  * avoid rcsid warnings
155  *
156  * Revision 1.28  2003/12/29 06:02:18  fwarmerdam
157  * added cpl_error.h option
158  *
159  * Revision 1.27  2003/04/21 18:30:37  warmerda
160  * added header write/update public methods
161  *
162  * Revision 1.26  2002/09/29 00:00:08  warmerda
163  * added FTLogical and logical attribute read/write calls
164  *
165  * Revision 1.25  2002/05/07 13:46:30  warmerda
166  * added DBFWriteAttributeDirectly().
167  *
168  * Revision 1.24  2002/04/10 16:59:54  warmerda
169  * added SHPRewindObject
170  *
171  * Revision 1.23  2002/01/15 14:36:07  warmerda
172  * updated email address
173  *
174  * Revision 1.22  2002/01/15 14:32:00  warmerda
175  * try to improve SHPAPI_CALL docs
176  */
177 
178 #include <stdio.h>
179 
180 #ifdef USE_DBMALLOC
181 #include <dbmalloc.h>
182 #endif
183 
184 #ifdef USE_CPL
185 #include "cpl_conv.h"
186 #endif
187 
188 #ifdef __cplusplus
189 extern "C" {
190 #endif
191 
192 /************************************************************************/
193 /*                        Configuration options.                        */
194 /************************************************************************/
195 
196 /* -------------------------------------------------------------------- */
197 /*      Should the DBFReadStringAttribute() strip leading and           */
198 /*      trailing white space?                                           */
199 /* -------------------------------------------------------------------- */
200 #define TRIM_DBF_WHITESPACE
201 
202 /* -------------------------------------------------------------------- */
203 /*      Should we write measure values to the Multipatch object?        */
204 /*      Reportedly ArcView crashes if we do write it, so for now it     */
205 /*      is disabled.                                                    */
206 /* -------------------------------------------------------------------- */
207 #define DISABLE_MULTIPATCH_MEASURE
208 
209 /* -------------------------------------------------------------------- */
210 /*      SHPAPI_CALL                                                     */
211 /*                                                                      */
212 /*      The following two macros are present to allow forcing           */
213 /*      various calling conventions on the Shapelib API.                */
214 /*                                                                      */
215 /*      To force __stdcall conventions (needed to call Shapelib         */
216 /*      from Visual Basic and/or Dephi I believe) the makefile could    */
217 /*      be modified to define:                                          */
218 /*                                                                      */
219 /*        /DSHPAPI_CALL=__stdcall                                       */
220 /*                                                                      */
221 /*      If it is desired to force export of the Shapelib API without    */
222 /*      using the shapelib.def file, use the following definition.      */
223 /*                                                                      */
224 /*        /DSHAPELIB_DLLEXPORT                                          */
225 /*                                                                      */
226 /*      To get both at once it will be necessary to hack this           */
227 /*      include file to define:                                         */
228 /*                                                                      */
229 /*        #define SHPAPI_CALL __declspec(dllexport) __stdcall           */
230 /*        #define SHPAPI_CALL1 __declspec(dllexport) * __stdcall        */
231 /*                                                                      */
232 /*      The complexity of the situation is partly caused by the        */
233 /*      peculiar requirement of Visual C++ that __stdcall appear        */
234 /*      after any "*"'s in the return value of a function while the     */
235 /*      __declspec(dllexport) must appear before them.                  */
236 /* -------------------------------------------------------------------- */
237 
238 #ifdef SHAPELIB_DLLEXPORT
239 #  define SHPAPI_CALL __declspec(dllexport)
240 #  define SHPAPI_CALL1(x)  __declspec(dllexport) x
241 #endif
242 
243 #ifndef SHPAPI_CALL
244 #  if defined(USE_GCC_VISIBILITY_FLAG)
245 #    define SHPAPI_CALL     __attribute__ ((visibility("default")))
246 #    define SHPAPI_CALL1(x) __attribute__ ((visibility("default")))     x
247 #  else
248 #    define SHPAPI_CALL
249 #  endif
250 #endif
251 
252 #ifndef SHPAPI_CALL1
253 #  define SHPAPI_CALL1(x)      x SHPAPI_CALL
254 #endif
255 
256 /* -------------------------------------------------------------------- */
257 /*      Macros for controlling CVSID and ensuring they don't appear     */
258 /*      as unreferenced variables resulting in lots of warnings.        */
259 /* -------------------------------------------------------------------- */
260 #ifndef DISABLE_CVSID
261 #  if defined(__GNUC__) && __GNUC__ >= 4
262 #    define SHP_CVSID(string)     static const char cpl_cvsid[] __attribute__((used)) = string;
263 #  else
264 #    define SHP_CVSID(string)     static const char cpl_cvsid[] = string; \
265 static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
266 #  endif
267 #else
268 #  define SHP_CVSID(string)
269 #endif
270 
271 /* -------------------------------------------------------------------- */
272 /*      On some platforms, additional file IO hooks are defined that    */
273 /*      UTF-8 encoded filenames Unicode filenames                       */
274 /* -------------------------------------------------------------------- */
275 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
276 #  define SHPAPI_WINDOWS
277 #  define SHPAPI_UTF8_HOOKS
278 #endif
279 
280 /* -------------------------------------------------------------------- */
281 /*      IO/Error hook functions.                                        */
282 /* -------------------------------------------------------------------- */
283 typedef int *SAFile;
284 
285 #ifndef SAOffset
286 typedef unsigned long SAOffset;
287 #endif
288 
289 typedef struct {
290     SAFile     (*FOpen) ( const char *filename, const char *access);
291     SAOffset   (*FRead) ( void *p, SAOffset size, SAOffset nmemb, SAFile file);
292     SAOffset   (*FWrite)( void *p, SAOffset size, SAOffset nmemb, SAFile file);
293     SAOffset   (*FSeek) ( SAFile file, SAOffset offset, int whence );
294     SAOffset   (*FTell) ( SAFile file );
295     int        (*FFlush)( SAFile file );
296     int        (*FClose)( SAFile file );
297     int        (*Remove) ( const char *filename );
298 
299     void       (*Error) ( const char *message );
300     double     (*Atof)  ( const char *str );
301 } SAHooks;
302 
303 void SHPAPI_CALL SASetupDefaultHooks( SAHooks *psHooks );
304 #ifdef SHPAPI_UTF8_HOOKS
305 void SHPAPI_CALL SASetupUtf8Hooks( SAHooks *psHooks );
306 #endif
307 
308 /************************************************************************/
309 /*                             SHP Support.                             */
310 /************************************************************************/
311 typedef struct tagSHPObject SHPObject;
312 
313 typedef struct
314 {
315     SAHooks sHooks;
316 
317     SAFile      fpSHP;
318     SAFile      fpSHX;
319 
320     int         nShapeType;  /* SHPT_* */
321 
322     unsigned int nFileSize;  /* SHP file */
323 
324     int         nRecords;
325     int         nMaxRecords;
326     unsigned int*panRecOffset;
327     unsigned int *panRecSize;
328 
329     double      adBoundsMin[4];
330     double      adBoundsMax[4];
331 
332     int         bUpdated;
333 
334     unsigned char *pabyRec;
335     int         nBufSize;
336 
337     int            bFastModeReadObject;
338     unsigned char *pabyObjectBuf;
339     int            nObjectBufSize;
340     SHPObject*     psCachedObject;
341 } SHPInfo;
342 
343 typedef SHPInfo * SHPHandle;
344 
345 /* -------------------------------------------------------------------- */
346 /*      Shape types (nSHPType)                                          */
347 /* -------------------------------------------------------------------- */
348 #define SHPT_NULL       0
349 #define SHPT_POINT      1
350 #define SHPT_ARC        3
351 #define SHPT_POLYGON    5
352 #define SHPT_MULTIPOINT 8
353 #define SHPT_POINTZ     11
354 #define SHPT_ARCZ       13
355 #define SHPT_POLYGONZ   15
356 #define SHPT_MULTIPOINTZ 18
357 #define SHPT_POINTM     21
358 #define SHPT_ARCM       23
359 #define SHPT_POLYGONM   25
360 #define SHPT_MULTIPOINTM 28
361 #define SHPT_MULTIPATCH 31
362 
363 /* -------------------------------------------------------------------- */
364 /*      Part types - everything but SHPT_MULTIPATCH just uses           */
365 /*      SHPP_RING.                                                      */
366 /* -------------------------------------------------------------------- */
367 
368 #define SHPP_TRISTRIP   0
369 #define SHPP_TRIFAN     1
370 #define SHPP_OUTERRING  2
371 #define SHPP_INNERRING  3
372 #define SHPP_FIRSTRING  4
373 #define SHPP_RING       5
374 
375 /* -------------------------------------------------------------------- */
376 /*      SHPObject - represents on shape (without attributes) read       */
377 /*      from the .shp file.                                             */
378 /* -------------------------------------------------------------------- */
379 struct tagSHPObject
380 {
381     int    nSHPType;
382 
383     int    nShapeId;  /* -1 is unknown/unassigned */
384 
385     int    nParts;
386     int    *panPartStart;
387     int    *panPartType;
388 
389     int    nVertices;
390     double *padfX;
391     double *padfY;
392     double *padfZ;
393     double *padfM;
394 
395     double dfXMin;
396     double dfYMin;
397     double dfZMin;
398     double dfMMin;
399 
400     double dfXMax;
401     double dfYMax;
402     double dfZMax;
403     double dfMMax;
404 
405     int    bMeasureIsUsed;
406     int    bFastModeReadObject;
407 };
408 
409 /* -------------------------------------------------------------------- */
410 /*      SHP API Prototypes                                              */
411 /* -------------------------------------------------------------------- */
412 
413 /* If pszAccess is read-only, the fpSHX field of the returned structure */
414 /* will be NULL as it is not necessary to keep the SHX file open */
415 SHPHandle SHPAPI_CALL
416       SHPOpen( const char * pszShapeFile, const char * pszAccess );
417 SHPHandle SHPAPI_CALL
418       SHPOpenLL( const char *pszShapeFile, const char *pszAccess,
419                  SAHooks *psHooks );
420 SHPHandle SHPAPI_CALL
421       SHPOpenLLEx( const char *pszShapeFile, const char *pszAccess,
422                   SAHooks *psHooks, int bRestoreSHX );
423 
424 int SHPAPI_CALL
425       SHPRestoreSHX( const char *pszShapeFile, const char *pszAccess,
426                   SAHooks *psHooks );
427 
428 /* If setting bFastMode = TRUE, the content of SHPReadObject() is owned by the SHPHandle. */
429 /* So you cannot have 2 valid instances of SHPReadObject() simultaneously. */
430 /* The SHPObject padfZ and padfM members may be NULL depending on the geometry */
431 /* type. It is illegal to free at hand any of the pointer members of the SHPObject structure */
432 void SHPAPI_CALL SHPSetFastModeReadObject( SHPHandle hSHP, int bFastMode );
433 
434 SHPHandle SHPAPI_CALL
435       SHPCreate( const char * pszShapeFile, int nShapeType );
436 SHPHandle SHPAPI_CALL
437       SHPCreateLL( const char * pszShapeFile, int nShapeType,
438                    SAHooks *psHooks );
439 void SHPAPI_CALL
440       SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * pnShapeType,
441                   double * padfMinBound, double * padfMaxBound );
442 
443 SHPObject SHPAPI_CALL1(*)
444       SHPReadObject( SHPHandle hSHP, int iShape );
445 int SHPAPI_CALL
446       SHPWriteObject( SHPHandle hSHP, int iShape, SHPObject * psObject );
447 
448 void SHPAPI_CALL
449       SHPDestroyObject( SHPObject * psObject );
450 void SHPAPI_CALL
451       SHPComputeExtents( SHPObject * psObject );
452 SHPObject SHPAPI_CALL1(*)
453       SHPCreateObject( int nSHPType, int nShapeId, int nParts,
454                        const int * panPartStart, const int * panPartType,
455                        int nVertices,
456                        const double * padfX, const double * padfY,
457                        const double * padfZ, const double * padfM );
458 SHPObject SHPAPI_CALL1(*)
459       SHPCreateSimpleObject( int nSHPType, int nVertices,
460                              const double * padfX,
461                              const double * padfY,
462                              const double * padfZ );
463 
464 int SHPAPI_CALL
465       SHPRewindObject( SHPHandle hSHP, SHPObject * psObject );
466 
467 void SHPAPI_CALL SHPClose( SHPHandle hSHP );
468 void SHPAPI_CALL SHPWriteHeader( SHPHandle hSHP );
469 
470 const char SHPAPI_CALL1(*)
471       SHPTypeName( int nSHPType );
472 const char SHPAPI_CALL1(*)
473       SHPPartTypeName( int nPartType );
474 
475 /* -------------------------------------------------------------------- */
476 /*      Shape quadtree indexing API.                                    */
477 /* -------------------------------------------------------------------- */
478 
479 /* this can be two or four for binary or quad tree */
480 #define MAX_SUBNODE 4
481 
482 /* upper limit of tree levels for automatic estimation */
483 #define MAX_DEFAULT_TREE_DEPTH 12
484 
485 typedef struct shape_tree_node
486 {
487     /* region covered by this node */
488     double      adfBoundsMin[4];
489     double      adfBoundsMax[4];
490 
491     /* list of shapes stored at this node.  The papsShapeObj pointers
492        or the whole list can be NULL */
493     int         nShapeCount;
494     int         *panShapeIds;
495     SHPObject   **papsShapeObj;
496 
497     int         nSubNodes;
498     struct shape_tree_node *apsSubNode[MAX_SUBNODE];
499 
500 } SHPTreeNode;
501 
502 typedef struct
503 {
504     SHPHandle   hSHP;
505 
506     int         nMaxDepth;
507     int         nDimension;
508     int         nTotalCount;
509 
510     SHPTreeNode *psRoot;
511 } SHPTree;
512 
513 SHPTree SHPAPI_CALL1(*)
514       SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth,
515                      double *padfBoundsMin, double *padfBoundsMax );
516 void SHPAPI_CALL
517       SHPDestroyTree( SHPTree * hTree );
518 
519 int SHPAPI_CALL
520       SHPWriteTree( SHPTree *hTree, const char * pszFilename );
521 
522 int SHPAPI_CALL
523       SHPTreeAddShapeId( SHPTree * hTree, SHPObject * psObject );
524 int SHPAPI_CALL
525       SHPTreeRemoveShapeId( SHPTree * hTree, int nShapeId );
526 
527 void SHPAPI_CALL
528       SHPTreeTrimExtraNodes( SHPTree * hTree );
529 
530 int SHPAPI_CALL1(*)
531       SHPTreeFindLikelyShapes( SHPTree * hTree,
532                                double * padfBoundsMin,
533                                double * padfBoundsMax,
534                                int * );
535 int SHPAPI_CALL
536       SHPCheckBoundsOverlap( double *, double *, double *, double *, int );
537 
538 int SHPAPI_CALL1(*)
539 SHPSearchDiskTree( FILE *fp,
540                    double *padfBoundsMin, double *padfBoundsMax,
541                    int *pnShapeCount );
542 
543 typedef struct SHPDiskTreeInfo* SHPTreeDiskHandle;
544 
545 SHPTreeDiskHandle SHPAPI_CALL
546     SHPOpenDiskTree( const char* pszQIXFilename,
547                      SAHooks *psHooks );
548 
549 void SHPAPI_CALL
550     SHPCloseDiskTree( SHPTreeDiskHandle hDiskTree );
551 
552 int SHPAPI_CALL1(*)
553 SHPSearchDiskTreeEx( SHPTreeDiskHandle hDiskTree,
554                      double *padfBoundsMin, double *padfBoundsMax,
555                      int *pnShapeCount );
556 
557 int SHPAPI_CALL
558     SHPWriteTreeLL(SHPTree *hTree, const char *pszFilename, SAHooks *psHooks );
559 
560 /* -------------------------------------------------------------------- */
561 /*      SBN Search API                                                  */
562 /* -------------------------------------------------------------------- */
563 
564 typedef struct SBNSearchInfo* SBNSearchHandle;
565 
566 SBNSearchHandle SHPAPI_CALL
567     SBNOpenDiskTree( const char* pszSBNFilename,
568                  SAHooks *psHooks );
569 
570 void SHPAPI_CALL
571     SBNCloseDiskTree( SBNSearchHandle hSBN );
572 
573 int SHPAPI_CALL1(*)
574 SBNSearchDiskTree( SBNSearchHandle hSBN,
575                    double *padfBoundsMin, double *padfBoundsMax,
576                    int *pnShapeCount );
577 
578 int SHPAPI_CALL1(*)
579 SBNSearchDiskTreeInteger( SBNSearchHandle hSBN,
580                           int bMinX, int bMinY, int bMaxX, int bMaxY,
581                           int *pnShapeCount );
582 
583 void SHPAPI_CALL SBNSearchFreeIds( int* panShapeId );
584 
585 /************************************************************************/
586 /*                             DBF Support.                             */
587 /************************************************************************/
588 typedef struct
589 {
590     SAHooks sHooks;
591 
592     SAFile      fp;
593 
594     int         nRecords;
595 
596     int         nRecordLength; /* Must fit on uint16 */
597     int         nHeaderLength; /* File header length (32) + field
598                                   descriptor length + spare space.
599                                   Must fit on uint16 */
600     int         nFields;
601     int         *panFieldOffset;
602     int         *panFieldSize;
603     int         *panFieldDecimals;
604     char        *pachFieldType;
605 
606     char        *pszHeader; /* Field descriptors */
607 
608     int         nCurrentRecord;
609     int         bCurrentRecordModified;
610     char        *pszCurrentRecord;
611 
612     int         nWorkFieldLength;
613     char        *pszWorkField;
614 
615     int         bNoHeader;
616     int         bUpdated;
617 
618     union
619     {
620         double      dfDoubleField;
621         int         nIntField;
622     } fieldValue;
623 
624     int         iLanguageDriver;
625     char        *pszCodePage;
626 
627     int         nUpdateYearSince1900; /* 0-255 */
628     int         nUpdateMonth; /* 1-12 */
629     int         nUpdateDay; /* 1-31 */
630 
631     int         bWriteEndOfFileChar; /* defaults to TRUE */
632 
633     int         bRequireNextWriteSeek;
634 } DBFInfo;
635 
636 typedef DBFInfo * DBFHandle;
637 
638 typedef enum {
639   FTString,
640   FTInteger,
641   FTDouble,
642   FTLogical,
643   FTDate,
644   FTInvalid
645 } DBFFieldType;
646 
647 /* Field descriptor/header size */
648 #define XBASE_FLDHDR_SZ         32
649 /* Shapelib read up to 11 characters, even if only 10 should normally be used */
650 #define XBASE_FLDNAME_LEN_READ  11
651 /* On writing, we limit to 10 characters */
652 #define XBASE_FLDNAME_LEN_WRITE 10
653 /* Normally only 254 characters should be used. We tolerate 255 historically */
654 #define XBASE_FLD_MAX_WIDTH     255
655 
656 DBFHandle SHPAPI_CALL
657       DBFOpen( const char * pszDBFFile, const char * pszAccess );
658 DBFHandle SHPAPI_CALL
659       DBFOpenLL( const char * pszDBFFile, const char * pszAccess,
660                  SAHooks *psHooks );
661 DBFHandle SHPAPI_CALL
662       DBFCreate( const char * pszDBFFile );
663 DBFHandle SHPAPI_CALL
664       DBFCreateEx( const char * pszDBFFile, const char * pszCodePage );
665 DBFHandle SHPAPI_CALL
666       DBFCreateLL( const char * pszDBFFile, const char * pszCodePage, SAHooks *psHooks );
667 
668 int SHPAPI_CALL
669       DBFGetFieldCount( DBFHandle psDBF );
670 int SHPAPI_CALL
671       DBFGetRecordCount( DBFHandle psDBF );
672 int SHPAPI_CALL
673       DBFAddField( DBFHandle hDBF, const char * pszFieldName,
674                    DBFFieldType eType, int nWidth, int nDecimals );
675 
676 int SHPAPI_CALL
677       DBFAddNativeFieldType( DBFHandle hDBF, const char * pszFieldName,
678                              char chType, int nWidth, int nDecimals );
679 
680 int SHPAPI_CALL
681       DBFDeleteField( DBFHandle hDBF, int iField );
682 
683 int SHPAPI_CALL
684       DBFReorderFields( DBFHandle psDBF, int* panMap );
685 
686 int SHPAPI_CALL
687       DBFAlterFieldDefn( DBFHandle psDBF, int iField, const char * pszFieldName,
688                          char chType, int nWidth, int nDecimals );
689 
690 DBFFieldType SHPAPI_CALL
691       DBFGetFieldInfo( DBFHandle psDBF, int iField,
692                        char * pszFieldName, int * pnWidth, int * pnDecimals );
693 
694 int SHPAPI_CALL
695       DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName);
696 
697 int SHPAPI_CALL
698       DBFReadIntegerAttribute( DBFHandle hDBF, int iShape, int iField );
699 double SHPAPI_CALL
700       DBFReadDoubleAttribute( DBFHandle hDBF, int iShape, int iField );
701 const char SHPAPI_CALL1(*)
702       DBFReadStringAttribute( DBFHandle hDBF, int iShape, int iField );
703 const char SHPAPI_CALL1(*)
704       DBFReadLogicalAttribute( DBFHandle hDBF, int iShape, int iField );
705 int SHPAPI_CALL
706       DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
707 
708 int SHPAPI_CALL
709       DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField,
710                                 int nFieldValue );
711 int SHPAPI_CALL
712       DBFWriteDoubleAttribute( DBFHandle hDBF, int iShape, int iField,
713                                double dFieldValue );
714 int SHPAPI_CALL
715       DBFWriteStringAttribute( DBFHandle hDBF, int iShape, int iField,
716                                const char * pszFieldValue );
717 int SHPAPI_CALL
718      DBFWriteNULLAttribute( DBFHandle hDBF, int iShape, int iField );
719 
720 int SHPAPI_CALL
721      DBFWriteLogicalAttribute( DBFHandle hDBF, int iShape, int iField,
722                                const char lFieldValue);
723 int SHPAPI_CALL
724      DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
725                                void * pValue );
726 const char SHPAPI_CALL1(*)
727       DBFReadTuple(DBFHandle psDBF, int hEntity );
728 int SHPAPI_CALL
729       DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple );
730 
731 int SHPAPI_CALL DBFIsRecordDeleted( DBFHandle psDBF, int iShape );
732 int SHPAPI_CALL DBFMarkRecordDeleted( DBFHandle psDBF, int iShape,
733                                       int bIsDeleted );
734 
735 DBFHandle SHPAPI_CALL
736       DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename );
737 
738 void SHPAPI_CALL
739       DBFClose( DBFHandle hDBF );
740 void    SHPAPI_CALL
741       DBFUpdateHeader( DBFHandle hDBF );
742 char SHPAPI_CALL
743       DBFGetNativeFieldType( DBFHandle hDBF, int iField );
744 
745 const char SHPAPI_CALL1(*)
746       DBFGetCodePage(DBFHandle psDBF );
747 
748 void SHPAPI_CALL
749     DBFSetLastModifiedDate( DBFHandle psDBF, int nYYSince1900, int nMM, int nDD );
750 
751 void SHPAPI_CALL DBFSetWriteEndOfFileChar( DBFHandle psDBF, int bWriteFlag );
752 
753 #ifdef __cplusplus
754 }
755 #endif
756 
757 #endif /* ndef SHAPEFILE_H_INCLUDED */
758