- Main Page
- Related Pages
- Classes
- Files

# cpl_string.h

Go to the documentation of this file.

    00001 /**********************************************************************
    00002  * $Id: cpl_string.h 23719 2012-01-07 18:18:41Z 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  *
    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   /* unescape only for now */
    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 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 /*                              CPLString                               */
    00184 /************************************************************************/
    00185
    00186 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
    00187
    00188 #include <string>
    00189
    00190 /*
    00191  * Simple trick to avoid "using" declaration in header for new compilers
    00192  * but make it still working with old compilers which throw C2614 errors.
    00193  *
    00194  * Define MSVC_OLD_STUPID_BEHAVIOUR
    00195  * for old compilers: VC++ 5 and 6 as well as eVC++ 3 and 4.
    00196  */
    00197
    00198 /*
    00199  * Detect old MSVC++ compiler <= 6.0
    00200  * 1200 - VC++ 6.0
    00201  * 1200-1202 - eVC++ 4.0
    00202  */
    00203 #if defined(_MSC_VER)
    00204 # if (_MSC_VER <= 1202)
    00205 #  define MSVC_OLD_STUPID_BEHAVIOUR
    00206 # endif
    00207 #endif
    00208
    00209 /* Avoid C2614 errors */
    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 /* Remove annoying warnings in Microsoft eVC++ and Microsoft Visual C++ */
    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     /* There seems to be a bug in the way the compiler count indices... Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
    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     /* case insensitive find alternates */
    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 /*      URL processing functions, here since they depend on CPLString.  */
    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 /*                            CPLStringList                             */
    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 //    CPLStringList &InsertStrings( int nInsertAtLineNo, char **papszNewLines );
    00314 //    CPLStringList &RemoveStrings( int nFirstLineToDelete, int nNumToRemove=1 );
    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 /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
    00347
    00348 #endif /* _CPL_STRING_H_INCLUDED */

Generated for GDAL by 1.5.6.