PostGIS 3.6.2dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ DBFGetCodePage()

const char SHPAPI_CALL1 * DBFGetCodePage ( DBFHandle  psDBF)

Definition at line 1807 of file dbfopen.c.

1809{
1810 if( psDBF == SHPLIB_NULLPTR )
1811 return SHPLIB_NULLPTR;
1812 return psDBF->pszCodePage;
1813}
1814
1815/************************************************************************/
1816/* DBFDeleteField() */
1817/* */
1818/* Remove a field from a .dbf file */
1819/************************************************************************/
1820
1821int SHPAPI_CALL
1822DBFDeleteField(DBFHandle psDBF, int iField)
1823{
1824 int nOldRecordLength, nOldHeaderLength;
1825 int nDeletedFieldOffset, nDeletedFieldSize;
1826 SAOffset nRecordOffset;
1827 char* pszRecord;
1828 int i, iRecord;
1829
1830 if (iField < 0 || iField >= psDBF->nFields)
1831 return FALSE;
1832
1833 /* make sure that everything is written in .dbf */
1834 if( !DBFFlushRecord( psDBF ) )
1835 return FALSE;
1836
1837 /* get information about field to be deleted */
1838 nOldRecordLength = psDBF->nRecordLength;
1839 nOldHeaderLength = psDBF->nHeaderLength;
1840 nDeletedFieldOffset = psDBF->panFieldOffset[iField];
1841 nDeletedFieldSize = psDBF->panFieldSize[iField];
1842
1843 /* update fields info */
1844 for (i = iField + 1; i < psDBF->nFields; i++)
1845 {
1846 psDBF->panFieldOffset[i-1] = psDBF->panFieldOffset[i] - nDeletedFieldSize;
1847 psDBF->panFieldSize[i-1] = psDBF->panFieldSize[i];
1848 psDBF->panFieldDecimals[i-1] = psDBF->panFieldDecimals[i];
1849 psDBF->pachFieldType[i-1] = psDBF->pachFieldType[i];
1850 }
1851
1852 /* resize fields arrays */
1853 psDBF->nFields--;
1854
1855 psDBF->panFieldOffset = STATIC_CAST(int *,
1856 SfRealloc( psDBF->panFieldOffset, sizeof(int) * psDBF->nFields ));
1857
1858 psDBF->panFieldSize = STATIC_CAST(int *,
1859 SfRealloc( psDBF->panFieldSize, sizeof(int) * psDBF->nFields ));
1860
1861 psDBF->panFieldDecimals = STATIC_CAST(int *,
1862 SfRealloc( psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields ));
1863
1864 psDBF->pachFieldType = STATIC_CAST(char *,
1865 SfRealloc( psDBF->pachFieldType, sizeof(char) * psDBF->nFields ));
1866
1867 /* update header information */
1869 psDBF->nRecordLength -= nDeletedFieldSize;
1870
1871 /* overwrite field information in header */
1872 memmove(psDBF->pszHeader + iField*XBASE_FLDHDR_SZ,
1873 psDBF->pszHeader + (iField+1)*XBASE_FLDHDR_SZ,
1874 sizeof(char) * (psDBF->nFields - iField)*XBASE_FLDHDR_SZ);
1875
1876 psDBF->pszHeader = STATIC_CAST(char *, SfRealloc(psDBF->pszHeader,
1877 psDBF->nFields*XBASE_FLDHDR_SZ));
1878
1879 /* update size of current record appropriately */
1881 psDBF->nRecordLength));
1882
1883 /* we're done if we're dealing with not yet created .dbf */
1884 if ( psDBF->bNoHeader && psDBF->nRecords == 0 )
1885 return TRUE;
1886
1887 /* force update of header with new header and record length */
1888 psDBF->bNoHeader = TRUE;
1889 DBFUpdateHeader( psDBF );
1890
1891 /* alloc record */
1892 pszRecord = STATIC_CAST(char *, malloc(sizeof(char) * nOldRecordLength));
1893
1894 /* shift records to their new positions */
1895 for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
1896 {
1897 nRecordOffset =
1898 nOldRecordLength * STATIC_CAST(SAOffset,iRecord) + nOldHeaderLength;
1899
1900 /* load record */
1901 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1902 psDBF->sHooks.FRead( pszRecord, nOldRecordLength, 1, psDBF->fp );
1903
1904 nRecordOffset =
1905 psDBF->nRecordLength * STATIC_CAST(SAOffset,iRecord) + psDBF->nHeaderLength;
1906
1907 /* move record in two steps */
1908 psDBF->sHooks.FSeek( psDBF->fp, nRecordOffset, 0 );
1909 psDBF->sHooks.FWrite( pszRecord, nDeletedFieldOffset, 1, psDBF->fp );
1910 psDBF->sHooks.FWrite( pszRecord + nDeletedFieldOffset + nDeletedFieldSize,
1911 nOldRecordLength - nDeletedFieldOffset - nDeletedFieldSize,
1912 1, psDBF->fp );
1913
1914 }
1915
1916 if( psDBF->bWriteEndOfFileChar )
1917 {
1918 char ch = END_OF_FILE_CHARACTER;
1919 SAOffset nEOFOffset =
1920 psDBF->nRecordLength * STATIC_CAST(SAOffset,psDBF->nRecords) + psDBF->nHeaderLength;
1921
1922 psDBF->sHooks.FSeek( psDBF->fp, nEOFOffset, 0 );
1923 psDBF->sHooks.FWrite( &ch, 1, 1, psDBF->fp );
1924 }
1925
1926 /* TODO: truncate file */
1927
1928 /* free record */
1929 free(pszRecord);
1930
1931 psDBF->nCurrentRecord = -1;
1933 psDBF->bUpdated = TRUE;
1934
1935 return TRUE;
1936}
static void * SfRealloc(void *pMem, int nNewSize)
Definition dbfopen.c:109
#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
#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.