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

◆ DBFGetCodePage()

const char SHPAPI_CALL1 * DBFGetCodePage ( DBFHandle  psDBF)

Definition at line 1739 of file dbfopen.c.

1740{
1741 if (psDBF == NULL)
1742 return NULL;
1743 return psDBF->pszCodePage;
1744}
1745
1746/************************************************************************/
1747/* DBFDeleteField() */
1748/* */
1749/* Remove a field from a .dbf file */
1750/************************************************************************/
1751
1752int SHPAPI_CALL
1753DBFDeleteField(DBFHandle psDBF, int iField)
1754{
1755 int nOldRecordLength, nOldHeaderLength;
1756 int nDeletedFieldOffset, nDeletedFieldSize;
1757 SAOffset nRecordOffset;
1758 char *pszRecord;
1759 int i, iRecord;
1760
1761 if (iField < 0 || iField >= psDBF->nFields)
1762 return FALSE;
1763
1764 /* make sure that everything is written in .dbf */
1765 if (!DBFFlushRecord(psDBF))
1766 return FALSE;
1767
1768 /* get information about field to be deleted */
1769 nOldRecordLength = psDBF->nRecordLength;
1770 nOldHeaderLength = psDBF->nHeaderLength;
1771 nDeletedFieldOffset = psDBF->panFieldOffset[iField];
1772 nDeletedFieldSize = psDBF->panFieldSize[iField];
1773
1774 /* update fields info */
1775 for (i = iField + 1; i < psDBF->nFields; i++)
1776 {
1777 psDBF->panFieldOffset[i - 1] = psDBF->panFieldOffset[i] - nDeletedFieldSize;
1778 psDBF->panFieldSize[i - 1] = psDBF->panFieldSize[i];
1779 psDBF->panFieldDecimals[i - 1] = psDBF->panFieldDecimals[i];
1780 psDBF->pachFieldType[i - 1] = psDBF->pachFieldType[i];
1781 }
1782
1783 /* resize fields arrays */
1784 psDBF->nFields--;
1785
1786 psDBF->panFieldOffset = (int *)SfRealloc(psDBF->panFieldOffset, sizeof(int) * psDBF->nFields);
1787
1788 psDBF->panFieldSize = (int *)SfRealloc(psDBF->panFieldSize, sizeof(int) * psDBF->nFields);
1789
1790 psDBF->panFieldDecimals = (int *)SfRealloc(psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields);
1791
1792 psDBF->pachFieldType = (char *)SfRealloc(psDBF->pachFieldType, sizeof(char) * psDBF->nFields);
1793
1794 /* update header information */
1795 psDBF->nHeaderLength -= 32;
1796 psDBF->nRecordLength -= nDeletedFieldSize;
1797
1798 /* overwrite field information in header */
1799 memmove(psDBF->pszHeader + iField * 32,
1800 psDBF->pszHeader + (iField + 1) * 32,
1801 sizeof(char) * (psDBF->nFields - iField) * 32);
1802
1803 psDBF->pszHeader = (char *)SfRealloc(psDBF->pszHeader, psDBF->nFields * 32);
1804
1805 /* update size of current record appropriately */
1806 psDBF->pszCurrentRecord = (char *)SfRealloc(psDBF->pszCurrentRecord, psDBF->nRecordLength);
1807
1808 /* we're done if we're dealing with not yet created .dbf */
1809 if (psDBF->bNoHeader && psDBF->nRecords == 0)
1810 return TRUE;
1811
1812 /* force update of header with new header and record length */
1813 psDBF->bNoHeader = TRUE;
1814 DBFUpdateHeader(psDBF);
1815
1816 /* alloc record */
1817 pszRecord = (char *)malloc(sizeof(char) * nOldRecordLength);
1818
1819 /* shift records to their new positions */
1820 for (iRecord = 0; iRecord < psDBF->nRecords; iRecord++)
1821 {
1822 nRecordOffset = nOldRecordLength * (SAOffset)iRecord + nOldHeaderLength;
1823
1824 /* load record */
1825 psDBF->sHooks.FSeek(psDBF->fp, nRecordOffset, 0);
1826 psDBF->sHooks.FRead(pszRecord, nOldRecordLength, 1, psDBF->fp);
1827
1828 nRecordOffset = psDBF->nRecordLength * (SAOffset)iRecord + psDBF->nHeaderLength;
1829
1830 /* move record in two steps */
1831 psDBF->sHooks.FSeek(psDBF->fp, nRecordOffset, 0);
1832 psDBF->sHooks.FWrite(pszRecord, nDeletedFieldOffset, 1, psDBF->fp);
1833 psDBF->sHooks.FWrite(pszRecord + nDeletedFieldOffset + nDeletedFieldSize,
1834 nOldRecordLength - nDeletedFieldOffset - nDeletedFieldSize,
1835 1,
1836 psDBF->fp);
1837 }
1838
1839 /* TODO: truncate file */
1840
1841 /* free record */
1842 free(pszRecord);
1843
1844 psDBF->nCurrentRecord = -1;
1845 psDBF->bCurrentRecordModified = FALSE;
1846
1847 return TRUE;
1848}
static void * SfRealloc(void *pMem, int nNewSize)
Definition dbfopen.c:180
#define TRUE
Definition dbfopen.c:169
#define FALSE
Definition dbfopen.c:168
static int DBFFlushRecord(DBFHandle psDBF)
Definition dbfopen.c:258
void SHPAPI_CALL DBFUpdateHeader(DBFHandle psDBF)
Definition dbfopen.c:324
void * malloc(YYSIZE_T)
void free(void *)
int SHPAPI_CALL DBFDeleteField(DBFHandle hDBF, int iField)
#define SHPAPI_CALL
Definition shapefil.h:207
unsigned long SAOffset
Definition shapefil.h:250