PostGIS  2.2.7dev-r@@SVN_REVISION@@
int SHPAPI_CALL DBFReorderFields ( DBFHandle  psDBF,
int *  panMap 
)

Definition at line 1924 of file dbfopen.c.

References DBFFlushRecord(), DBFUpdateHeader(), FALSE, free(), malloc(), and TRUE.

1925 {
1926  SAOffset nRecordOffset;
1927  int i, iRecord;
1928  int *panFieldOffsetNew;
1929  int *panFieldSizeNew;
1930  int *panFieldDecimalsNew;
1931  char *pachFieldTypeNew;
1932  char *pszHeaderNew;
1933  char *pszRecord;
1934  char *pszRecordNew;
1935 
1936  if ( psDBF->nFields == 0 )
1937  return TRUE;
1938 
1939  /* make sure that everything is written in .dbf */
1940  if( !DBFFlushRecord( psDBF ) )
1941  return FALSE;
1942 
1943  panFieldOffsetNew = (int *) malloc(sizeof(int) * psDBF->nFields);
1944  panFieldSizeNew = (int *) malloc(sizeof(int) * psDBF->nFields);
1945  panFieldDecimalsNew = (int *) malloc(sizeof(int) * psDBF->nFields);
1946  pachFieldTypeNew = (char *) malloc(sizeof(char) * psDBF->nFields);
1947  pszHeaderNew = (char*) malloc(sizeof(char) * 32 * psDBF->nFields);
1948 
1949  /* shuffle fields definitions */
1950  for(i=0; i < psDBF->nFields; i++)
1951  {
1952  panFieldSizeNew[i] = psDBF->panFieldSize[panMap[i]];
1953  panFieldDecimalsNew[i] = psDBF->panFieldDecimals[panMap[i]];
1954  pachFieldTypeNew[i] = psDBF->pachFieldType[panMap[i]];
1955  memcpy(pszHeaderNew + i * 32,
1956  psDBF->pszHeader + panMap[i] * 32, 32);
1957  }
1958  panFieldOffsetNew[0] = 1;
1959  for(i=1; i < psDBF->nFields; i++)
1960  {
1961  panFieldOffsetNew[i] = panFieldOffsetNew[i - 1] + panFieldSizeNew[i - 1];
1962  }
1963 
1964  free(psDBF->pszHeader);
1965  psDBF->pszHeader = pszHeaderNew;
1966 
1967  /* we're done if we're dealing with not yet created .dbf */
1968  if ( !(psDBF->bNoHeader && psDBF->nRecords == 0) )
1969  {
1970  /* force update of header with new header and record length */
1971  psDBF->bNoHeader = TRUE;
1972  DBFUpdateHeader( psDBF );
1973 
1974  /* alloc record */
1975  pszRecord = (char *) malloc(sizeof(char) * psDBF->nRecordLength);
1976  pszRecordNew = (char *) malloc(sizeof(char) * psDBF->nRecordLength);
1977 
1978  /* shuffle fields in records */
1979  for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
1980  {
1981  nRecordOffset =
1982  psDBF->nRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
1983 
1984  /* load record */
1985  psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1986  psDBF->sHooks.FRead( pszRecord, psDBF->nRecordLength, 1, psDBF->fp );
1987 
1988  pszRecordNew[0] = pszRecord[0];
1989 
1990  for(i=0; i < psDBF->nFields; i++)
1991  {
1992  memcpy(pszRecordNew + panFieldOffsetNew[i],
1993  pszRecord + psDBF->panFieldOffset[panMap[i]],
1994  psDBF->panFieldSize[panMap[i]]);
1995  }
1996 
1997  /* write record */
1998  psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1999  psDBF->sHooks.FWrite( pszRecordNew, psDBF->nRecordLength, 1, psDBF->fp );
2000  }
2001 
2002  /* free record */
2003  free(pszRecord);
2004  free(pszRecordNew);
2005  }
2006 
2007  free(psDBF->panFieldOffset);
2008  free(psDBF->panFieldSize);
2009  free(psDBF->panFieldDecimals);
2010  free(psDBF->pachFieldType);
2011 
2012  psDBF->panFieldOffset = panFieldOffsetNew;
2013  psDBF->panFieldSize = panFieldSizeNew;
2014  psDBF->panFieldDecimals =panFieldDecimalsNew;
2015  psDBF->pachFieldType = pachFieldTypeNew;
2016 
2017  psDBF->nCurrentRecord = -1;
2018  psDBF->bCurrentRecordModified = FALSE;
2019 
2020  return TRUE;
2021 }
unsigned long SAOffset
Definition: shapefil.h:250
static int DBFFlushRecord(DBFHandle psDBF)
Definition: dbfopen.c:258
#define FALSE
Definition: dbfopen.c:168
void free(void *)
void * malloc(YYSIZE_T)
#define TRUE
Definition: dbfopen.c:169
void SHPAPI_CALL DBFUpdateHeader(DBFHandle psDBF)
Definition: dbfopen.c:334

Here is the call graph for this function: