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

◆ DBFReorderFields()

int SHPAPI_CALL DBFReorderFields ( DBFHandle  psDBF,
int *  panMap 
)

Definition at line 1958 of file dbfopen.c.

1959{
1960 SAOffset nRecordOffset;
1961 int i, iRecord;
1962 int *panFieldOffsetNew;
1963 int *panFieldSizeNew;
1964 int *panFieldDecimalsNew;
1965 char *pachFieldTypeNew;
1966 char *pszHeaderNew;
1967 char *pszRecord;
1968 char *pszRecordNew;
1969
1970 if ( psDBF->nFields == 0 )
1971 return TRUE;
1972
1973 /* make sure that everything is written in .dbf */
1974 if( !DBFFlushRecord( psDBF ) )
1975 return FALSE;
1976
1977 /* a simple malloc() would be enough, but calloc() helps clang static analyzer */
1978 panFieldOffsetNew = STATIC_CAST(int *, calloc(psDBF->nFields, sizeof(int)));
1979 panFieldSizeNew = STATIC_CAST(int *, calloc(psDBF->nFields, sizeof(int)));
1980 panFieldDecimalsNew = STATIC_CAST(int *, calloc(psDBF->nFields, sizeof(int)));
1981 pachFieldTypeNew = STATIC_CAST(char *, calloc(psDBF->nFields, sizeof(char)));
1982 pszHeaderNew = STATIC_CAST(char*, malloc(sizeof(char) * XBASE_FLDHDR_SZ * psDBF->nFields));
1983
1984 if( panFieldOffsetNew == SHPLIB_NULLPTR || panFieldSizeNew == SHPLIB_NULLPTR ||
1985 panFieldDecimalsNew == SHPLIB_NULLPTR || pachFieldTypeNew == SHPLIB_NULLPTR ||
1986 pszHeaderNew == SHPLIB_NULLPTR )
1987 {
1988 free( panFieldOffsetNew );
1989 free( panFieldSizeNew );
1990 free( panFieldDecimalsNew );
1991 free( pachFieldTypeNew );
1992 free( pszHeaderNew );
1993 return FALSE;
1994 }
1995
1996 /* shuffle fields definitions */
1997 for(i=0; i < psDBF->nFields; i++)
1998 {
1999 panFieldSizeNew[i] = psDBF->panFieldSize[panMap[i]];
2000 panFieldDecimalsNew[i] = psDBF->panFieldDecimals[panMap[i]];
2001 pachFieldTypeNew[i] = psDBF->pachFieldType[panMap[i]];
2002 memcpy(pszHeaderNew + i * XBASE_FLDHDR_SZ,
2003 psDBF->pszHeader + panMap[i] * XBASE_FLDHDR_SZ, XBASE_FLDHDR_SZ);
2004 }
2005 panFieldOffsetNew[0] = 1;
2006 for(i=1; i < psDBF->nFields; i++)
2007 {
2008 panFieldOffsetNew[i] = panFieldOffsetNew[i - 1] + panFieldSizeNew[i - 1];
2009 }
2010
2011 free(psDBF->pszHeader);
2012 psDBF->pszHeader = pszHeaderNew;
2013
2014 /* we're done if we're dealing with not yet created .dbf */
2015 if ( !(psDBF->bNoHeader && psDBF->nRecords == 0) )
2016 {
2017 /* force update of header with new header and record length */
2018 psDBF->bNoHeader = TRUE;
2019 DBFUpdateHeader( psDBF );
2020
2021 /* alloc record */
2022 pszRecord = STATIC_CAST(char *, malloc(sizeof(char) * psDBF->nRecordLength));
2023 pszRecordNew = STATIC_CAST(char *, malloc(sizeof(char) * psDBF->nRecordLength));
2024
2025 /* shuffle fields in records */
2026 for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
2027 {
2028 nRecordOffset =
2029 psDBF->nRecordLength * STATIC_CAST(SAOffset,iRecord) + psDBF->nHeaderLength;
2030
2031 /* load record */
2032 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
2033 psDBF->sHooks.FRead( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
2034
2035 pszRecordNew[0] = pszRecord[0];
2036
2037 for(i=0; i < psDBF->nFields; i++)
2038 {
2039 memcpy(pszRecordNew + panFieldOffsetNew[i],
2040 pszRecord + psDBF->panFieldOffset[panMap[i]],
2041 psDBF->panFieldSize[panMap[i]]);
2042 }
2043
2044 /* write record */
2045 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
2046 psDBF->sHooks.FWrite( pszRecordNew, psDBF->nRecordLength, 1, psDBF->fp );
2047 }
2048
2049 /* free record */
2050 free(pszRecord);
2051 free(pszRecordNew);
2052 }
2053
2054 free(psDBF->panFieldOffset);
2055 free(psDBF->panFieldSize);
2056 free(psDBF->panFieldDecimals);
2057 free(psDBF->pachFieldType);
2058
2059 psDBF->panFieldOffset = panFieldOffsetNew;
2060 psDBF->panFieldSize = panFieldSizeNew;
2061 psDBF->panFieldDecimals =panFieldDecimalsNew;
2062 psDBF->pachFieldType = pachFieldTypeNew;
2063
2064 psDBF->nCurrentRecord = -1;
2066 psDBF->bUpdated = TRUE;
2067
2068 return TRUE;
2069}
#define STATIC_CAST(type, x)
Definition dbfopen.c:96
#define TRUE
Definition dbfopen.c:73
#define FALSE
Definition dbfopen.c:72
#define SHPLIB_NULLPTR
Definition dbfopen.c:99
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, SHPLIB_NULLPTR, STATIC_CAST, TRUE, and XBASE_FLDHDR_SZ.

Here is the call graph for this function: