/[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 1769 by bh, Thu Oct 2 15:15:16 2003 UTC branches/WIP-pyshapelib-bramz/libraries/shapelib/dbfopen.c revision 2734 by bramz, Thu Mar 1 12:42:59 2007 UTC
# Line 34  Line 34 
34   ******************************************************************************   ******************************************************************************
35   *   *
36   * $Log$   * $Log$
37   * Revision 1.2  2003/10/02 15:15:16  bh   * Revision 1.3  2004/05/17 15:47:57  bh
38   * Update to shapelib 1.2.10   * Update to newest shapelib and get rid of Thuban specific extensions,
39     * i.e. use the new DBFUpdateHeader instead of our DBFCommit kludge
40     *
41     * * libraries/shapelib/shpopen.c: Update to version from current
42     * shapelib CVS.
43     *
44     * * libraries/shapelib/shapefil.h: Update to version from current
45     * shapelib CVS.
46     *
47     * * libraries/shapelib/dbfopen.c: Update to version from current
48     * shapelib CVS.
49     * (DBFCommit): Effectively removed since shapelib itself has
50     * 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   * Revision 1.48  2003/03/10 14:51:27  warmerda
92   * DBFWrite* calls now return FALSE if they have to truncate   * DBFWrite* calls now return FALSE if they have to truncate
# Line 204  static int     nStringFieldLen = 0; Line 255  static int     nStringFieldLen = 0;
255  static char * pszStringField = NULL;  static char * pszStringField = NULL;
256    
257  /************************************************************************/  /************************************************************************/
258    /*                             DBFSet_atof_function()                   */
259    /*                                                                      */
260    /* This makes it possible to initialise a different atof() function     */
261    /* which might be necessary because the standard atof() might be        */
262    /* sensitive to locale settings.                                        */
263    /*                                                                      */
264    /* If the calling application uses a locale with different decimal_point*/
265    /* it should better also give us a locale agnostic atof() function.     */
266    /*                                                                      */
267    /* As far as I can see from Python PEP331 and GNU libc documentation    */
268    /* there is no standard for such a function yet.                        */
269    /*                                                                      */
270    /* [email protected] 20060924                               */
271    /************************************************************************/
272    
273    static double (* atof_function)(const char *nptr)  = &atof;
274    
275    void SHPAPI_CALL
276            DBFSetatof_function(  double (* new_atof_function)(const char *nptr))
277    {
278            atof_function = new_atof_function;
279    }
280    
281    /************************************************************************/
282  /*                             SfRealloc()                              */  /*                             SfRealloc()                              */
283  /*                                                                      */  /*                                                                      */
284  /*      A realloc cover function that will access a NULL pointer as     */  /*      A realloc cover function that will access a NULL pointer as     */
# Line 247  static void DBFWriteHeader(DBFHandle psD Line 322  static void DBFWriteHeader(DBFHandle psD
322    
323      abyHeader[0] = 0x03;                /* memo field? - just copying   */      abyHeader[0] = 0x03;                /* memo field? - just copying   */
324    
325      /* date updated on close, record count preset at zero */      /* write out a dummy date */
326        abyHeader[1] = 95;                  /* YY */
327        abyHeader[2] = 7;                   /* MM */
328        abyHeader[3] = 26;                  /* DD */
329    
330        /* record count preset at zero */
331    
332      abyHeader[8] = psDBF->nHeaderLength % 256;      abyHeader[8] = (unsigned char) (psDBF->nHeaderLength % 256);
333      abyHeader[9] = psDBF->nHeaderLength / 256;      abyHeader[9] = (unsigned char) (psDBF->nHeaderLength / 256);
334            
335      abyHeader[10] = psDBF->nRecordLength % 256;      abyHeader[10] = (unsigned char) (psDBF->nRecordLength % 256);
336      abyHeader[11] = psDBF->nRecordLength / 256;      abyHeader[11] = (unsigned char) (psDBF->nRecordLength / 256);
337    
338  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
339  /*      Write the initial 32 byte file header, and all the field        */  /*      Write the initial 32 byte file header, and all the field        */
# Line 299  static void DBFFlushRecord( DBFHandle ps Line 379  static void DBFFlushRecord( DBFHandle ps
379  }  }
380    
381  /************************************************************************/  /************************************************************************/
382    /*                          DBFUpdateHeader()                           */
383    /************************************************************************/
384    
385    void SHPAPI_CALL
386    DBFUpdateHeader( DBFHandle psDBF )
387    
388    {
389        unsigned char               abyFileHeader[32];
390    
391        if( psDBF->bNoHeader )
392            DBFWriteHeader( psDBF );
393    
394        DBFFlushRecord( psDBF );
395    
396        fseek( psDBF->fp, 0, 0 );
397        fread( abyFileHeader, 32, 1, psDBF->fp );
398        
399        abyFileHeader[4] = (unsigned char) (psDBF->nRecords % 256);
400        abyFileHeader[5] = (unsigned char) ((psDBF->nRecords/256) % 256);
401        abyFileHeader[6] = (unsigned char) ((psDBF->nRecords/(256*256)) % 256);
402        abyFileHeader[7] = (unsigned char) ((psDBF->nRecords/(256*256*256)) % 256);
403        
404        fseek( psDBF->fp, 0, 0 );
405        fwrite( abyFileHeader, 32, 1, psDBF->fp );
406    
407        fflush( psDBF->fp );
408    }
409    
410    /************************************************************************/
411  /*                              DBFOpen()                               */  /*                              DBFOpen()                               */
412  /*                                                                      */  /*                                                                      */
413  /*      Open a .dbf file.                                               */  /*      Open a .dbf file.                                               */
# Line 457  DBFClose(DBFHandle psDBF) Line 566  DBFClose(DBFHandle psDBF)
566  /*      write access.                                                   */  /*      write access.                                                   */
567  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
568      if( psDBF->bUpdated )      if( psDBF->bUpdated )
569      {          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 );  
     }  
