GDAL
cpl_string.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * $Id: cpl_string.h 38064 2017-04-19 08:58:09Z 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  * 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 
00087 #define CSLT_HONOURSTRINGS      0x0001
00088 
00089 #define CSLT_ALLOWEMPTYTOKENS   0x0002
00090 
00091 #define CSLT_PRESERVEQUOTES     0x0004
00092 
00093 #define CSLT_PRESERVEESCAPES    0x0008
00094 
00095 #define CSLT_STRIPLEADSPACES    0x0010
00096 
00097 #define CSLT_STRIPENDSPACES     0x0020
00098 
00099 int CPL_DLL CSLPrint(char **papszStrList, FILE *fpOut);
00100 char CPL_DLL **CSLLoad(const char *pszFname) CPL_WARN_UNUSED_RESULT;
00101 #ifdef __cplusplus
00102 char CPL_DLL **CSLLoad2(
00103     const char *pszFname, int nMaxLines, int nMaxCols,
00104     const char * const * papszOptions) CPL_WARN_UNUSED_RESULT;
00105 #else
00106 char CPL_DLL **CSLLoad2(
00107     const char *pszFname, int nMaxLines, int nMaxCols,
00108     char **papszOptions) CPL_WARN_UNUSED_RESULT;
00109 #endif
00110 int CPL_DLL CSLSave(char **papszStrList, const char *pszFname);
00111 
00112 char CPL_DLL **CSLInsertStrings(char **papszStrList, int nInsertAtLineNo,
00113                          char **papszNewLines) CPL_WARN_UNUSED_RESULT;
00114 char CPL_DLL **CSLInsertString(char **papszStrList, int nInsertAtLineNo,
00115                                const char *pszNewLine) CPL_WARN_UNUSED_RESULT;
00116 char CPL_DLL **CSLRemoveStrings(
00117     char **papszStrList, int nFirstLineToDelete,
00118     int nNumToRemove, char ***ppapszRetStrings) CPL_WARN_UNUSED_RESULT;
00119 #ifdef __cplusplus
00120 int CPL_DLL CSLFindString( const char * const *papszList,
00121                            const char *pszTarget );
00122 int CPL_DLL CSLFindStringCaseSensitive( const char * const *papszList,
00123                                         const char *pszTarget );
00124 int CPL_DLL CSLPartialFindString( const char * const *papszHaystack,
00125                                   const char *pszNeedle );
00126 #else
00127 // Present non-const to C code that does not like passing non-const to const.
00128 // Should be ABI compatible with the const versions.
00129 int CPL_DLL CSLFindString( char **papszList, const char *pszTarget );
00130 int CPL_DLL CSLFindStringCaseSensitive( char * const *papszList,
00131                                         const char *pszTarget );
00132 int CPL_DLL CSLPartialFindString( char * const *papszHaystack,
00133                                   const char *pszNeedle );
00134 #endif
00135 int CPL_DLL CSLFindName(char **papszStrList, const char *pszName);
00136 int CPL_DLL CSLFetchBoolean( char **papszStrList, const char *pszKey,
00137                              int bDefault );
00138 
00139 /* TODO: Deprecate CSLTestBoolean.  Remove in GDAL 3.x. */
00140 int CPL_DLL CSLTestBoolean( const char *pszValue );
00141 /* Do not use CPLTestBoolean in C++ code.  Use CPLTestBool. */
00142 int CPL_DLL CPLTestBoolean( const char *pszValue );
00143 
00144 #ifdef __cplusplus
00145 #ifdef DO_NOT_USE_DEBUG_BOOL
00146 #define CPLTestBool(x) CPL_TO_BOOL(CPLTestBoolean(x))
00147 #define CPLFetchBool(list,key,default) \
00148     CPL_TO_BOOL(CSLFetchBoolean(list,key,default))
00149 #else /* DO_NOT_USE_DEBUG_BOOL */
00150 /* Prefer these for C++ code. */
00151 #ifdef DEBUG_BOOL
00152 extern "C++" {
00153 #endif
00154 bool CPL_DLL CPLTestBool( const char *pszValue );
00155 bool CPL_DLL CPLFetchBool( const char * const *papszStrList, const char *pszKey,
00156                            bool bDefault );
00157 #ifdef DEBUG_BOOL
00158 }
00159 #endif
00160 #endif
00161 #endif  /* __cplusplus */
00162 
00163 const char CPL_DLL *
00164       CPLParseNameValue( const char *pszNameValue, char **ppszKey );
00165 
00166 #ifdef __cplusplus
00167 const char CPL_DLL *
00168       CSLFetchNameValue( const char * const *papszStrList, const char *pszName);
00169 const char CPL_DLL *
00170       CSLFetchNameValueDef( const char * const *papszStrList,
00171                             const char *pszName,
00172                             const char *pszDefault );
00173 #else
00174 const char CPL_DLL *
00175       CSLFetchNameValue( char **papszStrList, const char *pszName);
00176 const char CPL_DLL *
00177       CSLFetchNameValueDef( char **papszStrList,
00178                             const char *pszName,
00179                             const char *pszDefault );
00180 #endif
00181 
00182 char CPL_DLL **
00183       CSLFetchNameValueMultiple(char **papszStrList, const char *pszName);
00184 char CPL_DLL **
00185       CSLAddNameValue(char **papszStrList,
00186                       const char *pszName,
00187                       const char *pszValue) CPL_WARN_UNUSED_RESULT;
00188 char CPL_DLL **
00189       CSLSetNameValue(char **papszStrList,
00190                       const char *pszName,
00191                       const char *pszValue) CPL_WARN_UNUSED_RESULT;
00192 void CPL_DLL CSLSetNameValueSeparator( char ** papszStrList,
00193                                        const char *pszSeparator );
00194 
00195 char CPL_DLL ** CSLParseCommandLine(const char* pszCommandLine);
00196 
00198 #define CPLES_BackslashQuotable 0
00199 
00200 #define CPLES_XML               1
00201 
00202 #define CPLES_URL               2
00203 
00204 #define CPLES_SQL               3
00205 
00206 #define CPLES_CSV               4
00207 
00208 #define CPLES_XML_BUT_QUOTES    5
00209 
00210 char CPL_DLL *CPLEscapeString( const char *pszString, int nLength,
00211                                int nScheme ) CPL_WARN_UNUSED_RESULT;
00212 char CPL_DLL *CPLUnescapeString( const char *pszString, int *pnLength,
00213                                  int nScheme ) CPL_WARN_UNUSED_RESULT;
00214 
00215 char CPL_DLL *CPLBinaryToHex( int nBytes,
00216                               const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
00217 GByte CPL_DLL *CPLHexToBinary( const char *pszHex,
00218                                int *pnBytes ) CPL_WARN_UNUSED_RESULT;
00219 
00220 char CPL_DLL *CPLBase64Encode( int nBytes,
00221                                const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
00222 int CPL_DLL CPLBase64DecodeInPlace( GByte* pszBase64 );
00223 
00225 typedef enum
00226 {
00227     CPL_VALUE_STRING,  
00228     CPL_VALUE_REAL,    
00229     CPL_VALUE_INTEGER  
00230 } CPLValueType;
00231 
00232 CPLValueType CPL_DLL CPLGetValueType(const char* pszValue);
00233 
00234 size_t CPL_DLL CPLStrlcpy(char* pszDest, const char* pszSrc, size_t nDestSize);
00235 size_t CPL_DLL CPLStrlcat(char* pszDest, const char* pszSrc, size_t nDestSize);
00236 size_t CPL_DLL CPLStrnlen(const char *pszStr, size_t nMaxLen);
00237 
00238 /* -------------------------------------------------------------------- */
00239 /*      Locale independent formatting functions.                        */
00240 /* -------------------------------------------------------------------- */
00241 int CPL_DLL CPLvsnprintf( char *str, size_t size,
00242                           CPL_FORMAT_STRING(const char* fmt),
00243                           va_list args )
00244     CPL_PRINT_FUNC_FORMAT(3, 0 );
00245 
00246 /* ALIAS_CPLSNPRINTF_AS_SNPRINTF might be defined to enable GCC 7 */
00247 /* -Wformat-truncation= warnings, but shouldn't be set for normal use */
00248 #if defined(ALIAS_CPLSNPRINTF_AS_SNPRINTF)
00249 #define CPLsnprintf snprintf
00250 #else
00251 int CPL_DLL CPLsnprintf( char *str, size_t size,
00252                          CPL_FORMAT_STRING(const char* fmt), ... )
00253     CPL_PRINT_FUNC_FORMAT(3, 4);
00254 #endif
00255 
00257 #if defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
00258 int CPL_DLL CPLsprintf( char *str, CPL_FORMAT_STRING(const char* fmt), ... )
00259     CPL_PRINT_FUNC_FORMAT(2, 3)
00260     CPL_WARN_DEPRECATED("Use CPLsnprintf instead");
00261 #else
00262 int CPL_DLL CPLsprintf( char *str, CPL_FORMAT_STRING(const char* fmt), ... )
00263     CPL_PRINT_FUNC_FORMAT(2, 3);
00264 #endif
00265 
00266 int CPL_DLL CPLprintf( CPL_FORMAT_STRING(const char* fmt), ... )
00267     CPL_PRINT_FUNC_FORMAT(1, 2);
00268 
00269 /* For some reason Doxygen_Suppress is needed to avoid warning. Not sure why */
00271 /* caution: only works with limited number of formats */
00272 int CPL_DLL CPLsscanf( const char* str,
00273                        CPL_SCANF_FORMAT_STRING(const char* fmt), ... )
00274     CPL_SCAN_FUNC_FORMAT(2, 3);
00277 const char CPL_DLL *CPLSPrintf( CPL_FORMAT_STRING(const char *fmt), ... )
00278     CPL_PRINT_FUNC_FORMAT(1, 2) CPL_WARN_UNUSED_RESULT;
00279 char CPL_DLL **CSLAppendPrintf( char **papszStrList,
00280                                 CPL_FORMAT_STRING(const char *fmt), ... )
00281     CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_UNUSED_RESULT;
00282 int CPL_DLL CPLVASPrintf( char **buf,
00283                           CPL_FORMAT_STRING(const char *fmt), va_list args )
00284     CPL_PRINT_FUNC_FORMAT(2, 0);
00285 
00286 /* -------------------------------------------------------------------- */
00287 /*      RFC 23 character set conversion/recoding API (cpl_recode.cpp).  */
00288 /* -------------------------------------------------------------------- */
00290 #define CPL_ENC_LOCALE     ""
00291 
00292 #define CPL_ENC_UTF8       "UTF-8"
00293 
00294 #define CPL_ENC_UTF16      "UTF-16"
00295 
00296 #define CPL_ENC_UCS2       "UCS-2"
00297 
00298 #define CPL_ENC_UCS4       "UCS-4"
00299 
00300 #define CPL_ENC_ASCII      "ASCII"
00301 
00302 #define CPL_ENC_ISO8859_1  "ISO-8859-1"
00303 
00304 int CPL_DLL  CPLEncodingCharSize( const char *pszEncoding );
00306 void CPL_DLL  CPLClearRecodeWarningFlags( void );
00308 char CPL_DLL *CPLRecode(
00309     const char *pszSource, const char *pszSrcEncoding,
00310     const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
00311 char CPL_DLL *CPLRecodeFromWChar(
00312     const wchar_t *pwszSource, const char *pszSrcEncoding,
00313     const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
00314 wchar_t CPL_DLL *CPLRecodeToWChar(
00315     const char *pszSource, const char *pszSrcEncoding,
00316     const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
00317 int CPL_DLL CPLIsUTF8( const char* pabyData, int nLen );
00318 char CPL_DLL *CPLForceToASCII(
00319     const char* pabyData, int nLen,
00320     char chReplacementChar ) CPL_WARN_UNUSED_RESULT;
00321 int CPL_DLL CPLStrlenUTF8( const char *pszUTF8Str );
00322 
00323 CPL_C_END
00324 
00325 /************************************************************************/
00326 /*                              CPLString                               */
00327 /************************************************************************/
00328 
00329 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
00330 
00331 extern "C++"
00332 {
00333 #ifndef DOXYGEN_SKIP
00334 #include <string>
00335 #endif
00336 
00338 class CPL_DLL CPLString : public std::string
00339 {
00340 public:
00341 
00343     CPLString(void) {}
00345     // cppcheck-suppress noExplicitConstructor
00346     CPLString( const std::string &oStr ) : std::string( oStr ) {}
00348     // cppcheck-suppress noExplicitConstructor
00349     CPLString( const char *pszStr ) : std::string( pszStr ) {}
00351     CPLString( const char *pszStr, size_t n ) : std::string( pszStr, n ) {}
00352 
00354     operator const char* (void) const { return c_str(); }
00355 
00357     char& operator[](std::string::size_type i)
00358     {
00359         return std::string::operator[](i);
00360     }
00361 
00363     const char& operator[](std::string::size_type i) const
00364     {
00365         return std::string::operator[](i);
00366     }
00367 
00369     char& operator[](int i)
00370     {
00371         return std::string::operator[](
00372             static_cast<std::string::size_type>(i));
00373     }
00374 
00376     const char& operator[](int i) const
00377     {
00378         return std::string::operator[](
00379             static_cast<std::string::size_type>(i));
00380     }
00381 
00382     // Note: This is standard in C++11.
00383 #ifndef HAVE_CXX11
00384 
00385     const char& back() const { return operator[](size()-1); }
00387     char& back() { return operator[](size()-1); }
00388 #endif
00389 
00391     void Clear() { resize(0); }
00392 
00396     void Seize( char *pszValue )
00397     {
00398         if (pszValue == NULL )
00399             Clear();
00400         else
00401         {
00402             *this = pszValue;
00403             CPLFree(pszValue);
00404         }
00405     }
00406 
00407     /* There seems to be a bug in the way the compiler count indices...
00408      * Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
00409     CPLString &Printf(
00410         CPL_FORMAT_STRING(const char *pszFormat), ... )
00411         CPL_PRINT_FUNC_FORMAT (2, 3);
00412     CPLString &vPrintf(
00413         CPL_FORMAT_STRING(const char *pszFormat), va_list args )
00414         CPL_PRINT_FUNC_FORMAT(2, 0);
00415     CPLString &FormatC( double dfValue, const char *pszFormat = NULL );
00416     CPLString &Trim();
00417     CPLString &Recode( const char *pszSrcEncoding, const char *pszDstEncoding );
00418     CPLString &replaceAll(
00419         const std::string &osBefore, const std::string& osAfter );
00420     CPLString &replaceAll( const std::string &osBefore, char chAfter );
00421     CPLString &replaceAll( char chBefore, const std::string &osAfter );
00422     CPLString &replaceAll( char chBefore, char chAfter );
00423 
00424     /* case insensitive find alternates */
00425     size_t    ifind( const std::string & str, size_t pos = 0 ) const;
00426     size_t    ifind( const char * s, size_t pos = 0 ) const;
00427     CPLString &toupper( void );
00428     CPLString &tolower( void );
00429 };
00430 
00431 CPLString CPL_DLL CPLOPrintf(CPL_FORMAT_STRING(const char *pszFormat), ... )
00432     CPL_PRINT_FUNC_FORMAT (1, 2);
00433 CPLString CPL_DLL CPLOvPrintf(
00434     CPL_FORMAT_STRING(const char *pszFormat), va_list args)
00435     CPL_PRINT_FUNC_FORMAT (1, 0);
00436 
00437 /* -------------------------------------------------------------------- */
00438 /*      URL processing functions, here since they depend on CPLString.  */
00439 /* -------------------------------------------------------------------- */
00440 CPLString CPL_DLL CPLURLGetValue(const char* pszURL, const char* pszKey);
00441 CPLString CPL_DLL CPLURLAddKVP(const char* pszURL, const char* pszKey,
00442                                const char* pszValue);
00443 
00444 /************************************************************************/
00445 /*                            CPLStringList                             */
00446 /************************************************************************/
00447 
00449 class CPL_DLL CPLStringList
00450 {
00451     char **papszList;
00452     mutable int nCount;
00453     mutable int nAllocation;
00454     bool   bOwnList;
00455     bool   bIsSorted;
00456 
00457     void   Initialize();
00458     void   MakeOurOwnCopy();
00459     void   EnsureAllocation( int nMaxLength );
00460     int    FindSortedInsertionPoint( const char *pszLine );
00461 
00462   public:
00463     CPLStringList();
00464     CPLStringList( char **papszList, int bTakeOwnership=TRUE );
00465     CPLStringList( const CPLStringList& oOther );
00466     ~CPLStringList();
00467 
00468     CPLStringList &Clear();
00469 
00471     int    size() const { return Count(); }
00472     int    Count() const;
00473 
00475     bool   empty() const { return Count() == 0; }
00476 
00477     CPLStringList &AddString( const char *pszNewString );
00478     CPLStringList &AddStringDirectly( char *pszNewString );
00479 
00480     CPLStringList &InsertString( int nInsertAtLineNo, const char *pszNewLine )
00481     { return InsertStringDirectly( nInsertAtLineNo, CPLStrdup(pszNewLine) ); }
00482     CPLStringList &InsertStringDirectly( int nInsertAtLineNo, char *pszNewLine);
00483 
00484     // CPLStringList &InsertStrings( int nInsertAtLineNo, char **papszNewLines );
00485     // CPLStringList &RemoveStrings( int nFirstLineToDelete, int nNumToRemove=1 );
00486 
00488     int    FindString( const char *pszTarget ) const
00489     { return CSLFindString( papszList, pszTarget ); }
00491     int    PartialFindString( const char *pszNeedle ) const
00492     { return CSLPartialFindString( papszList, pszNeedle ); }
00493 
00494     int    FindName( const char *pszName ) const;
00495     bool   FetchBool( const char *pszKey, bool bDefault ) const;
00496     // Deprecated.
00497     int    FetchBoolean( const char *pszKey, int bDefault ) const;
00498     const char *FetchNameValue( const char *pszKey ) const;
00499     const char *FetchNameValueDef(
00500         const char *pszKey, const char *pszDefault ) const;
00501     CPLStringList &AddNameValue( const char *pszKey, const char *pszValue );
00502     CPLStringList &SetNameValue( const char *pszKey, const char *pszValue );
00503 
00504     CPLStringList &Assign( char **papszListIn, int bTakeOwnership=TRUE );
00506     CPLStringList &operator=(char **papszListIn) {
00507       return Assign( papszListIn, TRUE ); }
00509     CPLStringList &operator=(const CPLStringList& oOther);
00510 
00512     char * operator[](int i);
00514     char * operator[](size_t i) { return (*this)[static_cast<int>(i)]; }
00516     const char * operator[](int i) const;
00518     const char * operator[](size_t i) const {
00519       return (*this)[static_cast<int>(i)]; }
00520 
00522     char **List() { return papszList; }
00523     char **StealList();
00524 
00525     CPLStringList &Sort();
00527     int    IsSorted() const { return bIsSorted; }
00528 
00530     operator char**(void) { return List(); }
00531 };
00532 
00533 } // extern "C++"
00534 
00535 #endif /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
00536 
00537 #endif /* CPL_STRING_H_INCLUDED */

Generated for GDAL by doxygen 1.7.6.1.