PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ DBFWriteAttribute()

static int DBFWriteAttribute ( DBFHandle  psDBF,
int  hEntity,
int  iField,
void *  pValue 
)
static

Definition at line 1325 of file dbfopen.c.

1328{
1329 int i, j, nRetResult = TRUE;
1330 unsigned char *pabyRec;
1331 char szSField[XBASE_FLD_MAX_WIDTH+1], szFormat[20];
1332
1333/* -------------------------------------------------------------------- */
1334/* Is this a valid record? */
1335/* -------------------------------------------------------------------- */
1336 if( hEntity < 0 || hEntity > psDBF->nRecords )
1337 return( FALSE );
1338
1339 if( psDBF->bNoHeader )
1340 DBFWriteHeader(psDBF);
1341
1342/* -------------------------------------------------------------------- */
1343/* Is this a brand new record? */
1344/* -------------------------------------------------------------------- */
1345 if( hEntity == psDBF->nRecords )
1346 {
1347 if( !DBFFlushRecord( psDBF ) )
1348 return FALSE;
1349
1350 psDBF->nRecords++;
1351 for( i = 0; i < psDBF->nRecordLength; i++ )
1352 psDBF->pszCurrentRecord[i] = ' ';
1353
1354 psDBF->nCurrentRecord = hEntity;
1355 }
1356
1357/* -------------------------------------------------------------------- */
1358/* Is this an existing record, but different than the last one */
1359/* we accessed? */
1360/* -------------------------------------------------------------------- */
1361 if( !DBFLoadRecord( psDBF, hEntity ) )
1362 return FALSE;
1363
1364 pabyRec = REINTERPRET_CAST(unsigned char *,psDBF->pszCurrentRecord);
1365
1367 psDBF->bUpdated = TRUE;
1368
1369/* -------------------------------------------------------------------- */
1370/* Translate NULL value to valid DBF file representation. */
1371/* */
1372/* Contributed by Jim Matthews. */
1373/* -------------------------------------------------------------------- */
1374 if( pValue == SHPLIB_NULLPTR )
1375 {
1376 memset( pabyRec+psDBF->panFieldOffset[iField],
1377 DBFGetNullCharacter(psDBF->pachFieldType[iField]),
1378 psDBF->panFieldSize[iField] );
1379 return TRUE;
1380 }
1381
1382/* -------------------------------------------------------------------- */
1383/* Assign all the record fields. */
1384/* -------------------------------------------------------------------- */
1385 switch( psDBF->pachFieldType[iField] )
1386 {
1387 case 'D':
1388 case 'N':
1389 case 'F':
1390 {
1391 int nWidth = psDBF->panFieldSize[iField];
1392
1393 if( STATIC_CAST(int,sizeof(szSField))-2 < nWidth )
1394 nWidth = sizeof(szSField)-2;
1395
1396 snprintf( szFormat, sizeof(szFormat), "%%%d.%df",
1397 nWidth, psDBF->panFieldDecimals[iField] );
1398 CPLsnprintf(szSField, sizeof(szSField), szFormat, *STATIC_CAST(double *, pValue) );
1399 szSField[sizeof(szSField)-1] = '\0';
1400 if( STATIC_CAST(int,strlen(szSField)) > psDBF->panFieldSize[iField] )
1401 {
1402 szSField[psDBF->panFieldSize[iField]] = '\0';
1403 nRetResult = FALSE;
1404 }
1405 memcpy(REINTERPRET_CAST(char *, pabyRec+psDBF->panFieldOffset[iField]),
1406 szSField, strlen(szSField) );
1407 break;
1408 }
1409
1410 case 'L':
1411 if (psDBF->panFieldSize[iField] >= 1 &&
1412 (*STATIC_CAST(char*,pValue) == 'F' || *STATIC_CAST(char*,pValue) == 'T'))
1413 *(pabyRec+psDBF->panFieldOffset[iField]) = *STATIC_CAST(char*,pValue);
1414 break;
1415
1416 default:
1417 if( STATIC_CAST(int, strlen(STATIC_CAST(char *,pValue))) > psDBF->panFieldSize[iField] )
1418 {
1419 j = psDBF->panFieldSize[iField];
1420 nRetResult = FALSE;
1421 }
1422 else
1423 {
1424 memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
1425 psDBF->panFieldSize[iField] );
1426 j = STATIC_CAST(int, strlen(STATIC_CAST(char *,pValue)));
1427 }
1428
1429 strncpy(REINTERPRET_CAST(char *, pabyRec+psDBF->panFieldOffset[iField]),
1430 STATIC_CAST(const char *, pValue), j );
1431 break;
1432 }
1433
1434 return( nRetResult );
1435}
static int DBFLoadRecord(DBFHandle psDBF, int iRecord)
Definition dbfopen.c:257
#define STATIC_CAST(type, x)
Definition dbfopen.c:96
#define REINTERPRET_CAST(type, x)
Definition dbfopen.c:97
static void DBFWriteHeader(DBFHandle psDBF)
Definition dbfopen.c:127
#define TRUE
Definition dbfopen.c:73
#define FALSE
Definition dbfopen.c:72
static char DBFGetNullCharacter(char chType)
Definition dbfopen.c:820
#define SHPLIB_NULLPTR
Definition dbfopen.c:99
static int DBFFlushRecord(DBFHandle psDBF)
Definition dbfopen.c:195
#define CPLsnprintf
Definition dbfopen.c:66
#define XBASE_FLD_MAX_WIDTH
Definition shapefil.h:654
int nRecordLength
Definition shapefil.h:596
int * panFieldOffset
Definition shapefil.h:601
int * panFieldDecimals
Definition shapefil.h:603
int bUpdated
Definition shapefil.h:616
int * panFieldSize
Definition shapefil.h:602
char * pszCurrentRecord
Definition shapefil.h:610
int bCurrentRecordModified
Definition shapefil.h:609
char * pachFieldType
Definition shapefil.h:604
int bNoHeader
Definition shapefil.h:615
int nRecords
Definition shapefil.h:594
int nCurrentRecord
Definition shapefil.h:608

References DBFInfo::bCurrentRecordModified, DBFInfo::bNoHeader, DBFInfo::bUpdated, CPLsnprintf, DBFFlushRecord(), DBFGetNullCharacter(), DBFLoadRecord(), DBFWriteHeader(), FALSE, DBFInfo::nCurrentRecord, DBFInfo::nRecordLength, DBFInfo::nRecords, DBFInfo::pachFieldType, DBFInfo::panFieldDecimals, DBFInfo::panFieldOffset, DBFInfo::panFieldSize, DBFInfo::pszCurrentRecord, REINTERPRET_CAST, SHPLIB_NULLPTR, STATIC_CAST, TRUE, and XBASE_FLD_MAX_WIDTH.

Referenced by DBFWriteDoubleAttribute(), DBFWriteIntegerAttribute(), DBFWriteLogicalAttribute(), DBFWriteNULLAttribute(), and DBFWriteStringAttribute().

Here is the call graph for this function:
Here is the caller graph for this function: