/src/gdal/ogr/ogrsf_frmts/vfk/vfkpropertydefn.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: VFK Reader - Data block property definition |
4 | | * Purpose: Implements VFKPropertyDefn class. |
5 | | * Author: Martin Landa, landa.martin gmail.com |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2009-2010, 2012, Martin Landa <landa.martin gmail.com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "vfkreader.h" |
14 | | #include "vfkreaderp.h" |
15 | | |
16 | | #include "cpl_conv.h" |
17 | | #include "cpl_error.h" |
18 | | |
19 | | /*! |
20 | | \brief VFKPropertyDefn constructor |
21 | | |
22 | | \param pszName property name |
23 | | \param pszType property type (original, string) |
24 | | \param pszEncoding encoding (only for "text" type) |
25 | | */ |
26 | | VFKPropertyDefn::VFKPropertyDefn(const char *pszName, const char *pszType, |
27 | | const char *pszEncoding) |
28 | 57.5k | : m_pszName(CPLStrdup(pszName)), m_pszType(CPLStrdup(pszType)), |
29 | 57.5k | m_pszEncoding(nullptr), m_nWidth(0), m_nPrecision(0) |
30 | 57.5k | { |
31 | 57.5k | char *poWidth = m_pszType + 1; |
32 | 57.5k | char *poChar = m_pszType + 1; |
33 | 57.5k | int nLength = 0; // Used after for. |
34 | 356k | for (; *poChar && *poChar != '.'; nLength++, poChar++) |
35 | 298k | ; |
36 | | |
37 | 57.5k | char *pszWidth = static_cast<char *>(CPLMalloc(nLength + 1)); |
38 | 57.5k | strncpy(pszWidth, poWidth, nLength); |
39 | 57.5k | pszWidth[nLength] = '\0'; |
40 | | |
41 | 57.5k | m_nWidth = atoi(pszWidth); |
42 | 57.5k | CPLFree(pszWidth); |
43 | | |
44 | | // Type. |
45 | 57.5k | if (*m_pszType == 'N') |
46 | 17.6k | { |
47 | 17.6k | if (*poChar == '.') |
48 | 2.19k | { |
49 | 2.19k | m_eFType = OFTReal; |
50 | 2.19k | m_nPrecision = atoi(poChar + 1); |
51 | 2.19k | } |
52 | 15.4k | else |
53 | 15.4k | { |
54 | 15.4k | if (m_nWidth < 10) |
55 | 7.06k | m_eFType = OFTInteger; |
56 | 8.41k | else |
57 | 8.41k | { |
58 | 8.41k | m_eFType = OFTInteger64; |
59 | 8.41k | } |
60 | 15.4k | } |
61 | 17.6k | } |
62 | 39.9k | else if (*m_pszType == 'T') |
63 | 5.56k | { |
64 | | // String. |
65 | 5.56k | m_eFType = OFTString; |
66 | 5.56k | m_pszEncoding = CPLStrdup(pszEncoding); |
67 | 5.56k | } |
68 | 34.3k | else if (*m_pszType == 'D') |
69 | 7.89k | { |
70 | | // Date. |
71 | | // m_eFType = OFTDateTime; |
72 | 7.89k | m_eFType = OFTString; |
73 | 7.89k | m_nWidth = 25; |
74 | 7.89k | } |
75 | 26.4k | else |
76 | 26.4k | { |
77 | | // Unknown - string. |
78 | 26.4k | m_eFType = OFTString; |
79 | 26.4k | m_pszEncoding = CPLStrdup(pszEncoding); |
80 | 26.4k | } |
81 | 57.5k | } |
82 | | |
83 | | /*! |
84 | | \brief VFKPropertyDefn destructor |
85 | | */ |
86 | | VFKPropertyDefn::~VFKPropertyDefn() |
87 | 57.5k | { |
88 | 57.5k | CPLFree(m_pszName); |
89 | 57.5k | CPLFree(m_pszType); |
90 | 57.5k | if (m_pszEncoding) |
91 | 32.0k | CPLFree(m_pszEncoding); |
92 | 57.5k | } |
93 | | |
94 | | /*! |
95 | | \brief Get SQL data type |
96 | | |
97 | | \return string with data type ("text" by default) |
98 | | */ |
99 | | CPLString VFKPropertyDefn::GetTypeSQL() const |
100 | 7.57k | { |
101 | 7.57k | switch (m_eFType) |
102 | 7.57k | { |
103 | 1.39k | case OFTInteger: |
104 | 1.39k | return CPLString("integer"); |
105 | 1.79k | case OFTInteger64: |
106 | 1.79k | return CPLString("bigint"); |
107 | 331 | case OFTReal: |
108 | 331 | return CPLString("real"); |
109 | 4.05k | case OFTString: |
110 | 4.05k | return CPLString("text"); |
111 | 0 | default: |
112 | 0 | return CPLString("text"); |
113 | 7.57k | } |
114 | 7.57k | } |