📄 dbfopen.cpp
字号:
pszStringField[psDBF->panFieldSize[iField]] = '\0';
pReturnField = pszStringField;
/* -------------------------------------------------------------------- */
/* Decode the field. */
/* -------------------------------------------------------------------- */
if( chReqType == 'N' )
{
dDoubleField = atof(pszStringField);
pReturnField = &dDoubleField;
}
/* -------------------------------------------------------------------- */
/* Should we trim white space off the string attribute value? */
/* -------------------------------------------------------------------- */
#ifdef TRIM_DBF_WHITESPACE
else
{
char *pchSrc, *pchDst;
pchDst = pchSrc = pszStringField;
while( *pchSrc == ' ' )
pchSrc++;
while( *pchSrc != '\0' )
*(pchDst++) = *(pchSrc++);
*pchDst = '\0';
while( pchDst != pszStringField && *(--pchDst) == ' ' )
*pchDst = '\0';
}
#endif
return( pReturnField );
}
/************************************************************************/
/* DBFReadIntAttribute() */
/* */
/* Read an integer attribute. */
/************************************************************************/
int SHPAPI_CALL
DBFReadIntegerAttribute( DBFHandle psDBF, int iRecord, int iField )
{
double *pdValue;
pdValue = (double *) DBFReadAttribute( psDBF, iRecord, iField, 'N' );
if( pdValue == NULL )
return 0;
else
return( (int) *pdValue );
}
/************************************************************************/
/* DBFReadDoubleAttribute() */
/* */
/* Read a double attribute. */
/************************************************************************/
double SHPAPI_CALL
DBFReadDoubleAttribute( DBFHandle psDBF, int iRecord, int iField )
{
double *pdValue;
pdValue = (double *) DBFReadAttribute( psDBF, iRecord, iField, 'N' );
if( pdValue == NULL )
return 0.0;
else
return( *pdValue );
}
/************************************************************************/
/* DBFReadStringAttribute() */
/* */
/* Read a string attribute. */
/************************************************************************/
const char SHPAPI_CALL1(*)
DBFReadStringAttribute( DBFHandle psDBF, int iRecord, int iField )
{
return( (const char *) DBFReadAttribute( psDBF, iRecord, iField, 'C' ) );
}
/************************************************************************/
/* DBFReadLogicalAttribute() */
/* */
/* Read a logical attribute. */
/************************************************************************/
const char SHPAPI_CALL1(*)
DBFReadLogicalAttribute( DBFHandle psDBF, int iRecord, int iField )
{
return( (const char *) DBFReadAttribute( psDBF, iRecord, iField, 'L' ) );
}
/************************************************************************/
/* DBFIsAttributeNULL() */
/* */
/* Return TRUE if value for field is NULL. */
/* */
/* Contributed by Jim Matthews. */
/************************************************************************/
int SHPAPI_CALL
DBFIsAttributeNULL( DBFHandle psDBF, int iRecord, int iField )
{
const char *pszValue;
pszValue = DBFReadStringAttribute( psDBF, iRecord, iField );
switch(psDBF->pachFieldType[iField])
{
case 'N':
case 'F':
/* NULL numeric fields have value "****************" */
return pszValue[0] == '*';
case 'D':
/* NULL date fields have value "00000000" */
return strncmp(pszValue,"00000000",8) == 0;
case 'L':
/* NULL boolean fields have value "?" */
return pszValue[0] == '?';
default:
/* empty string fields are considered NULL */
return strlen(pszValue) == 0;
}
}
/************************************************************************/
/* DBFGetFieldCount() */
/* */
/* Return the number of fields in this table. */
/************************************************************************/
int SHPAPI_CALL
DBFGetFieldCount( DBFHandle psDBF )
{
return( psDBF->nFields );
}
/************************************************************************/
/* DBFGetRecordCount() */
/* */
/* Return the number of records in this table. */
/************************************************************************/
int SHPAPI_CALL
DBFGetRecordCount( DBFHandle psDBF )
{
return( psDBF->nRecords );
}
/************************************************************************/
/* DBFGetFieldInfo() */
/* */
/* Return any requested information about the field. */
/************************************************************************/
DBFFieldType SHPAPI_CALL
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] == 'L' )
return( FTLogical);
else 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, nRetResult = TRUE;
unsigned char *pabyRec;
char szSField[400], szFormat[20];
/* -------------------------------------------------------------------- */
/* 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;
psDBF->bCurrentRecordModified = TRUE;
psDBF->bUpdated = TRUE;
/* -------------------------------------------------------------------- */
/* Translate NULL value to valid DBF file representation. */
/* */
/* Contributed by Jim Matthews. */
/* -------------------------------------------------------------------- */
if( pValue == NULL )
{
switch(psDBF->pachFieldType[iField])
{
case 'N':
case 'F':
/* NULL numeric fields have value "****************" */
memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '*',
psDBF->panFieldSize[iField] );
break;
case 'D':
/* NULL date fields have value "00000000" */
memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '0',
psDBF->panFieldSize[iField] );
break;
case 'L':
/* NULL boolean fields have value "?" */
memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '?',
psDBF->panFieldSize[iField] );
break;
default:
/* empty string fields are considered NULL */
memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '\0',
psDBF->panFieldSize[iField] );
break;
}
return TRUE;
}
/* -------------------------------------------------------------------- */
/* 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"*/"%%%df", nWidth );
sprintf(szSField, szFormat, /*(int)*/ *((double *) pValue) );
if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )
{
szSField[psDBF->panFieldSize[iField]] = '\0';
nRetResult = FALSE;
}
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';
nRetResult = FALSE;
}
strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
szSField, strlen(szSField) );
}
break;
case 'L':
if (psDBF->panFieldSize[iField] >= 1 &&
(*(char*)pValue == 'F' || *(char*)pValue == 'T'))
*(pabyRec+psDBF->panFieldOffset[iField]) = *(char*)pValue;
break;
default:
if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )
{
j = psDBF->panFieldSize[iField];
nRetResult = FALSE;
}
else
{
memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
psDBF->panFieldSize[iField] );
j = strlen((char *) pValue);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -