cpl_string.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**********************************************************************
  2. * $Id: cpl_string.h 28025 2014-11-28 00:01:32Z rouault $
  3. *
  4. * Name: cpl_string.h
  5. * Project: CPL - Common Portability Library
  6. * Purpose: String and StringList functions.
  7. * Author: Daniel Morissette, dmorissette@mapgears.com
  8. *
  9. **********************************************************************
  10. * Copyright (c) 1998, Daniel Morissette
  11. * Copyright (c) 2008-2014, Even Rouault <even dot rouault at mines-paris dot org>
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included
  21. * in all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. * DEALINGS IN THE SOFTWARE.
  30. ****************************************************************************/
  31. #ifndef _CPL_STRING_H_INCLUDED
  32. #define _CPL_STRING_H_INCLUDED
  33. #include "cpl_vsi.h"
  34. #include "cpl_error.h"
  35. #include "cpl_conv.h"
  36. /**
  37. * \file cpl_string.h
  38. *
  39. * Various convenience functions for working with strings and string lists.
  40. *
  41. * A StringList is just an array of strings with the last pointer being
  42. * NULL. An empty StringList may be either a NULL pointer, or a pointer to
  43. * a pointer memory location with a NULL value.
  44. *
  45. * A common convention for StringLists is to use them to store name/value
  46. * lists. In this case the contents are treated like a dictionary of
  47. * name/value pairs. The actual data is formatted with each string having
  48. * the format "<name>:<value>" (though "=" is also an acceptable separator).
  49. * A number of the functions in the file operate on name/value style
  50. * string lists (such as CSLSetNameValue(), and CSLFetchNameValue()).
  51. *
  52. * To some extent the CPLStringList C++ class can be used to abstract
  53. * managing string lists a bit but still be able to return them from C
  54. * functions.
  55. *
  56. */
  57. CPL_C_START
  58. char CPL_DLL **CSLAddString(char **papszStrList, const char *pszNewString) CPL_WARN_UNUSED_RESULT;
  59. int CPL_DLL CSLCount(char **papszStrList);
  60. const char CPL_DLL *CSLGetField( char **, int );
  61. void CPL_DLL CPL_STDCALL CSLDestroy(char **papszStrList);
  62. char CPL_DLL **CSLDuplicate(char **papszStrList) CPL_WARN_UNUSED_RESULT;
  63. char CPL_DLL **CSLMerge( char **papszOrig, char **papszOverride ) CPL_WARN_UNUSED_RESULT;
  64. char CPL_DLL **CSLTokenizeString(const char *pszString ) CPL_WARN_UNUSED_RESULT;
  65. char CPL_DLL **CSLTokenizeStringComplex(const char *pszString,
  66. const char *pszDelimiter,
  67. int bHonourStrings, int bAllowEmptyTokens ) CPL_WARN_UNUSED_RESULT;
  68. char CPL_DLL **CSLTokenizeString2( const char *pszString,
  69. const char *pszDelimeter,
  70. int nCSLTFlags ) CPL_WARN_UNUSED_RESULT;
  71. #define CSLT_HONOURSTRINGS 0x0001
  72. #define CSLT_ALLOWEMPTYTOKENS 0x0002
  73. #define CSLT_PRESERVEQUOTES 0x0004
  74. #define CSLT_PRESERVEESCAPES 0x0008
  75. #define CSLT_STRIPLEADSPACES 0x0010
  76. #define CSLT_STRIPENDSPACES 0x0020
  77. int CPL_DLL CSLPrint(char **papszStrList, FILE *fpOut);
  78. char CPL_DLL **CSLLoad(const char *pszFname) CPL_WARN_UNUSED_RESULT;
  79. char CPL_DLL **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, char** papszOptions) CPL_WARN_UNUSED_RESULT;
  80. int CPL_DLL CSLSave(char **papszStrList, const char *pszFname);
  81. char CPL_DLL **CSLInsertStrings(char **papszStrList, int nInsertAtLineNo,
  82. char **papszNewLines) CPL_WARN_UNUSED_RESULT;
  83. char CPL_DLL **CSLInsertString(char **papszStrList, int nInsertAtLineNo,
  84. const char *pszNewLine) CPL_WARN_UNUSED_RESULT;
  85. char CPL_DLL **CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete,
  86. int nNumToRemove, char ***ppapszRetStrings) CPL_WARN_UNUSED_RESULT;
  87. int CPL_DLL CSLFindString( char **, const char * );
  88. int CPL_DLL CSLFindStringCaseSensitive( char **, const char * );
  89. int CPL_DLL CSLPartialFindString( char **papszHaystack,
  90. const char * pszNeedle );
  91. int CPL_DLL CSLFindName(char **papszStrList, const char *pszName);
  92. int CPL_DLL CSLTestBoolean( const char *pszValue );
  93. int CPL_DLL CSLFetchBoolean( char **papszStrList, const char *pszKey,
  94. int bDefault );
  95. const char CPL_DLL *
  96. CPLParseNameValue(const char *pszNameValue, char **ppszKey );
  97. const char CPL_DLL *
  98. CSLFetchNameValue(char **papszStrList, const char *pszName);
  99. const char CPL_DLL *
  100. CSLFetchNameValueDef(char **papszStrList, const char *pszName,
  101. const char *pszDefault );
  102. char CPL_DLL **
  103. CSLFetchNameValueMultiple(char **papszStrList, const char *pszName);
  104. char CPL_DLL **
  105. CSLAddNameValue(char **papszStrList,
  106. const char *pszName, const char *pszValue) CPL_WARN_UNUSED_RESULT;
  107. char CPL_DLL **
  108. CSLSetNameValue(char **papszStrList,
  109. const char *pszName, const char *pszValue) CPL_WARN_UNUSED_RESULT;
  110. void CPL_DLL CSLSetNameValueSeparator( char ** papszStrList,
  111. const char *pszSeparator );
  112. #define CPLES_BackslashQuotable 0
  113. #define CPLES_XML 1
  114. #define CPLES_URL 2
  115. #define CPLES_SQL 3
  116. #define CPLES_CSV 4
  117. #define CPLES_XML_BUT_QUOTES 5
  118. char CPL_DLL *CPLEscapeString( const char *pszString, int nLength,
  119. int nScheme ) CPL_WARN_UNUSED_RESULT;
  120. char CPL_DLL *CPLUnescapeString( const char *pszString, int *pnLength,
  121. int nScheme ) CPL_WARN_UNUSED_RESULT;
  122. char CPL_DLL *CPLBinaryToHex( int nBytes, const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
  123. GByte CPL_DLL *CPLHexToBinary( const char *pszHex, int *pnBytes ) CPL_WARN_UNUSED_RESULT;
  124. char CPL_DLL *CPLBase64Encode( int nBytes, const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
  125. int CPL_DLL CPLBase64DecodeInPlace(GByte* pszBase64);
  126. typedef enum
  127. {
  128. CPL_VALUE_STRING,
  129. CPL_VALUE_REAL,
  130. CPL_VALUE_INTEGER
  131. } CPLValueType;
  132. CPLValueType CPL_DLL CPLGetValueType(const char* pszValue);
  133. size_t CPL_DLL CPLStrlcpy(char* pszDest, const char* pszSrc, size_t nDestSize);
  134. size_t CPL_DLL CPLStrlcat(char* pszDest, const char* pszSrc, size_t nDestSize);
  135. size_t CPL_DLL CPLStrnlen (const char *pszStr, size_t nMaxLen);
  136. /* -------------------------------------------------------------------- */
  137. /* Locale independant formatting functions. */
  138. /* -------------------------------------------------------------------- */
  139. int CPL_DLL CPLvsnprintf(char *str, size_t size, const char* fmt, va_list args);
  140. int CPL_DLL CPLsnprintf(char *str, size_t size, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(3,4);
  141. int CPL_DLL CPLsprintf(char *str, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3);
  142. int CPL_DLL CPLprintf(const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(1, 2);
  143. int CPL_DLL CPLsscanf(const char* str, const char* fmt, ...); /* caution: only works with limited number of formats */
  144. const char CPL_DLL *CPLSPrintf(const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(1, 2);
  145. char CPL_DLL **CSLAppendPrintf(char **papszStrList, const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_UNUSED_RESULT;
  146. int CPL_DLL CPLVASPrintf(char **buf, const char *fmt, va_list args );
  147. /* -------------------------------------------------------------------- */
  148. /* RFC 23 character set conversion/recoding API (cpl_recode.cpp). */
  149. /* -------------------------------------------------------------------- */
  150. #define CPL_ENC_LOCALE ""
  151. #define CPL_ENC_UTF8 "UTF-8"
  152. #define CPL_ENC_UTF16 "UTF-16"
  153. #define CPL_ENC_UCS2 "UCS-2"
  154. #define CPL_ENC_UCS4 "UCS-4"
  155. #define CPL_ENC_ASCII "ASCII"
  156. #define CPL_ENC_ISO8859_1 "ISO-8859-1"
  157. int CPL_DLL CPLEncodingCharSize( const char *pszEncoding );
  158. void CPL_DLL CPLClearRecodeWarningFlags( void );
  159. char CPL_DLL *CPLRecode( const char *pszSource,
  160. const char *pszSrcEncoding,
  161. const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
  162. char CPL_DLL *CPLRecodeFromWChar( const wchar_t *pwszSource,
  163. const char *pszSrcEncoding,
  164. const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
  165. wchar_t CPL_DLL *CPLRecodeToWChar( const char *pszSource,
  166. const char *pszSrcEncoding,
  167. const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
  168. int CPL_DLL CPLIsUTF8(const char* pabyData, int nLen);
  169. char CPL_DLL *CPLForceToASCII(const char* pabyData, int nLen, char chReplacementChar) CPL_WARN_UNUSED_RESULT;
  170. int CPL_DLL CPLStrlenUTF8(const char *pszUTF8Str);
  171. CPL_C_END
  172. /************************************************************************/
  173. /* CPLString */
  174. /************************************************************************/
  175. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  176. #include <string>
  177. /*
  178. * Simple trick to avoid "using" declaration in header for new compilers
  179. * but make it still working with old compilers which throw C2614 errors.
  180. *
  181. * Define MSVC_OLD_STUPID_BEHAVIOUR
  182. * for old compilers: VC++ 5 and 6 as well as eVC++ 3 and 4.
  183. */
  184. /*
  185. * Detect old MSVC++ compiler <= 6.0
  186. * 1200 - VC++ 6.0
  187. * 1200-1202 - eVC++ 4.0
  188. */
  189. #if defined(_MSC_VER)
  190. # if (_MSC_VER <= 1202)
  191. # define MSVC_OLD_STUPID_BEHAVIOUR
  192. # endif
  193. #endif
  194. /* Avoid C2614 errors */
  195. #ifdef MSVC_OLD_STUPID_BEHAVIOUR
  196. using std::string;
  197. # define gdal_std_string string
  198. #else
  199. # define gdal_std_string std::string
  200. #endif
  201. /* Remove annoying warnings in Microsoft eVC++ and Microsoft Visual C++ */
  202. #if defined(WIN32CE)
  203. # pragma warning(disable:4251 4275 4786)
  204. #endif
  205. //! Convenient string class based on std::string.
  206. class CPL_DLL CPLString : public gdal_std_string
  207. {
  208. public:
  209. CPLString(void) {}
  210. CPLString( const std::string &oStr ) : gdal_std_string( oStr ) {}
  211. CPLString( const char *pszStr ) : gdal_std_string( pszStr ) {}
  212. operator const char* (void) const { return c_str(); }
  213. char& operator[](std::string::size_type i)
  214. {
  215. return gdal_std_string::operator[](i);
  216. }
  217. const char& operator[](std::string::size_type i) const
  218. {
  219. return gdal_std_string::operator[](i);
  220. }
  221. char& operator[](int i)
  222. {
  223. return gdal_std_string::operator[](static_cast<std::string::size_type>(i));
  224. }
  225. const char& operator[](int i) const
  226. {
  227. return gdal_std_string::operator[](static_cast<std::string::size_type>(i));
  228. }
  229. void Clear() { resize(0); }
  230. // NULL safe assign and free.
  231. void Seize(char *pszValue)
  232. {
  233. if (pszValue == NULL )
  234. Clear();
  235. else
  236. {
  237. *this = pszValue;
  238. CPLFree(pszValue);
  239. }
  240. }
  241. /* There seems to be a bug in the way the compiler count indices... Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
  242. CPLString &Printf( const char *pszFormat, ... ) CPL_PRINT_FUNC_FORMAT (2, 3);
  243. CPLString &vPrintf( const char *pszFormat, va_list args );
  244. CPLString &FormatC( double dfValue, const char *pszFormat = NULL );
  245. CPLString &Trim();
  246. CPLString &Recode( const char *pszSrcEncoding, const char *pszDstEncoding );
  247. /* case insensitive find alternates */
  248. size_t ifind( const std::string & str, size_t pos = 0 ) const;
  249. size_t ifind( const char * s, size_t pos = 0 ) const;
  250. CPLString &toupper( void );
  251. CPLString &tolower( void );
  252. };
  253. CPLString CPLOPrintf(const char *pszFormat, ... ) CPL_PRINT_FUNC_FORMAT (1, 2);
  254. CPLString CPLOvPrintf(const char *pszFormat, va_list args);
  255. /* -------------------------------------------------------------------- */
  256. /* URL processing functions, here since they depend on CPLString. */
  257. /* -------------------------------------------------------------------- */
  258. CPLString CPL_DLL CPLURLGetValue(const char* pszURL, const char* pszKey);
  259. CPLString CPL_DLL CPLURLAddKVP(const char* pszURL, const char* pszKey,
  260. const char* pszValue);
  261. /************************************************************************/
  262. /* CPLStringList */
  263. /************************************************************************/
  264. //! String list class designed around our use of C "char**" string lists.
  265. class CPL_DLL CPLStringList
  266. {
  267. char **papszList;
  268. mutable int nCount;
  269. mutable int nAllocation;
  270. int bOwnList;
  271. int bIsSorted;
  272. void Initialize();
  273. void MakeOurOwnCopy();
  274. void EnsureAllocation( int nMaxLength );
  275. int FindSortedInsertionPoint( const char *pszLine );
  276. public:
  277. CPLStringList();
  278. CPLStringList( char **papszList, int bTakeOwnership=TRUE );
  279. CPLStringList( const CPLStringList& oOther );
  280. ~CPLStringList();
  281. CPLStringList &Clear();
  282. int size() const { return Count(); }
  283. int Count() const;
  284. CPLStringList &AddString( const char *pszNewString );
  285. CPLStringList &AddStringDirectly( char *pszNewString );
  286. CPLStringList &InsertString( int nInsertAtLineNo, const char *pszNewLine )
  287. { return InsertStringDirectly( nInsertAtLineNo, CPLStrdup(pszNewLine) ); }
  288. CPLStringList &InsertStringDirectly( int nInsertAtLineNo, char *pszNewLine);
  289. // CPLStringList &InsertStrings( int nInsertAtLineNo, char **papszNewLines );
  290. // CPLStringList &RemoveStrings( int nFirstLineToDelete, int nNumToRemove=1 );
  291. int FindString( const char *pszTarget ) const
  292. { return CSLFindString( papszList, pszTarget ); }
  293. int PartialFindString( const char *pszNeedle ) const
  294. { return CSLPartialFindString( papszList, pszNeedle ); }
  295. int FindName( const char *pszName ) const;
  296. int FetchBoolean( const char *pszKey, int bDefault ) const;
  297. const char *FetchNameValue( const char *pszKey ) const;
  298. const char *FetchNameValueDef( const char *pszKey, const char *pszDefault ) const;
  299. CPLStringList &AddNameValue( const char *pszKey, const char *pszValue );
  300. CPLStringList &SetNameValue( const char *pszKey, const char *pszValue );
  301. CPLStringList &Assign( char **papszListIn, int bTakeOwnership=TRUE );
  302. CPLStringList &operator=(char **papszListIn) { return Assign( papszListIn, TRUE ); }
  303. CPLStringList &operator=(const CPLStringList& oOther);
  304. char * operator[](int i);
  305. char * operator[](size_t i) { return (*this)[(int)i]; }
  306. const char * operator[](int i) const;
  307. const char * operator[](size_t i) const { return (*this)[(int)i]; }
  308. char **List() { return papszList; }
  309. char **StealList();
  310. CPLStringList &Sort();
  311. int IsSorted() const { return bIsSorted; }
  312. operator char**(void) { return List(); }
  313. };
  314. #endif /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
  315. #endif /* _CPL_STRING_H_INCLUDED */