/[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 2734 by bramz, Thu Mar 1 12:42:59 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 183  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 226  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 278  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 289  DBFOpen( const char * pszFilename, const Line 419  DBFOpen( const char * pszFilename, const
419  {  {
420      DBFHandle           psDBF;      DBFHandle           psDBF;
421      unsigned char               *pabyBuf;      unsigned char               *pabyBuf;
422      int                 nFields, nRecords, nHeadLen, nRecLen, iField, i;      int                 nFields, nHeadLen, nRecLen, iField, i;
423      char                *pszBasename, *pszFullname;      char                *pszBasename, *pszFullname;
424    
425  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 349  DBFOpen( const char * pszFilename, const Line 479  DBFOpen( const char * pszFilename, const
479  /*  Read Table Header info                                              */  /*  Read Table Header info                                              */
480  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
481      pabyBuf = (unsigned char *) malloc(500);      pabyBuf = (unsigned char *) malloc(500);
482      fread( pabyBuf, 32, 1, psDBF->fp );      if( fread( pabyBuf, 32, 1, psDBF->fp ) != 1 )
483        {
484            fclose( psDBF->fp );
485            free( pabyBuf );
486            free( psDBF );
487            return NULL;
488        }
489    
490      psDBF->nRecords = nRecords =      psDBF->nRecords =
491       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;
492    
493      psDBF->nHeaderLength = nHeadLen = pabyBuf[8] + pabyBuf[9]*256;      psDBF->nHeaderLength = nHeadLen = pabyBuf[8] + pabyBuf[9]*256;
# Line 369  DBFOpen( const char * pszFilename, const Line 505  DBFOpen( const char * pszFilename, const
505      psDBF->pszHeader = (char *) pabyBuf;      psDBF->pszHeader = (char *) pabyBuf;
506    
507      fseek( psDBF->fp, 32, 0 );      fseek( psDBF->fp, 32, 0 );
508      fread( pabyBuf, nHeadLen, 1, psDBF->fp );      if( fread( pabyBuf, nHeadLen-32, 1, psDBF->fp ) != 1 )
509        {
510            fclose( psDBF->fp );
511            free( pabyBuf );
512            free( psDBF );
513            return NULL;
514        }
515    
516      psDBF->panFieldOffset = (int *) malloc(sizeof(int) * nFields);      psDBF->panFieldOffset = (int *) malloc(sizeof(int) * nFields);
517      psDBF->panFieldSize = (int *) malloc(sizeof(int) * nFields);      psDBF->panFieldSize = (int *) malloc(sizeof(int) * nFields);
# Line 424  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 571  DBFAddField(DBFHandle psDBF, const char Line 696  DBFAddField(DBFHandle psDBF, const char
696      if( eType != FTDouble && nDecimals != 0 )      if( eType != FTDouble && nDecimals != 0 )
697          return( -1 );          return( -1 );
698    
699        if( nWidth < 1 )
700            return -1;
701    
702  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
703  /*      SfRealloc all the arrays larger to hold the additional field      */  /*      SfRealloc all the arrays larger to hold the additional field      */
704  /*      information.                                                    */  /*      information.                                                    */
# Line 597  DBFAddField(DBFHandle psDBF, const char Line 725  DBFAddField(DBFHandle psDBF, const char
725      psDBF->panFieldSize[psDBF->nFields-1] = nWidth;      psDBF->panFieldSize[psDBF->nFields-1] = nWidth;
726      psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;      psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;
727    
728      if( eType == FTString )      if( eType == FTLogical )
729            psDBF->pachFieldType[psDBF->nFields-1] = 'L';
730        else if( eType == FTString )
731          psDBF->pachFieldType[psDBF->nFields-1] = 'C';          psDBF->pachFieldType[psDBF->nFields-1] = 'C';
732      else      else
733          psDBF->pachFieldType[psDBF->nFields-1] = 'N';          psDBF->pachFieldType[psDBF->nFields-1] = 'N';
# Line 624  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 720  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 803  DBFReadStringAttribute( DBFHandle psDBF, Line 933  DBFReadStringAttribute( DBFHandle psDBF,
933  }  }
934    
935  /************************************************************************/  /************************************************************************/
936    /*                        DBFReadLogicalAttribute()                     */
937    /*                                                                      */
938    /*      Read a logical attribute.                                       */
939    /************************************************************************/
940    
941    const char SHPAPI_CALL1(*)
942    DBFReadLogicalAttribute( DBFHandle psDBF, int iRecord, int iField )
943    
944    {
945        return( (const char *) DBFReadAttribute( psDBF, iRecord, iField, 'L' ) );
946    }
947    
948    /************************************************************************/
949  /*                         DBFIsAttributeNULL()                         */  /*                         DBFIsAttributeNULL()                         */
950  /*                                                                      */  /*                                                                      */
951  /*      Return TRUE if value for field is NULL.                         */  /*      Return TRUE if value for field is NULL.                         */
# Line 818  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 837  DBFIsAttributeNULL( DBFHandle psDBF, int Line 983  DBFIsAttributeNULL( DBFHandle psDBF, int
983          /* empty string fields are considered NULL */          /* empty string fields are considered NULL */
984          return strlen(pszValue) == 0;          return strlen(pszValue) == 0;
985      }      }
     return FALSE;  
986  }  }
987    
988  /************************************************************************/  /************************************************************************/
# Line 896  DBFGetFieldInfo( DBFHandle psDBF, int iF Line 1041  DBFGetFieldInfo( DBFHandle psDBF, int iF
1041              pszFieldName[i] = '\0';              pszFieldName[i] = '\0';
1042      }      }
1043    
1044      if( psDBF->pachFieldType[iField] == 'N'      if ( psDBF->pachFieldType[iField] == 'L' )
1045          || psDBF->pachFieldType[iField] == 'F'          return( FTLogical);
1046          || psDBF->pachFieldType[iField] == 'D' )  
1047        else if( psDBF->pachFieldType[iField] == 'N'
1048                 || psDBF->pachFieldType[iField] == 'F'
1049                 || psDBF->pachFieldType[iField] == 'D' )
1050      {      {
1051          if( psDBF->panFieldDecimals[iField] > 0 )          if( psDBF->panFieldDecimals[iField] > 0 )
1052              return( FTDouble );              return( FTDouble );
# Line 921  static int DBFWriteAttribute(DBFHandle p Line 1069  static int DBFWriteAttribute(DBFHandle p
1069                               void * pValue )                               void * pValue )
1070    
1071  {  {
1072      int         nRecordOffset, i, j;      int         nRecordOffset, i, j, nRetResult = TRUE;
1073      unsigned char       *pabyRec;      unsigned char       *pabyRec;
1074      char        szSField[400], szFormat[20];      char        szSField[400], szFormat[20];
1075    
# Line 1024  static int DBFWriteAttribute(DBFHandle p Line 1172  static int DBFWriteAttribute(DBFHandle p
1172              sprintf( szFormat, "%%%dd", nWidth );              sprintf( szFormat, "%%%dd", nWidth );
1173              sprintf(szSField, szFormat, (int) *((double *) pValue) );              sprintf(szSField, szFormat, (int) *((double *) pValue) );
1174              if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )              if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )
1175                {
1176                  szSField[psDBF->panFieldSize[iField]] = '\0';                  szSField[psDBF->panFieldSize[iField]] = '\0';
1177                    nRetResult = FALSE;
1178                }
1179    
1180              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1181                      szSField, strlen(szSField) );                      szSField, strlen(szSField) );
# Line 1040  static int DBFWriteAttribute(DBFHandle p Line 1191  static int DBFWriteAttribute(DBFHandle p
1191                       nWidth, psDBF->panFieldDecimals[iField] );                       nWidth, psDBF->panFieldDecimals[iField] );
1192              sprintf(szSField, szFormat, *((double *) pValue) );              sprintf(szSField, szFormat, *((double *) pValue) );
1193              if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )              if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )
1194                {
1195                  szSField[psDBF->panFieldSize[iField]] = '\0';                  szSField[psDBF->panFieldSize[iField]] = '\0';
1196                    nRetResult = FALSE;
1197                }
1198              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1199                      szSField, strlen(szSField) );                      szSField, strlen(szSField) );
1200          }          }
1201          break;          break;
1202    
1203          case 'L':
1204            if (psDBF->panFieldSize[iField] >= 1  &&
1205                (*(char*)pValue == 'F' || *(char*)pValue == 'T'))
1206                *(pabyRec+psDBF->panFieldOffset[iField]) = *(char*)pValue;
1207            break;
1208    
1209        default:        default:
1210          if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )          if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1211            {
1212              j = psDBF->panFieldSize[iField];              j = psDBF->panFieldSize[iField];
1213                nRetResult = FALSE;
1214            }
1215          else          else
1216          {          {
1217              memset( pabyRec+psDBF->panFieldOffset[iField], ' ',              memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
# Line 1061  static int DBFWriteAttribute(DBFHandle p Line 1224  static int DBFWriteAttribute(DBFHandle p
1224          break;          break;
1225      }      }
1226    
1227        return( nRetResult );
1228    }
1229    
1230    /************************************************************************/
1231    /*                     DBFWriteAttributeDirectly()                      */
1232    /*                                                                      */
1233    /*      Write an attribute record to the file, but without any          */
1234    /*      reformatting based on type.  The provided buffer is written     */
1235    /*      as is to the field position in the record.                      */
1236    /************************************************************************/
1237    
1238    int SHPAPI_CALL
1239    DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
1240                                  void * pValue )
1241    
1242    {
1243        int         nRecordOffset, i, j;
1244        unsigned char       *pabyRec;
1245    
1246    /* -------------------------------------------------------------------- */
1247    /*      Is this a valid record?                                         */
1248    /* -------------------------------------------------------------------- */
1249        if( hEntity < 0 || hEntity > psDBF->nRecords )
1250            return( FALSE );
1251    
1252        if( psDBF->bNoHeader )
1253            DBFWriteHeader(psDBF);
1254    
1255    /* -------------------------------------------------------------------- */
1256    /*      Is this a brand new record?                                     */
1257    /* -------------------------------------------------------------------- */
1258        if( hEntity == psDBF->nRecords )
1259        {
1260            DBFFlushRecord( psDBF );
1261    
1262            psDBF->nRecords++;
1263            for( i = 0; i < psDBF->nRecordLength; i++ )
1264                psDBF->pszCurrentRecord[i] = ' ';
1265    
1266            psDBF->nCurrentRecord = hEntity;
1267        }
1268    
1269    /* -------------------------------------------------------------------- */
1270    /*      Is this an existing record, but different than the last one     */
1271    /*      we accessed?                                                    */
1272    /* -------------------------------------------------------------------- */
1273        if( psDBF->nCurrentRecord != hEntity )
1274        {
1275            DBFFlushRecord( psDBF );
1276    
1277            nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;
1278    
1279            fseek( psDBF->fp, nRecordOffset, 0 );
1280            fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );
1281    
1282            psDBF->nCurrentRecord = hEntity;
1283        }
1284    
1285        pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
1286    
1287    /* -------------------------------------------------------------------- */
1288    /*      Assign all the record fields.                                   */
1289    /* -------------------------------------------------------------------- */
1290        if( (int)strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1291            j = psDBF->panFieldSize[iField];
1292        else
1293        {
1294            memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
1295                    psDBF->panFieldSize[iField] );
1296            j = strlen((char *) pValue);
1297        }
1298    
1299        strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1300                (char *) pValue, j );
1301    
1302        psDBF->bCurrentRecordModified = TRUE;
1303        psDBF->bUpdated = TRUE;
1304    
1305      return( TRUE );      return( TRUE );
1306  }  }
1307    
# Line 1122  DBFWriteNULLAttribute( DBFHandle psDBF, Line 1363  DBFWriteNULLAttribute( DBFHandle psDBF,
1363  }  }
1364    
1365  /************************************************************************/  /************************************************************************/
1366    /*                      DBFWriteLogicalAttribute()                      */
1367    /*                                                                      */
1368    /*      Write a logical attribute.                                      */
1369    /************************************************************************/
1370    
1371    int SHPAPI_CALL
1372    DBFWriteLogicalAttribute( DBFHandle psDBF, int iRecord, int iField,
1373                           const char lValue)
1374    
1375    {
1376        return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) (&lValue) ) );
1377    }
1378    
1379    /************************************************************************/
1380  /*                         DBFWriteTuple()                              */  /*                         DBFWriteTuple()                              */
1381  /*                                                                      */  /*                                                                      */
1382  /*      Write an attribute record to the file.                          */  /*      Write an attribute record to the file.                          */
# Line 1243  DBFCloneEmpty(DBFHandle psDBF, const cha Line 1498  DBFCloneEmpty(DBFHandle psDBF, const cha
1498     newDBF = DBFCreate ( pszFilename );     newDBF = DBFCreate ( pszFilename );
1499     if ( newDBF == NULL ) return ( NULL );     if ( newDBF == NULL ) return ( NULL );
1500        
1501     newDBF->pszHeader = (void *) malloc ( 32 * psDBF->nFields );     newDBF->pszHeader = (char *) malloc ( 32 * psDBF->nFields );
1502     memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields );     memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields );
1503        
1504     newDBF->nFields = psDBF->nFields;     newDBF->nFields = psDBF->nFields;
1505     newDBF->nRecordLength = psDBF->nRecordLength;     newDBF->nRecordLength = psDBF->nRecordLength;
1506     newDBF->nHeaderLength = psDBF->nHeaderLength;     newDBF->nHeaderLength = 32 * (psDBF->nFields+1);
1507            
1508     newDBF->panFieldOffset = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->panFieldOffset = (int *) malloc ( sizeof(int) * psDBF->nFields );
1509     memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
1510     newDBF->panFieldSize = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->panFieldSize = (int *) malloc ( sizeof(int) * psDBF->nFields );
1511     memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
1512     newDBF->panFieldDecimals = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->panFieldDecimals = (int *) malloc ( sizeof(int) * psDBF->nFields );
1513     memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
1514     newDBF->pachFieldType = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->pachFieldType = (char *) malloc ( sizeof(int) * psDBF->nFields );
1515     memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(int) * psDBF->nFields );
1516    
1517     newDBF->bNoHeader = TRUE;     newDBF->bNoHeader = TRUE;
# Line 1304  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 1323  DBFGetFieldIndex(DBFHandle psDBF, const Line 1578  DBFGetFieldIndex(DBFHandle psDBF, const
1578      int           i;      int           i;
1579    
1580      strncpy(name1, pszFieldName,11);      strncpy(name1, pszFieldName,11);
1581        name1[11] = '\0';
1582      str_to_upper(name1);      str_to_upper(name1);
1583    
1584      for( i = 0; i < DBFGetFieldCount(psDBF); i++ )      for( i = 0; i < DBFGetFieldCount(psDBF); i++ )
# Line 1336  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.1612  
changed lines
  Added in v.2734

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26