📄 dbfopen.c
字号:
/* -------------------------------------------------------------------- *//* Assign all the record fields. *//* -------------------------------------------------------------------- */ switch( psDBF->pachFieldType[iField] ) { case 'D': case 'N': case 'F': if( psDBF->panFieldDecimals[iField] == 0 ) { int nWidth = psDBF->panFieldSize[iField]; if( sizeof(szSField)-2 < nWidth ) nWidth = sizeof(szSField)-2; sprintf( szFormat, "%%%dd", nWidth ); sprintf(szSField, szFormat, (int) *((double *) pValue) ); if( (int)strlen(szSField) > psDBF->panFieldSize[iField] ) szSField[psDBF->panFieldSize[iField]] = '\0'; strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), szSField, strlen(szSField) ); } else { int nWidth = psDBF->panFieldSize[iField]; if( sizeof(szSField)-2 < nWidth ) nWidth = sizeof(szSField)-2; sprintf( szFormat, "%%%d.%df", nWidth, psDBF->panFieldDecimals[iField] ); sprintf(szSField, szFormat, *((double *) pValue) ); if( (int) strlen(szSField) > psDBF->panFieldSize[iField] ) szSField[psDBF->panFieldSize[iField]] = '\0'; strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), szSField, strlen(szSField) ); } break; default: 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 ); break; } 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 ) );}/************************************************************************//* DBFWriteTuple() *//* *//* Write an attribute record to the file. *//************************************************************************/int SHPAPI_CALLDBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple ){ int nRecordOffset, 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 ) { DBFFlushRecord( psDBF ); 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( psDBF->nCurrentRecord != hEntity ) { DBFFlushRecord( psDBF ); nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength; fseek( psDBF->fp, nRecordOffset, 0 ); fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp ); psDBF->nCurrentRecord = hEntity; } 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 ){ int nRecordOffset; 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( psDBF->nCurrentRecord != hEntity ) { DBFFlushRecord( psDBF ); nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength; fseek( psDBF->fp, nRecordOffset, 0 ); fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp ); psDBF->nCurrentRecord = hEntity; } 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->pszHeader = (void *) malloc ( 32 * psDBF->nFields ); memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields ); newDBF->nFields = psDBF->nFields; newDBF->nRecordLength = psDBF->nRecordLength; newDBF->nHeaderLength = psDBF->nHeaderLength; newDBF->panFieldOffset = (void *) malloc ( sizeof(int) * psDBF->nFields ); memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields ); newDBF->panFieldSize = (void *) malloc ( sizeof(int) * psDBF->nFields ); memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields ); newDBF->panFieldDecimals = (void *) malloc ( sizeof(int) * psDBF->nFields ); memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields ); newDBF->pachFieldType = (void *) malloc ( sizeof(int) * psDBF->nFields ); memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(int) * 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] = 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); 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);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -