/[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

revision 2747 by bramz, Tue Mar 20 23:12:45 2007 UTC revision 2752 by bramz, Tue Apr 10 23:45:00 2007 UTC
# Line 251  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    
# Line 335  static void DBFWriteHeader(DBFHandle psD Line 339  static void DBFWriteHeader(DBFHandle psD
339      abyHeader[10] = (unsigned char) (psDBF->nRecordLength % 256);      abyHeader[10] = (unsigned char) (psDBF->nRecordLength % 256);
340      abyHeader[11] = (unsigned char) (psDBF->nRecordLength / 256);      abyHeader[11] = (unsigned char) (psDBF->nRecordLength / 256);
341    
342            abyHeader[29] = (unsigned char) (psDBF->nLanguageDriver);
343    
344  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
345  /*      Write the initial 32 byte file header, and all the field        */  /*      Write the initial 32 byte file header, and all the field        */
346  /*      descriptions.                                                   */  /*      descriptions.                                                   */
# Line 417  DBFHandle SHPAPI_CALL Line 423  DBFHandle SHPAPI_CALL
423  DBFOpen( const char * pszFilename, const char * pszAccess )  DBFOpen( const char * pszFilename, const char * pszAccess )
424    
425  {  {
426      DBFHandle           psDBF;          FILE*           fp;
427      unsigned char               *pabyBuf;      int                 i;
     int                 nFields, nHeadLen, nRecLen, iField, i;  
428      char                *pszBasename, *pszFullname;      char                *pszBasename, *pszFullname;
429    
430  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 453  DBFOpen( const char * pszFilename, const Line 458  DBFOpen( const char * pszFilename, const
458      pszFullname = (char *) malloc(strlen(pszBasename) + 5);      pszFullname = (char *) malloc(strlen(pszBasename) + 5);
459      sprintf( pszFullname, "%s.dbf", pszBasename );      sprintf( pszFullname, "%s.dbf", pszBasename );
460                    
461      psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );      fp = fopen( pszFullname, pszAccess );
     psDBF->fp = fopen( pszFullname, pszAccess );  
462    
463      if( psDBF->fp == NULL )      if( fp == NULL )
464      {      {
465          sprintf( pszFullname, "%s.DBF", pszBasename );          sprintf( pszFullname, "%s.DBF", pszBasename );
466          psDBF->fp = fopen(pszFullname, pszAccess );          fp = fopen(pszFullname, pszAccess );
467      }      }
468            
469      free( pszBasename );      free( pszBasename );
470      free( pszFullname );      free( pszFullname );
471        
472      if( psDBF->fp == NULL )          return DBFOpenEx( fp );
473      {  }
474          free( psDBF );  
475    
476    
477    /************************************************************************/
478    /*                              DBFOpenW()                              */
479    /*                                                                      */
480    /*      Open a .dbf file with a wide character filename                 */
481    /************************************************************************/
482    
483    #ifdef SHPAPI_HAS_WIDE
484    
485    DBFHandle SHPAPI_CALL
486    DBFOpenW( const wchar_t * pszFilename, const wchar_t * pszAccess )
487    
488    {
489        FILE*               fp;
490        int                 i;
491        wchar_t             *pszBasename, *pszFullname;
492    
493    /* -------------------------------------------------------------------- */
494    /*      We only allow the access strings "rb" and "r+".                  */
495    /* -------------------------------------------------------------------- */
496        if( wcscmp(pszAccess,L"r") != 0 && wcscmp(pszAccess,L"r+") != 0
497            && wcscmp(pszAccess,L"rb") != 0 && wcscmp(pszAccess,L"rb+") != 0
498            && wcscmp(pszAccess,L"r+b") != 0 )
499          return( NULL );          return( NULL );
500    
501        if( wcscmp(pszAccess,L"r") == 0 )
502            pszAccess = L"rb";
503    
504        if( wcscmp(pszAccess,L"r+") == 0 )
505            pszAccess = L"rb+";
506    
507    /* -------------------------------------------------------------------- */
508    /*      Compute the base (layer) name.  If there is any extension       */
509    /*      on the passed in filename we will strip it off.                 */
510    /* -------------------------------------------------------------------- */
511        pszBasename = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszFilename)+5));
512        wcscpy( pszBasename, pszFilename );
513        for( i = wcslen(pszBasename)-1;
514             i > 0 && pszBasename[i] != L'.' && pszBasename[i] != L'/'
515                   && pszBasename[i] != L'\\';
516             i-- ) {}
517    
518        if( pszBasename[i] == L'.' )
519            pszBasename[i] = L'\0';
520    
521        pszFullname = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszBasename) + 5));
522        swprintf( pszFullname, L"%s.dbf", pszBasename );
523            
524        fp = _wfopen( pszFullname, pszAccess );
525    
526        if( fp == NULL )
527        {
528            swprintf( pszFullname, L"%s.DBF", pszBasename );
529            fp = _wfopen(pszFullname, pszAccess );
530      }      }
531        
532        free( pszBasename );
533        free( pszFullname );
534    
535            return DBFOpenEx( fp );
536    }
537    
538    #endif
539    
540    
541    
542    /************************************************************************/
543    /*                              DBFOpenEx()                             */
544    /*                                                                      */
545    /*      Open a .dbf file from a freshly opened FILE                     */
546    /************************************************************************/
547      
548    DBFHandle SHPAPI_CALL
549    DBFOpenEx( FILE* fp )
550    
551    {
552        unsigned char       *pabyBuf;
553        int                 nFields, nHeadLen, nRecLen, iField;
554            DBFHandle       psDBF = NULL;
555    
556            if( fp == NULL )
557            {
558                    return( NULL );
559            }
560    
561        psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );
562            psDBF->fp = fp;
563    
564      psDBF->bNoHeader = FALSE;      psDBF->bNoHeader = FALSE;
565      psDBF->nCurrentRecord = -1;      psDBF->nCurrentRecord = -1;
# Line 492  DBFOpen( const char * pszFilename, const Line 582  DBFOpen( const char * pszFilename, const
582    
583      psDBF->nHeaderLength = nHeadLen = pabyBuf[8] + pabyBuf[9]*256;      psDBF->nHeaderLength = nHeadLen = pabyBuf[8] + pabyBuf[9]*256;
584      psDBF->nRecordLength = nRecLen = pabyBuf[10] + pabyBuf[11]*256;      psDBF->nRecordLength = nRecLen = pabyBuf[10] + pabyBuf[11]*256;
585            psDBF->nLanguageDriver = pabyBuf[29];
586            
587      psDBF->nFields = nFields = (nHeadLen - 32) / 32;      psDBF->nFields = nFields = (nHeadLen - 32) / 32;
588    
# Line 553  DBFOpen( const char * pszFilename, const Line 644  DBFOpen( const char * pszFilename, const
644  void SHPAPI_CALL  void SHPAPI_CALL
645  DBFClose(DBFHandle psDBF)  DBFClose(DBFHandle psDBF)
646  {  {
647          if( psSHP == NULL )          if( psDBF == NULL )
648                  return;                  return;
649                                    
650  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 607  DBFHandle SHPAPI_CALL Line 698  DBFHandle SHPAPI_CALL
698  DBFCreate( const char * pszFilename )  DBFCreate( const char * pszFilename )
699    
700  {  {
     DBFHandle   psDBF;  
701      FILE        *fp;      FILE        *fp;
702      char        *pszFullname, *pszBasename;      char        *pszFullname, *pszBasename;
703      int         i;      int         i;
# Line 646  DBFCreate( const char * pszFilename ) Line 736  DBFCreate( const char * pszFilename )
736    
737      free( pszFullname );      free( pszFullname );
738    
739            return DBFCreateEx( fp );
740    }
741    
742    
743    
744    /************************************************************************/
745    /*                             DBFCreateW()                             */
746    /*                                                                      */
747    /*      Create a new .dbf file with a wide character filename           */
748    /************************************************************************/
749    
750    #ifdef SHPAPI_HAS_WIDE
751    
752    DBFHandle SHPAPI_CALL
753    DBFCreateW( const wchar_t * pszFilename )
754    
755    {
756        FILE        *fp;
757        wchar_t     *pszFullname, *pszBasename;
758        int         i;
759    
760    /* -------------------------------------------------------------------- */
761    /*      Compute the base (layer) name.  If there is any extension       */
762    /*      on the passed in filename we will strip it off.                 */
763    /* -------------------------------------------------------------------- */
764        pszBasename = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszFilename)+5));
765        wcscpy( pszBasename, pszFilename );
766        for( i = wcslen(pszBasename)-1;
767             i > 0 && pszBasename[i] != L'.' && pszBasename[i] != L'/'
768                   && pszBasename[i] != L'\\';
769             i-- ) {}
770    
771        if( pszBasename[i] == L'.' )
772            pszBasename[i] = L'\0';
773    
774        pszFullname = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszBasename) + 5));
775        swprintf( pszFullname, L"%s.dbf", pszBasename );
776        free( pszBasename );
777    
778    /* -------------------------------------------------------------------- */
779    /*      Create the file.                                                */
780    /* -------------------------------------------------------------------- */
781        fp = _wfopen( pszFullname, L"wb" );
782        if( fp == NULL )
783            return( NULL );
784    
785        fputc( 0, fp );
786        fclose( fp );
787    
788        fp = _wfopen( pszFullname, L"rb+" );
789        if( fp == NULL )
790            return( NULL );
791    
792        free( pszFullname );
793    
794            return DBFCreateEx( fp );
795    }
796    
797    #endif
798    
799    
800    /************************************************************************/
801    /*                             DBFCreateEx()                            */
802    /*                                                                      */
803    /*      Create a new .dbf file from a freshly created file              */
804    /************************************************************************/
805    
806    DBFHandle SHPAPI_CALL
807    DBFCreateEx( FILE* fp )
808    
809    {
810        DBFHandle   psDBF;
811    
812  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
813  /*      Create the info structure.                                      */  /*      Create the info structure.                                      */
814  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 668  DBFCreate( const char * pszFilename ) Line 831  DBFCreate( const char * pszFilename )
831      psDBF->pszCurrentRecord = NULL;      psDBF->pszCurrentRecord = NULL;
832    
833      psDBF->bNoHeader = TRUE;      psDBF->bNoHeader = TRUE;
834            psDBF->nLanguageDriver = 0x03; // ANSI
835    
836      return( psDBF );      return( psDBF );
837  }  }
838    
839    
840    
841  /************************************************************************/  /************************************************************************/
842  /*                            DBFAddField()                             */  /*                            DBFAddField()                             */
843  /*                                                                      */  /*                                                                      */
# Line 1499  DBFCloneEmpty(DBFHandle psDBF, const cha Line 1665  DBFCloneEmpty(DBFHandle psDBF, const cha
1665      DBFHandle   newDBF;      DBFHandle   newDBF;
1666    
1667     newDBF = DBFCreate ( pszFilename );     newDBF = DBFCreate ( pszFilename );
1668     if ( newDBF == NULL ) return ( NULL );     if ( newDBF == NULL ) return ( NULL );
1669    
1670       DBFCloneEmptyEx( psDBF, newDBF );
1671    
1672       DBFClose( newDBF );      
1673       newDBF = DBFOpen ( pszFilename, "rb+" );
1674    
1675       return ( newDBF );
1676    }
1677    
1678    
1679    
1680    
1681    /************************************************************************/
1682    /*                          DBFCloneEmptyW                              */
1683    /*                                                                      */
1684    /*      Read one of the attribute fields of a record.                   */
1685    /************************************************************************/
1686    
1687    #ifdef SHPAPI_HAS_WIDE
1688    
1689    DBFHandle SHPAPI_CALL
1690    DBFCloneEmptyW(DBFHandle psDBF, const wchar_t * pszFilename )
1691    {
1692        DBFHandle   newDBF;
1693    
1694       newDBF = DBFCreateW ( pszFilename );
1695       if ( newDBF == NULL ) return ( NULL );
1696    
1697       DBFCloneEmptyEx( psDBF, newDBF );
1698    
1699       DBFClose( newDBF );      
1700       newDBF = DBFOpenW ( pszFilename, L"rb+" );
1701    
1702       return ( newDBF );
1703    }
1704    
1705    #endif
1706    
1707    /************************************************************************/
1708    /*                          DBFCloneEmptyEx()                           */
1709    /*                                                                      */
1710    /*      Read one of the attribute fields of a record.                   */
1711    /************************************************************************/
1712    
1713    void SHPAPI_CALL
1714    DBFCloneEmptyEx(DBFHandle psDBF, DBFHandle newDBF)
1715    {
1716       if ( newDBF == NULL ) return;
1717        
1718     newDBF->pszHeader = (char *) malloc ( 32 * psDBF->nFields );     newDBF->pszHeader = (char *) malloc ( 32 * psDBF->nFields );
1719     memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields );     memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields );
# Line 1521  DBFCloneEmpty(DBFHandle psDBF, const cha Line 1735  DBFCloneEmpty(DBFHandle psDBF, const cha
1735     newDBF->bUpdated = TRUE;     newDBF->bUpdated = TRUE;
1736        
1737     DBFWriteHeader ( newDBF );     DBFWriteHeader ( newDBF );
    DBFClose ( newDBF );  
     
    newDBF = DBFOpen ( pszFilename, "rb+" );  
   
    return ( newDBF );  
1738  }  }
1739    
1740  /************************************************************************/  /************************************************************************/

Legend:
Removed from v.2747  
changed lines
  Added in v.2752

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26