⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbfopen.c

📁 Source code, and some other odds and ends can be downloaded from http://shapelib.maptools.org/dl.
💻 C
📖 第 1 页 / 共 4 页
字号:
	if( fseek( psDBF->fp, nRecordOffset, 0 ) != 0 )        {            fprintf( stderr, "fseek(%d) failed on DBF file.\n",                     nRecordOffset );            return NULL;        }	if( fread( psDBF->pszCurrentRecord, psDBF->nRecordLength,                    1, psDBF->fp ) != 1 )        {            fprintf( stderr, "fread(%d) failed on DBF file.\n",                     psDBF->nRecordLength );            return NULL;        }	psDBF->nCurrentRecord = hEntity;    }    pabyRec = (unsigned char *) psDBF->pszCurrentRecord;/* -------------------------------------------------------------------- *//*	Ensure our field buffer is large enough to hold this buffer.	*//* -------------------------------------------------------------------- */    if( psDBF->panFieldSize[iField]+1 > nStringFieldLen )    {	nStringFieldLen = psDBF->panFieldSize[iField]*2 + 10;	pszStringField = (char *) SfRealloc(pszStringField,nStringFieldLen);    }/* -------------------------------------------------------------------- *//*	Extract the requested field.					*//* -------------------------------------------------------------------- */    strncpy( pszStringField, 	     ((const char *) pabyRec) + psDBF->panFieldOffset[iField],	     psDBF->panFieldSize[iField] );    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_CALLDBFReadIntegerAttribute( 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_CALLDBFReadDoubleAttribute( 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' ) );}/************************************************************************//*                         DBFIsAttributeNULL()                         *//*                                                                      *//*      Return TRUE if value for field is NULL.                         *//*                                                                      *//*      Contributed by Jim Matthews.                                    *//************************************************************************/int SHPAPI_CALLDBFIsAttributeNULL( 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;    }    return FALSE;}/************************************************************************//*                          DBFGetFieldCount()                          *//*                                                                      *//*      Return the number of fields in this table.                      *//************************************************************************/int SHPAPI_CALLDBFGetFieldCount( DBFHandle psDBF ){    return( psDBF->nFields );}/************************************************************************//*                         DBFGetRecordCount()                          *//*                                                                      *//*      Return the number of records in this table.                     *//************************************************************************/int SHPAPI_CALLDBFGetRecordCount( DBFHandle psDBF ){    return( psDBF->nRecords );}/************************************************************************//*                          DBFGetFieldInfo()                           *//*                                                                      *//*      Return any requested information about the field.               *//************************************************************************/DBFFieldType SHPAPI_CALLDBFGetFieldInfo( 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;    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;    }

⌨️ 快捷键说明

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