Line data Source code
1 : /**********************************************************************
2 : *
3 : * Name: cpl_string.h
4 : * Project: CPL - Common Portability Library
5 : * Purpose: String and StringList functions.
6 : * Author: Daniel Morissette, dmorissette@mapgears.com
7 : *
8 : **********************************************************************
9 : * Copyright (c) 1998, Daniel Morissette
10 : * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
11 : *
12 : * SPDX-License-Identifier: MIT
13 : ****************************************************************************/
14 :
15 : #ifndef CPL_STRING_H_INCLUDED
16 : #define CPL_STRING_H_INCLUDED
17 :
18 : #include "cpl_error.h"
19 : #include "cpl_conv.h"
20 : #include "cpl_vsi.h"
21 :
22 : #include <stdbool.h>
23 :
24 : /**
25 : * \file cpl_string.h
26 : *
27 : * Various convenience functions for working with strings and string lists.
28 : *
29 : * A StringList is just an array of strings with the last pointer being
30 : * NULL. An empty StringList may be either a NULL pointer, or a pointer to
31 : * a pointer memory location with a NULL value.
32 : *
33 : * A common convention for StringLists is to use them to store name/value
34 : * lists. In this case the contents are treated like a dictionary of
35 : * name/value pairs. The actual data is formatted with each string having
36 : * the format "<name>:<value>" (though "=" is also an acceptable separator).
37 : * A number of the functions in the file operate on name/value style
38 : * string lists (such as CSLSetNameValue(), and CSLFetchNameValue()).
39 : *
40 : * To some extent the CPLStringList C++ class can be used to abstract
41 : * managing string lists a bit but still be able to return them from C
42 : * functions.
43 : *
44 : */
45 :
46 : CPL_C_START
47 :
48 : char CPL_DLL **CSLAddString(char **papszStrList,
49 : const char *pszNewString) CPL_WARN_UNUSED_RESULT;
50 : char CPL_DLL **
51 : CSLAddStringMayFail(char **papszStrList,
52 : const char *pszNewString) CPL_WARN_UNUSED_RESULT;
53 : int CPL_DLL CSLCount(CSLConstList papszStrList);
54 : const char CPL_DLL *CSLGetField(CSLConstList, int);
55 : void CPL_DLL CPL_STDCALL CSLDestroy(char **papszStrList);
56 : char CPL_DLL **CSLDuplicate(CSLConstList papszStrList) CPL_WARN_UNUSED_RESULT;
57 : char CPL_DLL **CSLMerge(char **papszOrig,
58 : CSLConstList papszOverride) CPL_WARN_UNUSED_RESULT;
59 :
60 : char CPL_DLL **CSLTokenizeString(const char *pszString) CPL_WARN_UNUSED_RESULT;
61 : char CPL_DLL **
62 : CSLTokenizeStringComplex(const char *pszString, const char *pszDelimiter,
63 : int bHonourStrings,
64 : int bAllowEmptyTokens) CPL_WARN_UNUSED_RESULT;
65 : char CPL_DLL **CSLTokenizeString2(const char *pszString,
66 : const char *pszDelimiter,
67 : int nCSLTFlags) CPL_WARN_UNUSED_RESULT;
68 :
69 : /** Flag for CSLTokenizeString2() to honour strings */
70 : #define CSLT_HONOURSTRINGS 0x0001
71 : /** Flag for CSLTokenizeString2() to allow empty tokens */
72 : #define CSLT_ALLOWEMPTYTOKENS 0x0002
73 : /** Flag for CSLTokenizeString2() to preserve quotes */
74 : #define CSLT_PRESERVEQUOTES 0x0004
75 : /** Flag for CSLTokenizeString2() to preserve escape characters */
76 : #define CSLT_PRESERVEESCAPES 0x0008
77 : /** Flag for CSLTokenizeString2() to strip leading spaces */
78 : #define CSLT_STRIPLEADSPACES 0x0010
79 : /** Flag for CSLTokenizeString2() to strip trailaing spaces */
80 : #define CSLT_STRIPENDSPACES 0x0020
81 :
82 : int CPL_DLL CSLPrint(CSLConstList papszStrList, FILE *fpOut);
83 : char CPL_DLL **CSLLoad(const char *pszFname) CPL_WARN_UNUSED_RESULT;
84 : char CPL_DLL **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols,
85 : CSLConstList papszOptions) CPL_WARN_UNUSED_RESULT;
86 : int CPL_DLL CSLSave(CSLConstList papszStrList, const char *pszFname);
87 :
88 : char CPL_DLL **
89 : CSLInsertStrings(char **papszStrList, int nInsertAtLineNo,
90 : CSLConstList papszNewLines) CPL_WARN_UNUSED_RESULT;
91 : char CPL_DLL **CSLInsertString(char **papszStrList, int nInsertAtLineNo,
92 : const char *pszNewLine) CPL_WARN_UNUSED_RESULT;
93 : char CPL_DLL **
94 : CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete, int nNumToRemove,
95 : char ***ppapszRetStrings) CPL_WARN_UNUSED_RESULT;
96 : int CPL_DLL CSLFindString(CSLConstList papszList, const char *pszTarget);
97 : int CPL_DLL CSLFindStringCaseSensitive(CSLConstList papszList,
98 : const char *pszTarget);
99 : int CPL_DLL CSLPartialFindString(CSLConstList papszHaystack,
100 : const char *pszNeedle);
101 : int CPL_DLL CSLFindName(CSLConstList papszStrList, const char *pszName);
102 : int CPL_DLL CSLFetchBoolean(CSLConstList papszStrList, const char *pszKey,
103 : int bDefault);
104 :
105 : /* TODO: Deprecate CSLTestBoolean. Remove in GDAL 3.x. */
106 : int CPL_DLL CSLTestBoolean(const char *pszValue);
107 : /* Do not use CPLTestBoolean in C++ code. Use CPLTestBool. */
108 : int CPL_DLL CPLTestBoolean(const char *pszValue);
109 :
110 : bool CPL_DLL CPLTestBool(const char *pszValue);
111 : bool CPL_DLL CPLFetchBool(CSLConstList papszStrList, const char *pszKey,
112 : bool bDefault);
113 :
114 : CPLErr CPL_DLL CPLParseMemorySize(const char *pszValue, GIntBig *pnValue,
115 : bool *pbUnitSpecified);
116 :
117 : const char CPL_DLL *CPLParseNameValue(const char *pszNameValue, char **ppszKey);
118 : const char CPL_DLL *CPLParseNameValueSep(const char *pszNameValue,
119 : char **ppszKey, char chSep);
120 :
121 : const char CPL_DLL *CSLFetchNameValue(CSLConstList papszStrList,
122 : const char *pszName);
123 : const char CPL_DLL *CSLFetchNameValueDef(CSLConstList papszStrList,
124 : const char *pszName,
125 : const char *pszDefault);
126 : char CPL_DLL **CSLFetchNameValueMultiple(CSLConstList papszStrList,
127 : const char *pszName);
128 : char CPL_DLL **CSLAddNameValue(char **papszStrList, const char *pszName,
129 : const char *pszValue) CPL_WARN_UNUSED_RESULT;
130 : char CPL_DLL **CSLSetNameValue(char **papszStrList, const char *pszName,
131 : const char *pszValue) CPL_WARN_UNUSED_RESULT;
132 : void CPL_DLL CSLSetNameValueSeparator(char **papszStrList,
133 : const char *pszSeparator);
134 :
135 : char CPL_DLL **CSLParseCommandLine(const char *pszCommandLine);
136 :
137 : /** Scheme for CPLEscapeString()/CPLUnescapeString() for backlash quoting */
138 : #define CPLES_BackslashQuotable 0
139 : /** Scheme for CPLEscapeString()/CPLUnescapeString() for XML */
140 : #define CPLES_XML 1
141 : /** Scheme for CPLEscapeString()/CPLUnescapeString() for URL */
142 : #define CPLES_URL 2
143 : /** Scheme for CPLEscapeString()/CPLUnescapeString() for SQL */
144 : #define CPLES_SQL 3
145 : /** Scheme for CPLEscapeString()/CPLUnescapeString() for CSV */
146 : #define CPLES_CSV 4
147 : /** Scheme for CPLEscapeString()/CPLUnescapeString() for XML (preserves quotes)
148 : */
149 : #define CPLES_XML_BUT_QUOTES 5
150 : /** Scheme for CPLEscapeString()/CPLUnescapeString() for CSV (forced quoting) */
151 : #define CPLES_CSV_FORCE_QUOTING 6
152 : /** Scheme for CPLEscapeString()/CPLUnescapeString() for SQL identifiers */
153 : #define CPLES_SQLI 7
154 :
155 : char CPL_DLL *CPLEscapeString(const char *pszString, int nLength,
156 : int nScheme) CPL_WARN_UNUSED_RESULT;
157 : char CPL_DLL *CPLUnescapeString(const char *pszString, int *pnLength,
158 : int nScheme) CPL_WARN_UNUSED_RESULT;
159 :
160 : char CPL_DLL *CPLBinaryToHex(int nBytes,
161 : const GByte *pabyData) CPL_WARN_UNUSED_RESULT;
162 : GByte CPL_DLL *CPLHexToBinary(const char *pszHex,
163 : int *pnBytes) CPL_WARN_UNUSED_RESULT;
164 :
165 : char CPL_DLL *CPLBase64Encode(int nBytes,
166 : const GByte *pabyData) CPL_WARN_UNUSED_RESULT;
167 : int CPL_DLL CPLBase64DecodeInPlace(GByte *pszBase64) CPL_WARN_UNUSED_RESULT;
168 :
169 : /** Type of value */
170 : typedef enum
171 : {
172 : CPL_VALUE_STRING, /**< String */
173 : CPL_VALUE_REAL, /**< Real number */
174 : CPL_VALUE_INTEGER /**< Integer */
175 : } CPLValueType;
176 :
177 : CPLValueType CPL_DLL CPLGetValueType(const char *pszValue);
178 :
179 : int CPL_DLL CPLToupper(int c);
180 : int CPL_DLL CPLTolower(int c);
181 :
182 : size_t CPL_DLL CPLStrlcpy(char *pszDest, const char *pszSrc, size_t nDestSize);
183 : size_t CPL_DLL CPLStrlcat(char *pszDest, const char *pszSrc, size_t nDestSize);
184 : size_t CPL_DLL CPLStrnlen(const char *pszStr, size_t nMaxLen);
185 :
186 : /* -------------------------------------------------------------------- */
187 : /* Locale independent formatting functions. */
188 : /* -------------------------------------------------------------------- */
189 : int CPL_DLL CPLvsnprintf(char *str, size_t size,
190 : CPL_FORMAT_STRING(const char *fmt), va_list args)
191 : CPL_PRINT_FUNC_FORMAT(3, 0);
192 :
193 : /* ALIAS_CPLSNPRINTF_AS_SNPRINTF might be defined to enable GCC 7 */
194 : /* -Wformat-truncation= warnings, but shouldn't be set for normal use */
195 : #if defined(ALIAS_CPLSNPRINTF_AS_SNPRINTF)
196 : #define CPLsnprintf snprintf
197 : #else
198 : int CPL_DLL CPLsnprintf(char *str, size_t size,
199 : CPL_FORMAT_STRING(const char *fmt), ...)
200 : CPL_PRINT_FUNC_FORMAT(3, 4);
201 : #endif
202 :
203 : /*! @cond Doxygen_Suppress */
204 : #if defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
205 : int CPL_DLL CPLsprintf(char *str, CPL_FORMAT_STRING(const char *fmt), ...)
206 : CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use CPLsnprintf instead");
207 : #else
208 : int CPL_DLL CPLsprintf(char *str, CPL_FORMAT_STRING(const char *fmt), ...)
209 : CPL_PRINT_FUNC_FORMAT(2, 3);
210 : #endif
211 : /*! @endcond */
212 : int CPL_DLL CPLprintf(CPL_FORMAT_STRING(const char *fmt), ...)
213 : CPL_PRINT_FUNC_FORMAT(1, 2);
214 :
215 : /* For some reason Doxygen_Suppress is needed to avoid warning. Not sure why */
216 : /*! @cond Doxygen_Suppress */
217 : /* caution: only works with limited number of formats */
218 : int CPL_DLL CPLsscanf(const char *str, CPL_SCANF_FORMAT_STRING(const char *fmt),
219 : ...) CPL_SCAN_FUNC_FORMAT(2, 3);
220 : /*! @endcond */
221 :
222 : const char CPL_DLL *CPLSPrintf(CPL_FORMAT_STRING(const char *fmt), ...)
223 : CPL_PRINT_FUNC_FORMAT(1, 2) CPL_WARN_UNUSED_RESULT;
224 : char CPL_DLL **CSLAppendPrintf(char **papszStrList,
225 : CPL_FORMAT_STRING(const char *fmt), ...)
226 : CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_UNUSED_RESULT;
227 : int CPL_DLL CPLVASPrintf(char **buf, CPL_FORMAT_STRING(const char *fmt),
228 : va_list args) CPL_PRINT_FUNC_FORMAT(2, 0);
229 :
230 : /* -------------------------------------------------------------------- */
231 : /* RFC 23 character set conversion/recoding API (cpl_recode.cpp). */
232 : /* -------------------------------------------------------------------- */
233 : /** Encoding of the current locale */
234 : #define CPL_ENC_LOCALE ""
235 : /** UTF-8 encoding */
236 : #define CPL_ENC_UTF8 "UTF-8"
237 : /** UTF-16 encoding */
238 : #define CPL_ENC_UTF16 "UTF-16"
239 : /** UCS-2 encoding */
240 : #define CPL_ENC_UCS2 "UCS-2"
241 : /** UCS-4 encoding */
242 : #define CPL_ENC_UCS4 "UCS-4"
243 : /** ASCII encoding */
244 : #define CPL_ENC_ASCII "ASCII"
245 : /** ISO-8859-1 (LATIN1) encoding */
246 : #define CPL_ENC_ISO8859_1 "ISO-8859-1"
247 :
248 : int CPL_DLL CPLEncodingCharSize(const char *pszEncoding);
249 : /*! @cond Doxygen_Suppress */
250 : void CPL_DLL CPLClearRecodeWarningFlags(void);
251 : /*! @endcond */
252 : char CPL_DLL *CPLRecode(const char *pszSource, const char *pszSrcEncoding,
253 : const char *pszDstEncoding)
254 : CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
255 : char CPL_DLL *
256 : CPLRecodeFromWChar(const wchar_t *pwszSource, const char *pszSrcEncoding,
257 : const char *pszDstEncoding) CPL_WARN_UNUSED_RESULT;
258 : wchar_t CPL_DLL *
259 : CPLRecodeToWChar(const char *pszSource, const char *pszSrcEncoding,
260 : const char *pszDstEncoding) CPL_WARN_UNUSED_RESULT;
261 : int CPL_DLL CPLIsUTF8(const char *pabyData, int nLen);
262 : bool CPL_DLL CPLIsASCII(const char *pabyData, size_t nLen);
263 : char CPL_DLL *CPLForceToASCII(const char *pabyData, int nLen,
264 : char chReplacementChar) CPL_WARN_UNUSED_RESULT;
265 : char CPL_DLL *CPLUTF8ForceToASCII(const char *pszStr, char chReplacementChar)
266 : CPL_WARN_UNUSED_RESULT;
267 : int CPL_DLL CPLStrlenUTF8(const char *pszUTF8Str);
268 : int CPL_DLL CPLCanRecode(const char *pszTestStr, const char *pszSrcEncoding,
269 : const char *pszDstEncoding) CPL_WARN_UNUSED_RESULT;
270 : CPL_C_END
271 :
272 : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
273 :
274 : extern "C++"
275 : {
276 : std::string CPL_DLL CPLRemoveSQLComments(const std::string &osInput);
277 : }
278 :
279 : #endif
280 :
281 : /************************************************************************/
282 : /* CPLString */
283 : /************************************************************************/
284 :
285 : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
286 :
287 : extern "C++"
288 : {
289 : #ifndef DOXYGEN_SKIP
290 : #include <string>
291 : #include <vector>
292 : #endif
293 :
294 : // VC++ implicitly applies __declspec(dllexport) to template base classes
295 : // of classes marked with __declspec(dllexport).
296 : // Hence, if marked with CPL_DLL, VC++ would export symbols for the
297 : // specialization of std::basic_string<char>, since it is a base class of
298 : // CPLString. As a result, if an application linked both gdal.dll and a static
299 : // library that (implicitly) instantiates std::string (almost all do!), then the
300 : // linker would emit an error concerning duplicate symbols for std::string. The
301 : // least intrusive solution is to not mark the whole class with
302 : // __declspec(dllexport) for VC++, but only its non-inline methods.
303 : #ifdef _MSC_VER
304 : #define CPLSTRING_CLASS_DLL
305 : #define CPLSTRING_METHOD_DLL CPL_DLL
306 : #else
307 : /*! @cond Doxygen_Suppress */
308 : #define CPLSTRING_CLASS_DLL CPL_DLL
309 : #define CPLSTRING_METHOD_DLL
310 : /*! @endcond */
311 : #endif
312 :
313 : //! Convenient string class based on std::string.
314 20 : class CPLSTRING_CLASS_DLL CPLString : public std::string
315 : {
316 : public:
317 : /** Constructor */
318 14536015 : CPLString(void)
319 14536015 : {
320 14535895 : }
321 :
322 : /** Constructor */
323 : // cppcheck-suppress noExplicitConstructor
324 3995480 : CPLString(const std::string &oStr) : std::string(oStr)
325 : {
326 3995480 : }
327 :
328 : /** Constructor */
329 : // cppcheck-suppress noExplicitConstructor
330 7301126 : CPLString(const char *pszStr) : std::string(pszStr)
331 : {
332 7300986 : }
333 :
334 : /** Constructor */
335 57966 : CPLString(const char *pszStr, size_t n) : std::string(pszStr, n)
336 : {
337 57957 : }
338 :
339 : /** Return string as zero terminated character array */
340 21961751 : operator const char *(void) const
341 : {
342 21961751 : return c_str();
343 : }
344 :
345 : /** Return character at specified index */
346 46103724 : char &operator[](std::string::size_type i)
347 : {
348 46103724 : return std::string::operator[](i);
349 : }
350 :
351 : /** Return character at specified index */
352 263689 : const char &operator[](std::string::size_type i) const
353 : {
354 263689 : return std::string::operator[](i);
355 : }
356 :
357 : /** Return character at specified index */
358 120410 : char &operator[](int i)
359 : {
360 : return std::string::operator[](
361 120410 : static_cast<std::string::size_type>(i));
362 : }
363 :
364 : /** Return character at specified index */
365 21250 : const char &operator[](int i) const
366 : {
367 : return std::string::operator[](
368 21250 : static_cast<std::string::size_type>(i));
369 : }
370 :
371 : /** Clear the string */
372 23760 : void Clear()
373 : {
374 23760 : resize(0);
375 23760 : }
376 :
377 : /** Assign specified string and take ownership of it (assumed to be
378 : * allocated with CPLMalloc()). NULL can be safely passed to clear the
379 : * string. */
380 0 : void Seize(char *pszValue)
381 : {
382 0 : if (pszValue == nullptr)
383 0 : Clear();
384 : else
385 : {
386 0 : *this = pszValue;
387 0 : CPLFree(pszValue);
388 : }
389 0 : }
390 :
391 : /* There seems to be a bug in the way the compiler count indices...
392 : * Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
393 : CPLSTRING_METHOD_DLL CPLString &
394 : Printf(CPL_FORMAT_STRING(const char *pszFormat), ...)
395 : CPL_PRINT_FUNC_FORMAT(2, 3);
396 : CPLSTRING_METHOD_DLL CPLString &
397 : vPrintf(CPL_FORMAT_STRING(const char *pszFormat), va_list args)
398 : CPL_PRINT_FUNC_FORMAT(2, 0);
399 : CPLSTRING_METHOD_DLL CPLString &
400 : FormatC(double dfValue, const char *pszFormat = nullptr);
401 : CPLSTRING_METHOD_DLL CPLString &Trim();
402 : CPLSTRING_METHOD_DLL CPLString &Recode(const char *pszSrcEncoding,
403 : const char *pszDstEncoding);
404 : CPLSTRING_METHOD_DLL CPLString &replaceAll(const std::string &osBefore,
405 : const std::string &osAfter);
406 : CPLSTRING_METHOD_DLL CPLString &replaceAll(const std::string &osBefore,
407 : char chAfter);
408 : CPLSTRING_METHOD_DLL CPLString &replaceAll(char chBefore,
409 : const std::string &osAfter);
410 : CPLSTRING_METHOD_DLL CPLString &replaceAll(char chBefore, char chAfter);
411 :
412 : /* case insensitive find alternates */
413 : CPLSTRING_METHOD_DLL size_t ifind(const std::string &str,
414 : size_t pos = 0) const;
415 : CPLSTRING_METHOD_DLL size_t ifind(const char *s, size_t pos = 0) const;
416 : CPLSTRING_METHOD_DLL CPLString &toupper(void);
417 : CPLSTRING_METHOD_DLL CPLString &tolower(void);
418 :
419 : CPLSTRING_METHOD_DLL bool endsWith(const std::string &osStr) const;
420 : };
421 :
422 : #undef CPLSTRING_CLASS_DLL
423 : #undef CPLSTRING_METHOD_DLL
424 :
425 : CPLString CPL_DLL CPLOPrintf(CPL_FORMAT_STRING(const char *pszFormat), ...)
426 : CPL_PRINT_FUNC_FORMAT(1, 2);
427 : CPLString CPL_DLL CPLOvPrintf(CPL_FORMAT_STRING(const char *pszFormat),
428 : va_list args) CPL_PRINT_FUNC_FORMAT(1, 0);
429 : CPLString CPL_DLL CPLQuotedSQLIdentifier(const char *pszIdent);
430 :
431 : /* -------------------------------------------------------------------- */
432 : /* URL processing functions, here since they depend on CPLString. */
433 : /* -------------------------------------------------------------------- */
434 : CPLString CPL_DLL CPLURLGetValue(const char *pszURL, const char *pszKey);
435 : CPLString CPL_DLL CPLURLAddKVP(const char *pszURL, const char *pszKey,
436 : const char *pszValue);
437 :
438 : /************************************************************************/
439 : /* CPLStringList */
440 : /************************************************************************/
441 :
442 : //! String list class designed around our use of C "char**" string lists.
443 9170860 : class CPL_DLL CPLStringList
444 : {
445 : char **papszList = nullptr;
446 : mutable int nCount = 0;
447 : mutable int nAllocation = 0;
448 : bool bOwnList = false;
449 : bool bIsSorted = false;
450 :
451 : bool MakeOurOwnCopy();
452 : bool EnsureAllocation(int nMaxLength);
453 : int FindSortedInsertionPoint(const char *pszLine);
454 :
455 : public:
456 : CPLStringList();
457 : explicit CPLStringList(char **papszList, int bTakeOwnership = TRUE);
458 : explicit CPLStringList(CSLConstList papszList);
459 : explicit CPLStringList(const std::vector<std::string> &aosList);
460 : explicit CPLStringList(std::initializer_list<const char *> oInitList);
461 : CPLStringList(const CPLStringList &oOther);
462 : CPLStringList(CPLStringList &&oOther);
463 : ~CPLStringList();
464 :
465 : static const CPLStringList BoundToConstList(CSLConstList papszList);
466 :
467 : CPLStringList &Clear();
468 :
469 : /** Clear the list */
470 1 : inline void clear()
471 : {
472 1 : Clear();
473 1 : }
474 :
475 : /** Return size of list */
476 920083 : int size() const
477 : {
478 920083 : return Count();
479 : }
480 :
481 : int Count() const;
482 :
483 : /** Return whether the list is empty. */
484 29623 : bool empty() const
485 : {
486 29623 : return Count() == 0;
487 : }
488 :
489 : CPLStringList &AddString(const char *pszNewString);
490 : CPLStringList &AddString(const std::string &newString);
491 : CPLStringList &AddStringDirectly(char *pszNewString);
492 :
493 : /** Add a string to the list */
494 140 : void push_back(const char *pszNewString)
495 : {
496 140 : AddString(pszNewString);
497 140 : }
498 :
499 : /** Add a string to the list */
500 1349 : void push_back(const std::string &osStr)
501 : {
502 1349 : AddString(osStr.c_str());
503 1349 : }
504 :
505 218 : CPLStringList &InsertString(int nInsertAtLineNo, const char *pszNewLine)
506 : {
507 218 : return InsertStringDirectly(nInsertAtLineNo, CPLStrdup(pszNewLine));
508 : }
509 :
510 : CPLStringList &InsertStringDirectly(int nInsertAtLineNo,
511 : char *pszNewLine);
512 :
513 : // CPLStringList &InsertStrings( int nInsertAtLineNo, char
514 : // **papszNewLines ); CPLStringList &RemoveStrings( int
515 : // nFirstLineToDelete, int nNumToRemove=1 );
516 :
517 : /** Return index of pszTarget in the list, or -1 */
518 16461 : int FindString(const char *pszTarget) const
519 : {
520 16461 : return CSLFindString(papszList, pszTarget);
521 : }
522 :
523 : /** Return index of pszTarget in the list (using partial search), or -1
524 : */
525 : int PartialFindString(const char *pszNeedle) const
526 : {
527 : return CSLPartialFindString(papszList, pszNeedle);
528 : }
529 :
530 : int FindName(const char *pszName) const;
531 : bool FetchBool(const char *pszKey, bool bDefault) const;
532 : // Deprecated.
533 : int FetchBoolean(const char *pszKey, int bDefault) const;
534 : const char *FetchNameValue(const char *pszKey) const;
535 : const char *FetchNameValueDef(const char *pszKey,
536 : const char *pszDefault) const;
537 : CPLStringList &AddNameValue(const char *pszKey, const char *pszValue);
538 : CPLStringList &SetNameValue(const char *pszKey, const char *pszValue);
539 :
540 : CPLStringList &Assign(char **papszListIn, int bTakeOwnership = TRUE);
541 :
542 : /** Assignment operator */
543 34748 : CPLStringList &operator=(char **papszListIn)
544 : {
545 34748 : return Assign(papszListIn, TRUE);
546 : }
547 :
548 : /** Assignment operator */
549 : CPLStringList &operator=(const CPLStringList &oOther);
550 : /** Assignment operator */
551 : CPLStringList &operator=(CSLConstList papszListIn);
552 : /** Move assignment operator */
553 : CPLStringList &operator=(CPLStringList &&oOther);
554 :
555 : /** Return string at specified index */
556 : char *operator[](int i);
557 :
558 : /** Return string at specified index */
559 1202 : char *operator[](size_t i)
560 : {
561 1202 : return (*this)[static_cast<int>(i)];
562 : }
563 :
564 : /** Return string at specified index */
565 : const char *operator[](int i) const;
566 :
567 : /** Return string at specified index */
568 320 : const char *operator[](size_t i) const
569 : {
570 320 : return (*this)[static_cast<int>(i)];
571 : }
572 :
573 : /** Return value corresponding to pszKey, or nullptr */
574 4196 : const char *operator[](const char *pszKey) const
575 : {
576 4196 : return FetchNameValue(pszKey);
577 : }
578 :
579 : /** Return first element */
580 : inline const char *front() const
581 : {
582 : return papszList[0];
583 : }
584 :
585 : /** Return last element */
586 453073 : inline const char *back() const
587 : {
588 453073 : return papszList[size() - 1];
589 : }
590 :
591 : /** begin() implementation */
592 5716 : const char *const *begin() const
593 : {
594 5716 : return papszList ? &papszList[0] : nullptr;
595 : }
596 :
597 : /** end() implementation */
598 5716 : const char *const *end() const
599 : {
600 5716 : return papszList ? &papszList[size()] : nullptr;
601 : }
602 :
603 : /** Return list. Ownership remains to the object */
604 2730334 : char **List()
605 : {
606 2730334 : return papszList;
607 : }
608 :
609 : /** Return list. Ownership remains to the object */
610 1841611 : CSLConstList List() const
611 : {
612 1841611 : return papszList;
613 : }
614 :
615 : char **StealList();
616 :
617 : CPLStringList &Sort();
618 :
619 : /** Returns whether the list is sorted */
620 18559891 : int IsSorted() const
621 : {
622 18559891 : return bIsSorted;
623 : }
624 :
625 : /** Return lists */
626 1463 : operator char **(void)
627 : {
628 1463 : return List();
629 : }
630 :
631 : /** Return lists */
632 731 : operator CSLConstList(void) const
633 : {
634 731 : return List();
635 : }
636 :
637 : /** Return the list as a vector of strings */
638 883 : operator std::vector<std::string>(void) const
639 : {
640 883 : return std::vector<std::string>{begin(), end()};
641 : }
642 : };
643 :
644 : #ifdef GDAL_COMPILATION
645 :
646 : #include <iterator> // For std::input_iterator_tag
647 : #include <memory>
648 : #include <utility> // For std::pair
649 :
650 : /*! @cond Doxygen_Suppress */
651 : struct CPL_DLL CSLDestroyReleaser
652 : {
653 : void operator()(char **papszStr) const
654 : {
655 : CSLDestroy(papszStr);
656 : }
657 : };
658 :
659 : /*! @endcond */
660 :
661 : /** Unique pointer type to use with CSL functions returning a char** */
662 : using CSLUniquePtr = std::unique_ptr<char *, CSLDestroyReleaser>;
663 :
664 : /** Unique pointer type to use with functions returning a char* to release
665 : * with VSIFree */
666 : using CPLCharUniquePtr = std::unique_ptr<char, VSIFreeReleaser>;
667 :
668 : namespace cpl
669 : {
670 :
671 : /*! @cond Doxygen_Suppress */
672 : /** Iterator for a CSLConstList */
673 : struct CPL_DLL CSLIterator
674 : {
675 : using iterator_category = std::input_iterator_tag;
676 : using difference_type = std::ptrdiff_t;
677 : using value_type = const char *;
678 : using pointer = value_type *;
679 : using reference = value_type &;
680 :
681 : CSLConstList m_papszList = nullptr;
682 : bool m_bAtEnd = false;
683 :
684 137070 : inline const char *operator*() const
685 : {
686 137070 : return *m_papszList;
687 : }
688 :
689 136862 : inline CSLIterator &operator++()
690 : {
691 136862 : if (m_papszList)
692 136862 : ++m_papszList;
693 136862 : return *this;
694 : }
695 :
696 : bool operator==(const CSLIterator &other) const;
697 :
698 255416 : inline bool operator!=(const CSLIterator &other) const
699 : {
700 255416 : return !(operator==(other));
701 : }
702 : };
703 :
704 : /*! @endcond */
705 :
706 : /** Wrapper for a CSLConstList that can be used with C++ iterators.
707 : *
708 : * @since GDAL 3.9
709 : */
710 : struct CPL_DLL CSLIteratorWrapper
711 : {
712 : public:
713 : /** Constructor */
714 118565 : inline explicit CSLIteratorWrapper(CSLConstList papszList)
715 118565 : : m_papszList(papszList)
716 : {
717 118565 : }
718 :
719 : /** Get the begin of the list */
720 118565 : inline CSLIterator begin() const
721 : {
722 118565 : return {m_papszList, false};
723 : }
724 :
725 : /** Get the end of the list */
726 118566 : inline CSLIterator end() const
727 : {
728 118566 : return {m_papszList, true};
729 : }
730 :
731 : private:
732 : CSLConstList m_papszList;
733 : };
734 :
735 : /** Wraps a CSLConstList in a structure that can be used with C++ iterators.
736 : *
737 : * @since GDAL 3.9
738 : */
739 118566 : inline CSLIteratorWrapper Iterate(CSLConstList papszList)
740 : {
741 118566 : return CSLIteratorWrapper{papszList};
742 : }
743 :
744 : /*! @cond Doxygen_Suppress */
745 2179 : inline CSLIteratorWrapper Iterate(const CPLStringList &aosList)
746 : {
747 2179 : return Iterate(aosList.List());
748 : }
749 :
750 : /*! @endcond */
751 :
752 : /*! @cond Doxygen_Suppress */
753 : inline CSLIteratorWrapper Iterate(char **) = delete;
754 :
755 : /*! @endcond */
756 :
757 : /*! @cond Doxygen_Suppress */
758 : /** Iterator for a CSLConstList as (name, value) pairs. */
759 : struct CPL_DLL CSLNameValueIterator
760 : {
761 : using iterator_category = std::input_iterator_tag;
762 : using difference_type = std::ptrdiff_t;
763 : using value_type = std::pair<const char *, const char *>;
764 : using pointer = value_type *;
765 : using reference = value_type &;
766 :
767 : CSLConstList m_papszList = nullptr;
768 : bool m_bReturnNullKeyIfNotNameValue = false;
769 : std::string m_osKey{};
770 :
771 : value_type operator*();
772 :
773 1264 : inline CSLNameValueIterator &operator++()
774 : {
775 1264 : if (m_papszList)
776 1264 : ++m_papszList;
777 1264 : return *this;
778 : }
779 :
780 5213 : inline bool operator==(const CSLNameValueIterator &other) const
781 : {
782 5213 : return m_papszList == other.m_papszList;
783 : }
784 :
785 5211 : inline bool operator!=(const CSLNameValueIterator &other) const
786 : {
787 5211 : return !(operator==(other));
788 : }
789 : };
790 :
791 : /*! @endcond */
792 :
793 : /** Wrapper for a CSLConstList that can be used with C++ iterators
794 : * to get (name, value) pairs.
795 : *
796 : * This can for example be used to do the following:
797 : * for (const auto& [name, value]: cpl::IterateNameValue(papszList)) {}
798 : *
799 : * Note that a (name, value) pair returned by dereferencing an iterator
800 : * is invalidated by the next iteration on the iterator.
801 : *
802 : * @since GDAL 3.9
803 : */
804 : struct CPL_DLL CSLNameValueIteratorWrapper
805 : {
806 : public:
807 : /** Constructor */
808 3949 : inline explicit CSLNameValueIteratorWrapper(
809 : CSLConstList papszList, bool bReturnNullKeyIfNotNameValue)
810 3949 : : m_papszList(papszList),
811 3949 : m_bReturnNullKeyIfNotNameValue(bReturnNullKeyIfNotNameValue)
812 : {
813 3949 : }
814 :
815 : /** Get the begin of the list */
816 3949 : inline CSLNameValueIterator begin() const
817 : {
818 3949 : return {m_papszList, m_bReturnNullKeyIfNotNameValue};
819 : }
820 :
821 : /** Get the end of the list */
822 : CSLNameValueIterator end() const;
823 :
824 : private:
825 : CSLConstList m_papszList;
826 : const bool m_bReturnNullKeyIfNotNameValue;
827 : };
828 :
829 : /** Wraps a CSLConstList in a structure that can be used with C++ iterators
830 : * to get (name, value) pairs.
831 : *
832 : * This can for example be used to do the following:
833 : * for (const auto& [name, value]: cpl::IterateNameValue(papszList)) {}
834 : *
835 : * Note that a (name, value) pair returned by dereferencing an iterator
836 : * is invalidated by the next iteration on the iterator.
837 : *
838 : * @param papszList List to iterate over.
839 : * @param bReturnNullKeyIfNotNameValue When this is set to true, if a string
840 : * contained in the list if not of the form name=value, then the value of
841 : * the iterator will be (nullptr, string).
842 : *
843 : * @since GDAL 3.9
844 : */
845 : inline CSLNameValueIteratorWrapper
846 3949 : IterateNameValue(CSLConstList papszList,
847 : bool bReturnNullKeyIfNotNameValue = false)
848 : {
849 3949 : return CSLNameValueIteratorWrapper{papszList,
850 3949 : bReturnNullKeyIfNotNameValue};
851 : }
852 :
853 : /*! @cond Doxygen_Suppress */
854 : inline CSLNameValueIteratorWrapper
855 2636 : IterateNameValue(const CPLStringList &aosList,
856 : bool bReturnNullKeyIfNotNameValue = false)
857 : {
858 2636 : return IterateNameValue(aosList.List(), bReturnNullKeyIfNotNameValue);
859 : }
860 :
861 : /*! @endcond */
862 :
863 : /*! @cond Doxygen_Suppress */
864 : inline CSLIteratorWrapper IterateNameValue(char **, bool = false) = delete;
865 :
866 : /*! @endcond */
867 :
868 : /** Converts a CSLConstList to a std::vector<std::string> */
869 94 : inline std::vector<std::string> ToVector(CSLConstList papszList)
870 : {
871 188 : return CPLStringList::BoundToConstList(papszList);
872 : }
873 :
874 : inline std::vector<std::string> ToVector(char **) = delete;
875 :
876 : } // namespace cpl
877 :
878 : #endif
879 :
880 : } // extern "C++"
881 :
882 : #endif /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
883 :
884 : #endif /* CPL_STRING_H_INCLUDED */
|