PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ 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(sizeof(int), psDBF->nFields));
1970  panFieldSizeNew = STATIC_CAST(int *, calloc(sizeof(int), psDBF->nFields));
1971  panFieldDecimalsNew = STATIC_CAST(int *, calloc(sizeof(int), psDBF->nFields));
1972  pachFieldTypeNew = STATIC_CAST(char *, calloc(sizeof(char), psDBF->nFields));
1973  pszHeaderNew = STATIC_CAST(char*, malloc(sizeof(char) * XBASE_FLDHDR_SZ *
1974  psDBF->nFields));
1975 
1976  /* shuffle fields definitions */
1977  for(i=0; i < psDBF->nFields; i++)
1978  {
1979  panFieldSizeNew[i] = psDBF->panFieldSize[panMap[i]];
1980  panFieldDecimalsNew[i] = psDBF->panFieldDecimals[panMap[i]];
1981  pachFieldTypeNew[i] = psDBF->pachFieldType[panMap[i]];
1982  memcpy(pszHeaderNew + i * XBASE_FLDHDR_SZ,
1983  psDBF->pszHeader + panMap[i] * XBASE_FLDHDR_SZ, XBASE_FLDHDR_SZ);
1984  }
1985  panFieldOffsetNew[0] = 1;
1986  for(i=1; i < psDBF->nFields; i++)
1987  {
1988  panFieldOffsetNew[i] = panFieldOffsetNew[i - 1] + panFieldSizeNew[i - 1];
1989  }
1990 
1991  free(psDBF->pszHeader);
1992  psDBF->pszHeader = pszHeaderNew;
1993 
1994  /* we're done if we're dealing with not yet created .dbf */
1995  if ( !(psDBF->bNoHeader && psDBF->nRecords == 0) )
1996  {
1997  /* force update of header with new header and record length */
1998  psDBF->bNoHeader = TRUE;
1999  DBFUpdateHeader( psDBF );
2000 
2001  /* alloc record */
2002  pszRecord = STATIC_CAST(char *, malloc(sizeof(char) * psDBF->nRecordLength));
2003  pszRecordNew = STATIC_CAST(char *, malloc(sizeof(char) * psDBF->nRecordLength));
2004 
2005  /* shuffle fields in records */
2006  for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
2007  {
2008  nRecordOffset =
2009  psDBF->nRecordLength * STATIC_CAST(SAOffset,iRecord) + psDBF->nHeaderLength;
2010 
2011  /* load record */
2012  psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
2013  psDBF->sHooks.FRead( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
2014 
2015  pszRecordNew[0] = pszRecord[0];
2016 
2017  for(i=0; i < psDBF->nFields; i++)
2018  {
2019  memcpy(pszRecordNew + panFieldOffsetNew[i],
2020  pszRecord + psDBF->panFieldOffset[panMap[i]],
2021  psDBF->panFieldSize[panMap[i]]);
2022  }
2023 
2024  /* write record */
2025  psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
2026  psDBF->sHooks.FWrite( pszRecordNew, psDBF->nRecordLength, 1, psDBF->fp );
2027  }
2028 
2029  /* free record */
2030  free(pszRecord);
2031  free(pszRecordNew);
2032  }
2033 
2034  free(psDBF->panFieldOffset);
2035  free(psDBF->panFieldSize);
2036  free(psDBF->panFieldDecimals);
2037  free(psDBF->pachFieldType);
2038 
2039  psDBF->panFieldOffset = panFieldOffsetNew;
2040  psDBF->panFieldSize = panFieldSizeNew;
2041  psDBF->panFieldDecimals =panFieldDecimalsNew;
2042  psDBF->pachFieldType = pachFieldTypeNew;
2043 
2044  psDBF->nCurrentRecord = -1;
2045  psDBF->bCurrentRecordModified = FALSE;
2046  psDBF->bUpdated = TRUE;
2047 
2048  return TRUE;
2049 }
#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: