- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

# cpl_string.h

Go to the documentation of this file.

    00001 /**********************************************************************
    00002  * $Id: cpl_string.h 27044 2014-03-16 23:41:27Z 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_vsi.h"
    00036 #include "cpl_error.h"
    00037 #include "cpl_conv.h"
    00038
    00061 CPL_C_START
    00062
    00063 char CPL_DLL **CSLAddString(char **papszStrList, const char *pszNewString) CPL_WARN_UNUSED_RESULT;
    00064 int CPL_DLL CSLCount(char **papszStrList);
    00065 const char CPL_DLL *CSLGetField( char **, int );
    00066 void CPL_DLL CPL_STDCALL CSLDestroy(char **papszStrList);
    00067 char CPL_DLL **CSLDuplicate(char **papszStrList) CPL_WARN_UNUSED_RESULT;
    00068 char CPL_DLL **CSLMerge( char **papszOrig, char **papszOverride ) CPL_WARN_UNUSED_RESULT;
    00069
    00070 char CPL_DLL **CSLTokenizeString(const char *pszString ) CPL_WARN_UNUSED_RESULT;
    00071 char CPL_DLL **CSLTokenizeStringComplex(const char *pszString,
    00072                                    const char *pszDelimiter,
    00073                                    int bHonourStrings, int bAllowEmptyTokens ) CPL_WARN_UNUSED_RESULT;
    00074 char CPL_DLL **CSLTokenizeString2( const char *pszString,
    00075                                    const char *pszDelimeter,
    00076                                    int nCSLTFlags ) CPL_WARN_UNUSED_RESULT;
    00077
    00078 #define CSLT_HONOURSTRINGS      0x0001
    00079 #define CSLT_ALLOWEMPTYTOKENS   0x0002
    00080 #define CSLT_PRESERVEQUOTES     0x0004
    00081 #define CSLT_PRESERVEESCAPES    0x0008
    00082 #define CSLT_STRIPLEADSPACES    0x0010
    00083 #define CSLT_STRIPENDSPACES     0x0020
    00084
    00085 int CPL_DLL CSLPrint(char **papszStrList, FILE *fpOut);
    00086 char CPL_DLL **CSLLoad(const char *pszFname) CPL_WARN_UNUSED_RESULT;
    00087 char CPL_DLL **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, char** papszOptions) CPL_WARN_UNUSED_RESULT;
    00088 int CPL_DLL CSLSave(char **papszStrList, const char *pszFname);
    00089
    00090 char CPL_DLL **CSLInsertStrings(char **papszStrList, int nInsertAtLineNo,
    00091                          char **papszNewLines) CPL_WARN_UNUSED_RESULT;
    00092 char CPL_DLL **CSLInsertString(char **papszStrList, int nInsertAtLineNo,
    00093                                const char *pszNewLine) CPL_WARN_UNUSED_RESULT;
    00094 char CPL_DLL **CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete,
    00095                          int nNumToRemove, char ***ppapszRetStrings) CPL_WARN_UNUSED_RESULT;
    00096 int CPL_DLL CSLFindString( char **, const char * );
    00097 int CPL_DLL CSLPartialFindString( char **papszHaystack,
    00098         const char * pszNeedle );
    00099 int CPL_DLL CSLFindName(char **papszStrList, const char *pszName);
    00100 int CPL_DLL CSLTestBoolean( const char *pszValue );
    00101 int CPL_DLL CSLFetchBoolean( char **papszStrList, const char *pszKey,
    00102                              int bDefault );
    00103
    00104 const char CPL_DLL *CPLSPrintf(const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(1, 2);
    00105 char CPL_DLL **CSLAppendPrintf(char **papszStrList, const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_UNUSED_RESULT;
    00106 int CPL_DLL CPLVASPrintf(char **buf, const char *fmt, va_list args );
    00107
    00108 const char CPL_DLL *
    00109       CPLParseNameValue(const char *pszNameValue, char **ppszKey );
    00110 const char CPL_DLL *
    00111       CSLFetchNameValue(char **papszStrList, const char *pszName);
    00112 const char CPL_DLL *
    00113       CSLFetchNameValueDef(char **papszStrList, const char *pszName,
    00114                            const char *pszDefault );
    00115 char CPL_DLL **
    00116       CSLFetchNameValueMultiple(char **papszStrList, const char *pszName);
    00117 char CPL_DLL **
    00118       CSLAddNameValue(char **papszStrList,
    00119                       const char *pszName, const char *pszValue) CPL_WARN_UNUSED_RESULT;
    00120 char CPL_DLL **
    00121       CSLSetNameValue(char **papszStrList,
    00122                       const char *pszName, const char *pszValue) CPL_WARN_UNUSED_RESULT;
    00123 void CPL_DLL CSLSetNameValueSeparator( char ** papszStrList,
    00124                                        const char *pszSeparator );
    00125
    00126 #define CPLES_BackslashQuotable 0
    00127 #define CPLES_XML               1
    00128 #define CPLES_URL               2
    00129 #define CPLES_SQL               3
    00130 #define CPLES_CSV               4
    00131 #define CPLES_XML_BUT_QUOTES    5
    00132
    00133 char CPL_DLL *CPLEscapeString( const char *pszString, int nLength,
    00134                                int nScheme ) CPL_WARN_UNUSED_RESULT;
    00135 char CPL_DLL *CPLUnescapeString( const char *pszString, int *pnLength,
    00136                                  int nScheme ) CPL_WARN_UNUSED_RESULT;
    00137
    00138 char CPL_DLL *CPLBinaryToHex( int nBytes, const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
    00139 GByte CPL_DLL *CPLHexToBinary( const char *pszHex, int *pnBytes ) CPL_WARN_UNUSED_RESULT;
    00140
    00141 char CPL_DLL *CPLBase64Encode( int nBytes, const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
    00142 int CPL_DLL CPLBase64DecodeInPlace(GByte* pszBase64);
    00143
    00144 typedef enum
    00145 {
    00146     CPL_VALUE_STRING,
    00147     CPL_VALUE_REAL,
    00148     CPL_VALUE_INTEGER
    00149 } CPLValueType;
    00150
    00151 CPLValueType CPL_DLL CPLGetValueType(const char* pszValue);
    00152
    00153 size_t CPL_DLL CPLStrlcpy(char* pszDest, const char* pszSrc, size_t nDestSize);
    00154 size_t CPL_DLL CPLStrlcat(char* pszDest, const char* pszSrc, size_t nDestSize);
    00155 size_t CPL_DLL CPLStrnlen (const char *pszStr, size_t nMaxLen);
    00156
    00157 /* -------------------------------------------------------------------- */
    00158 /*      RFC 23 character set conversion/recoding API (cpl_recode.cpp).  */
    00159 /* -------------------------------------------------------------------- */
    00160 #define CPL_ENC_LOCALE     ""
    00161 #define CPL_ENC_UTF8       "UTF-8"
    00162 #define CPL_ENC_UTF16      "UTF-16"
    00163 #define CPL_ENC_UCS2       "UCS-2"
    00164 #define CPL_ENC_UCS4       "UCS-4"
    00165 #define CPL_ENC_ASCII      "ASCII"
    00166 #define CPL_ENC_ISO8859_1  "ISO-8859-1"
    00167
    00168 int CPL_DLL  CPLEncodingCharSize( const char *pszEncoding );
    00169 void CPL_DLL  CPLClearRecodeWarningFlags( void );
    00170 char CPL_DLL *CPLRecode( const char *pszSource,
    00171                          const char *pszSrcEncoding,
    00172                          const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
    00173 char CPL_DLL *CPLRecodeFromWChar( const wchar_t *pwszSource,
    00174                                   const char *pszSrcEncoding,
    00175                                   const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
    00176 wchar_t CPL_DLL *CPLRecodeToWChar( const char *pszSource,
    00177                                    const char *pszSrcEncoding,
    00178                                    const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
    00179 int CPL_DLL CPLIsUTF8(const char* pabyData, int nLen);
    00180 char CPL_DLL *CPLForceToASCII(const char* pabyData, int nLen, char chReplacementChar) CPL_WARN_UNUSED_RESULT;
    00181 int CPL_DLL CPLStrlenUTF8(const char *pszUTF8Str);
    00182
    00183 CPL_C_END
    00184
    00185 /************************************************************************/
    00186 /*                              CPLString                               */
    00187 /************************************************************************/
    00188
    00189 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
    00190
    00191 #include <string>
    00192
    00193 /*
    00194  * Simple trick to avoid "using" declaration in header for new compilers
    00195  * but make it still working with old compilers which throw C2614 errors.
    00196  *
    00197  * Define MSVC_OLD_STUPID_BEHAVIOUR
    00198  * for old compilers: VC++ 5 and 6 as well as eVC++ 3 and 4.
    00199  */
    00200
    00201 /*
    00202  * Detect old MSVC++ compiler <= 6.0
    00203  * 1200 - VC++ 6.0
    00204  * 1200-1202 - eVC++ 4.0
    00205  */
    00206 #if defined(_MSC_VER)
    00207 # if (_MSC_VER <= 1202)
    00208 #  define MSVC_OLD_STUPID_BEHAVIOUR
    00209 # endif
    00210 #endif
    00211
    00212 /* Avoid C2614 errors */
    00213 #ifdef MSVC_OLD_STUPID_BEHAVIOUR
    00214     using std::string;
    00215 # define gdal_std_string string
    00216 #else
    00217 # define gdal_std_string std::string
    00218 #endif
    00219
    00220 /* Remove annoying warnings in Microsoft eVC++ and Microsoft Visual C++ */
    00221 #if defined(WIN32CE)
    00222 #  pragma warning(disable:4251 4275 4786)
    00223 #endif
    00224
    00226 class CPL_DLL CPLString : public gdal_std_string
    00227 {
    00228 public:
    00229
    00230
    00231     CPLString(void) {}
    00232     CPLString( const std::string &oStr ) : gdal_std_string( oStr ) {}
    00233     CPLString( const char *pszStr ) : gdal_std_string( pszStr ) {}
    00234
    00235     operator const char* (void) const { return c_str(); }
    00236
    00237     char& operator[](std::string::size_type i)
    00238     {
    00239         return gdal_std_string::operator[](i);
    00240     }
    00241
    00242     const char& operator[](std::string::size_type i) const
    00243     {
    00244         return gdal_std_string::operator[](i);
    00245     }
    00246
    00247     char& operator[](int i)
    00248     {
    00249         return gdal_std_string::operator[](static_cast<std::string::size_type>(i));
    00250     }
    00251
    00252     const char& operator[](int i) const
    00253     {
    00254         return gdal_std_string::operator[](static_cast<std::string::size_type>(i));
    00255     }
    00256
    00257     void Clear() { resize(0); }
    00258
    00259     // NULL safe assign and free.
    00260     void Seize(char *pszValue)
    00261     {
    00262         if (pszValue == NULL )
    00263             Clear();
    00264         else
    00265         {
    00266             *this = pszValue;
    00267             CPLFree(pszValue);
    00268         }
    00269     }
    00270
    00271     /* There seems to be a bug in the way the compiler count indices... Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
    00272     CPLString &Printf( const char *pszFormat, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
    00273     CPLString &vPrintf( const char *pszFormat, va_list args );
    00274     CPLString &FormatC( double dfValue, const char *pszFormat = NULL );
    00275     CPLString &Trim();
    00276     CPLString &Recode( const char *pszSrcEncoding, const char *pszDstEncoding );
    00277
    00278     /* case insensitive find alternates */
    00279     size_t    ifind( const std::string & str, size_t pos = 0 ) const;
    00280     size_t    ifind( const char * s, size_t pos = 0 ) const;
    00281     CPLString &toupper( void );
    00282     CPLString &tolower( void );
    00283 };
    00284
    00285 CPLString CPLOPrintf(const char *pszFormat, ... ) CPL_PRINT_FUNC_FORMAT (1, 2);
    00286 CPLString CPLOvPrintf(const char *pszFormat, va_list args);
    00287
    00288 /* -------------------------------------------------------------------- */
    00289 /*      URL processing functions, here since they depend on CPLString.  */
    00290 /* -------------------------------------------------------------------- */
    00291 CPLString CPL_DLL CPLURLGetValue(const char* pszURL, const char* pszKey);
    00292 CPLString CPL_DLL CPLURLAddKVP(const char* pszURL, const char* pszKey,
    00293                                const char* pszValue);
    00294
    00295 /************************************************************************/
    00296 /*                            CPLStringList                             */
    00297 /************************************************************************/
    00298
    00300 class CPL_DLL CPLStringList
    00301 {
    00302     char **papszList;
    00303     mutable int nCount;
    00304     mutable int nAllocation;
    00305     int    bOwnList;
    00306     int    bIsSorted;
    00307
    00308     void   Initialize();
    00309     void   MakeOurOwnCopy();
    00310     void   EnsureAllocation( int nMaxLength );
    00311     int    FindSortedInsertionPoint( const char *pszLine );
    00312
    00313   public:
    00314     CPLStringList();
    00315     CPLStringList( char **papszList, int bTakeOwnership=TRUE );
    00316     CPLStringList( const CPLStringList& oOther );
    00317     ~CPLStringList();
    00318
    00319     CPLStringList &Clear();
    00320
    00321     int    size() const { return Count(); }
    00322     int    Count() const;
    00323
    00324     CPLStringList &AddString( const char *pszNewString );
    00325     CPLStringList &AddStringDirectly( char *pszNewString );
    00326
    00327     CPLStringList &InsertString( int nInsertAtLineNo, const char *pszNewLine )
    00328     { return InsertStringDirectly( nInsertAtLineNo, CPLStrdup(pszNewLine) ); }
    00329     CPLStringList &InsertStringDirectly( int nInsertAtLineNo, char *pszNewLine);
    00330
    00331 //    CPLStringList &InsertStrings( int nInsertAtLineNo, char **papszNewLines );
    00332 //    CPLStringList &RemoveStrings( int nFirstLineToDelete, int nNumToRemove=1 );
    00333
    00334     int    FindString( const char *pszTarget ) const
    00335     { return CSLFindString( papszList, pszTarget ); }
    00336     int    PartialFindString( const char *pszNeedle ) const
    00337     { return CSLPartialFindString( papszList, pszNeedle ); }
    00338
    00339     int    FindName( const char *pszName ) const;
    00340     int    FetchBoolean( const char *pszKey, int bDefault ) const;
    00341     const char *FetchNameValue( const char *pszKey ) const;
    00342     const char *FetchNameValueDef( const char *pszKey, const char *pszDefault ) const;
    00343     CPLStringList &AddNameValue( const char *pszKey, const char *pszValue );
    00344     CPLStringList &SetNameValue( const char *pszKey, const char *pszValue );
    00345
    00346     CPLStringList &Assign( char **papszList, int bTakeOwnership=TRUE );
    00347     CPLStringList &operator=(char **papszListIn) { return Assign( papszListIn, TRUE ); }
    00348     CPLStringList &operator=(const CPLStringList& oOther);
    00349
    00350     char * operator[](int i);
    00351     char * operator[](size_t i) { return (*this)[(int)i]; }
    00352     const char * operator[](int i) const;
    00353     const char * operator[](size_t i) const { return (*this)[(int)i]; }
    00354
    00355     char **List() { return papszList; }
    00356     char **StealList();
    00357
    00358     CPLStringList &Sort();
    00359     int    IsSorted() const { return bIsSorted; }
    00360
    00361     operator char**(void) { return List(); }
    00362 };
    00363
    00364 #endif /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
    00365
    00366 #endif /* _CPL_STRING_H_INCLUDED */

Generated for GDAL by 1.7.1.