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

Definition at line 1925 of file dbfopen.c.

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

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

Here is the call graph for this function: