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

Generated for GDAL by doxygen 1.7.6.1.