OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimCplUtil.cpp
Go to the documentation of this file.
1 /*
2  * Copied from "gdal" project. See licence below.
3  *
4  * Global functions for gdal code compatibility.
5  *
6  * Project: ISO 8211 Access
7  * Purpose: Main declarations for ISO 8211.
8  * Author: Frank Warmerdam, warmerdam@pobox.com
9  *
10  ******************************************************************************
11  * Copyright (c) 1999, Frank Warmerdam <warmerdam@pobox.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ******************************************************************************
31  * $Id&
32  */
33 
34 #include <cstdlib>
35 #include <cstring>
36 
38 #include <ossim/base/ossimString.h>
39 
40 #ifndef OSSIM_CSLT_HONOURSTRINGS
41 # define OSSIM_CSLT_HONOURSTRINGS 0x0001
42 #endif
43 #ifndef OSSIM_CSLT_ALLOWEMPTYTOKENS
44 # define OSSIM_CSLT_ALLOWEMPTYTOKENS 0x0002
45 #endif
46 #ifndef OSSIM_CSLT_PRESERVEQUOTES
47 # define OSSIM_CSLT_PRESERVEQUOTES 0x0004
48 #endif
49 #ifndef OSSIM_CSLT_PRESERVEESCAPES
50 # define OSSIM_CSLT_PRESERVEESCAPES 0x0008
51 #endif
52 
53 /************************************************************************/
54 /* ossimCSLFindString() */
55 /* */
56 /* Find a string within a string list. The string must match */
57 /* the full length, but the comparison is case insensitive. */
58 /* Return -1 on failure. */
59 /************************************************************************/
60 
61 int ossimCSLFindString( char ** papszList, const char * pszTarget )
62 
63 {
64  int i;
65 
66  if( papszList == NULL )
67  return -1;
68 
69  for( i = 0; papszList[i] != NULL; i++ )
70  {
71  ossimString s = papszList[i];
72  if( s == pszTarget)
73  return i;
74  }
75 
76  return -1;
77 }
78 
79 /**********************************************************************
80  * ossimCSLTokenizeString()
81  *
82  * Tokenizes a string and returns a StringList with one string for
83  * each token.
84  **********************************************************************/
85 
86 char **ossimCSLTokenizeString( const char *pszString )
87 {
88  return ossimCSLTokenizeString2( pszString, " ", OSSIM_CSLT_HONOURSTRINGS );
89 }
90 
91 /************************************************************************/
92 /* ossimCSLTokenizeStringComplex() */
93 /* */
94 /* Obsolete tokenizing api. */
95 /************************************************************************/
96 
97 char ** ossimCSLTokenizeStringComplex( const char * pszString,
98  const char * pszDelimiters,
99  int bHonourStrings,
100  int bAllowEmptyTokens )
101 
102 {
103  int nFlags = 0;
104 
105  if( bHonourStrings )
106  nFlags |= OSSIM_CSLT_HONOURSTRINGS;
107  if( bAllowEmptyTokens )
108  nFlags |= OSSIM_CSLT_ALLOWEMPTYTOKENS;
109 
110  return ossimCSLTokenizeString2( pszString, pszDelimiters, nFlags );
111 }
112 
113 /************************************************************************/
114 /* ossimCSLTokenizeString2() */
115 /* */
116 /* The ultimate tokenizer? */
117 /************************************************************************/
118 
119 char ** ossimCSLTokenizeString2( const char * pszString,
120  const char * pszDelimiters,
121  int nCSLTFlags )
122 
123 {
124  char **papszRetList = NULL;
125  int nRetMax = 0, nRetLen = 0;
126  char *pszToken;
127  int nTokenMax, nTokenLen;
128  int bHonourStrings = (nCSLTFlags & OSSIM_CSLT_HONOURSTRINGS);
129  int bAllowEmptyTokens = (nCSLTFlags & OSSIM_CSLT_ALLOWEMPTYTOKENS);
130 
131  pszToken = (char *) calloc(10,1);
132  nTokenMax = 10;
133 
134  while( pszString != NULL && *pszString != '\0' )
135  {
136  int bInString = false;
137 
138  nTokenLen = 0;
139 
140  /* Try to find the next delimeter, marking end of token */
141  for( ; *pszString != '\0'; pszString++ )
142  {
143 
144  /* End if this is a delimeter skip it and break. */
145  if( !bInString && strchr(pszDelimiters, *pszString) != NULL )
146  {
147  pszString++;
148  break;
149  }
150 
151  /* If this is a quote, and we are honouring constant
152  strings, then process the constant strings, with out delim
153  but don't copy over the quotes */
154  if( bHonourStrings && *pszString == '"' )
155  {
156  if( nCSLTFlags & OSSIM_CSLT_PRESERVEQUOTES )
157  {
158  pszToken[nTokenLen] = *pszString;
159  nTokenLen++;
160  }
161 
162  if( bInString )
163  {
164  bInString = false;
165  continue;
166  }
167  else
168  {
169  bInString = true;
170  continue;
171  }
172  }
173 
174  /* Within string constants we allow for escaped quotes, but
175  in processing them we will unescape the quotes */
176  if( bInString && pszString[0] == '\\' && pszString[1] == '"' )
177  {
178  if( nCSLTFlags & OSSIM_CSLT_PRESERVEESCAPES )
179  {
180  pszToken[nTokenLen] = *pszString;
181  nTokenLen++;
182  }
183 
184  pszString++;
185  }
186 
187  /* Within string constants a \\ sequence reduces to \ */
188  else if( bInString
189  && pszString[0] == '\\' && pszString[1] == '\\' )
190  {
191  if( nCSLTFlags & OSSIM_CSLT_PRESERVEESCAPES )
192  {
193  pszToken[nTokenLen] = *pszString;
194  nTokenLen++;
195  }
196  pszString++;
197  }
198 
199  if( nTokenLen >= nTokenMax-3 )
200  {
201  nTokenMax = nTokenMax * 2 + 10;
202  pszToken = (char *) ossimCPLRealloc( pszToken, nTokenMax );
203  }
204 
205  pszToken[nTokenLen] = *pszString;
206  nTokenLen++;
207  }
208 
209  pszToken[nTokenLen] = '\0';
210 
211  /*
212  * If the last token is an empty token, then we have to catch
213  * it now, otherwise we won't reenter the loop and it will be lost.
214  */
215 
216  if( (pszToken[0] != '\0' || bAllowEmptyTokens)
217  || (*pszString == '\0' && bAllowEmptyTokens
218  && strchr(pszDelimiters, *(pszString-1)) ) )
219  {
220  if( nRetLen >= nRetMax - 1 )
221  {
222  nRetMax = nRetMax * 2 + 10;
223  papszRetList = (char **)
224  ossimCPLRealloc(papszRetList, sizeof(char*) * nRetMax );
225  }
226 
227  papszRetList[nRetLen++] = strdup( pszToken );
228  papszRetList[nRetLen] = NULL;
229  }
230  }
231 
232  if( papszRetList == NULL )
233  papszRetList = (char **) calloc(sizeof(char *),1);
234 
235  free( pszToken );
236 
237  return papszRetList;
238 }
239 
240 /**********************************************************************
241  * ossimCSLCount()
242  *
243  * Return the number of lines in a Stringlist.
244  **********************************************************************/
245 int ossimCSLCount(char **papszStrList)
246 {
247  int nItems=0;
248 
249  if (papszStrList)
250  {
251  while(*papszStrList != NULL)
252  {
253  nItems++;
254  papszStrList++;
255  }
256  }
257 
258  return nItems;
259 }
260 
261 /**********************************************************************
262  * ossimCSLDestroy()
263  *
264  * Free all memory used by a StringList.
265  **********************************************************************/
266 void ossimCSLDestroy(char **papszStrList)
267 {
268  char **papszPtr;
269 
270  if (papszStrList)
271  {
272  papszPtr = papszStrList;
273  while(*papszPtr != NULL)
274  {
275  free(*papszPtr);
276  papszPtr++;
277  }
278 
279  free(papszStrList);
280  }
281 }
282 
283 void * ossimCPLRealloc( void * pData, size_t nNewSize )
284 
285 {
286  void *pReturn;
287 
288  if ( nNewSize == 0 )
289  {
290  free(pData);
291  return NULL;
292  }
293 
294  if( pData == NULL )
295  pReturn = malloc( nNewSize );
296  else
297  pReturn = realloc( pData, nNewSize );
298 
299  return pReturn;
300 }
#define OSSIM_CSLT_PRESERVEQUOTES
int ossimCSLCount(char **papszStrList)
#define OSSIM_CSLT_PRESERVEESCAPES
char ** ossimCSLTokenizeStringComplex(const char *pszString, const char *pszDelimiters, int bHonourStrings, int bAllowEmptyTokens)
#define OSSIM_CSLT_HONOURSTRINGS
#define OSSIM_CSLT_ALLOWEMPTYTOKENS
void * ossimCPLRealloc(void *pData, size_t nNewSize)
int ossimCSLFindString(char **papszList, const char *pszTarget)
char ** ossimCSLTokenizeString2(const char *pszString, const char *pszDelimiters, int nCSLTFlags)
char ** ossimCSLTokenizeString(const char *pszString)
void ossimCSLDestroy(char **papszStrList)