1 : /**********************************************************************
2 : * $Id: gmlreadstate.cpp 10645 2007-01-18 02:22:39Z warmerdam $
3 : *
4 : * Project: GML Reader
5 : * Purpose: Implementation of GMLReadState class.
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 "gmlreaderp.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 :
34 : /************************************************************************/
35 : /* GMLReadState() */
36 : /************************************************************************/
37 :
38 79 : GMLReadState::GMLReadState()
39 :
40 : {
41 79 : m_poFeature = NULL;
42 79 : m_poParentState = NULL;
43 :
44 79 : m_pszPath = CPLStrdup("");
45 79 : m_nPathLength = 0;
46 79 : m_papszPathComponents = NULL;
47 79 : }
48 :
49 : /************************************************************************/
50 : /* ~GMLReadState() */
51 : /************************************************************************/
52 :
53 79 : GMLReadState::~GMLReadState()
54 :
55 : {
56 79 : CPLFree( m_pszPath );
57 79 : for( int i = 0; i < m_nPathLength; i++ )
58 0 : CPLFree( m_papszPathComponents[i] );
59 79 : CPLFree( m_papszPathComponents );
60 79 : }
61 :
62 : /************************************************************************/
63 : /* PushPath() */
64 : /************************************************************************/
65 :
66 646 : void GMLReadState::PushPath( const char *pszElement )
67 :
68 : {
69 646 : m_nPathLength++;
70 646 : m_papszPathComponents = CSLAddString( m_papszPathComponents, pszElement );
71 :
72 646 : RebuildPath();
73 646 : }
74 :
75 : /************************************************************************/
76 : /* PopPath() */
77 : /************************************************************************/
78 :
79 646 : void GMLReadState::PopPath()
80 :
81 : {
82 : CPLAssert( m_nPathLength > 0 );
83 646 : if( m_nPathLength <= 0 )
84 0 : return;
85 :
86 646 : CPLFree( m_papszPathComponents[m_nPathLength-1] );
87 646 : m_papszPathComponents[--m_nPathLength] = NULL;
88 :
89 646 : RebuildPath();
90 : }
91 :
92 : /************************************************************************/
93 : /* RebuildPath() */
94 : /************************************************************************/
95 :
96 1292 : void GMLReadState::RebuildPath()
97 :
98 : {
99 1292 : int nLength=0, i;
100 :
101 3700 : for( i = 0; i < m_nPathLength; i++ )
102 2408 : nLength += strlen(m_papszPathComponents[i]) + 1;
103 :
104 1292 : m_pszPath = (char *) CPLRealloc(m_pszPath, nLength );
105 :
106 1292 : nLength = 0;
107 3700 : for( i = 0; i < m_nPathLength; i++ )
108 : {
109 2408 : if( i > 0 )
110 1375 : m_pszPath[nLength++] = '|';
111 :
112 2408 : strcpy( m_pszPath + nLength, m_papszPathComponents[i] );
113 2408 : nLength += strlen(m_papszPathComponents[i]);
114 : }
115 1292 : }
116 :
117 : /************************************************************************/
118 : /* GetLastComponent() */
119 : /************************************************************************/
120 :
121 1212 : const char *GMLReadState::GetLastComponent() const
122 :
123 : {
124 1212 : if( m_nPathLength == 0 )
125 259 : return "";
126 : else
127 953 : return m_papszPathComponents[m_nPathLength-1];
128 : }
129 :
130 : /************************************************************************/
131 : /* MatchPath() */
132 : /* */
133 : /* Compare the passed in path to the current one and see if */
134 : /* they match. It is assumed that the passed in path may */
135 : /* contain one or more elements and must match the tail of the */
136 : /* current path. However, the passed in path does not need all */
137 : /* the components of the current read state path. */
138 : /* */
139 : /* Returns TRUE if the paths match. */
140 : /************************************************************************/
141 :
142 0 : int GMLReadState::MatchPath( const char *pszPathIn )
143 :
144 : {
145 : int iOffset;
146 0 : int nInputLength = strlen(pszPathIn);
147 0 : int nInternalLength = strlen(m_pszPath);
148 :
149 0 : if( nInputLength > nInternalLength )
150 0 : return FALSE;
151 :
152 0 : iOffset = nInternalLength - nInputLength;
153 0 : if( iOffset > 0 && m_pszPath[iOffset-1] != '|' )
154 0 : return FALSE;
155 :
156 0 : return strcmp(pszPathIn,m_pszPath + iOffset) == 0;
157 : }
|