847{
848 char *pszFInfo;
849 int i;
850 int nOldRecordLength, nOldHeaderLength;
851 char *pszRecord;
852 char chFieldFill;
854
855
857 return -1;
858
860 {
861 char szMessage[128];
862 snprintf( szMessage, sizeof(szMessage),
863 "Cannot add field %s. Header length limit reached "
864 "(max 65535 bytes, 2046 fields).",
865 pszFieldName );
866 psDBF->sHooks.Error( szMessage );
867 return -1;
868 }
869
870
871
872
873 if( nWidth < 1 )
874 return -1;
875
878
879 if( psDBF->nRecordLength + nWidth > 65535 )
880 {
881 char szMessage[128];
882 snprintf( szMessage, sizeof(szMessage),
883 "Cannot add field %s. Record length limit reached "
884 "(max 65535 bytes).",
885 pszFieldName );
886 psDBF->sHooks.Error( szMessage );
887 return -1;
888 }
889
890 nOldRecordLength = psDBF->nRecordLength;
891 nOldHeaderLength = psDBF->nHeaderLength;
892
893
894
895
896
897 psDBF->nFields++;
898
900 SfRealloc( psDBF->panFieldOffset,
sizeof(
int) * psDBF->nFields ));
901
903 SfRealloc( psDBF->panFieldSize,
sizeof(
int) * psDBF->nFields ));
904
906 SfRealloc( psDBF->panFieldDecimals,
sizeof(
int) * psDBF->nFields ));
907
909 SfRealloc( psDBF->pachFieldType,
sizeof(
char) * psDBF->nFields ));
910
911
912
913
914 psDBF->panFieldOffset[psDBF->nFields-1] = psDBF->nRecordLength;
915 psDBF->nRecordLength += nWidth;
916 psDBF->panFieldSize[psDBF->nFields-1] = nWidth;
917 psDBF->panFieldDecimals[psDBF->nFields-1] = nDecimals;
918 psDBF->pachFieldType[psDBF->nFields-1] = chType;
919
920
921
922
924 psDBF->bUpdated =
FALSE;
925
928
930
932 pszFInfo[i] = '\0';
933
935
936 pszFInfo[11] = psDBF->pachFieldType[psDBF->nFields-1];
937
938 if( chType == 'C' )
939 {
940 pszFInfo[16] =
STATIC_CAST(
unsigned char, nWidth % 256);
941 pszFInfo[17] =
STATIC_CAST(
unsigned char, nWidth / 256);
942 }
943 else
944 {
946 pszFInfo[17] =
STATIC_CAST(
unsigned char, nDecimals);
947 }
948
949
950
951
953 psDBF->nRecordLength));
954
955
956 if( psDBF->bNoHeader )
957 return( psDBF->nFields - 1 );
958
959
960
961
962
963
965
967
968 for (i = psDBF->nRecords-1; i >= 0; --i)
969 {
971
972
973 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
974 psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
975
976
977 memset(pszRecord + nOldRecordLength, chFieldFill, nWidth);
978
980
981
982 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
983 psDBF->sHooks.FWrite( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
984 }
985
986 if( psDBF->bWriteEndOfFileChar )
987 {
989
990 nRecordOffset =
992
993 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
994 psDBF->sHooks.FWrite( &ch, 1, 1, psDBF->fp );
995 }
996
997
999
1000
1001 psDBF->bNoHeader =
TRUE;
1003
1004 psDBF->nCurrentRecord = -1;
1005 psDBF->bCurrentRecordModified =
FALSE;
1006 psDBF->bUpdated =
TRUE;
1007
1008 return( psDBF->nFields-1 );
1009}
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