/[thuban]/branches/WIP-pyshapelib-bramz/libraries/shapelib/dbfopen.c
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/libraries/shapelib/dbfopen.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/thuban/libraries/shapelib/dbfopen.c revision 1612 by jan, Tue Aug 19 21:29:25 2003 UTC branches/WIP-pyshapelib-bramz/libraries/shapelib/dbfopen.c revision 2751 by bramz, Wed Mar 28 23:30:15 2007 UTC
# Line 3  Line 3 
3   *   *
4   * Project:  Shapelib   * Project:  Shapelib
5   * Purpose:  Implementation of .dbf access API documented in dbf_api.html.   * Purpose:  Implementation of .dbf access API documented in dbf_api.html.
6   * Author:   Frank Warmerdam, [email protected]   * Author:   Frank Warmerdam, [email protected]
7   *   *
8   ******************************************************************************   ******************************************************************************
9   * Copyright (c) 1999, Frank Warmerdam   * Copyright (c) 1999, Frank Warmerdam
# Line 34  Line 34 
34   ******************************************************************************   ******************************************************************************
35   *   *
36   * $Log$   * $Log$
37   * Revision 1.1  2003/08/19 21:29:25  jan   * Revision 1.3  2004/05/17 15:47:57  bh
38   * These files have been moved here from thuban/extensions/shapelib/   * Update to newest shapelib and get rid of Thuban specific extensions,
39   * See there in the Attic for the older history.   * i.e. use the new DBFUpdateHeader instead of our DBFCommit kludge
40   *   *
41   * Revision 1.4  2002/08/22 16:00:01  bh   * * libraries/shapelib/shpopen.c: Update to version from current
42   * * extensions/shapelib/shapefil.h (DBFCommit),   * shapelib CVS.
43   * extensions/shapelib/dbfopen.c (DBFCommit): New API function to   *
44   * commit any changes made to the DBF file.   * * libraries/shapelib/shapefil.h: Update to version from current
45   *   * shapelib CVS.
46   * Revision 1.3  2002/05/07 14:09:45  bh   *
47   * * extensions/shapelib/shpopen.c, extensions/shapelib/shapefil.h,   * * libraries/shapelib/dbfopen.c: Update to version from current
48   * extensions/shapelib/dbfopen.c: Really update to the versions of   * shapelib CVS.
49   * shapelib 1.2.9. For some reason it wasn't really done on   * (DBFCommit): Effectively removed since shapelib itself has
50   * 2002-04-11.   * DBFUpdateHeader now which is better for what DBFCommit wanted to
51     * achieve.
52     * We're now using an unmodified version of dbfopen.
53     *
54     * * libraries/pyshapelib/dbflib_wrap.c, libraries/pyshapelib/dbflib.py:
55     * Update from dbflib.i
56     *
57     * * libraries/pyshapelib/dbflib.i (DBFInfo_commit): New. Implementation of
58     * the commit method.  This new indirection is necessary because we use the
59     * DBFUpdateHeader function now which is not available in shapelib <=
60     * 1.2.10
61     * (DBFFile::commit): Use DBFInfo_commit as implementation
62     * (pragma __class__): New. Kludge to remove the commit method when
63     * the DBFUpdateHeader function isn't available
64     * (_have_commit): New. Helper for the pragma kludge.
65     *
66     * * libraries/pyshapelib/setup.py (dbf_macros): New. Return the
67     * preprocessor macros needed to compile the dbflib wrapper.  Determine
68     * whether DBFUpdateHeader is available and define the right value of
69     * HAVE_UPDATE_HEADER
70     * (extensions): Use dbf_macros for the dbflibc extension
71     *
72     * * setup.py (extensions): Add the HAVE_UPDATE_HEADER macro with
73     * value '1' to the Lib.dbflibc extension.  This simply reflects the
74     * shapelib and pyshapelib updates
75     *
76     * Revision 1.53  2003/12/29 00:00:30  fwarmerdam
77     * mark DBFWriteAttributeDirectly as SHPAPI_CALL
78     *
79     * Revision 1.52  2003/07/08 15:20:03  warmerda
80     * avoid warnings about downcasting to unsigned char
81     *
82     * Revision 1.51  2003/07/08 13:50:15  warmerda
83     * DBFIsAttributeNULL check for pszValue==NULL - bug 360
84     *
85     * Revision 1.50  2003/04/21 18:58:25  warmerda
86     * ensure current record is flushed at same time as header is updated
87     *
88     * Revision 1.49  2003/04/21 18:30:37  warmerda
89     * added header write/update public methods
90     *
91     * Revision 1.48  2003/03/10 14:51:27  warmerda
92     * DBFWrite* calls now return FALSE if they have to truncate
93     *
94     * Revision 1.47  2002/11/20 03:32:22  warmerda
95     * Ensure field name in DBFGetFieldIndex() is properly terminated.
96     *
97     * Revision 1.46  2002/10/09 13:10:21  warmerda
98     * Added check that width is positive.
99     *
100     * Revision 1.45  2002/09/29 00:00:08  warmerda
101     * added FTLogical and logical attribute read/write calls
102     *
103     * Revision 1.44  2002/05/07 13:46:11  warmerda
104     * Added DBFWriteAttributeDirectly().
105     *
106     * Revision 1.43  2002/02/13 19:39:21  warmerda
107     * Fix casting issues in DBFCloneEmpty().
108     *
109     * Revision 1.42  2002/01/15 14:36:07  warmerda
110     * updated email address
111     *
112     * Revision 1.41  2002/01/15 14:31:49  warmerda
113     * compute rather than copying nHeaderLength in DBFCloneEmpty()
114     *
115     * Revision 1.40  2002/01/09 04:32:35  warmerda
116     * fixed to read correct amount of header
117     *
118     * Revision 1.39  2001/12/11 22:41:03  warmerda
119     * improve io related error checking when reading header
120     *
121     * Revision 1.38  2001/11/28 16:07:31  warmerda
122     * Cleanup to avoid compiler warnings as suggested by Richard Hash.
123   *   *
124   * Revision 1.37  2001/07/04 05:18:09  warmerda   * Revision 1.37  2001/07/04 05:18:09  warmerda
125   * do last fix properly   * do last fix properly
# Line 179  static char rcsid[] = Line 251  static char rcsid[] =
251  #  define TRUE          1  #  define TRUE          1
252  #endif  #endif
253    
254    #if defined(_WIN32) || defined(_WIN64)
255    #       define MS_WINDOWS
256    #endif
257    
258  static int      nStringFieldLen = 0;  static int      nStringFieldLen = 0;
259  static char * pszStringField = NULL;  static char * pszStringField = NULL;
260    
261  /************************************************************************/  /************************************************************************/
262    /*                             DBFSet_atof_function()                   */
263    /*                                                                      */
264    /* This makes it possible to initialise a different atof() function     */
265    /* which might be necessary because the standard atof() might be        */
266    /* sensitive to locale settings.                                        */
267    /*                                                                      */
268    /* If the calling application uses a locale with different decimal_point*/
269    /* it should better also give us a locale agnostic atof() function.     */
270    /*                                                                      */
271    /* As far as I can see from Python PEP331 and GNU libc documentation    */
272    /* there is no standard for such a function yet.                        */
273    /*                                                                      */
274    /* [email protected] 20060924                               */
275    /************************************************************************/
276    
277    static double (* atof_function)(const char *nptr)  = &atof;
278    
279    void SHPAPI_CALL
280            DBFSetatof_function(  double (* new_atof_function)(const char *nptr))
281    {
282            atof_function = new_atof_function;
283    }
284    
285    /************************************************************************/
286  /*                             SfRealloc()                              */  /*                             SfRealloc()                              */
287  /*                                                                      */  /*                                                                      */
288  /*      A realloc cover function that will access a NULL pointer as     */  /*      A realloc cover function that will access a NULL pointer as     */
# Line 226  static void DBFWriteHeader(DBFHandle psD Line 326  static void DBFWriteHeader(DBFHandle psD
326    
327      abyHeader[0] = 0x03;                /* memo field? - just copying   */      abyHeader[0] = 0x03;                /* memo field? - just copying   */
328    
329      /* date updated on close, record count preset at zero */      /* write out a dummy date */
330        abyHeader[1] = 95;                  /* YY */
331        abyHeader[2] = 7;                   /* MM */
332        abyHeader[3] = 26;                  /* DD */
333    
334        /* record count preset at zero */
335    
336      abyHeader[8] = psDBF->nHeaderLength % 256;      abyHeader[8] = (unsigned char) (psDBF->nHeaderLength % 256);
337      abyHeader[9] = psDBF->nHeaderLength / 256;      abyHeader[9] = (unsigned char) (psDBF->nHeaderLength / 256);
338            
339      abyHeader[10] = psDBF->nRecordLength % 256;      abyHeader[10] = (unsigned char) (psDBF->nRecordLength % 256);
340      abyHeader[11] = psDBF->nRecordLength / 256;      abyHeader[11] = (unsigned char) (psDBF->nRecordLength / 256);
341    
342  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
343  /*      Write the initial 32 byte file header, and all the field        */  /*      Write the initial 32 byte file header, and all the field        */
# Line 278  static void DBFFlushRecord( DBFHandle ps Line 383  static void DBFFlushRecord( DBFHandle ps
383  }  }
384    
385  /************************************************************************/  /************************************************************************/
386    /*                          DBFUpdateHeader()                           */
387    /************************************************************************/
388    
389    void SHPAPI_CALL
390    DBFUpdateHeader( DBFHandle psDBF )
391    
392    {
393        unsigned char               abyFileHeader[32];
394    
395        if( psDBF->bNoHeader )
396            DBFWriteHeader( psDBF );
397    
398        DBFFlushRecord( psDBF );
399    
400        fseek( psDBF->fp, 0, 0 );
401        fread( abyFileHeader, 32, 1, psDBF->fp );
402        
403        abyFileHeader[4] = (unsigned char) (psDBF->nRecords % 256);
404        abyFileHeader[5] = (unsigned char) ((psDBF->nRecords/256) % 256);
405        abyFileHeader[6] = (unsigned char) ((psDBF->nRecords/(256*256)) % 256);
406        abyFileHeader[7] = (unsigned char) ((psDBF->nRecords/(256*256*256)) % 256);
407        
408        fseek( psDBF->fp, 0, 0 );
409        fwrite( abyFileHeader, 32, 1, psDBF->fp );
410    
411        fflush( psDBF->fp );
412    }
413    
414    /************************************************************************/
415  /*                              DBFOpen()                               */  /*                              DBFOpen()                               */
416  /*                                                                      */  /*                                                                      */
417  /*      Open a .dbf file.                                               */  /*      Open a .dbf file.                                               */
# Line 287  DBFHandle SHPAPI_CALL Line 421  DBFHandle SHPAPI_CALL
421  DBFOpen( const char * pszFilename, const char * pszAccess )  DBFOpen( const char * pszFilename, const char * pszAccess )
422    
423  {  {
424      DBFHandle           psDBF;          FILE*           fp;
425      unsigned char               *pabyBuf;      int                 i;
     int                 nFields, nRecords, nHeadLen, nRecLen, iField, i;  
426      char                *pszBasename, *pszFullname;      char                *pszBasename, *pszFullname;
427    
428  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 323  DBFOpen( const char * pszFilename, const Line 456  DBFOpen( const char * pszFilename, const
456      pszFullname = (char *) malloc(strlen(pszBasename) + 5);      pszFullname = (char *) malloc(strlen(pszBasename) + 5);
457      sprintf( pszFullname, "%s.dbf", pszBasename );      sprintf( pszFullname, "%s.dbf", pszBasename );
458                    
459      psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );      fp = fopen( pszFullname, pszAccess );
     psDBF->fp = fopen( pszFullname, pszAccess );  
460    
461      if( psDBF->fp == NULL )      if( fp == NULL )
462      {      {
463          sprintf( pszFullname, "%s.DBF", pszBasename );          sprintf( pszFullname, "%s.DBF", pszBasename );
464          psDBF->fp = fopen(pszFullname, pszAccess );          fp = fopen(pszFullname, pszAccess );
465      }      }
466            
467      free( pszBasename );      free( pszBasename );
468      free( pszFullname );      free( pszFullname );
469        
470      if( psDBF->fp == NULL )          return DBFOpenEx( fp );
471      {  }
472          free( psDBF );  
473    
474    
475    /************************************************************************/
476    /*                              DBFOpenW()                              */
477    /*                                                                      */
478    /*      Open a .dbf file with a wide character filename                 */
479    /************************************************************************/
480    
481    #ifdef SHPAPI_HAS_WIDE
482    
483    DBFHandle SHPAPI_CALL
484    DBFOpenW( const wchar_t * pszFilename, const wchar_t * pszAccess )
485    
486    {
487        FILE*               fp;
488        int                 i;
489        wchar_t             *pszBasename, *pszFullname;
490    
491    /* -------------------------------------------------------------------- */
492    /*      We only allow the access strings "rb" and "r+".                  */
493    /* -------------------------------------------------------------------- */
494        if( wcscmp(pszAccess,L"r") != 0 && wcscmp(pszAccess,L"r+") != 0
495            && wcscmp(pszAccess,L"rb") != 0 && wcscmp(pszAccess,L"rb+") != 0
496            && wcscmp(pszAccess,L"r+b") != 0 )
497          return( NULL );          return( NULL );
498    
499        if( wcscmp(pszAccess,L"r") == 0 )
500            pszAccess = L"rb";
501    
502        if( wcscmp(pszAccess,L"r+") == 0 )
503            pszAccess = L"rb+";
504    
505    /* -------------------------------------------------------------------- */
506    /*      Compute the base (layer) name.  If there is any extension       */
507    /*      on the passed in filename we will strip it off.                 */
508    /* -------------------------------------------------------------------- */
509        pszBasename = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszFilename)+5));
510        wcscpy( pszBasename, pszFilename );
511        for( i = wcslen(pszBasename)-1;
512             i > 0 && pszBasename[i] != L'.' && pszBasename[i] != L'/'
513                   && pszBasename[i] != L'\\';
514             i-- ) {}
515    
516        if( pszBasename[i] == L'.' )
517            pszBasename[i] = L'\0';
518    
519        pszFullname = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszBasename) + 5));
520        swprintf( pszFullname, L"%s.dbf", pszBasename );
521            
522        fp = _wfopen( pszFullname, pszAccess );
523    
524        if( fp == NULL )
525        {
526            swprintf( pszFullname, L"%s.DBF", pszBasename );
527            fp = _wfopen(pszFullname, pszAccess );
528      }      }
529        
530        free( pszBasename );
531        free( pszFullname );
532    
533            return DBFOpenEx( fp );
534    }
535    
536    #endif
537    
538    
539    
540    /************************************************************************/
541    /*                              DBFOpenEx()                             */
542    /*                                                                      */
543    /*      Open a .dbf file from a freshly opened FILE                     */
544    /************************************************************************/
545      
546    DBFHandle SHPAPI_CALL
547    DBFOpenEx( FILE* fp )
548    
549    {
550        unsigned char       *pabyBuf;
551        int                 nFields, nHeadLen, nRecLen, iField;
552            DBFHandle       psDBF = NULL;
553    
554            if( fp == NULL )
555            {
556                    return( NULL );
557            }
558    
559        psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );
560            psDBF->fp = fp;
561    
562      psDBF->bNoHeader = FALSE;      psDBF->bNoHeader = FALSE;
563      psDBF->nCurrentRecord = -1;      psDBF->nCurrentRecord = -1;
# Line 349  DBFOpen( const char * pszFilename, const Line 567  DBFOpen( const char * pszFilename, const
567  /*  Read Table Header info                                              */  /*  Read Table Header info                                              */
568  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
569      pabyBuf = (unsigned char *) malloc(500);      pabyBuf = (unsigned char *) malloc(500);
570      fread( pabyBuf, 32, 1, psDBF->fp );      if( fread( pabyBuf, 32, 1, psDBF->fp ) != 1 )
571        {
572            fclose( psDBF->fp );
573            free( pabyBuf );
574            free( psDBF );
575            return NULL;
576        }
577    
578      psDBF->nRecords = nRecords =      psDBF->nRecords =
579       pabyBuf[4] + pabyBuf[5]*256 + pabyBuf[6]*256*256 + pabyBuf[7]*256*256*256;       pabyBuf[4] + pabyBuf[5]*256 + pabyBuf[6]*256*256 + pabyBuf[7]*256*256*256;
580    
581      psDBF->nHeaderLength = nHeadLen = pabyBuf[8] + pabyBuf[9]*256;      psDBF->nHeaderLength = nHeadLen = pabyBuf[8] + pabyBuf[9]*256;
# Line 369  DBFOpen( const char * pszFilename, const Line 593  DBFOpen( const char * pszFilename, const
593      psDBF->pszHeader = (char *) pabyBuf;      psDBF->pszHeader = (char *) pabyBuf;
594    
595      fseek( psDBF->fp, 32, 0 );      fseek( psDBF->fp, 32, 0 );
596      fread( pabyBuf, nHeadLen, 1, psDBF->fp );      if( fread( pabyBuf, nHeadLen-32, 1, psDBF->fp ) != 1 )
597        {
598            fclose( psDBF->fp );
599            free( pabyBuf );
600            free( psDBF );
601            return NULL;
602        }
603    
604      psDBF->panFieldOffset = (int *) malloc(sizeof(int) * nFields);      psDBF->panFieldOffset = (int *) malloc(sizeof(int) * nFields);
605      psDBF->panFieldSize = (int *) malloc(sizeof(int) * nFields);      psDBF->panFieldSize = (int *) malloc(sizeof(int) * nFields);
# Line 411  DBFOpen( const char * pszFilename, const Line 641  DBFOpen( const char * pszFilename, const
641  void SHPAPI_CALL  void SHPAPI_CALL
642  DBFClose(DBFHandle psDBF)  DBFClose(DBFHandle psDBF)
643  {  {
644            if( psDBF == NULL )
645                    return;
646                    
647  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
648  /*      Write out header if not already written.                        */  /*      Write out header if not already written.                        */
649  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 424  DBFClose(DBFHandle psDBF) Line 657  DBFClose(DBFHandle psDBF)
657  /*      write access.                                                   */  /*      write access.                                                   */
658  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
659      if( psDBF->bUpdated )      if( psDBF->bUpdated )
660      {          DBFUpdateHeader( psDBF );
         unsigned char           abyFileHeader[32];  
   
         fseek( psDBF->fp, 0, 0 );  
         fread( abyFileHeader, 32, 1, psDBF->fp );  
   
         abyFileHeader[1] = 95;                  /* YY */  
         abyFileHeader[2] = 7;                   /* MM */  
         abyFileHeader[3] = 26;                  /* DD */  
   
         abyFileHeader[4] = psDBF->nRecords % 256;  
         abyFileHeader[5] = (psDBF->nRecords/256) % 256;  
         abyFileHeader[6] = (psDBF->nRecords/(256*256)) % 256;  
         abyFileHeader[7] = (psDBF->nRecords/(256*256*256)) % 256;  
   
         fseek( psDBF->fp, 0, 0 );  
         fwrite( abyFileHeader, 32, 1, psDBF->fp );  
     }  
661    
662  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
663  /*      Close, and free resources.                                      */  /*      Close, and free resources.                                      */
# Line 479  DBFHandle SHPAPI_CALL Line 695  DBFHandle SHPAPI_CALL
695  DBFCreate( const char * pszFilename )  DBFCreate( const char * pszFilename )
696    
697  {  {
     DBFHandle   psDBF;  
698      FILE        *fp;      FILE        *fp;
699      char        *pszFullname, *pszBasename;      char        *pszFullname, *pszBasename;
700      int         i;      int         i;
# Line 518  DBFCreate( const char * pszFilename ) Line 733  DBFCreate( const char * pszFilename )
733    
734      free( pszFullname );      free( pszFullname );
735    
736            return DBFCreateEx( fp );
737    }
738    
739    
740    
741    /************************************************************************/
742    /*                             DBFCreateW()                             */
743    /*                                                                      */
744    /*      Create a new .dbf file with a wide character filename           */
745    /************************************************************************/
746    
747    #ifdef SHPAPI_HAS_WIDE
748    
749    DBFHandle SHPAPI_CALL
750    DBFCreateW( const wchar_t * pszFilename )
751    
752    {
753        FILE        *fp;
754        wchar_t     *pszFullname, *pszBasename;
755        int         i;
756    
757    /* -------------------------------------------------------------------- */
758    /*      Compute the base (layer) name.  If there is any extension       */
759    /*      on the passed in filename we will strip it off.                 */
760    /* -------------------------------------------------------------------- */
761        pszBasename = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszFilename)+5));
762        wcscpy( pszBasename, pszFilename );
763        for( i = wcslen(pszBasename)-1;
764             i > 0 && pszBasename[i] != L'.' && pszBasename[i] != L'/'
765                   && pszBasename[i] != L'\\';
766             i-- ) {}
767    
768        if( pszBasename[i] == L'.' )
769            pszBasename[i] = L'\0';
770    
771        pszFullname = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszBasename) + 5));
772        swprintf( pszFullname, L"%s.dbf", pszBasename );
773        free( pszBasename );
774    
775    /* -------------------------------------------------------------------- */
776    /*      Create the file.                                                */
777    /* -------------------------------------------------------------------- */
778        fp = _wfopen( pszFullname, L"wb" );
779        if( fp == NULL )
780            return( NULL );
781    
782        fputc( 0, fp );
783        fclose( fp );
784    
785        fp = _wfopen( pszFullname, L"rb+" );
786        if( fp == NULL )
787            return( NULL );
788    
789        free( pszFullname );
790    
791            return DBFCreateEx( fp );
792    }
793    
794    #endif
795    
796    
797    /************************************************************************/
798    /*                             DBFCreateEx()                            */
799    /*                                                                      */
800    /*      Create a new .dbf file from a freshly created file              */
801    /************************************************************************/
802    
803    DBFHandle SHPAPI_CALL
804    DBFCreateEx( FILE* fp )
805    
806    {
807        DBFHandle   psDBF;
808    
809  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
810  /*      Create the info structure.                                      */  /*      Create the info structure.                                      */
811  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 544  DBFCreate( const char * pszFilename ) Line 832  DBFCreate( const char * pszFilename )
832      return( psDBF );      return( psDBF );
833  }  }
834    
835    
836    
837  /************************************************************************/  /************************************************************************/
838  /*                            DBFAddField()                             */  /*                            DBFAddField()                             */
839  /*                                                                      */  /*                                                                      */
# Line 571  DBFAddField(DBFHandle psDBF, const char Line 861  DBFAddField(DBFHandle psDBF, const char
861      if( eType != FTDouble && nDecimals != 0 )      if( eType != FTDouble && nDecimals != 0 )
862          return( -1 );          return( -1 );
863    
864        if( nWidth < 1 )
865            return -1;
866    
867  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
868  /*      SfRealloc all the arrays larger to hold the additional field      */  /*      SfRealloc all the arrays larger to hold the additional field      */
869  /*      information.                                                    */  /*      information.                                                    */
# Line 597  DBFAddField(DBFHandle psDBF, const char Line 890  DBFAddField(DBFHandle psDBF, const char
890      psDBF->panFieldSize[psDBF->nFields-1] = nWidth;      psDBF->panFieldSize[psDBF->nFields-1] = nWidth;
891      psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;      psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;
892    
893      if( eType == FTString )      if( eType == FTLogical )
894            psDBF->pachFieldType[psDBF->nFields-1] = 'L';
895        else if( eType == FTString )
896          psDBF->pachFieldType[psDBF->nFields-1] = 'C';          psDBF->pachFieldType[psDBF->nFields-1] = 'C';
897      else      else
898          psDBF->pachFieldType[psDBF->nFields-1] = 'N';          psDBF->pachFieldType[psDBF->nFields-1] = 'N';
# Line 624  DBFAddField(DBFHandle psDBF, const char Line 919  DBFAddField(DBFHandle psDBF, const char
919    
920      if( eType == FTString )      if( eType == FTString )
921      {      {
922          pszFInfo[16] = nWidth % 256;          pszFInfo[16] = (unsigned char) (nWidth % 256);
923          pszFInfo[17] = nWidth / 256;          pszFInfo[17] = (unsigned char) (nWidth / 256);
924      }      }
925      else      else
926      {      {
927          pszFInfo[16] = nWidth;          pszFInfo[16] = (unsigned char) nWidth;
928          pszFInfo[17] = nDecimals;          pszFInfo[17] = (unsigned char) nDecimals;
929      }      }
930            
931  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 720  static void *DBFReadAttribute(DBFHandle Line 1015  static void *DBFReadAttribute(DBFHandle
1015  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
1016      if( chReqType == 'N' )      if( chReqType == 'N' )
1017      {      {
1018          dDoubleField = atof(pszStringField);          dDoubleField = (*atof_function)(pszStringField);
1019    
1020          pReturnField = &dDoubleField;          pReturnField = &dDoubleField;
1021      }      }
# Line 803  DBFReadStringAttribute( DBFHandle psDBF, Line 1098  DBFReadStringAttribute( DBFHandle psDBF,
1098  }  }
1099    
1100  /************************************************************************/  /************************************************************************/
1101    /*                        DBFReadLogicalAttribute()                     */
1102    /*                                                                      */
1103    /*      Read a logical attribute.                                       */
1104    /************************************************************************/
1105    
1106    const char SHPAPI_CALL1(*)
1107    DBFReadLogicalAttribute( DBFHandle psDBF, int iRecord, int iField )
1108    
1109    {
1110        return( (const char *) DBFReadAttribute( psDBF, iRecord, iField, 'L' ) );
1111    }
1112    
1113    /************************************************************************/
1114  /*                         DBFIsAttributeNULL()                         */  /*                         DBFIsAttributeNULL()                         */
1115  /*                                                                      */  /*                                                                      */
1116  /*      Return TRUE if value for field is NULL.                         */  /*      Return TRUE if value for field is NULL.                         */
# Line 818  DBFIsAttributeNULL( DBFHandle psDBF, int Line 1126  DBFIsAttributeNULL( DBFHandle psDBF, int
1126    
1127      pszValue = DBFReadStringAttribute( psDBF, iRecord, iField );      pszValue = DBFReadStringAttribute( psDBF, iRecord, iField );
1128    
1129        if( pszValue == NULL )
1130            return TRUE;
1131    
1132      switch(psDBF->pachFieldType[iField])      switch(psDBF->pachFieldType[iField])
1133      {      {
1134        case 'N':        case 'N':
# Line 837  DBFIsAttributeNULL( DBFHandle psDBF, int Line 1148  DBFIsAttributeNULL( DBFHandle psDBF, int
1148          /* empty string fields are considered NULL */          /* empty string fields are considered NULL */
1149          return strlen(pszValue) == 0;          return strlen(pszValue) == 0;
1150      }      }
     return FALSE;  
