1 : /******************************************************************************
2 : * $Id: vfkpropertydefn.cpp 25201 2012-11-04 11:11:31Z martinl $
3 : *
4 : * Project: VFK Reader - Data block property definition
5 : * Purpose: Implements VFKPropertyDefn class.
6 : * Author: Martin Landa, landa.martin gmail.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2009-2010, 2012, Martin Landa <landa.martin gmail.com>
10 : *
11 : * Permission is hereby granted, free of charge, to any person
12 : * obtaining a copy of this software and associated documentation
13 : * files (the "Software"), to deal in the Software without
14 : * restriction, including without limitation the rights to use, copy,
15 : * modify, merge, publish, distribute, sublicense, and/or sell copies
16 : * of the Software, and to permit persons to whom the Software is
17 : * furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be
20 : * included in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 : * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 : * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 : * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 : * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 : * SOFTWARE.
30 : ****************************************************************************/
31 :
32 : #include "vfkreader.h"
33 : #include "vfkreaderp.h"
34 :
35 : #include "cpl_conv.h"
36 : #include "cpl_error.h"
37 :
38 : /*!
39 : \brief VFKPropertyDefn constructor
40 :
41 : \param pszName property name
42 : \param pszType property type (original, string)
43 : \param bLatin2 TRUE for "ISO-8859-2" otherwise "WINDOWS-1250" is used (only for "text" type)
44 : */
45 1150 : VFKPropertyDefn::VFKPropertyDefn(const char *pszName, const char *pszType,
46 1150 : bool bLatin2)
47 : {
48 : char *poChar, *poWidth, *pszWidth;
49 : int nLength;
50 :
51 1150 : m_pszName = CPLStrdup(pszName);
52 1150 : m_pszType = CPLStrdup(pszType);
53 :
54 1150 : poWidth = poChar = m_pszType + 1;
55 1150 : for (nLength = 0; *poChar && *poChar != '.'; nLength++, poChar++)
56 : ;
57 :
58 : /* width */
59 1150 : pszWidth = (char *) CPLMalloc(nLength+1);
60 1150 : strncpy(pszWidth, poWidth, nLength);
61 1150 : pszWidth[nLength] = '\0';
62 :
63 1150 : m_nWidth = atoi(pszWidth);
64 1150 : CPLFree(pszWidth);
65 :
66 : /* precision */
67 1150 : m_nPrecision = 0;
68 :
69 : /* type */
70 1150 : m_pszEncoding = NULL;
71 1150 : if (*m_pszType == 'N') {
72 670 : if (*poChar == '.') {
73 60 : m_eFType = OFTReal;
74 60 : m_nPrecision = atoi(poChar+1);
75 : }
76 : else {
77 610 : if (m_nWidth < 10)
78 298 : m_eFType = OFTInteger;
79 : else {
80 312 : m_eFType = OFTString;
81 : }
82 : }
83 : }
84 480 : else if (*m_pszType == 'T') {
85 : /* string */
86 282 : m_eFType = OFTString;
87 282 : m_pszEncoding = bLatin2 ? CPLStrdup("ISO-8859-2") : CPLStrdup("WINDOWS-1250");
88 : }
89 198 : else if (*m_pszType == 'D') {
90 : /* date */
91 : /* m_eFType = OFTDateTime; */
92 198 : m_eFType = OFTString;
93 198 : m_nWidth = 25;
94 : }
95 : else {
96 : /* unknown - string */
97 0 : m_eFType = OFTString;
98 0 : m_pszEncoding = bLatin2 ? CPLStrdup("ISO-8859-2") : CPLStrdup("WINDOWS-1250");
99 : }
100 1150 : }
101 :
102 : /*!
103 : \brief VFKPropertyDefn destructor
104 : */
105 1150 : VFKPropertyDefn::~VFKPropertyDefn()
106 : {
107 1150 : CPLFree(m_pszName);
108 1150 : CPLFree(m_pszType);
109 1150 : if (m_pszEncoding)
110 282 : CPLFree(m_pszEncoding);
111 1150 : }
112 :
113 : /*!
114 : \brief Get SQL data type
115 :
116 : \return string with data type ("text" by default)
117 : */
118 575 : CPLString VFKPropertyDefn::GetTypeSQL() const
119 : {
120 575 : switch(m_eFType) {
121 : case OFTInteger:
122 149 : return CPLString("integer");
123 : case OFTReal:
124 30 : return CPLString("real");
125 : case OFTString:
126 396 : if (m_pszType[0] == 'N')
127 156 : return CPLString("integer");
128 : else
129 240 : return CPLString("text");
130 : default:
131 0 : return CPLString("text");
132 : }
133 : }
|