PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ DBFWriteAttribute()

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

Definition at line 1271 of file dbfopen.c.

References DBFFlushRecord(), DBFGetNullCharacter(), DBFLoadRecord(), DBFWriteHeader(), FALSE, SHPAPI_CALL, and TRUE.

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

1274 {
1275  int i, j, nRetResult = TRUE;
1276  unsigned char *pabyRec;
1277  char szSField[400], szFormat[20];
1278 
1279 /* -------------------------------------------------------------------- */
1280 /* Is this a valid record? */
1281 /* -------------------------------------------------------------------- */
1282  if( hEntity < 0 || hEntity > psDBF->nRecords )
1283  return( FALSE );
1284 
1285  if( psDBF->bNoHeader )
1286  DBFWriteHeader(psDBF);
1287 
1288 /* -------------------------------------------------------------------- */
1289 /* Is this a brand new record? */
1290 /* -------------------------------------------------------------------- */
1291  if( hEntity == psDBF->nRecords )
1292  {
1293  if( !DBFFlushRecord( psDBF ) )
1294  return FALSE;
1295 
1296  psDBF->nRecords++;
1297  for( i = 0; i < psDBF->nRecordLength; i++ )
1298  psDBF->pszCurrentRecord[i] = ' ';
1299 
1300  psDBF->nCurrentRecord = hEntity;
1301  }
1302 
1303 /* -------------------------------------------------------------------- */
1304 /* Is this an existing record, but different than the last one */
1305 /* we accessed? */
1306 /* -------------------------------------------------------------------- */
1307  if( !DBFLoadRecord( psDBF, hEntity ) )
1308  return FALSE;
1309 
1310  pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
1311 
1312  psDBF->bCurrentRecordModified = TRUE;
1313  psDBF->bUpdated = TRUE;
1314 
1315 /* -------------------------------------------------------------------- */
1316 /* Translate NULL value to valid DBF file representation. */
1317 /* */
1318 /* Contributed by Jim Matthews. */
1319 /* -------------------------------------------------------------------- */
1320  if( pValue == NULL )
1321  {
1322  memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]),
1323  DBFGetNullCharacter(psDBF->pachFieldType[iField]),
1324  psDBF->panFieldSize[iField] );
1325  return TRUE;
1326  }
1327 
1328 /* -------------------------------------------------------------------- */
1329 /* Assign all the record fields. */
1330 /* -------------------------------------------------------------------- */
1331  switch( psDBF->pachFieldType[iField] )
1332  {
1333  case 'D':
1334  case 'N':
1335  case 'F':
1336  if( psDBF->panFieldDecimals[iField] == 0 )
1337  {
1338  int nWidth = psDBF->panFieldSize[iField];
1339 
1340  if( (int) sizeof(szSField)-2 < nWidth )
1341  nWidth = sizeof(szSField)-2;
1342 
1343  sprintf( szFormat, "%%%dd", nWidth );
1344  sprintf(szSField, szFormat, (int) *((double *) pValue) );
1345  if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )
1346  {
1347  szSField[psDBF->panFieldSize[iField]] = '\0';
1348  nRetResult = FALSE;
1349  }
1350 
1351  strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1352  szSField, strlen(szSField) );
1353  }
1354  else
1355  {
1356  int nWidth = psDBF->panFieldSize[iField];
1357 
1358  if( (int) sizeof(szSField)-2 < nWidth )
1359  nWidth = sizeof(szSField)-2;
1360 
1361  sprintf( szFormat, "%%%d.%df",
1362  nWidth, psDBF->panFieldDecimals[iField] );
1363  sprintf(szSField, szFormat, *((double *) pValue) );
1364  if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )
1365  {
1366  szSField[psDBF->panFieldSize[iField]] = '\0';
1367  nRetResult = FALSE;
1368  }
1369  strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1370  szSField, strlen(szSField) );
1371  }
1372  break;
1373 
1374  case 'L':
1375  if (psDBF->panFieldSize[iField] >= 1 &&
1376  (*(char*)pValue == 'F' || *(char*)pValue == 'T'))
1377  *(pabyRec+psDBF->panFieldOffset[iField]) = *(char*)pValue;
1378  break;
1379 
1380  default:
1381  if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )
1382  {
1383  j = psDBF->panFieldSize[iField];
1384  nRetResult = FALSE;
1385  }
1386  else
1387  {
1388  memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
1389  psDBF->panFieldSize[iField] );
1390  j = strlen((char *) pValue);
1391  }
1392 
1393  strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
1394  (char *) pValue, j );
1395  break;
1396  }
1397 
1398  return( nRetResult );
1399 }
static int DBFLoadRecord(DBFHandle psDBF, int iRecord)
Definition: dbfopen.c:291
static void DBFWriteHeader(DBFHandle psDBF)
Definition: dbfopen.c:197
static int DBFFlushRecord(DBFHandle psDBF)
Definition: dbfopen.c:258
#define FALSE
Definition: dbfopen.c:168
static char DBFGetNullCharacter(char chType)
Definition: dbfopen.c:796
#define TRUE
Definition: dbfopen.c:169
Here is the call graph for this function:
Here is the caller graph for this function: