PostGIS  2.1.10dev-r@@SVN_REVISION@@
const char SHPAPI_CALL1* DBFGetCodePage ( DBFHandle  psDBF)

Definition at line 1796 of file dbfopen.c.

1797 {
1798  if( psDBF == NULL )
1799  return NULL;
1800  return psDBF->pszCodePage;
1801 }
1802 
1803 /************************************************************************/
1804 /* DBFDeleteField() */
1805 /* */
1806 /* Remove a field from a .dbf file */
1807 /************************************************************************/
1808 
1809 int SHPAPI_CALL
1810 DBFDeleteField(DBFHandle psDBF, int iField)
1811 {
1812  int nOldRecordLength, nOldHeaderLength;
1813  int nDeletedFieldOffset, nDeletedFieldSize;
1814  SAOffset nRecordOffset;
1815  char* pszRecord;
1816  int i, iRecord;
1817 
1818  if (iField < 0 || iField >= psDBF->nFields)
1819  return FALSE;
1820 
1821  /* make sure that everything is written in .dbf */
1822  if( !DBFFlushRecord( psDBF ) )
1823  return FALSE;
1824 
1825  /* get information about field to be deleted */
1826  nOldRecordLength = psDBF->nRecordLength;
1827  nOldHeaderLength = psDBF->nHeaderLength;
1828  nDeletedFieldOffset = psDBF->panFieldOffset[iField];
1829  nDeletedFieldSize = psDBF->panFieldSize[iField];
1830 
1831  /* update fields info */
1832  for (i = iField + 1; i < psDBF->nFields; i++)
1833  {
1834  psDBF->panFieldOffset[i-1] = psDBF->panFieldOffset[i] - nDeletedFieldSize;
1835  psDBF->panFieldSize[i-1] = psDBF->panFieldSize[i];
1836  psDBF->panFieldDecimals[i-1] = psDBF->panFieldDecimals[i];
1837  psDBF->pachFieldType[i-1] = psDBF->pachFieldType[i];
1838  }
1839 
1840  /* resize fields arrays */
1841  psDBF->nFields--;
1842 
1843  psDBF->panFieldOffset = (int *)
1844  SfRealloc( psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
1845 
1846  psDBF->panFieldSize = (int *)
1847  SfRealloc( psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
1848 
1849  psDBF->panFieldDecimals = (int *)
1850  SfRealloc( psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
1851 
1852  psDBF->pachFieldType = (char *)
1853  SfRealloc( psDBF->pachFieldType, sizeof(char) * psDBF->nFields );
1854 
1855  /* update header information */
1856  psDBF->nHeaderLength -= 32;
1857  psDBF->nRecordLength -= nDeletedFieldSize;
1858 
1859  /* overwrite field information in header */
1860  memmove(psDBF->pszHeader + iField*32,
1861  psDBF->pszHeader + (iField+1)*32,
1862  sizeof(char) * (psDBF->nFields - iField)*32);
1863 
1864  psDBF->pszHeader = (char *) SfRealloc(psDBF->pszHeader,psDBF->nFields*32);
1865 
1866  /* update size of current record appropriately */
1867  psDBF->pszCurrentRecord = (char *) SfRealloc(psDBF->pszCurrentRecord,
1868  psDBF->nRecordLength);
1869 
1870  /* we're done if we're dealing with not yet created .dbf */
1871  if ( psDBF->bNoHeader && psDBF->nRecords == 0 )
1872  return TRUE;
1873 
1874  /* force update of header with new header and record length */
1875  psDBF->bNoHeader = TRUE;
1876  DBFUpdateHeader( psDBF );
1877 
1878  /* alloc record */
1879  pszRecord = (char *) malloc(sizeof(char) * nOldRecordLength);
1880 
1881  /* shift records to their new positions */
1882  for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
1883  {
1884  nRecordOffset =
1885  nOldRecordLength * (SAOffset) iRecord + nOldHeaderLength;
1886 
1887  /* load record */
1888  psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1889  psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
1890 
1891  nRecordOffset =
1892  psDBF->nRecordLength * (SAOffset) iRecord + psDBF->nHeaderLength;
1893 
1894  /* move record in two steps */
1895  psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1896  psDBF->sHooks.FWrite( pszRecord, nDeletedFieldOffset, 1, psDBF->fp );
1897  psDBF->sHooks.FWrite( pszRecord + nDeletedFieldOffset + nDeletedFieldSize,
1898  nOldRecordLength - nDeletedFieldOffset - nDeletedFieldSize,
1899  1, psDBF->fp );
1900 
1901  }
1902 
1903  /* TODO: truncate file */
1904 
1905  /* free record */
1906  free(pszRecord);
1907 
1908  psDBF->nCurrentRecord = -1;
1909  psDBF->bCurrentRecordModified = FALSE;
1910 
1911  return TRUE;
1912 }
static void * SfRealloc(void *pMem, int nNewSize)
Definition: dbfopen.c:180
unsigned long SAOffset
Definition: shapefil.h:251
static int DBFFlushRecord(DBFHandle psDBF)
Definition: dbfopen.c:259
#define SHPAPI_CALL
Definition: shapefil.h:208
#define FALSE
Definition: dbfopen.c:169
#define TRUE
Definition: dbfopen.c:170
int SHPAPI_CALL DBFDeleteField(DBFHandle hDBF, int iField)
void SHPAPI_CALL DBFUpdateHeader(DBFHandle psDBF)
Definition: dbfopen.c:335