PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ DBFGetCodePage()

const char SHPAPI_CALL1* DBFGetCodePage ( DBFHandle  psDBF)

Definition at line 1816 of file dbfopen.c.

1818 {
1819  if( psDBF == SHPLIB_NULLPTR )
1820  return SHPLIB_NULLPTR;
1821  return psDBF->pszCodePage;
1822 }
1823 
1824 /************************************************************************/
1825 /* DBFDeleteField() */
1826 /* */
1827 /* Remove a field from a .dbf file */
1828 /************************************************************************/
1829 
1830 int SHPAPI_CALL
1831 DBFDeleteField(DBFHandle psDBF, int iField)
1832 {
1833  int nOldRecordLength, nOldHeaderLength;
1834  int nDeletedFieldOffset, nDeletedFieldSize;
1835  SAOffset nRecordOffset;
1836  char* pszRecord;
1837  int i, iRecord;
1838 
1839  if (iField < 0 || iField >= psDBF->nFields)
1840  return FALSE;
1841 
1842  /* make sure that everything is written in .dbf */
1843  if( !DBFFlushRecord( psDBF ) )
1844  return FALSE;
1845 
1846  /* get information about field to be deleted */
1847  nOldRecordLength = psDBF->nRecordLength;
1848  nOldHeaderLength = psDBF->nHeaderLength;
1849  nDeletedFieldOffset = psDBF->panFieldOffset[iField];
1850  nDeletedFieldSize = psDBF->panFieldSize[iField];
1851 
1852  /* update fields info */
1853  for (i = iField + 1; i < psDBF->nFields; i++)
1854  {
1855  psDBF->panFieldOffset[i-1] = psDBF->panFieldOffset[i] - nDeletedFieldSize;
1856  psDBF->panFieldSize[i-1] = psDBF->panFieldSize[i];
1857  psDBF->panFieldDecimals[i-1] = psDBF->panFieldDecimals[i];
1858  psDBF->pachFieldType[i-1] = psDBF->pachFieldType[i];
1859  }
1860 
1861  /* resize fields arrays */
1862  psDBF->nFields--;
1863 
1864  psDBF->panFieldOffset = STATIC_CAST(int *,
1865  SfRealloc( psDBF->panFieldOffset, sizeof(int) * psDBF->nFields ));
1866 
1867  psDBF->panFieldSize = STATIC_CAST(int *,
1868  SfRealloc( psDBF->panFieldSize, sizeof(int) * psDBF->nFields ));
1869 
1870  psDBF->panFieldDecimals = STATIC_CAST(int *,
1871  SfRealloc( psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields ));
1872 
1873  psDBF->pachFieldType = STATIC_CAST(char *,
1874  SfRealloc( psDBF->pachFieldType, sizeof(char) * psDBF->nFields ));
1875 
1876  /* update header information */
1877  psDBF->nHeaderLength -= XBASE_FLDHDR_SZ;
1878  psDBF->nRecordLength -= nDeletedFieldSize;
1879 
1880  /* overwrite field information in header */
1881  memmove(psDBF->pszHeader + iField*XBASE_FLDHDR_SZ,
1882  psDBF->pszHeader + (iField+1)*XBASE_FLDHDR_SZ,
1883  sizeof(char) * (psDBF->nFields - iField)*XBASE_FLDHDR_SZ);
1884 
1885  psDBF->pszHeader = STATIC_CAST(char *, SfRealloc(psDBF->pszHeader,
1886  psDBF->nFields*XBASE_FLDHDR_SZ));
1887 
1888  /* update size of current record appropriately */
1889  psDBF->pszCurrentRecord = STATIC_CAST(char *, SfRealloc(psDBF->pszCurrentRecord,
1890  psDBF->nRecordLength));
1891 
1892  /* we're done if we're dealing with not yet created .dbf */
1893  if ( psDBF->bNoHeader && psDBF->nRecords == 0 )
1894  return TRUE;
1895 
1896  /* force update of header with new header and record length */
1897  psDBF->bNoHeader = TRUE;
1898  DBFUpdateHeader( psDBF );
1899 
1900  /* alloc record */
1901  pszRecord = STATIC_CAST(char *, malloc(sizeof(char) * nOldRecordLength));
1902 
1903  /* shift records to their new positions */
1904  for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
1905  {
1906  nRecordOffset =
1907  nOldRecordLength * STATIC_CAST(SAOffset,iRecord) + nOldHeaderLength;
1908 
1909  /* load record */
1910  psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1911  psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
1912 
1913  nRecordOffset =
1914  psDBF->nRecordLength * STATIC_CAST(SAOffset,iRecord) + psDBF->nHeaderLength;
1915 
1916  /* move record in two steps */
1917  psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1918  psDBF->sHooks.FWrite( pszRecord, nDeletedFieldOffset, 1, psDBF->fp );
1919  psDBF->sHooks.FWrite( pszRecord + nDeletedFieldOffset + nDeletedFieldSize,
1920  nOldRecordLength - nDeletedFieldOffset - nDeletedFieldSize,
1921  1, psDBF->fp );
1922 
1923  }
1924 
1925  if( psDBF->bWriteEndOfFileChar )
1926  {
1927  char ch = END_OF_FILE_CHARACTER;
1928  SAOffset nEOFOffset =
1929  psDBF->nRecordLength * STATIC_CAST(SAOffset,psDBF->nRecords) + psDBF->nHeaderLength;
1930 
1931  psDBF->sHooks.FSeek( psDBF->fp, nEOFOffset, 0 );
1932  psDBF->sHooks.FWrite( &ch, 1, 1, psDBF->fp );
1933  }
1934 
1935  /* TODO: truncate file */
1936 
1937  /* free record */
1938  free(pszRecord);
1939 
1940  psDBF->nCurrentRecord = -1;
1941  psDBF->bCurrentRecordModified = FALSE;
1942  psDBF->bUpdated = TRUE;
1943 
1944  return TRUE;
1945 }
#define STATIC_CAST(type, x)
Definition: dbfopen.c:96
#define TRUE
Definition: dbfopen.c:73
#define FALSE
Definition: dbfopen.c:72
#define END_OF_FILE_CHARACTER
Definition: dbfopen.c:82
static void * SfRealloc(void *pMem, int nNewSize)
Definition: dbfopen.c:109
#define SHPLIB_NULLPTR
Definition: dbfopen.c:99
static int DBFFlushRecord(DBFHandle psDBF)
Definition: dbfopen.c:195
void SHPAPI_CALL DBFUpdateHeader(DBFHandle psDBF)
Definition: dbfopen.c:304
void * malloc(YYSIZE_T)
void free(void *)
int SHPAPI_CALL DBFDeleteField(DBFHandle hDBF, int iField)
#define XBASE_FLDHDR_SZ
Definition: shapefil.h:648
#define SHPAPI_CALL
Definition: shapefil.h:248
unsigned long SAOffset
Definition: shapefil.h:286
int bWriteEndOfFileChar
Definition: shapefil.h:631
int nRecordLength
Definition: shapefil.h:596
int * panFieldOffset
Definition: shapefil.h:601
int * panFieldDecimals
Definition: shapefil.h:603
char * pszCodePage
Definition: shapefil.h:625
int nFields
Definition: shapefil.h:600
int bUpdated
Definition: shapefil.h:616
int nHeaderLength
Definition: shapefil.h:597
int * panFieldSize
Definition: shapefil.h:602
char * pszCurrentRecord
Definition: shapefil.h:610
int bCurrentRecordModified
Definition: shapefil.h:609
char * pszHeader
Definition: shapefil.h:606
SAFile fp
Definition: shapefil.h:592
char * pachFieldType
Definition: shapefil.h:604
SAHooks sHooks
Definition: shapefil.h:590
int bNoHeader
Definition: shapefil.h:615
int nRecords
Definition: shapefil.h:594
int nCurrentRecord
Definition: shapefil.h:608
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:292
SAOffset(* FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:291
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)
Definition: shapefil.h:293

References SHPLIB_NULLPTR.