00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef CPL_STRING_H_INCLUDED
00033 #define CPL_STRING_H_INCLUDED
00034
00035 #include "cpl_error.h"
00036 #include "cpl_conv.h"
00037 #include "cpl_vsi.h"
00038
00061 CPL_C_START
00062
00063 char CPL_DLL **CSLAddString(char **papszStrList,
00064 const char *pszNewString) CPL_WARN_UNUSED_RESULT;
00065 char CPL_DLL **CSLAddStringMayFail(
00066 char **papszStrList, const char *pszNewString) CPL_WARN_UNUSED_RESULT;
00067 #ifdef __cplusplus
00068 int CPL_DLL CSLCount(const char * const *papszStrList);
00069 #else
00070 int CPL_DLL CSLCount(char **papszStrList);
00071 #endif
00072 const char CPL_DLL *CSLGetField( char **, int );
00073 void CPL_DLL CPL_STDCALL CSLDestroy(char **papszStrList);
00074 char CPL_DLL **CSLDuplicate(char **papszStrList) CPL_WARN_UNUSED_RESULT;
00075 char CPL_DLL **CSLMerge( char **papszOrig,
00076 char **papszOverride ) CPL_WARN_UNUSED_RESULT;
00077
00078 char CPL_DLL **CSLTokenizeString(const char *pszString ) CPL_WARN_UNUSED_RESULT;
00079 char CPL_DLL **CSLTokenizeStringComplex(
00080 const char *pszString, const char *pszDelimiter, int bHonourStrings,
00081 int bAllowEmptyTokens ) CPL_WARN_UNUSED_RESULT;
00082 char CPL_DLL **CSLTokenizeString2( const char *pszString,
00083 const char *pszDelimiter,
00084 int nCSLTFlags ) CPL_WARN_UNUSED_RESULT;
00085
00086 #define CSLT_HONOURSTRINGS 0x0001
00087 #define CSLT_ALLOWEMPTYTOKENS 0x0002
00088 #define CSLT_PRESERVEQUOTES 0x0004
00089 #define CSLT_PRESERVEESCAPES 0x0008
00090 #define CSLT_STRIPLEADSPACES 0x0010
00091 #define CSLT_STRIPENDSPACES 0x0020
00092
00093 int CPL_DLL CSLPrint(char **papszStrList, FILE *fpOut);
00094 char CPL_DLL **CSLLoad(const char *pszFname) CPL_WARN_UNUSED_RESULT;
00095 char CPL_DLL **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols,
00096 char** papszOptions) CPL_WARN_UNUSED_RESULT;
00097 int CPL_DLL CSLSave(char **papszStrList, const char *pszFname);
00098
00099 char CPL_DLL **CSLInsertStrings(char **papszStrList, int nInsertAtLineNo,
00100 char **papszNewLines) CPL_WARN_UNUSED_RESULT;
00101 char CPL_DLL **CSLInsertString(char **papszStrList, int nInsertAtLineNo,
00102 const char *pszNewLine) CPL_WARN_UNUSED_RESULT;
00103 char CPL_DLL **CSLRemoveStrings(
00104 char **papszStrList, int nFirstLineToDelete,
00105 int nNumToRemove, char ***ppapszRetStrings) CPL_WARN_UNUSED_RESULT;
00106 int CPL_DLL CSLFindString( char **, const char * );
00107 int CPL_DLL CSLFindStringCaseSensitive( char **, const char * );
00108 int CPL_DLL CSLPartialFindString( char **papszHaystack,
00109 const char * pszNeedle );
00110 int CPL_DLL CSLFindName(char **papszStrList, const char *pszName);
00111 int CPL_DLL CSLFetchBoolean( char **papszStrList, const char *pszKey,
00112 int bDefault );
00113
00114
00115 int CPL_DLL CSLTestBoolean( const char *pszValue );
00116 int CPL_DLL CPLTestBoolean( const char *pszValue );
00117
00118 #ifdef __cplusplus
00119 #ifdef DO_NOT_USE_DEBUG_BOOL
00120 #define CPLTestBool(x) CPL_TO_BOOL(CPLTestBoolean(x))
00121 #else
00122
00123 #ifdef DEBUG_BOOL
00124 extern "C++" {
00125 #endif
00126 bool CPL_DLL CPLTestBool( const char *pszValue );
00127 #ifdef DEBUG_BOOL
00128 }
00129 #endif
00130 #endif
00131 bool CPL_DLL CPLFetchBool( const char **papszStrList, const char *pszKey,
00132 bool bDefault );
00133 #endif
00134
00135 const char CPL_DLL *
00136 CPLParseNameValue(const char *pszNameValue, char **ppszKey );
00137 const char CPL_DLL *
00138 CSLFetchNameValue(char **papszStrList, const char *pszName);
00139 const char CPL_DLL *
00140 CSLFetchNameValueDef(char **papszStrList, const char *pszName,
00141 const char *pszDefault );
00142 char CPL_DLL **
00143 CSLFetchNameValueMultiple(char **papszStrList, const char *pszName);
00144 char CPL_DLL **
00145 CSLAddNameValue(char **papszStrList,
00146 const char *pszName,
00147 const char *pszValue) CPL_WARN_UNUSED_RESULT;
00148 char CPL_DLL **
00149 CSLSetNameValue(char **papszStrList,
00150 const char *pszName,
00151 const char *pszValue) CPL_WARN_UNUSED_RESULT;
00152 void CPL_DLL CSLSetNameValueSeparator( char ** papszStrList,
00153 const char *pszSeparator );
00154
00155 char CPL_DLL ** CSLParseCommandLine(const char* pszCommandLine);
00156
00157 #define CPLES_BackslashQuotable 0
00158 #define CPLES_XML 1
00159 #define CPLES_URL 2
00160 #define CPLES_SQL 3
00161 #define CPLES_CSV 4
00162 #define CPLES_XML_BUT_QUOTES 5
00163
00164 char CPL_DLL *CPLEscapeString( const char *pszString, int nLength,
00165 int nScheme ) CPL_WARN_UNUSED_RESULT;
00166 char CPL_DLL *CPLUnescapeString( const char *pszString, int *pnLength,
00167 int nScheme ) CPL_WARN_UNUSED_RESULT;
00168
00169 char CPL_DLL *CPLBinaryToHex( int nBytes,
00170 const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
00171 GByte CPL_DLL *CPLHexToBinary( const char *pszHex,
00172 int *pnBytes ) CPL_WARN_UNUSED_RESULT;
00173
00174 char CPL_DLL *CPLBase64Encode( int nBytes,
00175 const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
00176 int CPL_DLL CPLBase64DecodeInPlace( GByte* pszBase64 );
00177
00178 typedef enum
00179 {
00180 CPL_VALUE_STRING,
00181 CPL_VALUE_REAL,
00182 CPL_VALUE_INTEGER
00183 } CPLValueType;
00184
00185 CPLValueType CPL_DLL CPLGetValueType(const char* pszValue);
00186
00187 size_t CPL_DLL CPLStrlcpy(char* pszDest, const char* pszSrc, size_t nDestSize);
00188 size_t CPL_DLL CPLStrlcat(char* pszDest, const char* pszSrc, size_t nDestSize);
00189 size_t CPL_DLL CPLStrnlen(const char *pszStr, size_t nMaxLen);
00190
00191
00192
00193
00194 int CPL_DLL CPLvsnprintf(char *str, size_t size, const char* fmt,
00195 va_list args) CPL_PRINT_FUNC_FORMAT (3, 0);
00196 int CPL_DLL CPLsnprintf(char *str, size_t size,
00197 const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(3,4);
00198 #if defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
00199 int CPL_DLL CPLsprintf(char *str, const char* fmt, ...)
00200 CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use CPLsnprintf instead");
00201 #else
00202 int CPL_DLL CPLsprintf(char *str, const char* fmt, ...)
00203 CPL_PRINT_FUNC_FORMAT(2, 3);
00204 #endif
00205 int CPL_DLL CPLprintf(const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(1, 2);
00206
00207 int CPL_DLL CPLsscanf(const char* str, const char* fmt, ...)
00208 CPL_SCAN_FUNC_FORMAT(2, 3);
00209
00210 const char CPL_DLL *CPLSPrintf(const char *fmt, ...)
00211 CPL_PRINT_FUNC_FORMAT(1, 2) CPL_WARN_UNUSED_RESULT;
00212 char CPL_DLL **CSLAppendPrintf(char **papszStrList, const char *fmt, ...)
00213 CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_UNUSED_RESULT;
00214 int CPL_DLL CPLVASPrintf(char **buf, const char *fmt, va_list args )
00215 CPL_PRINT_FUNC_FORMAT(2, 0);
00216
00217
00218
00219
00220 #define CPL_ENC_LOCALE ""
00221 #define CPL_ENC_UTF8 "UTF-8"
00222 #define CPL_ENC_UTF16 "UTF-16"
00223 #define CPL_ENC_UCS2 "UCS-2"
00224 #define CPL_ENC_UCS4 "UCS-4"
00225 #define CPL_ENC_ASCII "ASCII"
00226 #define CPL_ENC_ISO8859_1 "ISO-8859-1"
00227
00228 int CPL_DLL CPLEncodingCharSize( const char *pszEncoding );
00229 void CPL_DLL CPLClearRecodeWarningFlags( void );
00230 char CPL_DLL *CPLRecode(
00231 const char *pszSource, const char *pszSrcEncoding,
00232 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
00233 char CPL_DLL *CPLRecodeFromWChar(
00234 const wchar_t *pwszSource, const char *pszSrcEncoding,
00235 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
00236 wchar_t CPL_DLL *CPLRecodeToWChar(
00237 const char *pszSource, const char *pszSrcEncoding,
00238 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
00239 int CPL_DLL CPLIsUTF8(const char* pabyData, int nLen);
00240 char CPL_DLL *CPLForceToASCII(
00241 const char* pabyData, int nLen,
00242 char chReplacementChar) CPL_WARN_UNUSED_RESULT;
00243 int CPL_DLL CPLStrlenUTF8(const char *pszUTF8Str);
00244
00245 CPL_C_END
00246
00247
00248
00249
00250
00251 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
00252
00253 #include <string>
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 #if defined(_MSC_VER)
00269 # if (_MSC_VER <= 1202)
00270 # define MSVC_OLD_STUPID_BEHAVIOUR
00271 # endif
00272 #endif
00273
00274
00275 #ifdef MSVC_OLD_STUPID_BEHAVIOUR
00276 using std::string;
00277 # define gdal_std_string string
00278 #else
00279 # define gdal_std_string std::string
00280 #endif
00281
00283 class CPL_DLL CPLString : public gdal_std_string
00284 {
00285 public:
00286
00287 CPLString(void) {}
00288 CPLString( const std::string &oStr ) : gdal_std_string( oStr ) {}
00289 CPLString( const char *pszStr ) : gdal_std_string( pszStr ) {}
00290
00291 operator const char* (void) const { return c_str(); }
00292
00293 char& operator[](std::string::size_type i)
00294 {
00295 return gdal_std_string::operator[](i);
00296 }
00297
00298 const char& operator[](std::string::size_type i) const
00299 {
00300 return gdal_std_string::operator[](i);
00301 }
00302
00303 char& operator[](int i)
00304 {
00305 return gdal_std_string::operator[](
00306 static_cast<std::string::size_type>(i));
00307 }
00308
00309 const char& operator[](int i) const
00310 {
00311 return gdal_std_string::operator[](
00312 static_cast<std::string::size_type>(i));
00313 }
00314
00315 void Clear() { resize(0); }
00316
00317
00318 void Seize(char *pszValue)
00319 {
00320 if (pszValue == NULL )
00321 Clear();
00322 else
00323 {
00324 *this = pszValue;
00325 CPLFree(pszValue);
00326 }
00327 }
00328
00329
00330
00331 CPLString &Printf(
00332 const char *pszFormat, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
00333 CPLString &vPrintf(
00334 const char *pszFormat, va_list args ) CPL_PRINT_FUNC_FORMAT(2, 0);
00335 CPLString &FormatC( double dfValue, const char *pszFormat = NULL );
00336 CPLString &Trim();
00337 CPLString &Recode( const char *pszSrcEncoding, const char *pszDstEncoding );
00338
00339
00340 size_t ifind( const std::string & str, size_t pos = 0 ) const;
00341 size_t ifind( const char * s, size_t pos = 0 ) const;
00342 CPLString &toupper( void );
00343 CPLString &tolower( void );
00344 };
00345
00346 CPLString CPLOPrintf(const char *pszFormat, ... ) CPL_PRINT_FUNC_FORMAT (1, 2);
00347 CPLString CPLOvPrintf(
00348 const char *pszFormat, va_list args) CPL_PRINT_FUNC_FORMAT (1, 0);
00349
00350
00351
00352
00353 CPLString CPL_DLL CPLURLGetValue(const char* pszURL, const char* pszKey);
00354 CPLString CPL_DLL CPLURLAddKVP(const char* pszURL, const char* pszKey,
00355 const char* pszValue);
00356
00357
00358
00359
00360
00362 class CPL_DLL CPLStringList
00363 {
00364 char **papszList;
00365 mutable int nCount;
00366 mutable int nAllocation;
00367 bool bOwnList;
00368 bool bIsSorted;
00369
00370 void Initialize();
00371 void MakeOurOwnCopy();
00372 void EnsureAllocation( int nMaxLength );
00373 int FindSortedInsertionPoint( const char *pszLine );
00374
00375 public:
00376 CPLStringList();
00377 CPLStringList( char **papszList, int bTakeOwnership=TRUE );
00378 CPLStringList( const CPLStringList& oOther );
00379 ~CPLStringList();
00380
00381 CPLStringList &Clear();
00382
00383 int size() const { return Count(); }
00384 int Count() const;
00385
00386 CPLStringList &AddString( const char *pszNewString );
00387 CPLStringList &AddStringDirectly( char *pszNewString );
00388
00389 CPLStringList &InsertString( int nInsertAtLineNo, const char *pszNewLine )
00390 { return InsertStringDirectly( nInsertAtLineNo, CPLStrdup(pszNewLine) ); }
00391 CPLStringList &InsertStringDirectly( int nInsertAtLineNo, char *pszNewLine);
00392
00393
00394
00395
00396 int FindString( const char *pszTarget ) const
00397 { return CSLFindString( papszList, pszTarget ); }
00398 int PartialFindString( const char *pszNeedle ) const
00399 { return CSLPartialFindString( papszList, pszNeedle ); }
00400
00401 int FindName( const char *pszName ) const;
00402 bool FetchBool( const char *pszKey, bool bDefault ) const;
00403
00404 int FetchBoolean( const char *pszKey, int bDefault ) const;
00405 const char *FetchNameValue( const char *pszKey ) const;
00406 const char *FetchNameValueDef(
00407 const char *pszKey, const char *pszDefault ) const;
00408 CPLStringList &AddNameValue( const char *pszKey, const char *pszValue );
00409 CPLStringList &SetNameValue( const char *pszKey, const char *pszValue );
00410
00411 CPLStringList &Assign( char **papszListIn, int bTakeOwnership=TRUE );
00412 CPLStringList &operator=(char **papszListIn) {
00413 return Assign( papszListIn, TRUE ); }
00414 CPLStringList &operator=(const CPLStringList& oOther);
00415
00416 char * operator[](int i);
00417 char * operator[](size_t i) { return (*this)[static_cast<int>(i)]; }
00418 const char * operator[](int i) const;
00419 const char * operator[](size_t i) const {
00420 return (*this)[static_cast<int>(i)]; }
00421
00422 char **List() { return papszList; }
00423 char **StealList();
00424
00425 CPLStringList &Sort();
00426 int IsSorted() const { return bIsSorted; }
00427
00428 operator char**(void) { return List(); }
00429 };
00430
00431 #endif
00432
00433 #endif