1151  }  }
1152    
1153  /************************************************************************/  /************************************************************************/
# Line 896  DBFGetFieldInfo( DBFHandle psDBF, int iF Line 1206  DBFGetFieldInfo( DBFHandle psDBF, int iF
1206              pszFieldName[i] = '\0';              pszFieldName[i] = '\0';
1207      }      }
1208    
1209      if( psDBF->pachFieldType[iField] == 'N'      if ( psDBF->pachFieldType[iField] == 'L' )
1210          || psDBF->pachFieldType[iField] == 'F'          return( FTLogical);
1211          || psDBF->pachFieldType[iField] == 'D' )  
1212        else if( psDBF->pachFieldType[iField] == 'N'
1213                 || psDBF->pachFieldType[iField] == 'F'
1214                 || psDBF->pachFieldType[iField] == 'D' )
1215      {      {
1216          if( psDBF->panFieldDecimals[iField] > 0 )          if( psDBF->panFieldDecimals[iField] > 0 )
1217              return( FTDouble );              return( FTDouble );
# Line 921  static int DBFWriteAttribute(DBFHandle p Line 1234  static int DBFWriteAttribute(DBFHandle p
1234                               void * pValue )                               void * pValue )
1235    
1236  {  {
1237      int         nRecordOffset, i, j;      int         nRecordOffset, i, j, nRetResult = TRUE;
1238      unsigned char       *pabyRec;      unsigned char       *pabyRec;
1239      char        szSField[400], szFormat[20];      char        szSField[400], szFormat[20];
1240    
# Line 1024  static int DBFWriteAttribute(DBFHandle p Line 1337  static int DBFWriteAttribute(DBFHandle p
1337              sprintf( szFormat, "%%%dd", nWidth );              sprintf( szFormat, "%%%dd", nWidth );
1338              sprintf(szSField, szFormat, (int) *((double *) pValue) );              sprintf(szSField, szFormat, (int) *((double *) pValue) );
1339              if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )              if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )
1340                {
1341                  szSField[psDBF->panFieldSize[iField]] = '\0';                  szSField[psDBF->panFieldSize[iField]] = '\0';
1342                    nRetResult = FALSE;
1343                }
1344    
1345              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1346                      szSField, strlen(szSField) );                      szSField, strlen(szSField) );
# Line 1040  static int DBFWriteAttribute(DBFHandle p Line 1356  static int DBFWriteAttribute(DBFHandle p
1356                       nWidth, psDBF->panFieldDecimals[iField] );                       nWidth, psDBF->panFieldDecimals[iField] );
1357              sprintf(szSField, szFormat, *((double *) pValue) );              sprintf(szSField, szFormat, *((double *) pValue) );
1358              if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )              if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )
1359                {
1360                  szSField[psDBF->panFieldSize[iField]] = '\0';                  szSField[psDBF->panFieldSize[iField]] = '\0';
1361                    nRetResult = FALSE;
1362                }
1363              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1364                      szSField, strlen(szSField) );                      szSField, strlen(szSField) );
1365          }          }
1366          break;          break;
1367    
1368          case 'L':
1369            if (psDBF->panFieldSize[iField] >= 1  &&
1370                (*(char*)pValue == 'F' || *(char*)pValue == 'T'))
1371                *(pabyRec+psDBF->panFieldOffset[iField]) = *(char*)pValue;
1372            break;
1373    
1374        default:        default:
1375          if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )          if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1376            {
1377              j = psDBF->panFieldSize[iField];              j = psDBF->panFieldSize[iField];
1378                nRetResult = FALSE;
1379            }
1380          else          else
1381          {          {
1382              memset( pabyRec+psDBF->panFieldOffset[iField], ' ',              memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
# Line 1061  static int DBFWriteAttribute(DBFHandle p Line 1389  static int DBFWriteAttribute(DBFHandle p
1389          break;          break;
1390      }      }
1391    
1392        return( nRetResult );
1393    }
1394    
1395    /************************************************************************/
1396    /*                     DBFWriteAttributeDirectly()                      */
1397    /*                                                                      */
1398    /*      Write an attribute record to the file, but without any          */
1399    /*      reformatting based on type.  The provided buffer is written     */
1400    /*      as is to the field position in the record.                      */
1401    /************************************************************************/
1402    
1403    int SHPAPI_CALL
1404    DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
1405                                  void * pValue )
1406    
1407    {
1408        int         nRecordOffset, i, j;
1409        unsigned char       *pabyRec;
1410    
1411    /* -------------------------------------------------------------------- */
1412    /*      Is this a valid record?                                         */
1413    /* -------------------------------------------------------------------- */
1414        if( hEntity < 0 || hEntity > psDBF->nRecords )
1415            return( FALSE );
1416    
1417        if( psDBF->bNoHeader )
1418            DBFWriteHeader(psDBF);
1419    
1420    /* -------------------------------------------------------------------- */
1421    /*      Is this a brand new record?                                     */
1422    /* -------------------------------------------------------------------- */
1423        if( hEntity == psDBF->nRecords )
1424        {
1425            DBFFlushRecord( psDBF );
1426    
1427            psDBF->nRecords++;
1428            for( i = 0; i < psDBF->nRecordLength; i++ )
1429                psDBF->pszCurrentRecord[i] = ' ';
1430    
1431            psDBF->nCurrentRecord = hEntity;
1432        }
1433    
1434    /* -------------------------------------------------------------------- */
1435    /*      Is this an existing record, but different than the last one     */
1436    /*      we accessed?                                                    */
1437    /* -------------------------------------------------------------------- */
1438        if( psDBF->nCurrentRecord != hEntity )
1439        {
1440            DBFFlushRecord( psDBF );
1441    
1442            nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;
1443    
1444            fseek( psDBF->fp, nRecordOffset, 0 );
1445            fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );
1446    
1447            psDBF->nCurrentRecord = hEntity;
1448        }
1449    
1450        pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
1451    
1452    /* -------------------------------------------------------------------- */
1453    /*      Assign all the record fields.                                   */
1454    /* -------------------------------------------------------------------- */
1455        if( (int)strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1456            j = psDBF->panFieldSize[iField];
1457        else
1458        {
1459            memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
1460                    psDBF->panFieldSize[iField] );
1461            j = strlen((char *) pValue);
1462        }
1463    
1464        strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1465                (char *) pValue, j );
1466    
1467        psDBF->bCurrentRecordModified = TRUE;
1468        psDBF->bUpdated = TRUE;
1469    
1470      return( TRUE );      return( TRUE );
1471  }  }
1472    
# Line 1122  DBFWriteNULLAttribute( DBFHandle psDBF, Line 1528  DBFWriteNULLAttribute( DBFHandle psDBF,
1528  }  }
1529    
1530  /************************************************************************/  /************************************************************************/
1531    /*                      DBFWriteLogicalAttribute()                      */
1532    /*                                                                      */
1533    /*      Write a logical attribute.                                      */
1534    /************************************************************************/
1535    
1536    int SHPAPI_CALL
1537    DBFWriteLogicalAttribute( DBFHandle psDBF, int iRecord, int iField,
1538                           const char lValue)
1539    
1540    {
1541        return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) (&lValue) ) );
1542    }
1543    
1544    /************************************************************************/
1545  /*                         DBFWriteTuple()                              */  /*                         DBFWriteTuple()                              */
1546  /*                                                                      */  /*                                                                      */
1547  /*      Write an attribute record to the file.                          */  /*      Write an attribute record to the file.                          */
# Line 1241  DBFCloneEmpty(DBFHandle psDBF, const cha Line 1661  DBFCloneEmpty(DBFHandle psDBF, const cha
1661      DBFHandle   newDBF;      DBFHandle   newDBF;
1662    
1663     newDBF = DBFCreate ( pszFilename );     newDBF = DBFCreate ( pszFilename );
1664       if ( newDBF == NULL ) return ( NULL );
1665    
1666       DBFCloneEmptyEx( psDBF, newDBF );
1667    
1668       DBFClose( newDBF );      
1669       newDBF = DBFOpen ( pszFilename, "rb+" );
1670    
1671       return ( newDBF );
1672    }
1673    
1674    
1675    
1676    
1677    /************************************************************************/
1678    /*                          DBFCloneEmptyW                              */
1679    /*                                                                      */
1680    /*      Read one of the attribute fields of a record.                   */
1681    /************************************************************************/
1682    
1683    #ifdef SHPAPI_HAS_WIDE
1684    
1685    DBFHandle SHPAPI_CALL
1686    DBFCloneEmptyW(DBFHandle psDBF, const wchar_t * pszFilename )
1687    {
1688        DBFHandle   newDBF;
1689    
1690       newDBF = DBFCreateW ( pszFilename );
1691       if ( newDBF == NULL ) return ( NULL );
1692    
1693       DBFCloneEmptyEx( psDBF, newDBF );
1694    
1695       DBFClose( newDBF );      
1696       newDBF = DBFOpenW ( pszFilename, L"rb+" );
1697    
1698       return ( newDBF );
1699    }
1700    
1701    #endif
1702    
1703    /************************************************************************/
1704    /*                          DBFCloneEmptyEx()                           */
1705    /*                                                                      */
1706    /*      Read one of the attribute fields of a record.                   */
1707    /************************************************************************/
1708    
1709    void SHPAPI_CALL
1710    DBFCloneEmptyEx(DBFHandle psDBF, DBFHandle newDBF)
1711    {
1712     if ( newDBF == NULL ) return ( NULL );     if ( newDBF == NULL ) return ( NULL );
1713        
1714     newDBF->pszHeader = (void *) malloc ( 32 * psDBF->nFields );     newDBF->pszHeader = (char *) malloc ( 32 * psDBF->nFields );
1715     memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields );     memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields );
1716        
1717     newDBF->nFields = psDBF->nFields;     newDBF->nFields = psDBF->nFields;
1718     newDBF->nRecordLength = psDBF->nRecordLength;     newDBF->nRecordLength = psDBF->nRecordLength;
1719     newDBF->nHeaderLength = psDBF->nHeaderLength;     newDBF->nHeaderLength = 32 * (psDBF->nFields+1);
1720            
1721     newDBF->panFieldOffset = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->panFieldOffset = (int *) malloc ( sizeof(int) * psDBF->nFields );
1722     memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
1723     newDBF->panFieldSize = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->panFieldSize = (int *) malloc ( sizeof(int) * psDBF->nFields );
1724     memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
1725     newDBF->panFieldDecimals = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->panFieldDecimals = (int *) malloc ( sizeof(int) * psDBF->nFields );
1726     memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
1727     newDBF->pachFieldType = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->pachFieldType = (char *) malloc ( sizeof(int) * psDBF->nFields );
1728     memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(int) * psDBF->nFields );
1729    
1730     newDBF->bNoHeader = TRUE;     newDBF->bNoHeader = TRUE;
1731     newDBF->bUpdated = TRUE;     newDBF->bUpdated = TRUE;
1732        
1733     DBFWriteHeader ( newDBF );     DBFWriteHeader ( newDBF );
    DBFClose ( newDBF );  
     
    newDBF = DBFOpen ( pszFilename, "rb+" );  
   
    return ( newDBF );  
