gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

562 lines
22KB

  1. /**********************************************************************
  2. * $Id: cpl_string.h c7b8420914709a42da0c491963bfa89a4b9df3d5 2019-03-23 16:03:44 +0100 Even 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_error.h"
  34. #include "cpl_conv.h"
  35. #include "cpl_vsi.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,
  59. const char *pszNewString) CPL_WARN_UNUSED_RESULT;
  60. char CPL_DLL **CSLAddStringMayFail(
  61. char **papszStrList, const char *pszNewString) CPL_WARN_UNUSED_RESULT;
  62. int CPL_DLL CSLCount(CSLConstList papszStrList);
  63. const char CPL_DLL *CSLGetField( CSLConstList, int );
  64. void CPL_DLL CPL_STDCALL CSLDestroy(char **papszStrList);
  65. char CPL_DLL **CSLDuplicate(CSLConstList papszStrList) CPL_WARN_UNUSED_RESULT;
  66. char CPL_DLL **CSLMerge( char **papszOrig,
  67. CSLConstList papszOverride ) CPL_WARN_UNUSED_RESULT;
  68. char CPL_DLL **CSLTokenizeString(const char *pszString ) CPL_WARN_UNUSED_RESULT;
  69. char CPL_DLL **CSLTokenizeStringComplex(
  70. const char *pszString, const char *pszDelimiter, int bHonourStrings,
  71. int bAllowEmptyTokens ) CPL_WARN_UNUSED_RESULT;
  72. char CPL_DLL **CSLTokenizeString2( const char *pszString,
  73. const char *pszDelimiter,
  74. int nCSLTFlags ) CPL_WARN_UNUSED_RESULT;
  75. /** Flag for CSLTokenizeString2() to honour strings */
  76. #define CSLT_HONOURSTRINGS 0x0001
  77. /** Flag for CSLTokenizeString2() to allow empty tokens */
  78. #define CSLT_ALLOWEMPTYTOKENS 0x0002
  79. /** Flag for CSLTokenizeString2() to preserve quotes */
  80. #define CSLT_PRESERVEQUOTES 0x0004
  81. /** Flag for CSLTokenizeString2() to preserve escape characters */
  82. #define CSLT_PRESERVEESCAPES 0x0008
  83. /** Flag for CSLTokenizeString2() to strip leading spaces */
  84. #define CSLT_STRIPLEADSPACES 0x0010
  85. /** Flag for CSLTokenizeString2() to strip trailaing spaces */
  86. #define CSLT_STRIPENDSPACES 0x0020
  87. int CPL_DLL CSLPrint(CSLConstList papszStrList, FILE *fpOut);
  88. char CPL_DLL **CSLLoad(const char *pszFname) CPL_WARN_UNUSED_RESULT;
  89. char CPL_DLL **CSLLoad2(
  90. const char *pszFname, int nMaxLines, int nMaxCols,
  91. CSLConstList papszOptions) CPL_WARN_UNUSED_RESULT;
  92. int CPL_DLL CSLSave(CSLConstList papszStrList, const char *pszFname);
  93. char CPL_DLL **CSLInsertStrings(char **papszStrList, int nInsertAtLineNo,
  94. CSLConstList papszNewLines) CPL_WARN_UNUSED_RESULT;
  95. char CPL_DLL **CSLInsertString(char **papszStrList, int nInsertAtLineNo,
  96. const char *pszNewLine) CPL_WARN_UNUSED_RESULT;
  97. char CPL_DLL **CSLRemoveStrings(
  98. char **papszStrList, int nFirstLineToDelete,
  99. int nNumToRemove, char ***ppapszRetStrings) CPL_WARN_UNUSED_RESULT;
  100. int CPL_DLL CSLFindString( CSLConstList papszList, const char *pszTarget );
  101. int CPL_DLL CSLFindStringCaseSensitive( CSLConstList papszList,
  102. const char *pszTarget );
  103. int CPL_DLL CSLPartialFindString( CSLConstList papszHaystack,
  104. const char *pszNeedle );
  105. int CPL_DLL CSLFindName(CSLConstList papszStrList, const char *pszName);
  106. int CPL_DLL CSLFetchBoolean( CSLConstList papszStrList, const char *pszKey,
  107. int bDefault );
  108. /* TODO: Deprecate CSLTestBoolean. Remove in GDAL 3.x. */
  109. int CPL_DLL CSLTestBoolean( const char *pszValue );
  110. /* Do not use CPLTestBoolean in C++ code. Use CPLTestBool. */
  111. int CPL_DLL CPLTestBoolean( const char *pszValue );
  112. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  113. #ifdef DO_NOT_USE_DEBUG_BOOL
  114. #define CPLTestBool(x) CPL_TO_BOOL(CPLTestBoolean(x))
  115. #define CPLFetchBool(list,key,default) \
  116. CPL_TO_BOOL(CSLFetchBoolean(list,key,default))
  117. #else /* DO_NOT_USE_DEBUG_BOOL */
  118. /* Prefer these for C++ code. */
  119. #ifdef DEBUG_BOOL
  120. extern "C++" {
  121. #endif
  122. bool CPL_DLL CPLTestBool( const char *pszValue );
  123. bool CPL_DLL CPLFetchBool( CSLConstList papszStrList, const char *pszKey,
  124. bool bDefault );
  125. #ifdef DEBUG_BOOL
  126. }
  127. #endif
  128. #endif
  129. #endif /* __cplusplus */
  130. const char CPL_DLL *
  131. CPLParseNameValue( const char *pszNameValue, char **ppszKey );
  132. const char CPL_DLL *
  133. CSLFetchNameValue( CSLConstList papszStrList, const char *pszName);
  134. const char CPL_DLL *
  135. CSLFetchNameValueDef( CSLConstList papszStrList,
  136. const char *pszName,
  137. const char *pszDefault );
  138. char CPL_DLL **
  139. CSLFetchNameValueMultiple(CSLConstList papszStrList, const char *pszName);
  140. char CPL_DLL **
  141. CSLAddNameValue(char **papszStrList,
  142. const char *pszName,
  143. const char *pszValue) CPL_WARN_UNUSED_RESULT;
  144. char CPL_DLL **
  145. CSLSetNameValue(char **papszStrList,
  146. const char *pszName,
  147. const char *pszValue) CPL_WARN_UNUSED_RESULT;
  148. void CPL_DLL CSLSetNameValueSeparator( char ** papszStrList,
  149. const char *pszSeparator );
  150. char CPL_DLL ** CSLParseCommandLine(const char* pszCommandLine);
  151. /** Scheme for CPLEscapeString()/CPLUnescapeString() for backlash quoting */
  152. #define CPLES_BackslashQuotable 0
  153. /** Scheme for CPLEscapeString()/CPLUnescapeString() for XML */
  154. #define CPLES_XML 1
  155. /** Scheme for CPLEscapeString()/CPLUnescapeString() for URL */
  156. #define CPLES_URL 2
  157. /** Scheme for CPLEscapeString()/CPLUnescapeString() for SQL */
  158. #define CPLES_SQL 3
  159. /** Scheme for CPLEscapeString()/CPLUnescapeString() for CSV */
  160. #define CPLES_CSV 4
  161. /** Scheme for CPLEscapeString()/CPLUnescapeString() for XML (preserves quotes) */
  162. #define CPLES_XML_BUT_QUOTES 5
  163. /** Scheme for CPLEscapeString()/CPLUnescapeString() for CSV (forced quoting) */
  164. #define CPLES_CSV_FORCE_QUOTING 6
  165. /** Scheme for CPLEscapeString()/CPLUnescapeString() for SQL identifiers */
  166. #define CPLES_SQLI 7
  167. char CPL_DLL *CPLEscapeString( const char *pszString, int nLength,
  168. int nScheme ) CPL_WARN_UNUSED_RESULT;
  169. char CPL_DLL *CPLUnescapeString( const char *pszString, int *pnLength,
  170. int nScheme ) CPL_WARN_UNUSED_RESULT;
  171. char CPL_DLL *CPLBinaryToHex( int nBytes,
  172. const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
  173. GByte CPL_DLL *CPLHexToBinary( const char *pszHex,
  174. int *pnBytes ) CPL_WARN_UNUSED_RESULT;
  175. char CPL_DLL *CPLBase64Encode( int nBytes,
  176. const GByte *pabyData ) CPL_WARN_UNUSED_RESULT;
  177. int CPL_DLL CPLBase64DecodeInPlace( GByte* pszBase64 ) CPL_WARN_UNUSED_RESULT;
  178. /** Type of value */
  179. typedef enum
  180. {
  181. CPL_VALUE_STRING, /**< String */
  182. CPL_VALUE_REAL, /**< Real number */
  183. CPL_VALUE_INTEGER /**< Integer */
  184. } CPLValueType;
  185. CPLValueType CPL_DLL CPLGetValueType(const char* pszValue);
  186. size_t CPL_DLL CPLStrlcpy(char* pszDest, const char* pszSrc, size_t nDestSize);
  187. size_t CPL_DLL CPLStrlcat(char* pszDest, const char* pszSrc, size_t nDestSize);
  188. size_t CPL_DLL CPLStrnlen(const char *pszStr, size_t nMaxLen);
  189. /* -------------------------------------------------------------------- */
  190. /* Locale independent formatting functions. */
  191. /* -------------------------------------------------------------------- */
  192. int CPL_DLL CPLvsnprintf( char *str, size_t size,
  193. CPL_FORMAT_STRING(const char* fmt),
  194. va_list args )
  195. CPL_PRINT_FUNC_FORMAT(3, 0 );
  196. /* ALIAS_CPLSNPRINTF_AS_SNPRINTF might be defined to enable GCC 7 */
  197. /* -Wformat-truncation= warnings, but shouldn't be set for normal use */
  198. #if defined(ALIAS_CPLSNPRINTF_AS_SNPRINTF)
  199. #define CPLsnprintf snprintf
  200. #else
  201. int CPL_DLL CPLsnprintf( char *str, size_t size,
  202. CPL_FORMAT_STRING(const char* fmt), ... )
  203. CPL_PRINT_FUNC_FORMAT(3, 4);
  204. #endif
  205. /*! @cond Doxygen_Suppress */
  206. #if defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
  207. int CPL_DLL CPLsprintf( char *str, CPL_FORMAT_STRING(const char* fmt), ... )
  208. CPL_PRINT_FUNC_FORMAT(2, 3)
  209. CPL_WARN_DEPRECATED("Use CPLsnprintf instead");
  210. #else
  211. int CPL_DLL CPLsprintf( char *str, CPL_FORMAT_STRING(const char* fmt), ... )
  212. CPL_PRINT_FUNC_FORMAT(2, 3);
  213. #endif
  214. /*! @endcond */
  215. int CPL_DLL CPLprintf( CPL_FORMAT_STRING(const char* fmt), ... )
  216. CPL_PRINT_FUNC_FORMAT(1, 2);
  217. /* For some reason Doxygen_Suppress is needed to avoid warning. Not sure why */
  218. /*! @cond Doxygen_Suppress */
  219. /* caution: only works with limited number of formats */
  220. int CPL_DLL CPLsscanf( const char* str,
  221. CPL_SCANF_FORMAT_STRING(const char* fmt), ... )
  222. CPL_SCAN_FUNC_FORMAT(2, 3);
  223. /*! @endcond */
  224. const char CPL_DLL *CPLSPrintf( CPL_FORMAT_STRING(const char *fmt), ... )
  225. CPL_PRINT_FUNC_FORMAT(1, 2) CPL_WARN_UNUSED_RESULT;
  226. char CPL_DLL **CSLAppendPrintf( char **papszStrList,
  227. CPL_FORMAT_STRING(const char *fmt), ... )
  228. CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_UNUSED_RESULT;
  229. int CPL_DLL CPLVASPrintf( char **buf,
  230. CPL_FORMAT_STRING(const char *fmt), va_list args )
  231. CPL_PRINT_FUNC_FORMAT(2, 0);
  232. /* -------------------------------------------------------------------- */
  233. /* RFC 23 character set conversion/recoding API (cpl_recode.cpp). */
  234. /* -------------------------------------------------------------------- */
  235. /** Encoding of the current locale */
  236. #define CPL_ENC_LOCALE ""
  237. /** UTF-8 encoding */
  238. #define CPL_ENC_UTF8 "UTF-8"
  239. /** UTF-16 encoding */
  240. #define CPL_ENC_UTF16 "UTF-16"
  241. /** UCS-2 encoding */
  242. #define CPL_ENC_UCS2 "UCS-2"
  243. /** UCS-4 encoding */
  244. #define CPL_ENC_UCS4 "UCS-4"
  245. /** ASCII encoding */
  246. #define CPL_ENC_ASCII "ASCII"
  247. /** ISO-8859-1 (LATIN1) encoding */
  248. #define CPL_ENC_ISO8859_1 "ISO-8859-1"
  249. int CPL_DLL CPLEncodingCharSize( const char *pszEncoding );
  250. /*! @cond Doxygen_Suppress */
  251. void CPL_DLL CPLClearRecodeWarningFlags( void );
  252. /*! @endcond */
  253. char CPL_DLL *CPLRecode(
  254. const char *pszSource, const char *pszSrcEncoding,
  255. const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
  256. char CPL_DLL *CPLRecodeFromWChar(
  257. const wchar_t *pwszSource, const char *pszSrcEncoding,
  258. const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
  259. wchar_t CPL_DLL *CPLRecodeToWChar(
  260. const char *pszSource, const char *pszSrcEncoding,
  261. const char *pszDstEncoding ) CPL_WARN_UNUSED_RESULT;
  262. int CPL_DLL CPLIsUTF8( const char* pabyData, int nLen );
  263. char CPL_DLL *CPLForceToASCII(
  264. const char* pabyData, int nLen,
  265. char chReplacementChar ) CPL_WARN_UNUSED_RESULT;
  266. int CPL_DLL CPLStrlenUTF8( const char *pszUTF8Str );
  267. CPL_C_END
  268. /************************************************************************/
  269. /* CPLString */
  270. /************************************************************************/
  271. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  272. extern "C++"
  273. {
  274. #ifndef DOXYGEN_SKIP
  275. #include <string>
  276. #endif
  277. // VC++ implicitly applies __declspec(dllexport) to template base classes
  278. // of classes marked with __declspec(dllexport).
  279. // Hence, if marked with CPL_DLL, VC++ would export symbols for the specialization
  280. // of std::basic_string<char>, since it is a base class of CPLString.
  281. // As a result, if an application linked both gdal.dll and a static library that
  282. // (implicitly) instantiates std::string (almost all do!), then the linker would
  283. // emit an error concerning duplicate symbols for std::string.
  284. // The least intrusive solution is to not mark the whole class with
  285. // __declspec(dllexport) for VC++, but only its non-inline methods.
  286. #ifdef _MSC_VER
  287. # define CPLSTRING_CLASS_DLL
  288. # define CPLSTRING_METHOD_DLL CPL_DLL
  289. #else
  290. /*! @cond Doxygen_Suppress */
  291. # define CPLSTRING_CLASS_DLL CPL_DLL
  292. # define CPLSTRING_METHOD_DLL
  293. /*! @endcond */
  294. #endif
  295. //! Convenient string class based on std::string.
  296. class CPLSTRING_CLASS_DLL CPLString : public std::string
  297. {
  298. public:
  299. /** Constructor */
  300. CPLString(void) {}
  301. /** Constructor */
  302. // cppcheck-suppress noExplicitConstructor
  303. CPLString( const std::string &oStr ) : std::string( oStr ) {}
  304. /** Constructor */
  305. // cppcheck-suppress noExplicitConstructor
  306. CPLString( const char *pszStr ) : std::string( pszStr ) {}
  307. /** Constructor */
  308. CPLString( const char *pszStr, size_t n ) : std::string( pszStr, n ) {}
  309. /** Return string as zero terminated character array */
  310. operator const char* (void) const { return c_str(); }
  311. /** Return character at specified index */
  312. char& operator[](std::string::size_type i)
  313. {
  314. return std::string::operator[](i);
  315. }
  316. /** Return character at specified index */
  317. const char& operator[](std::string::size_type i) const
  318. {
  319. return std::string::operator[](i);
  320. }
  321. /** Return character at specified index */
  322. char& operator[](int i)
  323. {
  324. return std::string::operator[](
  325. static_cast<std::string::size_type>(i));
  326. }
  327. /** Return character at specified index */
  328. const char& operator[](int i) const
  329. {
  330. return std::string::operator[](
  331. static_cast<std::string::size_type>(i));
  332. }
  333. /** Clear the string */
  334. void Clear() { resize(0); }
  335. /** Assign specified string and take ownership of it (assumed to be
  336. * allocated with CPLMalloc()). NULL can be safely passed to clear the
  337. * string. */
  338. void Seize( char *pszValue )
  339. {
  340. if (pszValue == nullptr )
  341. Clear();
  342. else
  343. {
  344. *this = pszValue;
  345. CPLFree(pszValue);
  346. }
  347. }
  348. /* There seems to be a bug in the way the compiler count indices...
  349. * Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
  350. CPLSTRING_METHOD_DLL CPLString &Printf(
  351. CPL_FORMAT_STRING(const char *pszFormat), ... )
  352. CPL_PRINT_FUNC_FORMAT (2, 3);
  353. CPLSTRING_METHOD_DLL CPLString &vPrintf(
  354. CPL_FORMAT_STRING(const char *pszFormat), va_list args )
  355. CPL_PRINT_FUNC_FORMAT(2, 0);
  356. CPLSTRING_METHOD_DLL CPLString &FormatC( double dfValue, const char *pszFormat = nullptr );
  357. CPLSTRING_METHOD_DLL CPLString &Trim();
  358. CPLSTRING_METHOD_DLL CPLString &Recode( const char *pszSrcEncoding, const char *pszDstEncoding );
  359. CPLSTRING_METHOD_DLL CPLString &replaceAll(
  360. const std::string &osBefore, const std::string& osAfter );
  361. CPLSTRING_METHOD_DLL CPLString &replaceAll( const std::string &osBefore, char chAfter );
  362. CPLSTRING_METHOD_DLL CPLString &replaceAll( char chBefore, const std::string &osAfter );
  363. CPLSTRING_METHOD_DLL CPLString &replaceAll( char chBefore, char chAfter );
  364. /* case insensitive find alternates */
  365. CPLSTRING_METHOD_DLL size_t ifind( const std::string & str, size_t pos = 0 ) const;
  366. CPLSTRING_METHOD_DLL size_t ifind( const char * s, size_t pos = 0 ) const;
  367. CPLSTRING_METHOD_DLL CPLString &toupper( void );
  368. CPLSTRING_METHOD_DLL CPLString &tolower( void );
  369. CPLSTRING_METHOD_DLL bool endsWith( const std::string& osStr ) const;
  370. };
  371. #undef CPLSTRING_CLASS_DLL
  372. #undef CPLSTRING_METHOD_DLL
  373. CPLString CPL_DLL CPLOPrintf(CPL_FORMAT_STRING(const char *pszFormat), ... )
  374. CPL_PRINT_FUNC_FORMAT (1, 2);
  375. CPLString CPL_DLL CPLOvPrintf(
  376. CPL_FORMAT_STRING(const char *pszFormat), va_list args)
  377. CPL_PRINT_FUNC_FORMAT (1, 0);
  378. CPLString CPL_DLL CPLQuotedSQLIdentifier(const char *pszIdent);
  379. /* -------------------------------------------------------------------- */
  380. /* URL processing functions, here since they depend on CPLString. */
  381. /* -------------------------------------------------------------------- */
  382. CPLString CPL_DLL CPLURLGetValue(const char* pszURL, const char* pszKey);
  383. CPLString CPL_DLL CPLURLAddKVP(const char* pszURL, const char* pszKey,
  384. const char* pszValue);
  385. /************************************************************************/
  386. /* CPLStringList */
  387. /************************************************************************/
  388. //! String list class designed around our use of C "char**" string lists.
  389. class CPL_DLL CPLStringList
  390. {
  391. char **papszList = nullptr;
  392. mutable int nCount = 0;
  393. mutable int nAllocation = 0;
  394. bool bOwnList = false;
  395. bool bIsSorted = false;
  396. void MakeOurOwnCopy();
  397. void EnsureAllocation( int nMaxLength );
  398. int FindSortedInsertionPoint( const char *pszLine );
  399. public:
  400. CPLStringList();
  401. explicit CPLStringList( char **papszList, int bTakeOwnership=TRUE );
  402. explicit CPLStringList( CSLConstList papszList );
  403. CPLStringList( const CPLStringList& oOther );
  404. ~CPLStringList();
  405. CPLStringList &Clear();
  406. /** Return size of list */
  407. int size() const { return Count(); }
  408. int Count() const;
  409. /** Return whether the list is empty. */
  410. bool empty() const { return Count() == 0; }
  411. CPLStringList &AddString( const char *pszNewString );
  412. CPLStringList &AddStringDirectly( char *pszNewString );
  413. CPLStringList &InsertString( int nInsertAtLineNo, const char *pszNewLine )
  414. { return InsertStringDirectly( nInsertAtLineNo, CPLStrdup(pszNewLine) ); }
  415. CPLStringList &InsertStringDirectly( int nInsertAtLineNo, char *pszNewLine);
  416. // CPLStringList &InsertStrings( int nInsertAtLineNo, char **papszNewLines );
  417. // CPLStringList &RemoveStrings( int nFirstLineToDelete, int nNumToRemove=1 );
  418. /** Return index of pszTarget in the list, or -1 */
  419. int FindString( const char *pszTarget ) const
  420. { return CSLFindString( papszList, pszTarget ); }
  421. /** Return index of pszTarget in the list (using partial search), or -1 */
  422. int PartialFindString( const char *pszNeedle ) const
  423. { return CSLPartialFindString( papszList, pszNeedle ); }
  424. int FindName( const char *pszName ) const;
  425. bool FetchBool( const char *pszKey, bool bDefault ) const;
  426. // Deprecated.
  427. int FetchBoolean( const char *pszKey, int bDefault ) const;
  428. const char *FetchNameValue( const char *pszKey ) const;
  429. const char *FetchNameValueDef(
  430. const char *pszKey, const char *pszDefault ) const;
  431. CPLStringList &AddNameValue( const char *pszKey, const char *pszValue );
  432. CPLStringList &SetNameValue( const char *pszKey, const char *pszValue );
  433. CPLStringList &Assign( char **papszListIn, int bTakeOwnership=TRUE );
  434. /** Assignment operator */
  435. CPLStringList &operator=(char **papszListIn) {
  436. return Assign( papszListIn, TRUE ); }
  437. /** Assignment operator */
  438. CPLStringList &operator=(const CPLStringList& oOther);
  439. /** Assignment operator */
  440. CPLStringList &operator=(CSLConstList papszListIn);
  441. /** Return string at specified index */
  442. char * operator[](int i);
  443. /** Return string at specified index */
  444. char * operator[](size_t i) { return (*this)[static_cast<int>(i)]; }
  445. /** Return string at specified index */
  446. const char * operator[](int i) const;
  447. /** Return string at specified index */
  448. const char * operator[](size_t i) const {
  449. return (*this)[static_cast<int>(i)]; }
  450. /** Return value corresponding to pszKey, or nullptr */
  451. const char * operator[](const char* pszKey) const {
  452. return FetchNameValue(pszKey); }
  453. /** Return list. Ownership remains to the object */
  454. char** List() { return papszList; }
  455. /** Return list. Ownership remains to the object */
  456. CSLConstList List() const { return papszList; }
  457. char **StealList();
  458. CPLStringList &Sort();
  459. /** Returns whether the list is sorted */
  460. int IsSorted() const { return bIsSorted; }
  461. /** Return lists */
  462. operator char**(void) { return List(); }
  463. /** Return lists */
  464. operator CSLConstList(void) const { return List(); }
  465. };
  466. #ifdef GDAL_COMPILATION
  467. #include <memory>
  468. /*! @cond Doxygen_Suppress */
  469. struct CPL_DLL CSLDestroyReleaser
  470. {
  471. void operator()(char** papszStr) const { CSLDestroy(papszStr); }
  472. };
  473. /*! @endcond */
  474. /** Unique pointer type to use with CSL functions returning a char** */
  475. using CSLUniquePtr = std::unique_ptr< char*, CSLDestroyReleaser>;
  476. /*! @cond Doxygen_Suppress */
  477. struct CPL_DLL CPLFreeReleaser
  478. {
  479. void operator()(void* p) const { CPLFree(p); }
  480. };
  481. /*! @endcond */
  482. /** Unique pointer type to use with functions returning a char* to release with CPLFree */
  483. using CPLCharUniquePtr = std::unique_ptr<char, CPLFreeReleaser>;
  484. #endif
  485. } // extern "C++"
  486. #endif /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
  487. #endif /* CPL_STRING_H_INCLUDED */
上海开阖软件有限公司 沪ICP备12045867号-1