/ msDBFAddField() */ / Add a field to a newly created .dbf file before any records */ are written. */ /
| 379 | /* are written. */ |
| 380 | /************************************************************************/ |
| 381 | int msDBFAddField(DBFHandle psDBF, const char * pszFieldName, DBFFieldType eType, int nWidth, int nDecimals ) |
| 382 | { |
| 383 | char *pszFInfo; |
| 384 | int i; |
| 385 | |
| 386 | /* -------------------------------------------------------------------- */ |
| 387 | /* Do some checking to ensure we can add records to this file. */ |
| 388 | /* -------------------------------------------------------------------- */ |
| 389 | if( psDBF->nRecords > 0 ) |
| 390 | return( MS_FALSE ); |
| 391 | |
| 392 | if( !psDBF->bNoHeader ) |
| 393 | return( MS_FALSE ); |
| 394 | |
| 395 | if( eType != FTDouble && nDecimals != 0 ) |
| 396 | return( MS_FALSE ); |
| 397 | |
| 398 | /* -------------------------------------------------------------------- */ |
| 399 | /* SfRealloc all the arrays larger to hold the additional field */ |
| 400 | /* information. */ |
| 401 | /* -------------------------------------------------------------------- */ |
| 402 | psDBF->nFields++; |
| 403 | |
| 404 | psDBF->panFieldOffset = (int *) |
| 405 | SfRealloc( psDBF->panFieldOffset, sizeof(int) * psDBF->nFields ); |
| 406 | |
| 407 | psDBF->panFieldSize = (int *) |
| 408 | SfRealloc( psDBF->panFieldSize, sizeof(int) * psDBF->nFields ); |
| 409 | |
| 410 | psDBF->panFieldDecimals = (int *) |
| 411 | SfRealloc( psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields ); |
| 412 | |
| 413 | psDBF->pachFieldType = (char *) |
| 414 | SfRealloc( psDBF->pachFieldType, sizeof(char) * psDBF->nFields ); |
| 415 | |
| 416 | /* -------------------------------------------------------------------- */ |
| 417 | /* Assign the new field information fields. */ |
| 418 | /* -------------------------------------------------------------------- */ |
| 419 | psDBF->panFieldOffset[psDBF->nFields-1] = psDBF->nRecordLength; |
| 420 | psDBF->nRecordLength += nWidth; |
| 421 | psDBF->panFieldSize[psDBF->nFields-1] = nWidth; |
| 422 | psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals; |
| 423 | |
| 424 | if( eType == FTString ) |
| 425 | psDBF->pachFieldType[psDBF->nFields-1] = 'C'; |
| 426 | else |
| 427 | psDBF->pachFieldType[psDBF->nFields-1] = 'N'; |
| 428 | |
| 429 | /* -------------------------------------------------------------------- */ |
| 430 | /* Extend the required header information. */ |
| 431 | /* -------------------------------------------------------------------- */ |
| 432 | psDBF->nHeaderLength += 32; |
| 433 | psDBF->bUpdated = MS_FALSE; |
| 434 | |
| 435 | psDBF->pszHeader = (char *) SfRealloc(psDBF->pszHeader,psDBF->nFields*32); |
| 436 | |
| 437 | pszFInfo = psDBF->pszHeader + 32 * (psDBF->nFields-1); |
| 438 |
no test coverage detected