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 #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
00128 #define CPLES_SQL 3
00129 #define CPLES_CSV 4
00130 #define CPLES_XML_BUT_QUOTES 5
00131
00132 char CPL_DLL *CPLEscapeString( const char *pszString, int nLength,
00133 int nScheme ) CPL_WARN_UNUSED_RESULT;
00134 char CPL_DLL *CPLUnescapeString( const char *pszString, int *pnLength,
00135 int nScheme ) CPL_WARN_UNUSED_RESULT;
00136
00137 char CPL_DLL *CPLBinaryToHex( int nBytes, const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
00138 GByte CPL_DLL *CPLHexToBinary( const char *pszHex, int *pnBytes ) CPL_WARN_UNUSED_RESULT;
00139
00140 char CPL_DLL *CPLBase64Encode( int nBytes, const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
00141 int CPL_DLL CPLBase64DecodeInPlace(GByte* pszBase64);
00142
00143 typedef enum
00144 {
00145 CPL_VALUE_STRING,
00146 CPL_VALUE_REAL,
00147 CPL_VALUE_INTEGER
00148 } CPLValueType;
00149
00150 CPLValueType CPL_DLL CPLGetValueType(const char* pszValue);
00151
00152 size_t CPL_DLL CPLStrlcpy(char* pszDest, const char* pszSrc, size_t nDestSize);
00153 size_t CPL_DLL CPLStrlcat(char* pszDest, const char* pszSrc, size_t nDestSize);
00154 size_t CPL_DLL CPLStrnlen (const char *pszStr, size_t nMaxLen);
00155
00156
00157
00158
00159 #define CPL_ENC_LOCALE ""
00160 #define CPL_ENC_UTF8 "UTF-8"
00161 #define CPL_ENC_UTF16 "UTF-16"
00162 #define CPL_ENC_UCS2 "UCS-2"
00163 #define CPL_ENC_UCS4 "UCS-4"
00164 #define CPL_ENC_ASCII "ASCII"
00165 #define CPL_ENC_ISO8859_1 "ISO-8859-1"
00166
00167 int CPL_DLL CPLEncodingCharSize( const char *pszEncoding );
00168 void CPL_DLL CPLClearRecodeWarningFlags();
00169 char CPL_DLL *CPLRecode( const char *pszSource,
00170 const char *pszSrcEncoding,
00171 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
00172 char CPL_DLL *CPLRecodeFromWChar( const wchar_t *pwszSource,
00173 const char *pszSrcEncoding,
00174 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
00175 wchar_t CPL_DLL *CPLRecodeToWChar( const char *pszSource,
00176 const char *pszSrcEncoding,
00177 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
00178 int CPL_DLL CPLIsUTF8(const char* pabyData, int nLen);
00179 char CPL_DLL *CPLForceToASCII(const char* pabyData, int nLen, char chReplacementChar) CPL_WARN_UNUSED_RESULT;
00180
00181 CPL_C_END
00182
00183
00184
00185
00186
00187 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
00188
00189 #include <string>
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 #if defined(_MSC_VER)
00205 # if (_MSC_VER <= 1202)
00206 # define MSVC_OLD_STUPID_BEHAVIOUR
00207 # endif
00208 #endif
00209
00210
00211 #ifdef MSVC_OLD_STUPID_BEHAVIOUR
00212 using std::string;
00213 # define gdal_std_string string
00214 #else
00215 # define gdal_std_string std::string
00216 #endif
00217
00218
00219 #if defined(WIN32CE)
00220 # pragma warning(disable:4251 4275 4786)
00221 #endif
00222
00224 class CPL_DLL CPLString : public gdal_std_string
00225 {
00226 public:
00227
00228
00229 CPLString(void) {}
00230 CPLString( const std::string &oStr ) : gdal_std_string( oStr ) {}
00231 CPLString( const char *pszStr ) : gdal_std_string( pszStr ) {}
00232
00233 operator const char* (void) const { return c_str(); }
00234
00235 char& operator[](std::string::size_type i)
00236 {
00237 return gdal_std_string::operator[](i);
00238 }
00239
00240 const char& operator[](std::string::size_type i) const
00241 {
00242 return gdal_std_string::operator[](i);
00243 }
00244
00245 char& operator[](int i)
00246 {
00247 return gdal_std_string::operator[](static_cast<std::string::size_type>(i));
00248 }
00249
00250 const char& operator[](int i) const
00251 {
00252 return gdal_std_string::operator[](static_cast<std::string::size_type>(i));
00253 }
00254
00255 void Clear() { resize(0); }
00256
00257
00258 void Seize(char *pszValue)
00259 {
00260 if (pszValue == NULL )
00261 Clear();
00262 else
00263 {
00264 *this = pszValue;
00265 CPLFree(pszValue);
00266 }
00267 }
00268
00269
00270 CPLString &Printf( const char *pszFormat, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
00271 CPLString &vPrintf( const char *pszFormat, va_list args );
00272 CPLString &FormatC( double dfValue, const char *pszFormat = NULL );
00273 CPLString &Trim();
00274 CPLString &Recode( const char *pszSrcEncoding, const char *pszDstEncoding );
00275
00276
00277 size_t ifind( const std::string & str, size_t pos = 0 ) const;
00278 size_t ifind( const char * s, size_t pos = 0 ) const;
00279 CPLString &toupper( void );
00280 CPLString &tolower( void );
00281 };
00282
00283
00284
00285
00286 CPLString CPL_DLL CPLURLGetValue(const char* pszURL, const char* pszKey);
00287 CPLString CPL_DLL CPLURLAddKVP(const char* pszURL, const char* pszKey,
00288 const char* pszValue);
00289
00290
00291
00292
00293
00295 class CPL_DLL CPLStringList
00296 {
00297 char **papszList;
00298 mutable int nCount;
00299 mutable int nAllocation;
00300 int bOwnList;
00301 int bIsSorted;
00302
00303 void Initialize();
00304 void MakeOurOwnCopy();
00305 void EnsureAllocation( int nMaxLength );
00306 int FindSortedInsertionPoint( const char *pszLine );
00307
00308 public:
00309 CPLStringList();
00310 CPLStringList( char **papszList, int bTakeOwnership=TRUE );
00311 CPLStringList( const CPLStringList& oOther );
00312 ~CPLStringList();
00313
00314 CPLStringList &Clear();
00315
00316 int size() const { return Count(); }
00317 int Count() const;
00318
00319 CPLStringList &AddString( const char *pszNewString );
00320 CPLStringList &AddStringDirectly( char *pszNewString );
00321
00322 CPLStringList &InsertString( int nInsertAtLineNo, const char *pszNewLine )
00323 { return InsertStringDirectly( nInsertAtLineNo, CPLStrdup(pszNewLine) ); }
00324 CPLStringList &InsertStringDirectly( int nInsertAtLineNo, char *pszNewLine);
00325
00326
00327
00328
00329 int FindString( const char *pszTarget ) const
00330 { return CSLFindString( papszList, pszTarget ); }
00331 int PartialFindString( const char *pszNeedle ) const
00332 { return CSLPartialFindString( papszList, pszNeedle ); }
00333
00334 int FindName( const char *pszName ) const;
00335 int FetchBoolean( const char *pszKey, int bDefault ) const;
00336 const char *FetchNameValue( const char *pszKey ) const;
00337 const char *FetchNameValueDef( const char *pszKey, const char *pszDefault ) const;
00338 CPLStringList &AddNameValue( const char *pszKey, const char *pszValue );
00339 CPLStringList &SetNameValue( const char *pszKey, const char *pszValue );
00340
00341 CPLStringList &Assign( char **papszList, int bTakeOwnership=TRUE );
00342 CPLStringList &operator=(char **papszListIn) { return Assign( papszListIn, TRUE ); }
00343 CPLStringList &operator=(const CPLStringList& oOther);
00344
00345 char * operator[](int i);
00346 char * operator[](size_t i) { return (*this)[(int)i]; }
00347 const char * operator[](int i) const;
00348 const char * operator[](size_t i) const { return (*this)[(int)i]; }
00349
00350 char **List() { return papszList; }
00351 char **StealList();
00352
00353 CPLStringList &Sort();
00354 int IsSorted() const { return bIsSorted; }
00355
00356 operator char**(void) { return List(); }
00357 };
00358
00359 #endif
00360
00361 #endif