1 : /**********************************************************************
2 : * $Id: cpl_string.h 18103 2009-11-25 21:03:23Z rouault $
3 : *
4 : * Name: cpl_string.h
5 : * Project: CPL - Common Portability Library
6 : * Purpose: String and StringList functions.
7 : * Author: Daniel Morissette, dmorissette@mapgears.com
8 : *
9 : **********************************************************************
10 : * Copyright (c) 1998, Daniel Morissette
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #ifndef _CPL_STRING_H_INCLUDED
32 : #define _CPL_STRING_H_INCLUDED
33 :
34 : #include "cpl_vsi.h"
35 : #include "cpl_error.h"
36 : #include "cpl_conv.h"
37 :
38 : /**
39 : * \file cpl_string.h
40 : *
41 : * Various convenience functions for working with strings and string lists.
42 : *
43 : * A StringList is just an array of strings with the last pointer being
44 : * NULL. An empty StringList may be either a NULL pointer, or a pointer to
45 : * a pointer memory location with a NULL value.
46 : *
47 : * A common convention for StringLists is to use them to store name/value
48 : * lists. In this case the contents are treated like a dictionary of
49 : * name/value pairs. The actual data is formatted with each string having
50 : * the format "<name>:<value>" (though "=" is also an acceptable separator).
51 : * A number of the functions in the file operate on name/value style
52 : * string lists (such as CSLSetNameValue(), and CSLFetchNameValue()).
53 : *
54 : */
55 :
56 : CPL_C_START
57 :
58 : char CPL_DLL **CSLAddString(char **papszStrList, const char *pszNewString);
59 : int CPL_DLL CSLCount(char **papszStrList);
60 : const char CPL_DLL *CSLGetField( char **, int );
61 : void CPL_DLL CPL_STDCALL CSLDestroy(char **papszStrList);
62 : char CPL_DLL **CSLDuplicate(char **papszStrList);
63 : char CPL_DLL **CSLMerge( char **papszOrig, char **papszOverride );
64 :
65 : char CPL_DLL **CSLTokenizeString(const char *pszString );
66 : char CPL_DLL **CSLTokenizeStringComplex(const char *pszString,
67 : const char *pszDelimiter,
68 : int bHonourStrings, int bAllowEmptyTokens );
69 : char CPL_DLL **CSLTokenizeString2( const char *pszString,
70 : const char *pszDelimeter,
71 : int nCSLTFlags );
72 :
73 : #define CSLT_HONOURSTRINGS 0x0001
74 : #define CSLT_ALLOWEMPTYTOKENS 0x0002
75 : #define CSLT_PRESERVEQUOTES 0x0004
76 : #define CSLT_PRESERVEESCAPES 0x0008
77 : #define CSLT_STRIPLEADSPACES 0x0010
78 : #define CSLT_STRIPENDSPACES 0x0020
79 :
80 : int CPL_DLL CSLPrint(char **papszStrList, FILE *fpOut);
81 : char CPL_DLL **CSLLoad(const char *pszFname);
82 : char CPL_DLL **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, char** papszOptions);
83 : int CPL_DLL CSLSave(char **papszStrList, const char *pszFname);
84 :
85 : char CPL_DLL **CSLInsertStrings(char **papszStrList, int nInsertAtLineNo,
86 : char **papszNewLines);
87 : char CPL_DLL **CSLInsertString(char **papszStrList, int nInsertAtLineNo,
88 : const char *pszNewLine);
89 : char CPL_DLL **CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete,
90 : int nNumToRemove, char ***ppapszRetStrings);
91 : int CPL_DLL CSLFindString( char **, const char * );
92 : int CPL_DLL CSLPartialFindString( char **papszHaystack,
93 : const char * pszNeedle );
94 : int CPL_DLL CSLFindName(char **papszStrList, const char *pszName);
95 : int CPL_DLL CSLTestBoolean( const char *pszValue );
96 : int CPL_DLL CSLFetchBoolean( char **papszStrList, const char *pszKey,
97 : int bDefault );
98 :
99 : const char CPL_DLL *CPLSPrintf(const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(1, 2);
100 : char CPL_DLL **CSLAppendPrintf(char **papszStrList, const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3);
101 : int CPL_DLL CPLVASPrintf(char **buf, const char *fmt, va_list args );
102 :
103 : const char CPL_DLL *
104 : CPLParseNameValue(const char *pszNameValue, char **ppszKey );
105 : const char CPL_DLL *
106 : CSLFetchNameValue(char **papszStrList, const char *pszName);
107 : const char CPL_DLL *
108 : CSLFetchNameValueDef(char **papszStrList, const char *pszName,
109 : const char *pszDefault );
110 : char CPL_DLL **
111 : CSLFetchNameValueMultiple(char **papszStrList, const char *pszName);
112 : char CPL_DLL **
113 : CSLAddNameValue(char **papszStrList,
114 : const char *pszName, const char *pszValue);
115 : char CPL_DLL **
116 : CSLSetNameValue(char **papszStrList,
117 : const char *pszName, const char *pszValue);
118 : void CPL_DLL CSLSetNameValueSeparator( char ** papszStrList,
119 : const char *pszSeparator );
120 :
121 : #define CPLES_BackslashQuotable 0
122 : #define CPLES_XML 1
123 : #define CPLES_URL 2 /* unescape only for now */
124 : #define CPLES_SQL 3
125 : #define CPLES_CSV 4
126 :
127 : char CPL_DLL *CPLEscapeString( const char *pszString, int nLength,
128 : int nScheme );
129 : char CPL_DLL *CPLUnescapeString( const char *pszString, int *pnLength,
130 : int nScheme );
131 :
132 : char CPL_DLL *CPLBinaryToHex( int nBytes, const GByte *pabyData );
133 : GByte CPL_DLL *CPLHexToBinary( const char *pszHex, int *pnBytes );
134 :
135 : typedef enum
136 : {
137 : CPL_VALUE_STRING,
138 : CPL_VALUE_REAL,
139 : CPL_VALUE_INTEGER
140 : } CPLValueType;
141 :
142 : CPLValueType CPL_DLL CPLGetValueType(const char* pszValue);
143 :
144 : size_t CPL_DLL CPLStrlcpy(char* pszDest, const char* pszSrc, size_t nDestSize);
145 : size_t CPL_DLL CPLStrlcat(char* pszDest, const char* pszSrc, size_t nDestSize);
146 : size_t CPL_DLL CPLStrnlen (const char *pszStr, size_t nMaxLen);
147 :
148 : /* -------------------------------------------------------------------- */
149 : /* RFC 23 character set conversion/recoding API (cpl_recode.cpp). */
150 : /* -------------------------------------------------------------------- */
151 : #define CPL_ENC_LOCALE ""
152 : #define CPL_ENC_UTF8 "UTF-8"
153 : #define CPL_ENC_UTF16 "UTF-16"
154 : #define CPL_ENC_UCS2 "UCS-2"
155 : #define CPL_ENC_UCS4 "UCS-4"
156 : #define CPL_ENC_ASCII "ASCII"
157 : #define CPL_ENC_ISO8859_1 "ISO-8859-1"
158 :
159 : char CPL_DLL *CPLRecode( const char *pszSource,
160 : const char *pszSrcEncoding,
161 : const char *pszDstEncoding );
162 : char CPL_DLL *CPLRecodeFromWChar( const wchar_t *pwszSource,
163 : const char *pszSrcEncoding,
164 : const char *pszDstEncoding );
165 : wchar_t CPL_DLL *CPLRecodeToWChar( const char *pszSource,
166 : const char *pszSrcEncoding,
167 : const char *pszDstEncoding );
168 : int CPL_DLL CPLIsUTF8(const char* pabyData, int nLen);
169 : char CPL_DLL *CPLForceToASCII(const char* pabyData, int nLen, char chReplacementChar);
170 :
171 : CPL_C_END
172 :
173 : /************************************************************************/
174 : /* CPLString */
175 : /************************************************************************/
176 :
177 : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
178 :
179 : #include <string>
180 :
181 : /*
182 : * Simple trick to avoid "using" declaration in header for new compilers
183 : * but make it still working with old compilers which throw C2614 errors.
184 : *
185 : * Define MSVC_OLD_STUPID_BEHAVIOUR
186 : * for old compilers: VC++ 5 and 6 as well as eVC++ 3 and 4.
187 : */
188 :
189 : /*
190 : * Detect old MSVC++ compiler <= 6.0
191 : * 1200 - VC++ 6.0
192 : * 1200-1202 - eVC++ 4.0
193 : */
194 : #if defined(_MSC_VER)
195 : # if (_MSC_VER <= 1202)
196 : # define MSVC_OLD_STUPID_BEHAVIOUR
197 : # endif
198 : #endif
199 :
200 : /* Avoid C2614 errors */
201 : #ifdef MSVC_OLD_STUPID_BEHAVIOUR
202 : using std::string;
203 : # define std_string string
204 : #else
205 : # define std_string std::string
206 : #endif
207 :
208 : /* Remove annoying warnings in Microsoft eVC++ and Microsoft Visual C++ */
209 : #if defined(WIN32CE)
210 : # pragma warning(disable:4251 4275 4786)
211 : #endif
212 :
213 :
214 :
215 :
216 : class CPL_DLL CPLString : public std_string
217 1200845 : {
218 : public:
219 :
220 :
221 813882 : CPLString(void) {}
222 12786 : CPLString( const std::string &oStr ) : std_string( oStr ) {}
223 209293 : CPLString( const char *pszStr ) : std_string( pszStr ) {}
224 :
225 142601 : operator const char* (void) const { return c_str(); }
226 :
227 56147 : char& operator[](std::string::size_type i)
228 : {
229 56147 : return std_string::operator[](i);
230 : }
231 :
232 : const char& operator[](std::string::size_type i) const
233 : {
234 : return std_string::operator[](i);
235 : }
236 :
237 39228 : char& operator[](int i)
238 : {
239 39228 : return std_string::operator[](static_cast<std::string::size_type>(i));
240 : }
241 :
242 : const char& operator[](int i) const
243 : {
244 : return std_string::operator[](static_cast<std::string::size_type>(i));
245 : }
246 :
247 : void Clear() { resize(0); }
248 :
249 : /* There seems to be a bug in the way the compiler count indices... Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
250 : CPLString &Printf( const char *pszFormat, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
251 : CPLString &vPrintf( const char *pszFormat, va_list args );
252 : CPLString &FormatC( double dfValue, const char *pszFormat = NULL );
253 : CPLString &Trim();
254 : };
255 :
256 : #endif /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
257 :
258 : #endif /* _CPL_STRING_H_INCLUDED */
|