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
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
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 void CPL_DLL CPLClearRecodeWarningFlags();
00168 char CPL_DLL *CPLRecode( const char *pszSource,
00169 const char *pszSrcEncoding,
00170 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
00171 char CPL_DLL *CPLRecodeFromWChar( const wchar_t *pwszSource,
00172 const char *pszSrcEncoding,
00173 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
00174 wchar_t CPL_DLL *CPLRecodeToWChar( const char *pszSource,
00175 const char *pszSrcEncoding,
00176 const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
00177 int CPL_DLL CPLIsUTF8(const char* pabyData, int nLen);
00178 char CPL_DLL *CPLForceToASCII(const char* pabyData, int nLen, char chReplacementChar) CPL_WARN_UNUSED_RESULT;
00179
00180 CPL_C_END
00181
00182
00183
00184
00185
00186 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
00187
00188 #include <string>
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 #if defined(_MSC_VER)
00204 # if (_MSC_VER <= 1202)
00205 # define MSVC_OLD_STUPID_BEHAVIOUR
00206 # endif
00207 #endif
00208
00209
00210 #ifdef MSVC_OLD_STUPID_BEHAVIOUR
00211 using std::string;
00212 # define gdal_std_string string
00213 #else
00214 # define gdal_std_string std::string
00215 #endif
00216
00217
00218 #if defined(WIN32CE)
00219 # pragma warning(disable:4251 4275 4786)
00220 #endif
00221
00223 class CPL_DLL CPLString : public gdal_std_string
00224 {
00225 public:
00226
00227
00228 CPLString(void) {}
00229 CPLString( const std::string &oStr ) : gdal_std_string( oStr ) {}
00230 CPLString( const char *pszStr ) : gdal_std_string( pszStr ) {}
00231
00232 operator const char* (void) const { return c_str(); }
00233
00234 char& operator[](std::string::size_type i)
00235 {
00236 return gdal_std_string::operator[](i);
00237 }
00238
00239 const char& operator[](std::string::size_type i) const
00240 {
00241 return gdal_std_string::operator[](i);
00242 }
00243
00244 char& operator[](int i)
00245 {
00246 return gdal_std_string::operator[](static_cast<std::string::size_type>(i));
00247 }
00248
00249 const char& operator[](int i) const
00250 {
00251 return gdal_std_string::operator[](static_cast<std::string::size_type>(i));
00252 }
00253
00254 void Clear() { resize(0); }
00255
00256
00257 CPLString &Printf( const char *pszFormat, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
00258 CPLString &vPrintf( const char *pszFormat, va_list args );
00259 CPLString &FormatC( double dfValue, const char *pszFormat = NULL );
00260 CPLString &Trim();
00261 CPLString &Recode( const char *pszSrcEncoding, const char *pszDstEncoding );
00262
00263
00264 size_t ifind( const std::string & str, size_t pos = 0 ) const;
00265 size_t ifind( const char * s, size_t pos = 0 ) const;
00266 CPLString &toupper( void );
00267 CPLString &tolower( void );
00268 };
00269
00270
00271
00272
00273 CPLString CPL_DLL CPLURLGetValue(const char* pszURL, const char* pszKey);
00274 CPLString CPL_DLL CPLURLAddKVP(const char* pszURL, const char* pszKey,
00275 const char* pszValue);
00276
00277
00278
00279
00280
00282 class CPL_DLL CPLStringList
00283 {
00284 char **papszList;
00285 mutable int nCount;
00286 mutable int nAllocation;
00287 int bOwnList;
00288 int bIsSorted;
00289
00290 void Initialize();
00291 void MakeOurOwnCopy();
00292 void EnsureAllocation( int nMaxLength );
00293 int FindSortedInsertionPoint( const char *pszLine );
00294
00295 public:
00296 CPLStringList();
00297 CPLStringList( char **papszList, int bTakeOwnership=TRUE );
00298 CPLStringList( const CPLStringList& oOther );
00299 ~CPLStringList();
00300
00301 CPLStringList &Clear();
00302
00303 int size() const { return Count(); }
00304 int Count() const;
00305
00306 CPLStringList &AddString( const char *pszNewString );
00307 CPLStringList &AddStringDirectly( char *pszNewString );
00308
00309 CPLStringList &InsertString( int nInsertAtLineNo, const char *pszNewLine )
00310 { return InsertStringDirectly( nInsertAtLineNo, CPLStrdup(pszNewLine) ); }
00311 CPLStringList &InsertStringDirectly( int nInsertAtLineNo, char *pszNewLine);
00312
00313
00314
00315
00316 int FindString( const char *pszTarget ) const
00317 { return CSLFindString( papszList, pszTarget ); }
00318 int PartialFindString( const char *pszNeedle ) const
00319 { return CSLPartialFindString( papszList, pszNeedle ); }
00320
00321 int FindName( const char *pszName ) const;
00322 int FetchBoolean( const char *pszKey, int bDefault ) const;
00323 const char *FetchNameValue( const char *pszKey ) const;
00324 const char *FetchNameValueDef( const char *pszKey, const char *pszDefault ) const;
00325 CPLStringList &AddNameValue( const char *pszKey, const char *pszValue );
00326 CPLStringList &SetNameValue( const char *pszKey, const char *pszValue );
00327
00328 CPLStringList &Assign( char **papszList, int bTakeOwnership=TRUE );
00329 CPLStringList &operator=(char **papszListIn) { return Assign( papszListIn, TRUE ); }
00330 CPLStringList &operator=(const CPLStringList& oOther);
00331
00332 char * operator[](int i);
00333 char * operator[](size_t i) { return (*this)[(int)i]; }
00334 const char * operator[](int i) const;
00335 const char * operator[](size_t i) const { return (*this)[(int)i]; }
00336
00337 char **List() { return papszList; }
00338 char **StealList();
00339
00340 CPLStringList &Sort();
00341 int IsSorted() const { return bIsSorted; }
00342
00343 operator char**(void) { return List(); }
00344 };
00345
00346 #endif
00347
00348 #endif