dbfopen.c

来自「支持各种栅格图像和矢量图像读取的库」· C语言 代码 · 共 1,540 行 · 第 1/4 页

C
1,540
字号
/* -------------------------------------------------------------------- *//*      Is this a brand new record?                                     *//* -------------------------------------------------------------------- */    if( hEntity == psDBF->nRecords )    {	if( !DBFFlushRecord( psDBF ) )            return FALSE;	psDBF->nRecords++;	for( i = 0; i < psDBF->nRecordLength; i++ )	    psDBF->pszCurrentRecord[i] = ' ';	psDBF->nCurrentRecord = hEntity;    }/* -------------------------------------------------------------------- *//*      Is this an existing record, but different than the last one     *//*      we accessed?                                                    *//* -------------------------------------------------------------------- */    if( !DBFLoadRecord( psDBF, hEntity ) )        return FALSE;    pabyRec = (unsigned char *) psDBF->pszCurrentRecord;/* -------------------------------------------------------------------- *//*      Assign all the record fields.                                   *//* -------------------------------------------------------------------- */    if( (int)strlen((char *) pValue) > psDBF->panFieldSize[iField] )        j = psDBF->panFieldSize[iField];    else    {        memset( pabyRec+psDBF->panFieldOffset[iField], ' ',                psDBF->panFieldSize[iField] );        j = strlen((char *) pValue);    }    strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),            (char *) pValue, j );    psDBF->bCurrentRecordModified = TRUE;    psDBF->bUpdated = TRUE;    return( TRUE );}/************************************************************************//*                      DBFWriteDoubleAttribute()                       *//*                                                                      *//*      Write a double attribute.                                       *//************************************************************************/int SHPAPI_CALLDBFWriteDoubleAttribute( DBFHandle psDBF, int iRecord, int iField,                         double dValue ){    return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );}/************************************************************************//*                      DBFWriteIntegerAttribute()                      *//*                                                                      *//*      Write a integer attribute.                                      *//************************************************************************/int SHPAPI_CALLDBFWriteIntegerAttribute( DBFHandle psDBF, int iRecord, int iField,                          int nValue ){    double	dValue = nValue;    return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );}/************************************************************************//*                      DBFWriteStringAttribute()                       *//*                                                                      *//*      Write a string attribute.                                       *//************************************************************************/int SHPAPI_CALLDBFWriteStringAttribute( DBFHandle psDBF, int iRecord, int iField,                         const char * pszValue ){    return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) pszValue ) );}/************************************************************************//*                      DBFWriteNULLAttribute()                         *//*                                                                      *//*      Write a string attribute.                                       *//************************************************************************/int SHPAPI_CALLDBFWriteNULLAttribute( DBFHandle psDBF, int iRecord, int iField ){    return( DBFWriteAttribute( psDBF, iRecord, iField, NULL ) );}/************************************************************************//*                      DBFWriteLogicalAttribute()                      *//*                                                                      *//*      Write a logical attribute.                                      *//************************************************************************/int SHPAPI_CALLDBFWriteLogicalAttribute( DBFHandle psDBF, int iRecord, int iField,		       const char lValue){    return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) (&lValue) ) );}/************************************************************************//*                         DBFWriteTuple()                              *//*									*//*	Write an attribute record to the file.				*//************************************************************************/int SHPAPI_CALLDBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple ){    int	       		i;    unsigned char	*pabyRec;/* -------------------------------------------------------------------- *//*	Is this a valid record?						*//* -------------------------------------------------------------------- */    if( hEntity < 0 || hEntity > psDBF->nRecords )        return( FALSE );    if( psDBF->bNoHeader )        DBFWriteHeader(psDBF);/* -------------------------------------------------------------------- *//*      Is this a brand new record?                                     *//* -------------------------------------------------------------------- */    if( hEntity == psDBF->nRecords )    {	if( !DBFFlushRecord( psDBF ) )            return FALSE;	psDBF->nRecords++;	for( i = 0; i < psDBF->nRecordLength; i++ )	    psDBF->pszCurrentRecord[i] = ' ';	psDBF->nCurrentRecord = hEntity;    }/* -------------------------------------------------------------------- *//*      Is this an existing record, but different than the last one     *//*      we accessed?                                                    *//* -------------------------------------------------------------------- */    if( !DBFLoadRecord( psDBF, hEntity ) )        return FALSE;    pabyRec = (unsigned char *) psDBF->pszCurrentRecord;    memcpy ( pabyRec, pRawTuple,  psDBF->nRecordLength );    psDBF->bCurrentRecordModified = TRUE;    psDBF->bUpdated = TRUE;    return( TRUE );}/************************************************************************//*                          DBFReadTuple()                              *//*                                                                      *//*      Read one of the attribute fields of a record.                   *//************************************************************************/const char SHPAPI_CALL1(*)DBFReadTuple(DBFHandle psDBF, int hEntity ){    unsigned char	*pabyRec;    static char	*pReturnTuple = NULL;    static int	nTupleLen = 0;/* -------------------------------------------------------------------- *//*	Have we read the record?					*//* -------------------------------------------------------------------- */    if( hEntity < 0 || hEntity >= psDBF->nRecords )        return( NULL );    if( !DBFLoadRecord( psDBF, hEntity ) )        return NULL;    pabyRec = (unsigned char *) psDBF->pszCurrentRecord;    if ( nTupleLen < psDBF->nRecordLength) {        nTupleLen = psDBF->nRecordLength;        pReturnTuple = (char *) SfRealloc(pReturnTuple, psDBF->nRecordLength);    }        memcpy ( pReturnTuple, pabyRec, psDBF->nRecordLength );            return( pReturnTuple );}/************************************************************************//*                          DBFCloneEmpty()                              *//*                                                                      *//*      Read one of the attribute fields of a record.                   *//************************************************************************/DBFHandle SHPAPI_CALLDBFCloneEmpty(DBFHandle psDBF, const char * pszFilename ) {    DBFHandle	newDBF;   newDBF = DBFCreate ( pszFilename );   if ( newDBF == NULL ) return ( NULL );       newDBF->nFields = psDBF->nFields;   newDBF->nRecordLength = psDBF->nRecordLength;   newDBF->nHeaderLength = psDBF->nHeaderLength;       newDBF->pszHeader = (char *) malloc ( newDBF->nHeaderLength );   memcpy ( newDBF->pszHeader, psDBF->pszHeader, newDBF->nHeaderLength );      newDBF->panFieldOffset = (int *) malloc ( sizeof(int) * psDBF->nFields );    memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );   newDBF->panFieldSize = (int *) malloc ( sizeof(int) * psDBF->nFields );   memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );   newDBF->panFieldDecimals = (int *) malloc ( sizeof(int) * psDBF->nFields );   memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );   newDBF->pachFieldType = (char *) malloc ( sizeof(char) * psDBF->nFields );   memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(char)*psDBF->nFields );   newDBF->bNoHeader = TRUE;   newDBF->bUpdated = TRUE;      DBFWriteHeader ( newDBF );   DBFClose ( newDBF );      newDBF = DBFOpen ( pszFilename, "rb+" );   return ( newDBF );}/************************************************************************//*                       DBFGetNativeFieldType()                        *//*                                                                      *//*      Return the DBase field type for the specified field.            *//*                                                                      *//*      Value can be one of: 'C' (String), 'D' (Date), 'F' (Float),     *//*                           'N' (Numeric, with or without decimal),    *//*                           'L' (Logical),                             *//*                           'M' (Memo: 10 digits .DBT block ptr)       *//************************************************************************/char SHPAPI_CALLDBFGetNativeFieldType( DBFHandle psDBF, int iField ){    if( iField >=0 && iField < psDBF->nFields )        return psDBF->pachFieldType[iField];    return  ' ';}/************************************************************************//*                            str_to_upper()                            *//************************************************************************/static void str_to_upper (char *string){    int len;    short i = -1;    len = strlen (string);    while (++i < len)        if (isalpha(string[i]) && islower(string[i]))            string[i] = (char) toupper ((int)string[i]);}/************************************************************************//*                          DBFGetFieldIndex()                          *//*                                                                      *//*      Get the index number for a field in a .dbf file.                *//*                                                                      *//*      Contributed by Jim Matthews.                                    *//************************************************************************/int SHPAPI_CALLDBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName){    char          name[12], name1[12], name2[12];    int           i;    strncpy(name1, pszFieldName,11);    name1[11] = '\0';    str_to_upper(name1);    for( i = 0; i < DBFGetFieldCount(psDBF); i++ )    {        DBFGetFieldInfo( psDBF, i, name, NULL, NULL );        strncpy(name2,name,11);        str_to_upper(name2);        if(!strncmp(name1,name2,10))            return(i);    }    return(-1);}/************************************************************************//*                         DBFIsRecordDeleted()                         *//*                                                                      *//*      Returns TRUE if the indicated record is deleted, otherwise      *//*      it returns FALSE.                                               *//************************************************************************/int SHPAPI_CALL DBFIsRecordDeleted( DBFHandle psDBF, int iShape ){/* -------------------------------------------------------------------- *//*      Verify selection.                                               *//* -------------------------------------------------------------------- */    if( iShape < 0 || iShape >= psDBF->nRecords )        return TRUE;/* -------------------------------------------------------------------- *//*	Have we read the record?					*//* -------------------------------------------------------------------- */    if( !DBFLoadRecord( psDBF, iShape ) )        return FALSE;/* -------------------------------------------------------------------- *//*      '*' means deleted.                                              *//* -------------------------------------------------------------------- */    return psDBF->pszCurrentRecord[0] == '*';}/************************************************************************//*                        DBFMarkRecordDeleted()                        *//************************************************************************/int SHPAPI_CALL DBFMarkRecordDeleted( DBFHandle psDBF, int iShape,                                       int bIsDeleted ){    char chNewFlag;/* -------------------------------------------------------------------- *//*      Verify selection.                                               *//* -------------------------------------------------------------------- */    if( iShape < 0 || iShape >= psDBF->nRecords )        return FALSE;/* -------------------------------------------------------------------- *//*      Is this an existing record, but different than the last one     *//*      we accessed?                                                    *//* -------------------------------------------------------------------- */    if( !DBFLoadRecord( psDBF, iShape ) )        return FALSE;/* -------------------------------------------------------------------- *//*      Assign value, marking record as dirty if it changes.            *//* -------------------------------------------------------------------- */    if( bIsDeleted )        chNewFlag = '*';    else         chNewFlag = ' ';    if( psDBF->pszCurrentRecord[0] != chNewFlag )    {        psDBF->bCurrentRecordModified = TRUE;        psDBF->bUpdated = TRUE;        psDBF->pszCurrentRecord[0] = chNewFlag;    }    return TRUE;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?