PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ DBFGetCodePage()

const char SHPAPI_CALL1* DBFGetCodePage ( DBFHandle  psDBF)

Definition at line 1799 of file dbfopen.c.

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

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