OGR
|
00001 /********************************************************************** 00002 * $Id: cpl_string.h 23431 2011-11-27 15:02:24Z rouault $ 00003 * 00004 * Name: cpl_string.h 00005 * Project: CPL - Common Portability Library 00006 * Purpose: String and StringList functions. 00007 * Author: Daniel Morissette, dmorissette@mapgears.com 00008 * 00009 ********************************************************************** 00010 * Copyright (c) 1998, Daniel Morissette 00011 * 00012 * Permission is hereby granted, free of charge, to any person obtaining a 00013 * copy of this software and associated documentation files (the "Software"), 00014 * to deal in the Software without restriction, including without limitation 00015 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00016 * and/or sell copies of the Software, and to permit persons to whom the 00017 * Software is furnished to do so, subject to the following conditions: 00018 * 00019 * The above copyright notice and this permission notice shall be included 00020 * in all copies or substantial portions of the Software. 00021 * 00022 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00023 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00024 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00025 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00026 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00027 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00028 * DEALINGS IN THE SOFTWARE. 00029 ****************************************************************************/ 00030 00031 #ifndef _CPL_STRING_H_INCLUDED 00032 #define _CPL_STRING_H_INCLUDED 00033 00034 #include "cpl_vsi.h" 00035 #include "cpl_error.h" 00036 #include "cpl_conv.h" 00037 00060 CPL_C_START 00061 00062 char CPL_DLL **CSLAddString(char **papszStrList, const char *pszNewString) CPL_WARN_UNUSED_RESULT; 00063 int CPL_DLL CSLCount(char **papszStrList); 00064 const char CPL_DLL *CSLGetField( char **, int ); 00065 void CPL_DLL CPL_STDCALL CSLDestroy(char **papszStrList); 00066 char CPL_DLL **CSLDuplicate(char **papszStrList) CPL_WARN_UNUSED_RESULT; 00067 char CPL_DLL **CSLMerge( char **papszOrig, char **papszOverride ) CPL_WARN_UNUSED_RESULT; 00068 00069 char CPL_DLL **CSLTokenizeString(const char *pszString ) CPL_WARN_UNUSED_RESULT; 00070 char CPL_DLL **CSLTokenizeStringComplex(const char *pszString, 00071 const char *pszDelimiter, 00072 int bHonourStrings, int bAllowEmptyTokens ) CPL_WARN_UNUSED_RESULT; 00073 char CPL_DLL **CSLTokenizeString2( const char *pszString, 00074 const char *pszDelimeter, 00075 int nCSLTFlags ) CPL_WARN_UNUSED_RESULT; 00076 00077 #define CSLT_HONOURSTRINGS 0x0001 00078 #define CSLT_ALLOWEMPTYTOKENS 0x0002 00079 #define CSLT_PRESERVEQUOTES 0x0004 00080 #define CSLT_PRESERVEESCAPES 0x0008 00081 #define CSLT_STRIPLEADSPACES 0x0010 00082 #define CSLT_STRIPENDSPACES 0x0020 00083 00084 int CPL_DLL CSLPrint(char **papszStrList, FILE *fpOut); 00085 char CPL_DLL **CSLLoad(const char *pszFname) CPL_WARN_UNUSED_RESULT; 00086 char CPL_DLL **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, char** papszOptions) CPL_WARN_UNUSED_RESULT; 00087 int CPL_DLL CSLSave(char **papszStrList, const char *pszFname); 00088 00089 char CPL_DLL **CSLInsertStrings(char **papszStrList, int nInsertAtLineNo, 00090 char **papszNewLines) CPL_WARN_UNUSED_RESULT; 00091 char CPL_DLL **CSLInsertString(char **papszStrList, int nInsertAtLineNo, 00092 const char *pszNewLine) CPL_WARN_UNUSED_RESULT; 00093 char CPL_DLL **CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete, 00094 int nNumToRemove, char ***ppapszRetStrings) CPL_WARN_UNUSED_RESULT; 00095 int CPL_DLL CSLFindString( char **, const char * ); 00096 int CPL_DLL CSLPartialFindString( char **papszHaystack, 00097 const char * pszNeedle ); 00098 int CPL_DLL CSLFindName(char **papszStrList, const char *pszName); 00099 int CPL_DLL CSLTestBoolean( const char *pszValue ); 00100 int CPL_DLL CSLFetchBoolean( char **papszStrList, const char *pszKey, 00101 int bDefault ); 00102 00103 const char CPL_DLL *CPLSPrintf(const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(1, 2); 00104 char CPL_DLL **CSLAppendPrintf(char **papszStrList, const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_UNUSED_RESULT; 00105 int CPL_DLL CPLVASPrintf(char **buf, const char *fmt, va_list args ); 00106 00107 const char CPL_DLL * 00108 CPLParseNameValue(const char *pszNameValue, char **ppszKey ); 00109 const char CPL_DLL * 00110 CSLFetchNameValue(char **papszStrList, const char *pszName); 00111 const char CPL_DLL * 00112 CSLFetchNameValueDef(char **papszStrList, const char *pszName, 00113 const char *pszDefault ); 00114 char CPL_DLL ** 00115 CSLFetchNameValueMultiple(char **papszStrList, const char *pszName); 00116 char CPL_DLL ** 00117 CSLAddNameValue(char **papszStrList, 00118 const char *pszName, const char *pszValue) CPL_WARN_UNUSED_RESULT; 00119 char CPL_DLL ** 00120 CSLSetNameValue(char **papszStrList, 00121 const char *pszName, const char *pszValue) CPL_WARN_UNUSED_RESULT; 00122 void CPL_DLL CSLSetNameValueSeparator( char ** papszStrList, 00123 const char *pszSeparator ); 00124 00125 #define CPLES_BackslashQuotable 0 00126 #define CPLES_XML 1 00127 #define CPLES_URL 2 /* unescape only for now */ 00128 #define CPLES_SQL 3 00129 #define CPLES_CSV 4 00130 00131 char CPL_DLL *CPLEscapeString( const char *pszString, int nLength, 00132 int nScheme ) CPL_WARN_UNUSED_RESULT; 00133 char CPL_DLL *CPLUnescapeString( const char *pszString, int *pnLength, 00134 int nScheme ) CPL_WARN_UNUSED_RESULT; 00135 00136 char CPL_DLL *CPLBinaryToHex( int nBytes, const GByte *pabyData ) CPL_WARN_UNUSED_RESULT; 00137 GByte CPL_DLL *CPLHexToBinary( const char *pszHex, int *pnBytes ) CPL_WARN_UNUSED_RESULT; 00138 00139 char CPL_DLL *CPLBase64Encode( int nBytes, const GByte *pabyData ) CPL_WARN_UNUSED_RESULT; 00140 int CPL_DLL CPLBase64DecodeInPlace(GByte* pszBase64); 00141 00142 typedef enum 00143 { 00144 CPL_VALUE_STRING, 00145 CPL_VALUE_REAL, 00146 CPL_VALUE_INTEGER 00147 } CPLValueType; 00148 00149 CPLValueType CPL_DLL CPLGetValueType(const char* pszValue); 00150 00151 size_t CPL_DLL CPLStrlcpy(char* pszDest, const char* pszSrc, size_t nDestSize); 00152 size_t CPL_DLL CPLStrlcat(char* pszDest, const char* pszSrc, size_t nDestSize); 00153 size_t CPL_DLL CPLStrnlen (const char *pszStr, size_t nMaxLen); 00154 00155 /* -------------------------------------------------------------------- */ 00156 /* RFC 23 character set conversion/recoding API (cpl_recode.cpp). */ 00157 /* -------------------------------------------------------------------- */ 00158 #define CPL_ENC_LOCALE "" 00159 #define CPL_ENC_UTF8 "UTF-8" 00160 #define CPL_ENC_UTF16 "UTF-16" 00161 #define CPL_ENC_UCS2 "UCS-2" 00162 #define CPL_ENC_UCS4 "UCS-4" 00163 #define CPL_ENC_ASCII "ASCII" 00164 #define CPL_ENC_ISO8859_1 "ISO-8859-1" 00165 00166 int CPL_DLL CPLEncodingCharSize( const char *pszEncoding ); 00167 char CPL_DLL *CPLRecode( const char *pszSource, 00168 const char *pszSrcEncoding, 00169 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT; 00170 char CPL_DLL *CPLRecodeFromWChar( const wchar_t *pwszSource, 00171 const char *pszSrcEncoding, 00172 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT; 00173 wchar_t CPL_DLL *CPLRecodeToWChar( const char *pszSource, 00174 const char *pszSrcEncoding, 00175 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT; 00176 int CPL_DLL CPLIsUTF8(const char* pabyData, int nLen); 00177 char CPL_DLL *CPLForceToASCII(const char* pabyData, int nLen, char chReplacementChar) CPL_WARN_UNUSED_RESULT; 00178 00179 CPL_C_END 00180 00181 /************************************************************************/ 00182 /* CPLString */ 00183 /************************************************************************/ 00184 00185 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) 00186 00187 #include <string> 00188 00189 /* 00190 * Simple trick to avoid "using" declaration in header for new compilers 00191 * but make it still working with old compilers which throw C2614 errors. 00192 * 00193 * Define MSVC_OLD_STUPID_BEHAVIOUR 00194 * for old compilers: VC++ 5 and 6 as well as eVC++ 3 and 4. 00195 */ 00196 00197 /* 00198 * Detect old MSVC++ compiler <= 6.0 00199 * 1200 - VC++ 6.0 00200 * 1200-1202 - eVC++ 4.0 00201 */ 00202 #if defined(_MSC_VER) 00203 # if (_MSC_VER <= 1202) 00204 # define MSVC_OLD_STUPID_BEHAVIOUR 00205 # endif 00206 #endif 00207 00208 /* Avoid C2614 errors */ 00209 #ifdef MSVC_OLD_STUPID_BEHAVIOUR 00210 using std::string; 00211 # define gdal_std_string string 00212 #else 00213 # define gdal_std_string std::string 00214 #endif 00215 00216 /* Remove annoying warnings in Microsoft eVC++ and Microsoft Visual C++ */ 00217 #if defined(WIN32CE) 00218 # pragma warning(disable:4251 4275 4786) 00219 #endif 00220 00222 class CPL_DLL CPLString : public gdal_std_string 00223 { 00224 public: 00225 00226 00227 CPLString(void) {} 00228 CPLString( const std::string &oStr ) : gdal_std_string( oStr ) {} 00229 CPLString( const char *pszStr ) : gdal_std_string( pszStr ) {} 00230 00231 operator const char* (void) const { return c_str(); } 00232 00233 char& operator[](std::string::size_type i) 00234 { 00235 return gdal_std_string::operator[](i); 00236 } 00237 00238 const char& operator[](std::string::size_type i) const 00239 { 00240 return gdal_std_string::operator[](i); 00241 } 00242 00243 char& operator[](int i) 00244 { 00245 return gdal_std_string::operator[](static_cast<std::string::size_type>(i)); 00246 } 00247 00248 const char& operator[](int i) const 00249 { 00250 return gdal_std_string::operator[](static_cast<std::string::size_type>(i)); 00251 } 00252 00253 void Clear() { resize(0); } 00254 00255 /* There seems to be a bug in the way the compiler count indices... Should be CPL_PRINT_FUNC_FORMAT (1, 2) */ 00256 CPLString &Printf( const char *pszFormat, ... ) CPL_PRINT_FUNC_FORMAT (2, 3); 00257 CPLString &vPrintf( const char *pszFormat, va_list args ); 00258 CPLString &FormatC( double dfValue, const char *pszFormat = NULL ); 00259 CPLString &Trim(); 00260 CPLString &Recode( const char *pszSrcEncoding, const char *pszDstEncoding ); 00261 00262 /* case insensitive find alternates */ 00263 size_t ifind( const std::string & str, size_t pos = 0 ) const; 00264 size_t ifind( const char * s, size_t pos = 0 ) const; 00265 CPLString &toupper( void ); 00266 CPLString &tolower( void ); 00267 }; 00268 00269 /* -------------------------------------------------------------------- */ 00270 /* URL processing functions, here since they depend on CPLString. */ 00271 /* -------------------------------------------------------------------- */ 00272 CPLString CPL_DLL CPLURLGetValue(const char* pszURL, const char* pszKey); 00273 CPLString CPL_DLL CPLURLAddKVP(const char* pszURL, const char* pszKey, 00274 const char* pszValue); 00275 00276 /************************************************************************/ 00277 /* CPLStringList */ 00278 /************************************************************************/ 00279 00281 class CPL_DLL CPLStringList 00282 { 00283 char **papszList; 00284 mutable int nCount; 00285 mutable int nAllocation; 00286 int bOwnList; 00287 int bIsSorted; 00288 00289 void Initialize(); 00290 void MakeOurOwnCopy(); 00291 void EnsureAllocation( int nMaxLength ); 00292 int FindSortedInsertionPoint( const char *pszLine ); 00293 00294 public: 00295 CPLStringList(); 00296 CPLStringList( char **papszList, int bTakeOwnership=TRUE ); 00297 CPLStringList( const CPLStringList& oOther ); 00298 ~CPLStringList(); 00299 00300 CPLStringList &Clear(); 00301 00302 int size() const { return Count(); } 00303 int Count() const; 00304 00305 CPLStringList &AddString( const char *pszNewString ); 00306 CPLStringList &AddStringDirectly( char *pszNewString ); 00307 00308 CPLStringList &InsertString( int nInsertAtLineNo, const char *pszNewLine ) 00309 { return InsertStringDirectly( nInsertAtLineNo, CPLStrdup(pszNewLine) ); } 00310 CPLStringList &InsertStringDirectly( int nInsertAtLineNo, char *pszNewLine); 00311 00312 // CPLStringList &InsertStrings( int nInsertAtLineNo, char **papszNewLines ); 00313 // CPLStringList &RemoveStrings( int nFirstLineToDelete, int nNumToRemove=1 ); 00314 00315 int FindString( const char *pszTarget ) const 00316 { return CSLFindString( papszList, pszTarget ); } 00317 int PartialFindString( const char *pszNeedle ) const 00318 { return CSLPartialFindString( papszList, pszNeedle ); } 00319 00320 int FindName( const char *pszName ) const; 00321 int FetchBoolean( const char *pszKey, int bDefault ) const; 00322 const char *FetchNameValue( const char *pszKey ) const; 00323 const char *FetchNameValueDef( const char *pszKey, const char *pszDefault ) const; 00324 CPLStringList &AddNameValue( const char *pszKey, const char *pszValue ); 00325 CPLStringList &SetNameValue( const char *pszKey, const char *pszValue ); 00326 00327 CPLStringList &Assign( char **papszList, int bTakeOwnership=TRUE ); 00328 CPLStringList &operator=(char **papszListIn) { return Assign( papszListIn, TRUE ); } 00329 CPLStringList &operator=(const CPLStringList& oOther); 00330 00331 char * operator[](int i); 00332 char * operator[](size_t i) { return (*this)[(int)i]; } 00333 const char * operator[](int i) const; 00334 const char * operator[](size_t i) const { return (*this)[(int)i]; } 00335 00336 char **List() { return papszList; } 00337 char **StealList(); 00338 00339 CPLStringList &Sort(); 00340 int IsSorted() const { return bIsSorted; } 00341 00342 operator char**(void) { return List(); } 00343 }; 00344 00345 #endif /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */ 00346 00347 #endif /* _CPL_STRING_H_INCLUDED */