570    
571  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
572  /*      Close, and free resources.                                      */  /*      Close, and free resources.                                      */
# Line 662  DBFAddField(DBFHandle psDBF, const char Line 754  DBFAddField(DBFHandle psDBF, const char
754    
755      if( eType == FTString )      if( eType == FTString )
756      {      {
757          pszFInfo[16] = nWidth % 256;          pszFInfo[16] = (unsigned char) (nWidth % 256);
758          pszFInfo[17] = nWidth / 256;          pszFInfo[17] = (unsigned char) (nWidth / 256);
759      }      }
760      else      else
761      {      {
762          pszFInfo[16] = nWidth;          pszFInfo[16] = (unsigned char) nWidth;
763          pszFInfo[17] = nDecimals;          pszFInfo[17] = (unsigned char) nDecimals;
764      }      }
765            
766  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 758  static void *DBFReadAttribute(DBFHandle Line 850  static void *DBFReadAttribute(DBFHandle
850  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
851      if( chReqType == 'N' )      if( chReqType == 'N' )
852      {      {
853          dDoubleField = atof(pszStringField);          dDoubleField = (*atof_function)(pszStringField);
854    
855          pReturnField = &dDoubleField;          pReturnField = &dDoubleField;
856      }      }
# Line 869  DBFIsAttributeNULL( DBFHandle psDBF, int Line 961  DBFIsAttributeNULL( DBFHandle psDBF, int
961    
962      pszValue = DBFReadStringAttribute( psDBF, iRecord, iField );      pszValue = DBFReadStringAttribute( psDBF, iRecord, iField );
963    
964        if( pszValue == NULL )
965            return TRUE;
966    
967      switch(psDBF->pachFieldType[iField])      switch(psDBF->pachFieldType[iField])
968      {      {
969        case 'N':        case 'N':
# Line 1140  static int DBFWriteAttribute(DBFHandle p Line 1235  static int DBFWriteAttribute(DBFHandle p
1235  /*      as is to the field position in the record.                      */  /*      as is to the field position in the record.                      */
1236  /************************************************************************/  /************************************************************************/
1237    
1238  int DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,  int SHPAPI_CALL
1239    DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
1240                                void * pValue )                                void * pValue )
1241    
1242  {  {
# Line 1463  static void str_to_upper (char *string) Line 1559  static void str_to_upper (char *string)
1559    
1560      while (++i < len)      while (++i < len)
1561          if (isalpha(string[i]) && islower(string[i]))          if (isalpha(string[i]) && islower(string[i]))
1562              string[i] = toupper ((int)string[i]);              string[i] = (char) toupper ((int)string[i]);
1563  }  }
1564    
1565  /************************************************************************/  /************************************************************************/
# Line 1496  DBFGetFieldIndex(DBFHandle psDBF, const Line 1592  DBFGetFieldIndex(DBFHandle psDBF, const
1592      }      }
1593      return(-1);      return(-1);
1594  }  }
   
 /************************************************************************/  
 /*                          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.1769  
changed lines
  Added in v.2734

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26