1 : /**********************************************************************
2 : * $Id: gmlpropertydefn.cpp 22954 2011-08-19 21:47:19Z rouault $
3 : *
4 : * Project: GML Reader
5 : * Purpose: Implementation of GMLPropertyDefn
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : **********************************************************************
9 : * Copyright (c) 2002, Frank Warmerdam
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "gmlreader.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 :
34 : /************************************************************************/
35 : /* GMLPropertyDefn */
36 : /************************************************************************/
37 :
38 2106 : GMLPropertyDefn::GMLPropertyDefn( const char *pszName,
39 : const char *pszSrcElement )
40 :
41 : {
42 2106 : m_pszName = CPLStrdup( pszName );
43 2106 : if( pszSrcElement != NULL )
44 : {
45 2106 : m_nSrcElementLen = strlen( pszSrcElement );
46 2106 : m_pszSrcElement = CPLStrdup( pszSrcElement );
47 : }
48 : else
49 : {
50 0 : m_nSrcElementLen = 0;
51 0 : m_pszSrcElement = NULL;
52 : }
53 2106 : m_eType = GMLPT_Untyped;
54 2106 : m_nWidth = 0;
55 2106 : m_nPrecision = 0;
56 2106 : m_nIndex = -1;
57 2106 : }
58 :
59 : /************************************************************************/
60 : /* ~GMLPropertyDefn() */
61 : /************************************************************************/
62 :
63 2106 : GMLPropertyDefn::~GMLPropertyDefn()
64 :
65 : {
66 2106 : CPLFree( m_pszName );
67 2106 : CPLFree( m_pszSrcElement );
68 2106 : }
69 :
70 : /************************************************************************/
71 : /* SetSrcElement() */
72 : /************************************************************************/
73 :
74 0 : void GMLPropertyDefn::SetSrcElement( const char *pszSrcElement )
75 :
76 : {
77 0 : CPLFree( m_pszSrcElement );
78 0 : if( pszSrcElement != NULL )
79 : {
80 0 : m_nSrcElementLen = strlen( pszSrcElement );
81 0 : m_pszSrcElement = CPLStrdup( pszSrcElement );
82 : }
83 : else
84 : {
85 0 : m_nSrcElementLen = 0;
86 0 : m_pszSrcElement = NULL;
87 : }
88 0 : }
89 :
90 : /************************************************************************/
91 : /* AnalysePropertyValue() */
92 : /* */
93 : /* Examine the passed property value, and see if we need to */
94 : /* make the field type more specific, or more general. */
95 : /************************************************************************/
96 :
97 2238 : void GMLPropertyDefn::AnalysePropertyValue( const GMLProperty* psGMLProperty )
98 :
99 : {
100 : /* -------------------------------------------------------------------- */
101 : /* Does the string consist entirely of numeric values? */
102 : /* -------------------------------------------------------------------- */
103 2238 : int bIsReal = FALSE;
104 :
105 : int j;
106 4559 : for(j=0;j<psGMLProperty->nSubProperties;j++)
107 : {
108 2321 : if (j > 0)
109 : {
110 83 : if( m_eType == GMLPT_Integer )
111 0 : m_eType = GMLPT_IntegerList;
112 83 : else if( m_eType == GMLPT_Real )
113 0 : m_eType = GMLPT_RealList;
114 83 : else if( m_eType == GMLPT_String )
115 : {
116 3 : m_eType = GMLPT_StringList;
117 3 : m_nWidth = 0;
118 : }
119 : }
120 2321 : const char* pszValue = psGMLProperty->papszSubProperties[j];
121 : /* -------------------------------------------------------------------- */
122 : /* If it is a zero length string, just return. We can't deduce */
123 : /* much from this. */
124 : /* -------------------------------------------------------------------- */
125 2321 : if( *pszValue == '\0' )
126 8 : continue;
127 :
128 2313 : CPLValueType valueType = CPLGetValueType(pszValue);
129 :
130 : /* This might not fit into a int32. For now, let's */
131 : /* consider this as a real value then. */
132 : /* FIXME once RFC31 / 64 bit support is set, we could */
133 : /* choose a different behaviour */
134 2313 : if (valueType == CPL_VALUE_INTEGER && strlen(pszValue) >= 10)
135 : {
136 : /* Skip leading spaces */
137 0 : while( isspace( (unsigned char)*pszValue ) )
138 0 : pszValue ++;
139 : char szVal[32];
140 0 : sprintf(szVal, "%d", atoi(pszValue));
141 0 : if (strcmp(pszValue, szVal) != 0)
142 0 : valueType = CPL_VALUE_REAL;
143 : }
144 :
145 2375 : if (valueType == CPL_VALUE_STRING
146 : && m_eType != GMLPT_String
147 : && m_eType != GMLPT_StringList )
148 : {
149 62 : if( m_eType == GMLPT_IntegerList
150 : || m_eType == GMLPT_RealList )
151 0 : m_eType = GMLPT_StringList;
152 : else
153 62 : m_eType = GMLPT_String;
154 : }
155 : else
156 2251 : bIsReal = (valueType == CPL_VALUE_REAL);
157 :
158 2313 : if( m_eType == GMLPT_String )
159 : {
160 : /* grow the Width to the length of the string passed in */
161 : int nWidth;
162 1033 : nWidth = strlen(pszValue);
163 1033 : if ( m_nWidth < nWidth )
164 78 : SetWidth( nWidth );
165 : }
166 2276 : else if( m_eType == GMLPT_Untyped || m_eType == GMLPT_Integer )
167 : {
168 996 : if( bIsReal )
169 9 : m_eType = GMLPT_Real;
170 : else
171 987 : m_eType = GMLPT_Integer;
172 : }
173 284 : else if( m_eType == GMLPT_IntegerList && bIsReal )
174 : {
175 0 : m_eType = GMLPT_RealList;
176 : }
177 : }
178 2238 : }
|