24#include "cpl_string.h"
28#define STRCASECMP(a, b) (_stricmp(a, b))
30#define STRCASECMP(a, b) (stricmp(a, b))
33#define STRCASECMP(a, b) (strcasecmp(a, b))
38#define snprintf _snprintf
42#define snprintf _snprintf
46#define CPLsprintf sprintf
47#define CPLsnprintf snprintf
56#define XBASE_FILEHDR_SZ 32
58#define HEADER_RECORD_TERMINATOR 0x0D
61#define END_OF_FILE_CHARACTER 0x1A
68#define CPL_IGNORE_RET_VAL_INT(x) x
84 if (!
psDBF->bNoHeader)
121 if (
psDBF->nHeaderLength >
130 if (
psDBF->nRecords == 0 &&
psDBF->bWriteEndOfFileChar) {
145 if (
psDBF->bCurrentRecordModified &&
psDBF->nCurrentRecord > -1) {
149 psDBF->nRecordLength *
151 psDBF->nHeaderLength;
159 if (
psDBF->bRequireNextWriteSeek ||
165 "Failure seeking to position before writing DBF record %d.",
166 psDBF->nCurrentRecord);
172 if (
psDBF->sHooks.FWrite(
psDBF->pszCurrentRecord,
psDBF->nRecordLength,
176 "Failure writing DBF record %d.",
psDBF->nCurrentRecord);
188 if (
psDBF->nCurrentRecord ==
psDBF->nRecords - 1) {
189 if (
psDBF->bWriteEndOfFileChar) {
206 if (!DBFFlushRecord(
psDBF))
211 psDBF->nHeaderLength;
216 "fseek(%ld) failed on DBF file.",
226 "fread(%d) failed on DBF file.",
psDBF->nRecordLength);
250 if (
psDBF->bNoHeader)
251 DBFWriteHeader(
psDBF);
253 if (!DBFFlushRecord(
psDBF))
309static int DBFGetLenWithoutExtension(
const char *
pszBasename)
312 for (
int i =
nLen - 1;
380 psDBF->nCurrentRecord = -1;
386 const int nBufSize = 500;
417 psDBF->nFields = nFields;
439 psDBF->iLanguageDriver);
506 psDBF->nRecordLength) {
530 if (
psDBF->bNoHeader)
531 DBFWriteHeader(
psDBF);
582 const char *pszCodePage)
598 const char *pszCodePage,
622 if (
strncmp(pszCodePage,
"LDID/", 5) == 0) {
652 psDBF->nRecordLength = 1;
653 psDBF->nHeaderLength =
662 psDBF->nCurrentRecord = -1;
712static char DBFGetNullCharacter(
char chType)
738 if (!DBFFlushRecord(
psDBF))
744 "Cannot add field %s. Header length limit reached "
745 "(max 65535 bytes, 2046 fields).",
763 "Cannot add field %s. Record length limit reached "
764 "(max 65535 bytes).",
834 if (
psDBF->bNoHeader)
835 return (
psDBF->nFields - 1);
848 for (
int i =
psDBF->nRecords - 1; i >= 0; --i) {
864 psDBF->nHeaderLength;
871 if (
psDBF->bWriteEndOfFileChar) {
876 psDBF->nHeaderLength;
889 psDBF->nCurrentRecord = -1;
893 return (
psDBF->nFields - 1);
920 const unsigned char *pabyRec =
929 psDBF->pszWorkField =
956 psDBF->fieldValue.dfDoubleField =
965#ifdef TRIM_DBF_WHITESPACE
1098 for (
int i = 0;
pszValue[i] !=
'\0'; i++) {
1154 return (
psDBF->nFields);
1165 return (
psDBF->nRecords);
1234 if (
psDBF->bNoHeader)
1235 DBFWriteHeader(
psDBF);
1241 if (!DBFFlushRecord(
psDBF))
1245 for (
int i = 0; i <
psDBF->nRecordLength; i++)
1246 psDBF->pszCurrentRecord[i] =
' ';
1258 unsigned char *pabyRec =
1271 DBFGetNullCharacter(
psDBF->pachFieldType[
iField]),
1359 if (
psDBF->bNoHeader)
1360 DBFWriteHeader(
psDBF);
1366 if (!DBFFlushRecord(
psDBF))
1370 for (
int i = 0; i <
psDBF->nRecordLength; i++)
1371 psDBF->pszCurrentRecord[i] =
' ';
1384 unsigned char *pabyRec =
1519 if (
psDBF->bNoHeader)
1520 DBFWriteHeader(
psDBF);
1526 if (!DBFFlushRecord(
psDBF))
1530 for (
int i = 0; i <
psDBF->nRecordLength; i++)
1531 psDBF->pszCurrentRecord[i] =
' ';
1543 unsigned char *pabyRec =
1591 if (
psDBF->pszHeader) {
1601 sizeof(
int) *
psDBF->nFields);
1605 sizeof(
int) *
psDBF->nFields);
1606 newDBF->panFieldDecimals =
1609 sizeof(
int) *
psDBF->nFields);
1613 sizeof(
char) *
psDBF->nFields);
1617 newDBF->bWriteEndOfFileChar =
psDBF->bWriteEndOfFileChar;
1623 newDBF->bWriteEndOfFileChar =
psDBF->bWriteEndOfFileChar;
1692 return psDBF->pszCurrentRecord[0] ==
'*';
1741 return psDBF->pszCodePage;
1756 if (!DBFFlushRecord(
psDBF))
1766 for (
int i =
iField + 1; i <
psDBF->nFields; i++) {
1767 psDBF->panFieldOffset[i - 1] =
1769 psDBF->panFieldSize[i - 1] =
psDBF->panFieldSize[i];
1770 psDBF->panFieldDecimals[i - 1] =
psDBF->panFieldDecimals[i];
1771 psDBF->pachFieldType[i - 1] =
psDBF->pachFieldType[i];
1806 if (
psDBF->bNoHeader &&
psDBF->nRecords == 0)
1832 psDBF->nHeaderLength;
1837 psDBF->sHooks.FWrite(
1843 if (
psDBF->bWriteEndOfFileChar) {
1847 psDBF->nHeaderLength;
1858 psDBF->nCurrentRecord = -1;
1877 if (
psDBF->nFields == 0)
1881 if (!DBFFlushRecord(
psDBF))
1898 for (
int i = 0; i <
psDBF->nFields; i++) {
1906 for (
int i = 1; i <
psDBF->nFields; i++) {
1917 if (!(
psDBF->bNoHeader &&
psDBF->nRecords == 0)) {
1932 psDBF->nHeaderLength;
1944 for (
int i = 0; i <
psDBF->nFields; i++) {
1966 psDBF->nCurrentRecord = -1;
1982 psDBF->nCurrentRecord = -1;
2003 if (!DBFFlushRecord(
psDBF))
2063 if (
psDBF->bNoHeader &&
psDBF->nRecords == 0)
2084 psDBF->nHeaderLength;
2119 psDBF->nHeaderLength;
2131 psDBF->nHeaderLength;
2153 psDBF->nHeaderLength;
2192 psDBF->nHeaderLength;
2204 psDBF->nHeaderLength;
2215 psDBF->nCurrentRecord = -1;
2221 psDBF->nCurrentRecord = -1;
DBFHandle DBFCloneEmpty(const DBFHandle psDBF, const char *pszFilename)
const char * DBFGetCodePage(const DBFHandle psDBF)
int DBFWriteIntegerAttribute(DBFHandle psDBF, int iRecord, int iField, int nValue)
const char * DBFReadTuple(DBFHandle psDBF, int hEntity)
DBFHandle DBFCreate(const char *pszFilename)
int DBFWriteStringAttribute(DBFHandle psDBF, int iRecord, int iField, const char *pszValue)
int DBFGetFieldCount(const DBFHandle psDBF)
int DBFMarkRecordDeleted(DBFHandle psDBF, int iShape, int bIsDeleted)
int DBFWriteLogicalAttribute(DBFHandle psDBF, int iRecord, int iField, const char lValue)
DBFHandle DBFOpenLL(const char *pszFilename, const char *pszAccess, const SAHooks *psHooks)
int DBFDeleteField(DBFHandle psDBF, int iField)
void DBFClose(DBFHandle psDBF)
int DBFAddField(DBFHandle psDBF, const char *pszFieldName, DBFFieldType eType, int nWidth, int nDecimals)
int DBFAlterFieldDefn(DBFHandle psDBF, int iField, const char *pszFieldName, char chType, int nWidth, int nDecimals)
int DBFWriteNULLAttribute(DBFHandle psDBF, int iRecord, int iField)
SHPDate DBFReadDateAttribute(DBFHandle psDBF, int iRecord, int iField)
int DBFIsRecordDeleted(const DBFHandle psDBF, int iShape)
void DBFSetWriteEndOfFileChar(DBFHandle psDBF, int bWriteFlag)
int DBFReadIntegerAttribute(DBFHandle psDBF, int iRecord, int iField)
DBFHandle DBFCreateEx(const char *pszFilename, const char *pszCodePage)
int DBFIsAttributeNULL(const DBFHandle psDBF, int iRecord, int iField)
DBFFieldType DBFGetFieldInfo(const DBFHandle psDBF, int iField, char *pszFieldName, int *pnWidth, int *pnDecimals)
int DBFReorderFields(DBFHandle psDBF, const int *panMap)
const char * DBFReadLogicalAttribute(DBFHandle psDBF, int iRecord, int iField)
int DBFWriteDoubleAttribute(DBFHandle psDBF, int iRecord, int iField, double dValue)
double DBFReadDoubleAttribute(DBFHandle psDBF, int iRecord, int iField)
#define HEADER_RECORD_TERMINATOR
void DBFSetLastModifiedDate(DBFHandle psDBF, int nYYSince1900, int nMM, int nDD)
void DBFUpdateHeader(DBFHandle psDBF)
char DBFGetNativeFieldType(const DBFHandle psDBF, int iField)
DBFHandle DBFCreateLL(const char *pszFilename, const char *pszCodePage, const SAHooks *psHooks)
int DBFAddNativeFieldType(DBFHandle psDBF, const char *pszFieldName, char chType, int nWidth, int nDecimals)
#define END_OF_FILE_CHARACTER
int DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField, const void *pValue)
int DBFGetRecordCount(const DBFHandle psDBF)
const char * DBFReadStringAttribute(DBFHandle psDBF, int iRecord, int iField)
int DBFWriteDateAttribute(DBFHandle psDBF, int iRecord, int iField, const SHPDate *lValue)
int DBFWriteTuple(DBFHandle psDBF, int hEntity, const void *pRawTuple)
DBFHandle DBFOpen(const char *pszFilename, const char *pszAccess)
int DBFGetFieldIndex(const DBFHandle psDBF, const char *pszFieldName)
#define CPL_IGNORE_RET_VAL_INT(x)
void SASetupDefaultHooks(SAHooks *psHooks)
#define XBASE_FLDNAME_LEN_WRITE
#define XBASE_FLDNAME_LEN_READ
#define XBASE_FLD_MAX_WIDTH
#define STATIC_CAST(type, x)
#define REINTERPRET_CAST(type, x)
#define CONST_CAST(type, x)