PostGIS  3.4.0dev-r@@SVN_REVISION@@
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages

◆ DBFWriteAttribute()

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

Definition at line 1316 of file dbfopen.c.

1319 {
1320  int i, j, nRetResult = TRUE;
1321  unsigned char *pabyRec;
1322  char szSField[XBASE_FLD_MAX_WIDTH+1], szFormat[20];
1323 
1324 /* -------------------------------------------------------------------- */
1325 /* Is this a valid record? */
1326 /* -------------------------------------------------------------------- */
1327  if( hEntity < 0 || hEntity > psDBF->nRecords )
1328  return( FALSE );
1329 
1330  if( psDBF->bNoHeader )
1331  DBFWriteHeader(psDBF);
1332 
1333 /* -------------------------------------------------------------------- */
1334 /* Is this a brand new record? */
1335 /* -------------------------------------------------------------------- */
1336  if( hEntity == psDBF->nRecords )
1337  {
1338  if( !DBFFlushRecord( psDBF ) )
1339  return FALSE;
1340 
1341  psDBF->nRecords++;
1342  for( i = 0; i < psDBF->nRecordLength; i++ )
1343  psDBF->pszCurrentRecord[i] = ' ';
1344 
1345  psDBF->nCurrentRecord = hEntity;
1346  }
1347 
1348 /* -------------------------------------------------------------------- */
1349 /* Is this an existing record, but different than the last one */
1350 /* we accessed? */
1351 /* -------------------------------------------------------------------- */
1352  if( !DBFLoadRecord( psDBF, hEntity ) )
1353  return FALSE;
1354 
1355  pabyRec = REINTERPRET_CAST(unsigned char *,psDBF->pszCurrentRecord);
1356 
1357  psDBF->bCurrentRecordModified = TRUE;
1358  psDBF->bUpdated = TRUE;
1359 
1360 /* -------------------------------------------------------------------- */
1361 /* Translate NULL value to valid DBF file representation. */
1362 /* */
1363 /* Contributed by Jim Matthews. */
1364 /* -------------------------------------------------------------------- */
1365  if( pValue == SHPLIB_NULLPTR )
1366  {
1367  memset( pabyRec+psDBF->panFieldOffset[iField],
1368  DBFGetNullCharacter(psDBF->pachFieldType[iField]),
1369  psDBF->panFieldSize[iField] );
1370  return TRUE;
1371  }
1372 
1373 /* -------------------------------------------------------------------- */
1374 /* Assign all the record fields. */
1375 /* -------------------------------------------------------------------- */
1376  switch( psDBF->pachFieldType[iField] )
1377  {
1378  case 'D':
1379  case 'N':
1380  case 'F':
1381  {
1382  int nWidth = psDBF->panFieldSize[iField];
1383 
1384  if( STATIC_CAST(int,sizeof(szSField))-2 < nWidth )
1385  nWidth = sizeof(szSField)-2;
1386 
1387  snprintf( szFormat, sizeof(szFormat), "%%%d.%df",
1388  nWidth, psDBF->panFieldDecimals[iField] );
1389  CPLsnprintf(szSField, sizeof(szSField), szFormat, *STATIC_CAST(double *, pValue) );
1390  szSField[sizeof(szSField)-1] = '\0';
1391  if( STATIC_CAST(int,strlen(szSField)) > psDBF->panFieldSize[iField] )
1392  {
1393  szSField[psDBF->panFieldSize[iField]] = '\0';
1394  nRetResult = FALSE;
1395  }
1396  memcpy(REINTERPRET_CAST(char *, pabyRec+psDBF->panFieldOffset[iField]),
1397  szSField, strlen(szSField) );
1398  break;
1399  }
1400 
1401  case 'L':
1402  if (psDBF->panFieldSize[iField] >= 1 &&
1403  (*STATIC_CAST(char*,pValue) == 'F' || *STATIC_CAST(char*,pValue) == 'T'))
1404  *(pabyRec+psDBF->panFieldOffset[iField]) = *STATIC_CAST(char*,pValue);
1405  break;
1406 
1407  default:
1408  if( STATIC_CAST(int, strlen(STATIC_CAST(char *,pValue))) > psDBF->panFieldSize[iField] )
1409  {
1410  j = psDBF->panFieldSize[iField];
1411  nRetResult = FALSE;
1412  }
1413  else
1414  {
1415  memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
1416  psDBF->panFieldSize[iField] );
1417  j = STATIC_CAST(int, strlen(STATIC_CAST(char *,pValue)));
1418  }
1419 
1420  strncpy(REINTERPRET_CAST(char *, pabyRec+psDBF->panFieldOffset[iField]),
1421  STATIC_CAST(const char *, pValue), j );
1422  break;
1423  }
1424 
1425  return( nRetResult );
1426 }
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:811
#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: