/[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 2748 by bramz, Tue Mar 20 23:18:34 2007 UTC revision 2751 by bramz, Wed Mar 28 23:30:15 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 417  DBFHandle SHPAPI_CALL Line 421  DBFHandle SHPAPI_CALL
421  DBFOpen( const char * pszFilename, const char * pszAccess )  DBFOpen( const char * pszFilename, const char * pszAccess )
422    
423  {  {
424      DBFHandle           psDBF;          FILE*           fp;
425      unsigned char               *pabyBuf;      int                 i;
     int                 nFields, nHeadLen, nRecLen, iField, i;  
426      char                *pszBasename, *pszFullname;      char                *pszBasename, *pszFullname;
427    
428  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 453  DBFOpen( const char * pszFilename, const Line 456  DBFOpen( const char * pszFilename, const
456      pszFullname = (char *) malloc(strlen(pszBasename) + 5);      pszFullname = (char *) malloc(strlen(pszBasename) + 5);
457      sprintf( pszFullname, "%s.dbf", pszBasename );      sprintf( pszFullname, "%s.dbf", pszBasename );
458                    
459      psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );      fp = fopen( pszFullname, pszAccess );
     psDBF->fp = fopen( pszFullname, pszAccess );  
460    
461      if( psDBF->fp == NULL )      if( fp == NULL )
462      {      {
463          sprintf( pszFullname, "%s.DBF", pszBasename );          sprintf( pszFullname, "%s.DBF", pszBasename );
464          psDBF->fp = fopen(pszFullname, pszAccess );          fp = fopen(pszFullname, pszAccess );
465      }      }
466            
467      free( pszBasename );      free( pszBasename );
468      free( pszFullname );      free( pszFullname );
469        
470      if( psDBF->fp == NULL )          return DBFOpenEx( fp );
471      {  }
472          free( psDBF );  
473    
474    
475    /************************************************************************/
476    /*                              DBFOpenW()                              */
477    /*                                                                      */
478    /*      Open a .dbf file with a wide character filename                 */
479    /************************************************************************/
480    
481    #ifdef SHPAPI_HAS_WIDE
482    
483    DBFHandle SHPAPI_CALL
484    DBFOpenW( const wchar_t * pszFilename, const wchar_t * pszAccess )
485    
486    {
487        FILE*               fp;
488        int                 i;
489        wchar_t             *pszBasename, *pszFullname;
490    
491    /* -------------------------------------------------------------------- */
492    /*      We only allow the access strings "rb" and "r+".                  */
493    /* -------------------------------------------------------------------- */
494        if( wcscmp(pszAccess,L"r") != 0 && wcscmp(pszAccess,L"r+") != 0
495            && wcscmp(pszAccess,L"rb") != 0 && wcscmp(pszAccess,L"rb+") != 0
496            && wcscmp(pszAccess,L"r+b") != 0 )
497          return( NULL );          return( NULL );
498    
499        if( wcscmp(pszAccess,L"r") == 0 )
500            pszAccess = L"rb";
501    
502        if( wcscmp(pszAccess,L"r+") == 0 )
503            pszAccess = L"rb+";
504    
505    /* -------------------------------------------------------------------- */
506    /*      Compute the base (layer) name.  If there is any extension       */
507    /*      on the passed in filename we will strip it off.                 */
508    /* -------------------------------------------------------------------- */
509        pszBasename = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszFilename)+5));
510        wcscpy( pszBasename, pszFilename );
511        for( i = wcslen(pszBasename)-1;
512             i > 0 && pszBasename[i] != L'.' && pszBasename[i] != L'/'
513                   && pszBasename[i] != L'\\';
514             i-- ) {}
515    
516        if( pszBasename[i] == L'.' )
517            pszBasename[i] = L'\0';
518    
519        pszFullname = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszBasename) + 5));
520        swprintf( pszFullname, L"%s.dbf", pszBasename );
521            
522        fp = _wfopen( pszFullname, pszAccess );
523    
524        if( fp == NULL )
525        {
526            swprintf( pszFullname, L"%s.DBF", pszBasename );
527            fp = _wfopen(pszFullname, pszAccess );
528      }      }
529        
530        free( pszBasename );
531        free( pszFullname );
532    
533            return DBFOpenEx( fp );
534    }
535    
536    #endif
537    
538    
539    
540    /************************************************************************/
541    /*                              DBFOpenEx()                             */
542    /*                                                                      */
543    /*      Open a .dbf file from a freshly opened FILE                     */
544    /************************************************************************/
545      
546    DBFHandle SHPAPI_CALL
547    DBFOpenEx( FILE* fp )
548    
549    {
550        unsigned char       *pabyBuf;
551        int                 nFields, nHeadLen, nRecLen, iField;
552            DBFHandle       psDBF = NULL;
553    
554            if( fp == NULL )
555            {
556                    return( NULL );
557            }
558    
559        psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );
560            psDBF->fp = fp;
561    
562      psDBF->bNoHeader = FALSE;      psDBF->bNoHeader = FALSE;
563      psDBF->nCurrentRecord = -1;      psDBF->nCurrentRecord = -1;
# Line 607  DBFHandle SHPAPI_CALL Line 695  DBFHandle SHPAPI_CALL
695  DBFCreate( const char * pszFilename )  DBFCreate( const char * pszFilename )
696    
697  {  {
     DBFHandle   psDBF;  
698      FILE        *fp;      FILE        *fp;
699      char        *pszFullname, *pszBasename;      char        *pszFullname, *pszBasename;
700      int         i;      int         i;
# Line 646  DBFCreate( const char * pszFilename ) Line 733  DBFCreate( const char * pszFilename )
733    
734      free( pszFullname );      free( pszFullname );
735    
736            return DBFCreateEx( fp );
737    }
738    
739    
740    
741    /************************************************************************/
742    /*                             DBFCreateW()                             */
743    /*                                                                      */
744    /*      Create a new .dbf file with a wide character filename           */
745    /************************************************************************/
746    
747    #ifdef SHPAPI_HAS_WIDE
748    
749    DBFHandle SHPAPI_CALL
750    DBFCreateW( const wchar_t * pszFilename )
751    
752    {
753        FILE        *fp;
754        wchar_t     *pszFullname, *pszBasename;
755        int         i;
756    
757    /* -------------------------------------------------------------------- */
758    /*      Compute the base (layer) name.  If there is any extension       */
759    /*      on the passed in filename we will strip it off.                 */
760    /* -------------------------------------------------------------------- */
761        pszBasename = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszFilename)+5));
762        wcscpy( pszBasename, pszFilename );
763        for( i = wcslen(pszBasename)-1;
764             i > 0 && pszBasename[i] != L'.' && pszBasename[i] != L'/'
765                   && pszBasename[i] != L'\\';
766             i-- ) {}
767    
768        if( pszBasename[i] == L'.' )
769            pszBasename[i] = L'\0';
770    
771        pszFullname = (wchar_t *) malloc(sizeof(wchar_t)*(wcslen(pszBasename) + 5));
772        swprintf( pszFullname, L"%s.dbf", pszBasename );
773        free( pszBasename );
774    
775    /* -------------------------------------------------------------------- */
776    /*      Create the file.                                                */
777    /* -------------------------------------------------------------------- */
778        fp = _wfopen( pszFullname, L"wb" );
779        if( fp == NULL )
780            return( NULL );
781    
782        fputc( 0, fp );
783        fclose( fp );
784    
785        fp = _wfopen( pszFullname, L"rb+" );
786        if( fp == NULL )
787            return( NULL );
788    
789        free( pszFullname );
790    
791            return DBFCreateEx( fp );
792    }
793    
794    #endif
795    
796    
797    /************************************************************************/
798    /*                             DBFCreateEx()                            */
799    /*                                                                      */
800    /*      Create a new .dbf file from a freshly created file              */
801    /************************************************************************/
802    
803    DBFHandle SHPAPI_CALL
804    DBFCreateEx( FILE* fp )
805    
806    {
807        DBFHandle   psDBF;
808    
809  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
810  /*      Create the info structure.                                      */  /*      Create the info structure.                                      */
811  /* -------------------------------------------------------------------- */  /* -------------------------------------------------------------------- */
# Line 672  DBFCreate( const char * pszFilename ) Line 832  DBFCreate( const char * pszFilename )
832      return( psDBF );      return( psDBF );
833  }  }
834    
835    
836    
837  /************************************************************************/  /************************************************************************/
838  /*                            DBFAddField()                             */  /*                            DBFAddField()                             */
839  /*                                                                      */  /*                                                                      */
# Line 1499  DBFCloneEmpty(DBFHandle psDBF, const cha Line 1661  DBFCloneEmpty(DBFHandle psDBF, const cha
1661      DBFHandle   newDBF;      DBFHandle   newDBF;
1662    
1663     newDBF = DBFCreate ( pszFilename );     newDBF = DBFCreate ( pszFilename );
1664       if ( newDBF == NULL ) return ( NULL );
1665    
1666       DBFCloneEmptyEx( psDBF, newDBF );
1667    
1668       DBFClose( newDBF );      
1669       newDBF = DBFOpen ( pszFilename, "rb+" );
1670    
1671       return ( newDBF );
1672    }
1673    
1674    
1675    
1676    
1677    /************************************************************************/
1678    /*                          DBFCloneEmptyW                              */
1679    /*                                                                      */
1680    /*      Read one of the attribute fields of a record.                   */
1681    /************************************************************************/
1682    
1683    #ifdef SHPAPI_HAS_WIDE
1684    
1685    DBFHandle SHPAPI_CALL
1686    DBFCloneEmptyW(DBFHandle psDBF, const wchar_t * pszFilename )
1687    {
1688        DBFHandle   newDBF;
1689    
1690       newDBF = DBFCreateW ( pszFilename );
1691       if ( newDBF == NULL ) return ( NULL );
1692    
1693       DBFCloneEmptyEx( psDBF, newDBF );
1694    
1695       DBFClose( newDBF );      
1696       newDBF = DBFOpenW ( pszFilename, L"rb+" );
1697    
1698       return ( newDBF );
1699    }
1700    
1701    #endif
1702    
1703    /************************************************************************/
1704    /*                          DBFCloneEmptyEx()                           */
1705    /*                                                                      */
1706    /*      Read one of the attribute fields of a record.                   */
1707    /************************************************************************/
1708    
1709    void SHPAPI_CALL
1710    DBFCloneEmptyEx(DBFHandle psDBF, DBFHandle newDBF)
1711    {
1712     if ( newDBF == NULL ) return ( NULL );     if ( newDBF == NULL ) return ( NULL );
1713        
1714     newDBF->pszHeader = (char *) malloc ( 32 * psDBF->nFields );     newDBF->pszHeader = (char *) malloc ( 32 * psDBF->nFields );
# Line 1521  DBFCloneEmpty(DBFHandle psDBF, const cha Line 1731  DBFCloneEmpty(DBFHandle psDBF, const cha
1731     newDBF->bUpdated = TRUE;     newDBF->bUpdated = TRUE;
1732        
1733     DBFWriteHeader ( newDBF );     DBFWriteHeader ( newDBF );
    DBFClose ( newDBF );  
     
    newDBF = DBFOpen ( pszFilename, "rb+" );  
   
    return ( newDBF );  
1734  }  }
1735    
1736  /************************************************************************/  /************************************************************************/

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26