PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ DBFReorderFields()

int SHPAPI_CALL DBFReorderFields ( DBFHandle  psDBF,
int *  panMap 
)

Definition at line 1928 of file dbfopen.c.

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

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