1734  }  }
1735    
1736  /************************************************************************/  /************************************************************************/
# Line 1304  static void str_to_upper (char *string) Line 1767  static void str_to_upper (char *string)
1767    
1768      while (++i < len)      while (++i < len)
1769          if (isalpha(string[i]) && islower(string[i]))          if (isalpha(string[i]) && islower(string[i]))
1770              string[i] = toupper ((int)string[i]);              string[i] = (char) toupper ((int)string[i]);
1771  }  }
1772    
1773  /************************************************************************/  /************************************************************************/
# Line 1323  DBFGetFieldIndex(DBFHandle psDBF, const Line 1786  DBFGetFieldIndex(DBFHandle psDBF, const
1786      int           i;      int           i;
1787    
1788      strncpy(name1, pszFieldName,11);      strncpy(name1, pszFieldName,11);
1789        name1[11] = '\0';
1790      str_to_upper(name1);      str_to_upper(name1);
1791    
1792      for( i = 0; i < DBFGetFieldCount(psDBF); i++ )      for( i = 0; i < DBFGetFieldCount(psDBF); i++ )
# Line 1336  DBFGetFieldIndex(DBFHandle psDBF, const Line 1800  DBFGetFieldIndex(DBFHandle psDBF, const
1800      }      }
1801      return(-1);      return(-1);
1802  }  }
   
 /************************************************************************/  
 /*                          DBFCommit()                                 */  
 /*                                                                      */  
 /*      Write any changes made into the file.                           */  
 /*                                                                      */  
 /************************************************************************/  
 int SHPAPI_CALL  
 DBFCommit( DBFHandle psDBF )  
   
 {  
     DBFFlushRecord( psDBF );  
     if (fflush( psDBF->fp ) == EOF)  
         return FALSE;  
   
     return TRUE;  
 }  

Legend:
Removed from v.1612  
changed lines
  Added in v.2751

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26