Line data Source code
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 : * Permission is hereby granted, free of charge, to any person 11 : * obtaining a copy of this software and associated documentation 12 : * files (the "Software"), to deal in the Software without 13 : * restriction, including without limitation the rights to use, copy, 14 : * modify, merge, publish, distribute, sublicense, and/or sell copies 15 : * of the Software, and to permit persons to whom the Software is 16 : * furnished to do so, subject to the following conditions: 17 : * 18 : * The above copyright notice and this permission notice shall be 19 : * included in all copies or substantial portions of the Software. 20 : * 21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 : * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 25 : * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 26 : * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 27 : * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 : * SOFTWARE. 29 : ****************************************************************************/ 30 : 31 : #include "vfkreader.h" 32 : #include "vfkreaderp.h" 33 : 34 : #include "cpl_conv.h" 35 : #include "cpl_error.h" 36 : 37 : /*! 38 : \brief VFKPropertyDefn constructor 39 : 40 : \param pszName property name 41 : \param pszType property type (original, string) 42 : \param pszEncoding encoding (only for "text" type) 43 : */ 44 9232 : VFKPropertyDefn::VFKPropertyDefn(const char *pszName, const char *pszType, 45 9232 : const char *pszEncoding) 46 27696 : : m_pszName(CPLStrdup(pszName)), m_pszType(CPLStrdup(pszType)), 47 9232 : m_pszEncoding(nullptr), m_nWidth(0), m_nPrecision(0) 48 : { 49 9232 : char *poWidth = m_pszType + 1; 50 9232 : char *poChar = m_pszType + 1; 51 9232 : int nLength = 0; // Used after for. 52 21696 : for (; *poChar && *poChar != '.'; nLength++, poChar++) 53 : ; 54 : 55 9232 : char *pszWidth = static_cast<char *>(CPLMalloc(nLength + 1)); 56 9232 : strncpy(pszWidth, poWidth, nLength); 57 9232 : pszWidth[nLength] = '\0'; 58 : 59 9232 : m_nWidth = atoi(pszWidth); 60 9232 : CPLFree(pszWidth); 61 : 62 : // Type. 63 9232 : if (*m_pszType == 'N') 64 : { 65 5328 : if (*poChar == '.') 66 : { 67 480 : m_eFType = OFTReal; 68 480 : m_nPrecision = atoi(poChar + 1); 69 : } 70 : else 71 : { 72 4848 : if (m_nWidth < 10) 73 2384 : m_eFType = OFTInteger; 74 : else 75 : { 76 2464 : m_eFType = OFTInteger64; 77 : } 78 : } 79 : } 80 3904 : else if (*m_pszType == 'T') 81 : { 82 : // String. 83 2288 : m_eFType = OFTString; 84 2288 : m_pszEncoding = CPLStrdup(pszEncoding); 85 : } 86 1616 : else if (*m_pszType == 'D') 87 : { 88 : // Date. 89 : // m_eFType = OFTDateTime; 90 1616 : m_eFType = OFTString; 91 1616 : m_nWidth = 25; 92 : } 93 : else 94 : { 95 : // Unknown - string. 96 0 : m_eFType = OFTString; 97 0 : m_pszEncoding = CPLStrdup(pszEncoding); 98 : } 99 9232 : } 100 : 101 : /*! 102 : \brief VFKPropertyDefn destructor 103 : */ 104 27696 : VFKPropertyDefn::~VFKPropertyDefn() 105 : { 106 9232 : CPLFree(m_pszName); 107 9232 : CPLFree(m_pszType); 108 9232 : if (m_pszEncoding) 109 2288 : CPLFree(m_pszEncoding); 110 18464 : } 111 : 112 : /*! 113 : \brief Get SQL data type 114 : 115 : \return string with data type ("text" by default) 116 : */ 117 8655 : CPLString VFKPropertyDefn::GetTypeSQL() const 118 : { 119 8655 : switch (m_eFType) 120 : { 121 2235 : case OFTInteger: 122 2235 : return CPLString("integer"); 123 2310 : case OFTInteger64: 124 2310 : return CPLString("bigint"); 125 450 : case OFTReal: 126 450 : return CPLString("real"); 127 3660 : case OFTString: 128 3660 : return CPLString("text"); 129 0 : default: 130 0 : return CPLString("text"); 131 : } 132 : }