1 : /******************************************************************************
2 : * $Id: vfkpropertydefn.cpp 24217 2012-04-11 14:25:09Z 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 : */
44 1150 : VFKPropertyDefn::VFKPropertyDefn(const char *pszName, const char *pszType)
45 : {
46 : char *poChar, *poWidth, *pszWidth;
47 : int nLength;
48 :
49 1150 : m_pszName = CPLStrdup(pszName);
50 1150 : m_pszType = CPLStrdup(pszType);
51 :
52 1150 : poWidth = poChar = m_pszType + 1;
53 1150 : for (nLength = 0; *poChar && *poChar != '.'; nLength++, poChar++)
54 : ;
55 :
56 : /* width */
57 1150 : pszWidth = (char *) CPLMalloc(nLength+1);
58 1150 : strncpy(pszWidth, poWidth, nLength);
59 1150 : pszWidth[nLength] = '\0';
60 :
61 1150 : m_nWidth = atoi(pszWidth);
62 1150 : CPLFree(pszWidth);
63 :
64 : /* precision */
65 1150 : m_nPrecision = 0;
66 :
67 : /* type */
68 1150 : if (*m_pszType == 'N') {
69 670 : if (*poChar == '.') {
70 60 : m_eFType = OFTReal;
71 60 : m_nPrecision = atoi(poChar+1);
72 : }
73 : else {
74 610 : if (m_nWidth < 10)
75 298 : m_eFType = OFTInteger;
76 : else {
77 312 : m_eFType = OFTString;
78 : }
79 : }
80 : }
81 480 : else if (*m_pszType == 'T') {
82 : /* string */
83 282 : m_eFType = OFTString;
84 : }
85 198 : else if (*m_pszType == 'D') {
86 : /* date */
87 : /* m_eFType = OFTDateTime; */
88 198 : m_eFType = OFTString;
89 198 : m_nWidth = 25;
90 : }
91 : else {
92 : /* unknown - string */
93 0 : m_eFType = OFTString;
94 : }
95 1150 : }
96 :
97 : /*!
98 : \brief VFKPropertyDefn destructor
99 : */
100 1150 : VFKPropertyDefn::~VFKPropertyDefn()
101 : {
102 1150 : CPLFree(m_pszName);
103 1150 : CPLFree(m_pszType);
104 1150 : }
105 :
106 : /*!
107 : \brief Get SQL data type
108 :
109 : \return string with data type ("text" by default)
110 : */
111 575 : CPLString VFKPropertyDefn::GetTypeSQL() const
112 : {
113 575 : switch(m_eFType) {
114 : case OFTInteger:
115 149 : return CPLString("integer");
116 : case OFTReal:
117 30 : return CPLString("real");
118 : case OFTString:
119 396 : if (m_pszType[0] == 'N')
120 156 : return CPLString("integer");
121 : else
122 240 : return CPLString("text");
123 : default:
124 0 : return CPLString("text");
125 : }
126 : }
|