/[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 2747 by bramz, Tue Mar 20 23:12:45 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 411  DBFOpen( const char * pszFilename, const Line 553  DBFOpen( const char * pszFilename, const
553  void SHPAPI_CALL  void SHPAPI_CALL
554  DBFClose(DBFHandle psDBF)  DBFClose(DBFHandle psDBF)
555  {  {
556            if( psSHP == NULL )
557                    return;
558                    
559  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
560  /*      Write out header if not already written.                        */  /*      Write out header if not already written.                        */
561  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 424  DBFClose(DBFHandle psDBF) Line 569  DBFClose(DBFHandle psDBF)
569  /*      write access.                                                   */  /*      write access.                                                   */
570  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
571      if( psDBF->bUpdated )      if( psDBF->bUpdated )
572      {          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 );  
     }  
573    
574  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
575  /*      Close, and free resources.                                      */  /*      Close, and free resources.                                      */
# Line 571  DBFAddField(DBFHandle psDBF, const char Line 699  DBFAddField(DBFHandle psDBF, const char
699      if( eType != FTDouble && nDecimals != 0 )      if( eType != FTDouble && nDecimals != 0 )
700          return( -1 );          return( -1 );
701    
702        if( nWidth < 1 )
703            return -1;
704    
705  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
706  /*      SfRealloc all the arrays larger to hold the additional field      */  /*      SfRealloc all the arrays larger to hold the additional field      */
707  /*      information.                                                    */  /*      information.                                                    */
# Line 597  DBFAddField(DBFHandle psDBF, const char Line 728  DBFAddField(DBFHandle psDBF, const char
728      psDBF->panFieldSize[psDBF->nFields-1] = nWidth;      psDBF->panFieldSize[psDBF->nFields-1] = nWidth;
729      psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;      psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;
730    
731      if( eType == FTString )      if( eType == FTLogical )
732            psDBF->pachFieldType[psDBF->nFields-1] = 'L';
733        else if( eType == FTString )
734          psDBF->pachFieldType[psDBF->nFields-1] = 'C';          psDBF->pachFieldType[psDBF->nFields-1] = 'C';
735      else      else
736          psDBF->pachFieldType[psDBF->nFields-1] = 'N';          psDBF->pachFieldType[psDBF->nFields-1] = 'N';
# Line 624  DBFAddField(DBFHandle psDBF, const char Line 757  DBFAddField(DBFHandle psDBF, const char
757    
758      if( eType == FTString )      if( eType == FTString )
759      {      {
760          pszFInfo[16] = nWidth % 256;          pszFInfo[16] = (unsigned char) (nWidth % 256);
761          pszFInfo[17] = nWidth / 256;          pszFInfo[17] = (unsigned char) (nWidth / 256);
762      }      }
763      else      else
764      {      {
765          pszFInfo[16] = nWidth;          pszFInfo[16] = (unsigned char) nWidth;
766          pszFInfo[17] = nDecimals;          pszFInfo[17] = (unsigned char) nDecimals;
767      }      }
768            
769  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 720  static void *DBFReadAttribute(DBFHandle Line 853  static void *DBFReadAttribute(DBFHandle
853  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
854      if( chReqType == 'N' )      if( chReqType == 'N' )
855      {      {
856          dDoubleField = atof(pszStringField);          dDoubleField = (*atof_function)(pszStringField);
857    
858          pReturnField = &dDoubleField;          pReturnField = &dDoubleField;
859      }      }
# Line 803  DBFReadStringAttribute( DBFHandle psDBF, Line 936  DBFReadStringAttribute( DBFHandle psDBF,
936  }  }
937    
938  /************************************************************************/  /************************************************************************/
939    /*                        DBFReadLogicalAttribute()                     */
940    /*                                                                      */
941    /*      Read a logical attribute.                                       */
942    /************************************************************************/
943    
944    const char SHPAPI_CALL1(*)
945    DBFReadLogicalAttribute( DBFHandle psDBF, int iRecord, int iField )
946    
947    {
948        return( (const char *) DBFReadAttribute( psDBF, iRecord, iField, 'L' ) );
949    }
950    
951    /************************************************************************/
952  /*                         DBFIsAttributeNULL()                         */  /*                         DBFIsAttributeNULL()                         */
953  /*                                                                      */  /*                                                                      */
954  /*      Return TRUE if value for field is NULL.                         */  /*      Return TRUE if value for field is NULL.                         */
# Line 818  DBFIsAttributeNULL( DBFHandle psDBF, int Line 964  DBFIsAttributeNULL( DBFHandle psDBF, int
964    
965      pszValue = DBFReadStringAttribute( psDBF, iRecord, iField );      pszValue = DBFReadStringAttribute( psDBF, iRecord, iField );
966    
967        if( pszValue == NULL )
968            return TRUE;
969    
970      switch(psDBF->pachFieldType[iField])      switch(psDBF->pachFieldType[iField])
971      {      {
972        case 'N':        case 'N':
# Line 837  DBFIsAttributeNULL( DBFHandle psDBF, int Line 986  DBFIsAttributeNULL( DBFHandle psDBF, int
986          /* empty string fields are considered NULL */          /* empty string fields are considered NULL */
987          return strlen(pszValue) == 0;          return strlen(pszValue) == 0;
988      }      }
     return FALSE;  
989  }  }
990    
991  /************************************************************************/  /************************************************************************/
# Line 896  DBFGetFieldInfo( DBFHandle psDBF, int iF Line 1044  DBFGetFieldInfo( DBFHandle psDBF, int iF
1044              pszFieldName[i] = '\0';              pszFieldName[i] = '\0';
1045      }      }
1046    
1047      if( psDBF->pachFieldType[iField] == 'N'      if ( psDBF->pachFieldType[iField] == 'L' )
1048          || psDBF->pachFieldType[iField] == 'F'          return( FTLogical);
1049          || psDBF->pachFieldType[iField] == 'D' )  
1050        else if( psDBF->pachFieldType[iField] == 'N'
1051                 || psDBF->pachFieldType[iField] == 'F'
1052                 || psDBF->pachFieldType[iField] == 'D' )
1053      {      {
1054          if( psDBF->panFieldDecimals[iField] > 0 )          if( psDBF->panFieldDecimals[iField] > 0 )
1055              return( FTDouble );              return( FTDouble );
# Line 921  static int DBFWriteAttribute(DBFHandle p Line 1072  static int DBFWriteAttribute(DBFHandle p
1072                               void * pValue )                               void * pValue )
1073    
1074  {  {
1075      int         nRecordOffset, i, j;      int         nRecordOffset, i, j, nRetResult = TRUE;
1076      unsigned char       *pabyRec;      unsigned char       *pabyRec;
1077      char        szSField[400], szFormat[20];      char        szSField[400], szFormat[20];
1078    
# Line 1024  static int DBFWriteAttribute(DBFHandle p Line 1175  static int DBFWriteAttribute(DBFHandle p
1175              sprintf( szFormat, "%%%dd", nWidth );              sprintf( szFormat, "%%%dd", nWidth );
1176              sprintf(szSField, szFormat, (int) *((double *) pValue) );              sprintf(szSField, szFormat, (int) *((double *) pValue) );
1177              if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )              if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )
1178                {
1179                  szSField[psDBF->panFieldSize[iField]] = '\0';                  szSField[psDBF->panFieldSize[iField]] = '\0';
1180                    nRetResult = FALSE;
1181                }
1182    
1183              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1184                      szSField, strlen(szSField) );                      szSField, strlen(szSField) );
# Line 1040  static int DBFWriteAttribute(DBFHandle p Line 1194  static int DBFWriteAttribute(DBFHandle p
1194                       nWidth, psDBF->panFieldDecimals[iField] );                       nWidth, psDBF->panFieldDecimals[iField] );
1195              sprintf(szSField, szFormat, *((double *) pValue) );              sprintf(szSField, szFormat, *((double *) pValue) );
1196              if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )              if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )
1197                {
1198                  szSField[psDBF->panFieldSize[iField]] = '\0';                  szSField[psDBF->panFieldSize[iField]] = '\0';
1199                    nRetResult = FALSE;
1200                }
1201              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),              strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1202                      szSField, strlen(szSField) );                      szSField, strlen(szSField) );
1203          }          }
1204          break;          break;
1205    
1206          case 'L':
1207            if (psDBF->panFieldSize[iField] >= 1  &&
1208                (*(char*)pValue == 'F' || *(char*)pValue == 'T'))
1209                *(pabyRec+psDBF->panFieldOffset[iField]) = *(char*)pValue;
1210            break;
1211    
1212        default:        default:
1213          if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )          if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1214            {
1215              j = psDBF->panFieldSize[iField];              j = psDBF->panFieldSize[iField];
1216                nRetResult = FALSE;
1217            }
1218          else          else
1219          {          {
1220              memset( pabyRec+psDBF->panFieldOffset[iField], ' ',              memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
# Line 1061  static int DBFWriteAttribute(DBFHandle p Line 1227  static int DBFWriteAttribute(DBFHandle p
1227          break;          break;
1228      }      }
1229    
1230        return( nRetResult );
1231    }
1232    
1233    /************************************************************************/
1234    /*                     DBFWriteAttributeDirectly()                      */
1235    /*                                                                      */
1236    /*      Write an attribute record to the file, but without any          */
1237    /*      reformatting based on type.  The provided buffer is written     */
1238    /*      as is to the field position in the record.                      */
1239    /************************************************************************/
1240    
1241    int SHPAPI_CALL
1242    DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
1243                                  void * pValue )
1244    
1245    {
1246        int         nRecordOffset, i, j;
1247        unsigned char       *pabyRec;
1248    
1249    /* -------------------------------------------------------------------- */
1250    /*      Is this a valid record?                                         */
1251    /* -------------------------------------------------------------------- */
1252        if( hEntity < 0 || hEntity > psDBF->nRecords )
1253            return( FALSE );
1254    
1255        if( psDBF->bNoHeader )
1256            DBFWriteHeader(psDBF);
1257    
1258    /* -------------------------------------------------------------------- */
1259    /*      Is this a brand new record?                                     */
1260    /* -------------------------------------------------------------------- */
1261        if( hEntity == psDBF->nRecords )
1262        {
1263            DBFFlushRecord( psDBF );
1264    
1265            psDBF->nRecords++;
1266            for( i = 0; i < psDBF->nRecordLength; i++ )
1267                psDBF->pszCurrentRecord[i] = ' ';
1268    
1269            psDBF->nCurrentRecord = hEntity;
1270        }
1271    
1272    /* -------------------------------------------------------------------- */
1273    /*      Is this an existing record, but different than the last one     */
1274    /*      we accessed?                                                    */
1275    /* -------------------------------------------------------------------- */
1276        if( psDBF->nCurrentRecord != hEntity )
1277        {
1278            DBFFlushRecord( psDBF );
1279    
1280            nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;
1281    
1282            fseek( psDBF->fp, nRecordOffset, 0 );
1283            fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );
1284    
1285            psDBF->nCurrentRecord = hEntity;
1286        }
1287    
1288        pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
1289    
1290    /* -------------------------------------------------------------------- */
1291    /*      Assign all the record fields.                                   */
1292    /* -------------------------------------------------------------------- */
1293        if( (int)strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1294            j = psDBF->panFieldSize[iField];
1295        else
1296        {
1297            memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
1298                    psDBF->panFieldSize[iField] );
1299            j = strlen((char *) pValue);
1300        }
1301    
1302        strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1303                (char *) pValue, j );
1304    
1305        psDBF->bCurrentRecordModified = TRUE;
1306        psDBF->bUpdated = TRUE;
1307    
1308      return( TRUE );      return( TRUE );
1309  }  }
1310    
# Line 1122  DBFWriteNULLAttribute( DBFHandle psDBF, Line 1366  DBFWriteNULLAttribute( DBFHandle psDBF,
1366  }  }
1367    
1368  /************************************************************************/  /************************************************************************/
1369    /*                      DBFWriteLogicalAttribute()                      */
1370    /*                                                                      */
1371    /*      Write a logical attribute.                                      */
1372    /************************************************************************/
1373    
1374    int SHPAPI_CALL
1375    DBFWriteLogicalAttribute( DBFHandle psDBF, int iRecord, int iField,
1376                           const char lValue)
1377    
1378    {
1379        return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) (&lValue) ) );
1380    }
1381    
1382    /************************************************************************/
1383  /*                         DBFWriteTuple()                              */  /*                         DBFWriteTuple()                              */
1384  /*                                                                      */  /*                                                                      */
1385  /*      Write an attribute record to the file.                          */  /*      Write an attribute record to the file.                          */
# Line 1243  DBFCloneEmpty(DBFHandle psDBF, const cha Line 1501  DBFCloneEmpty(DBFHandle psDBF, const cha
1501     newDBF = DBFCreate ( pszFilename );     newDBF = DBFCreate ( pszFilename );
1502     if ( newDBF == NULL ) return ( NULL );     if ( newDBF == NULL ) return ( NULL );
1503        
1504     newDBF->pszHeader = (void *) malloc ( 32 * psDBF->nFields );     newDBF->pszHeader = (char *) malloc ( 32 * psDBF->nFields );
1505     memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields );     memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields );
1506        
1507     newDBF->nFields = psDBF->nFields;     newDBF->nFields = psDBF->nFields;
1508     newDBF->nRecordLength = psDBF->nRecordLength;     newDBF->nRecordLength = psDBF->nRecordLength;
1509     newDBF->nHeaderLength = psDBF->nHeaderLength;     newDBF->nHeaderLength = 32 * (psDBF->nFields+1);
1510            
1511     newDBF->panFieldOffset = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->panFieldOffset = (int *) malloc ( sizeof(int) * psDBF->nFields );
1512     memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
1513     newDBF->panFieldSize = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->panFieldSize = (int *) malloc ( sizeof(int) * psDBF->nFields );
1514     memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
1515     newDBF->panFieldDecimals = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->panFieldDecimals = (int *) malloc ( sizeof(int) * psDBF->nFields );
1516     memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
1517     newDBF->pachFieldType = (void *) malloc ( sizeof(int) * psDBF->nFields );     newDBF->pachFieldType = (char *) malloc ( sizeof(int) * psDBF->nFields );
1518     memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(int) * psDBF->nFields );     memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(int) * psDBF->nFields );
1519    
1520     newDBF->bNoHeader = TRUE;     newDBF->bNoHeader = TRUE;
# Line 1304  static void str_to_upper (char *string) Line 1562  static void str_to_upper (char *string)
1562    
1563      while (++i < len)      while (++i < len)
1564          if (isalpha(string[i]) && islower(string[i]))          if (isalpha(string[i]) && islower(string[i]))
1565              string[i] = toupper ((int)string[i]);              string[i] = (char) toupper ((int)string[i]);
1566  }  }
1567    
1568  /************************************************************************/  /************************************************************************/
# Line 1323  DBFGetFieldIndex(DBFHandle psDBF, const Line 1581  DBFGetFieldIndex(DBFHandle psDBF, const
1581      int           i;      int           i;
1582    
1583      strncpy(name1, pszFieldName,11);      strncpy(name1, pszFieldName,11);
1584        name1[11] = '\0';
1585      str_to_upper(name1);      str_to_upper(name1);
1586    
1587      for( i = 0; i < DBFGetFieldCount(psDBF); i++ )      for( i = 0; i < DBFGetFieldCount(psDBF); i++ )
# Line 1336  DBFGetFieldIndex(DBFHandle psDBF, const Line 1595  DBFGetFieldIndex(DBFHandle psDBF, const
1595      }      }
1596      return(-1);      return(-1);
1597  }  }
   
 /************************************************************************/  
 /*                          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.2747

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26