838{
839 char *pszFInfo;
840 int i;
841 int nOldRecordLength, nOldHeaderLength;
842 char *pszRecord;
843 char chFieldFill;
845
846
848 return -1;
849
851 {
852 char szMessage[128];
853 snprintf( szMessage, sizeof(szMessage),
854 "Cannot add field %s. Header length limit reached "
855 "(max 65535 bytes, 2046 fields).",
856 pszFieldName );
857 psDBF->sHooks.Error( szMessage );
858 return -1;
859 }
860
861
862
863
864 if( nWidth < 1 )
865 return -1;
866
869
870 if( psDBF->nRecordLength + nWidth > 65535 )
871 {
872 char szMessage[128];
873 snprintf( szMessage, sizeof(szMessage),
874 "Cannot add field %s. Record length limit reached "
875 "(max 65535 bytes).",
876 pszFieldName );
877 psDBF->sHooks.Error( szMessage );
878 return -1;
879 }
880
881 nOldRecordLength = psDBF->nRecordLength;
882 nOldHeaderLength = psDBF->nHeaderLength;
883
884
885
886
887
888 psDBF->nFields++;
889
891 SfRealloc( psDBF->panFieldOffset,
sizeof(
int) * psDBF->nFields ));
892
894 SfRealloc( psDBF->panFieldSize,
sizeof(
int) * psDBF->nFields ));
895
897 SfRealloc( psDBF->panFieldDecimals,
sizeof(
int) * psDBF->nFields ));
898
900 SfRealloc( psDBF->pachFieldType,
sizeof(
char) * psDBF->nFields ));
901
902
903
904
905 psDBF->panFieldOffset[psDBF->nFields-1] = psDBF->nRecordLength;
906 psDBF->nRecordLength += nWidth;
907 psDBF->panFieldSize[psDBF->nFields-1] = nWidth;
908 psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;
909 psDBF->pachFieldType[psDBF->nFields-1] = chType;
910
911
912
913
915 psDBF->bUpdated =
FALSE;
916
919
921
923 pszFInfo[i] = '\0';
924
926
927 pszFInfo[11] = psDBF->pachFieldType[psDBF->nFields-1];
928
929 if( chType == 'C' )
930 {
931 pszFInfo[16] =
STATIC_CAST(
unsigned char, nWidth % 256);
932 pszFInfo[17] =
STATIC_CAST(
unsigned char, nWidth / 256);
933 }
934 else
935 {
937 pszFInfo[17] =
STATIC_CAST(
unsigned char, nDecimals);
938 }
939
940
941
942
944 psDBF->nRecordLength));
945
946
947 if( psDBF->bNoHeader )
948 return( psDBF->nFields - 1 );
949
950
951
952
953
954
956
958
959 for (i = psDBF->nRecords-1; i >= 0; --i)
960 {
962
963
964 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
965 psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
966
967
968 memset(pszRecord + nOldRecordLength, chFieldFill, nWidth);
969
971
972
973 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
974 psDBF->sHooks.FWrite( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
975 }
976
977 if( psDBF->bWriteEndOfFileChar )
978 {
980
981 nRecordOffset =
983
984 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
985 psDBF->sHooks.FWrite( &ch, 1, 1, psDBF->fp );
986 }
987
988
990
991
992 psDBF->bNoHeader =
TRUE;
994
995 psDBF->nCurrentRecord = -1;
996 psDBF->bCurrentRecordModified =
FALSE;
997 psDBF->bUpdated =
TRUE;
998
999 return( psDBF->nFields-1 );
1000}
static void * SfRealloc(void *pMem, int nNewSize)
#define STATIC_CAST(type, x)
#define END_OF_FILE_CHARACTER
static char DBFGetNullCharacter(char chType)
static int DBFFlushRecord(DBFHandle psDBF)
void SHPAPI_CALL DBFUpdateHeader(DBFHandle psDBF)
#define XBASE_FLDNAME_LEN_WRITE
#define XBASE_FLD_MAX_WIDTH