# cpl_string.h

Go to the documentation of this file.

    00001 /**********************************************************************
    00002  * $Id: cpl_string.h 25477 2013-01-10 00:27:30Z warmerdam $
    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  *
    00012  * Permission is hereby granted, free of charge, to any person obtaining a
    00013  * copy of this software and associated documentation files (the "Software"),
    00014  * to deal in the Software without restriction, including without limitation
    00015  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00016  * and/or sell copies of the Software, and to permit persons to whom the
    00017  * Software is furnished to do so, subject to the following conditions:
    00018  *
    00019  * The above copyright notice and this permission notice shall be included
    00020  * in all copies or substantial portions of the Software.
    00021  *
    00022  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00023  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00024  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
    00025  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00026  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    00027  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    00028  * DEALINGS IN THE SOFTWARE.
    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 /*      RFC 23 character set conversion/recoding API (cpl_recode.cpp).  */
    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 /*                              CPLString                               */
    00185 /************************************************************************/
    00186
    00187 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
    00188
    00189 #include <string>
    00190
    00191 /*
    00192  * Simple trick to avoid "using" declaration in header for new compilers
    00193  * but make it still working with old compilers which throw C2614 errors.
    00194  *
    00195  * Define MSVC_OLD_STUPID_BEHAVIOUR
    00196  * for old compilers: VC++ 5 and 6 as well as eVC++ 3 and 4.
    00197  */
    00198
    00199 /*
    00200  * Detect old MSVC++ compiler <= 6.0
    00201  * 1200 - VC++ 6.0
    00202  * 1200-1202 - eVC++ 4.0
    00203  */
    00204 #if defined(_MSC_VER)
    00205 # if (_MSC_VER <= 1202)
    00206 #  define MSVC_OLD_STUPID_BEHAVIOUR
    00207 # endif
    00208 #endif
    00209
    00210 /* Avoid C2614 errors */
    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 /* Remove annoying warnings in Microsoft eVC++ and Microsoft Visual C++ */
    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     // NULL safe assign and free.
    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     /* There seems to be a bug in the way the compiler count indices... Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
    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     /* case insensitive find alternates */
    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 /*      URL processing functions, here since they depend on CPLString.  */
    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 /*                            CPLStringList                             */
    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 //    CPLStringList &InsertStrings( int nInsertAtLineNo, char **papszNewLines );
    00327 //    CPLStringList &RemoveStrings( int nFirstLineToDelete, int nNumToRemove=1 );
    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 /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
    00360
    00361 #endif /* _CPL_STRING_H_INCLUDED */

Generated for GDAL by 1.7.1.