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 1797 : GMLPropertyDefn::GMLPropertyDefn( const char *pszName,
39 : const char *pszSrcElement )
40 :
41 : {
42 1797 : m_pszName = CPLStrdup( pszName );
43 1797 : if( pszSrcElement != NULL )
44 : {
45 1797 : m_nSrcElementLen = strlen( pszSrcElement );
46 1797 : m_pszSrcElement = CPLStrdup( pszSrcElement );
47 : }
48 : else
49 : {
50 0 : m_nSrcElementLen = 0;
51 0 : m_pszSrcElement = NULL;
52 : }
53 1797 : m_eType = GMLPT_Untyped;
54 1797 : m_nWidth = 0;
55 1797 : m_nPrecision = 0;
56 1797 : m_nIndex = -1;
57 1797 : }
58 :
59 : /************************************************************************/
60 : /* ~GMLPropertyDefn() */
61 : /************************************************************************/
62 :
63 1797 : GMLPropertyDefn::~GMLPropertyDefn()
64 :
65 : {
66 1797 : CPLFree( m_pszName );
67 1797 : CPLFree( m_pszSrcElement );
68 1797 : }
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 1368 : void GMLPropertyDefn::AnalysePropertyValue( const GMLProperty* psGMLProperty )
98 :
99 : {
100 : /* -------------------------------------------------------------------- */
101 : /* Does the string consist entirely of numeric values? */
102 : /* -------------------------------------------------------------------- */
103 1368 : int bIsReal = FALSE;
104 :
105 : int j;
106 2827 : for(j=0;j<psGMLProperty->nSubProperties;j++)
107 : {
108 1459 : if (j > 0)
109 : {
110 91 : if( m_eType == GMLPT_Integer )
111 0 : m_eType = GMLPT_IntegerList;
112 91 : else if( m_eType == GMLPT_Real )
113 0 : m_eType = GMLPT_RealList;
114 91 : else if( m_eType == GMLPT_String )
115 : {
116 5 : m_eType = GMLPT_StringList;
117 5 : m_nWidth = 0;
118 : }
119 : }
120 1459 : 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 1459 : if( *pszValue == '\0' )
126 8 : continue;
127 :
128 1451 : 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 1451 : 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 1503 : if (valueType == CPL_VALUE_STRING
146 : && m_eType != GMLPT_String
147 : && m_eType != GMLPT_StringList )
148 : {
149 52 : if( m_eType == GMLPT_IntegerList
150 : || m_eType == GMLPT_RealList )
151 0 : m_eType = GMLPT_StringList;
152 : else
153 52 : m_eType = GMLPT_String;
154 : }
155 : else
156 1399 : bIsReal = (valueType == CPL_VALUE_REAL);
157 :
158 1451 : if( m_eType == GMLPT_String )
159 : {
160 : /* grow the Width to the length of the string passed in */
161 : int nWidth;
162 661 : nWidth = strlen(pszValue);
163 661 : if ( m_nWidth < nWidth )
164 66 : SetWidth( nWidth );
165 : }
166 1282 : else if( m_eType == GMLPT_Untyped || m_eType == GMLPT_Integer )
167 : {
168 492 : if( bIsReal )
169 9 : m_eType = GMLPT_Real;
170 : else
171 483 : m_eType = GMLPT_Integer;
172 : }
173 298 : else if( m_eType == GMLPT_IntegerList && bIsReal )
174 : {
175 0 : m_eType = GMLPT_RealList;
176 : }
177 : }
178 1368 : }
|