PostGIS 3.6.2dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ DBFReorderFields()

int SHPAPI_CALL DBFReorderFields ( DBFHandle  psDBF,
int *  panMap 
)

Definition at line 1949 of file dbfopen.c.

1950{
1951 SAOffset nRecordOffset;
1952 int i, iRecord;
1953 int *panFieldOffsetNew;
1954 int *panFieldSizeNew;
1955 int *panFieldDecimalsNew;
1956 char *pachFieldTypeNew;
1957 char *pszHeaderNew;
1958 char *pszRecord;
1959 char *pszRecordNew;
1960
1961 if ( psDBF->nFields == 0 )
1962 return TRUE;
1963
1964 /* make sure that everything is written in .dbf */
1965 if( !DBFFlushRecord( psDBF ) )
1966 return FALSE;
1967
1968 /* a simple malloc() would be enough, but calloc() helps clang static analyzer */
1969 panFieldOffsetNew = STATIC_CAST(int *, calloc(psDBF->nFields, sizeof(int)));
1970 panFieldSizeNew = STATIC_CAST(int *, calloc(psDBF->nFields, sizeof(int)));
1971 panFieldDecimalsNew = STATIC_CAST(int *, calloc(psDBF->nFields, sizeof(int)));
1972 pachFieldTypeNew = STATIC_CAST(char *, calloc(psDBF->nFields, sizeof(char)));
1973 pszHeaderNew = STATIC_CAST(char*, malloc(sizeof(char) * XBASE_FLDHDR_SZ * psDBF->nFields));
1974
1975 /* shuffle fields definitions */
1976 for(i=0; i < psDBF->nFields; i++)
1977 {
1978 panFieldSizeNew[i] = psDBF->panFieldSize[panMap[i]];
1979 panFieldDecimalsNew[i] = psDBF->panFieldDecimals[panMap[i]];
1980 pachFieldTypeNew[i] = psDBF->pachFieldType[panMap[i]];
1981 memcpy(pszHeaderNew + i * XBASE_FLDHDR_SZ,
1982 psDBF->pszHeader + panMap[i] * XBASE_FLDHDR_SZ, XBASE_FLDHDR_SZ);
1983 }
1984 panFieldOffsetNew[0] = 1;
1985 for(i=1; i < psDBF->nFields; i++)
1986 {
1987 panFieldOffsetNew[i] = panFieldOffsetNew[i - 1] + panFieldSizeNew[i - 1];
1988 }
1989
1990 free(psDBF->pszHeader);
1991 psDBF->pszHeader = pszHeaderNew;
1992
1993 /* we're done if we're dealing with not yet created .dbf */
1994 if ( !(psDBF->bNoHeader && psDBF->nRecords == 0) )
1995 {
1996 /* force update of header with new header and record length */
1997 psDBF->bNoHeader = TRUE;
1998 DBFUpdateHeader( psDBF );
1999
2000 /* alloc record */
2001 pszRecord = STATIC_CAST(char *, malloc(sizeof(char) * psDBF->nRecordLength));
2002 pszRecordNew = STATIC_CAST(char *, malloc(sizeof(char) * psDBF->nRecordLength));
2003
2004 /* shuffle fields in records */
2005 for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
2006 {
2007 nRecordOffset =
2008 psDBF->nRecordLength * STATIC_CAST(SAOffset,iRecord) + psDBF->nHeaderLength;
2009
2010 /* load record */
2011 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
2012 psDBF->sHooks.FRead( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
2013
2014 pszRecordNew[0] = pszRecord[0];
2015
2016 for(i=0; i < psDBF->nFields; i++)
2017 {
2018 memcpy(pszRecordNew + panFieldOffsetNew[i],
2019 pszRecord + psDBF->panFieldOffset[panMap[i]],
2020 psDBF->panFieldSize[panMap[i]]);
2021 }
2022
2023 /* write record */
2024 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
2025 psDBF->sHooks.FWrite( pszRecordNew, psDBF->nRecordLength, 1, psDBF->fp );
2026 }
2027
2028 /* free record */
2029 free(pszRecord);
2030 free(pszRecordNew);
2031 }
2032
2033 free(psDBF->panFieldOffset);
2034 free(psDBF->panFieldSize);
2035 free(psDBF->panFieldDecimals);
2036 free(psDBF->pachFieldType);
2037
2038 psDBF->panFieldOffset = panFieldOffsetNew;
2039 psDBF->panFieldSize = panFieldSizeNew;
2040 psDBF->panFieldDecimals =panFieldDecimalsNew;
2041 psDBF->pachFieldType = pachFieldTypeNew;
2042
2043 psDBF->nCurrentRecord = -1;
2045 psDBF->bUpdated = TRUE;
2046
2047 return TRUE;
2048}
#define STATIC_CAST(type, x)
Definition dbfopen.c:96
#define TRUE
Definition dbfopen.c:73
#define FALSE
Definition dbfopen.c:72
static int DBFFlushRecord(DBFHandle psDBF)
Definition dbfopen.c:195
void SHPAPI_CALL DBFUpdateHeader(DBFHandle psDBF)
Definition dbfopen.c:304
void * malloc(YYSIZE_T)
void free(void *)
#define XBASE_FLDHDR_SZ
Definition shapefil.h:648
unsigned long SAOffset
Definition shapefil.h:286
int nRecordLength
Definition shapefil.h:596
int * panFieldOffset
Definition shapefil.h:601
int * panFieldDecimals
Definition shapefil.h:603
int nFields
Definition shapefil.h:600
int bUpdated
Definition shapefil.h:616
int nHeaderLength
Definition shapefil.h:597
int * panFieldSize
Definition shapefil.h:602
int bCurrentRecordModified
Definition shapefil.h:609
char * pszHeader
Definition shapefil.h:606
SAFile fp
Definition shapefil.h:592
char * pachFieldType
Definition shapefil.h:604
SAHooks sHooks
Definition shapefil.h:590
int bNoHeader
Definition shapefil.h:615
int nRecords
Definition shapefil.h:594
int nCurrentRecord
Definition shapefil.h:608
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition shapefil.h:292
SAOffset(* FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition shapefil.h:291
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)
Definition shapefil.h:293

References DBFInfo::bCurrentRecordModified, DBFInfo::bNoHeader, DBFInfo::bUpdated, DBFFlushRecord(), DBFUpdateHeader(), FALSE, DBFInfo::fp, SAHooks::FRead, free(), SAHooks::FSeek, SAHooks::FWrite, malloc(), DBFInfo::nCurrentRecord, DBFInfo::nFields, DBFInfo::nHeaderLength, DBFInfo::nRecordLength, DBFInfo::nRecords, DBFInfo::pachFieldType, DBFInfo::panFieldDecimals, DBFInfo::panFieldOffset, DBFInfo::panFieldSize, DBFInfo::pszHeader, DBFInfo::sHooks, STATIC_CAST, TRUE, and XBASE_FLDHDR_SZ.

Here is the call graph for this function: