📄 dbfopen.c
字号:
int DBFGetFieldCount( DBFHandle psDBF ){ return( psDBF->nFields );}/************************************************************************//* DBFGetRecordCount() *//* *//* Return the number of records in this table. *//************************************************************************/int DBFGetRecordCount( DBFHandle psDBF ){ return( psDBF->nRecords );}/************************************************************************//* DBFGetFieldInfo() *//* *//* Return any requested information about the field. *//************************************************************************/DBFFieldType DBFGetFieldInfo( DBFHandle psDBF, int iField, char * pszFieldName, int * pnWidth, int * pnDecimals ){ if( iField < 0 || iField >= psDBF->nFields ) return( FTInvalid ); if( pnWidth != NULL ) *pnWidth = psDBF->panFieldSize[iField]; if( pnDecimals != NULL ) *pnDecimals = psDBF->panFieldDecimals[iField]; if( pszFieldName != NULL ) { int i; strncpy( pszFieldName, (char *) psDBF->pszHeader+iField*32, 11 ); pszFieldName[11] = '\0'; for( i = 10; i > 0 && pszFieldName[i] == ' '; i-- ) pszFieldName[i] = '\0'; } if( psDBF->pachFieldType[iField] == 'N' || psDBF->pachFieldType[iField] == 'F' || psDBF->pachFieldType[iField] == 'D' ) { if( psDBF->panFieldDecimals[iField] > 0 ) return( FTDouble ); else return( FTInteger ); } else { return( FTString ); }}/************************************************************************//* DBFWriteAttribute() *//* *//* Write an attribute record to the file. *//************************************************************************/static int DBFWriteAttribute(DBFHandle psDBF, int hEntity, int iField, void * pValue ){ int nRecordOffset, i, j; uchar *pabyRec; char szSField[40], szFormat[12];/* -------------------------------------------------------------------- *//* 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 = (uchar *) psDBF->pszCurrentRecord;/* -------------------------------------------------------------------- *//* Assign all the record fields. *//* -------------------------------------------------------------------- */ switch( psDBF->pachFieldType[iField] ) { case 'D': case 'N': case 'F': if( psDBF->panFieldDecimals[iField] == 0 ) { sprintf( szFormat, "%%%dd", psDBF->panFieldSize[iField] ); sprintf(szSField, szFormat, (int) *((double *) pValue) ); if( strlen(szSField) > psDBF->panFieldSize[iField] ) szSField[psDBF->panFieldSize[iField]] = '\0'; strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), szSField, strlen(szSField) ); } else { sprintf( szFormat, "%%%d.%df", psDBF->panFieldSize[iField], psDBF->panFieldDecimals[iField] ); sprintf(szSField, szFormat, *((double *) pValue) ); if( strlen(szSField) > psDBF->panFieldSize[iField] ) szSField[psDBF->panFieldSize[iField]] = '\0'; strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), szSField, strlen(szSField) ); } break; default: if( 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; } psDBF->bCurrentRecordModified = TRUE; psDBF->bUpdated = TRUE; return( TRUE );}/************************************************************************//* DBFWriteDoubleAttribute() *//* *//* Write a double attribute. *//************************************************************************/int DBFWriteDoubleAttribute( DBFHandle psDBF, int iRecord, int iField, double dValue ){ return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );}/************************************************************************//* DBFWriteIntegerAttribute() *//* *//* Write a integer attribute. *//************************************************************************/int DBFWriteIntegerAttribute( DBFHandle psDBF, int iRecord, int iField, int nValue ){ double dValue = nValue; return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );}/************************************************************************//* DBFWriteStringAttribute() *//* *//* Write a string attribute. *//************************************************************************/int DBFWriteStringAttribute( DBFHandle psDBF, int iRecord, int iField, const char * pszValue ){ return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) pszValue ) );}/************************************************************************//* DBFWriteTuple() *//* *//* Write an attribute record to the file. *//************************************************************************/int DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple ){ int nRecordOffset, i; uchar *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 = (uchar *) 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 *DBFReadTuple(DBFHandle psDBF, int hEntity ){ int nRecordOffset; uchar *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 = (uchar *) 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 DBFCloneEmpty(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 );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -