cpl_string.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  * $Id: cpl_string.h 33788 2016-03-26 00:45:58Z goatbar $
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  * Copyright (c) 2008-2014, Even Rouault <even dot rouault at mines-paris dot org>
00012  *
00013  * Permission is hereby granted, free of charge, to any person obtaining a
00014  * copy of this software and associated documentation files (the "Software"),
00015  * to deal in the Software without restriction, including without limitation
00016  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00017  * and/or sell copies of the Software, and to permit persons to whom the
00018  * Software is furnished to do so, subject to the following conditions:
00019  *
00020  * The above copyright notice and this permission notice shall be included
00021  * in all copies or substantial portions of the Software.
00022  *
00023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00024  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00025  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00026  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00027  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00028  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00029  * DEALINGS IN THE SOFTWARE.
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 /* TODO: Deprecate CSLTestBoolean.  Remove in GDAL 3.x. */
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 /* Prefer these for C++ code. */
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  /* __cplusplus */
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 /*      Locale independent formatting functions.                        */
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 /* caution: only works with limited number of formats */
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 /*      RFC 23 character set conversion/recoding API (cpl_recode.cpp).  */
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 /*                              CPLString                               */
00249 /************************************************************************/
00250 
00251 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
00252 
00253 #include <string>
00254 
00255 /*
00256  * Simple trick to avoid "using" declaration in header for new compilers
00257  * but make it still working with old compilers which throw C2614 errors.
00258  *
00259  * Define MSVC_OLD_STUPID_BEHAVIOUR
00260  * for old compilers: VC++ 5 and 6 as well as eVC++ 3 and 4.
00261  */
00262 
00263 /*
00264  * Detect old MSVC++ compiler <= 6.0
00265  * 1200 - VC++ 6.0
00266  * 1200-1202 - eVC++ 4.0
00267  */
00268 #if defined(_MSC_VER)
00269 # if (_MSC_VER <= 1202)
00270 #  define MSVC_OLD_STUPID_BEHAVIOUR
00271 # endif
00272 #endif
00273 
00274 /* Avoid C2614 errors */
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     // NULL safe assign and free.
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     /* There seems to be a bug in the way the compiler count indices...
00330      * Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
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     /* case insensitive find alternates */
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 /*      URL processing functions, here since they depend on CPLString.  */
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 /*                            CPLStringList                             */
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     // CPLStringList &InsertStrings( int nInsertAtLineNo, char **papszNewLines );
00394     // CPLStringList &RemoveStrings( int nFirstLineToDelete, int nNumToRemove=1 );
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     // Deprecated.
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 /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
00432 
00433 #endif /* CPL_STRING_H_INCLUDED */

Generated for GDAL by doxygen 1.6.1.