GDAL
cpl_string.h
Go to the documentation of this file.
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
47
48char CPL_DLL **CSLAddString(char **papszStrList,
49 const char *pszNewString) CPL_WARN_UNUSED_RESULT;
50char CPL_DLL **
51CSLAddStringMayFail(char **papszStrList,
52 const char *pszNewString) CPL_WARN_UNUSED_RESULT;
53int CPL_DLL CSLCount(CSLConstList papszStrList);
54const char CPL_DLL *CSLGetField(CSLConstList, int);
55void CPL_DLL CPL_STDCALL CSLDestroy(char **papszStrList);
56char CPL_DLL **CSLDuplicate(CSLConstList papszStrList) CPL_WARN_UNUSED_RESULT;
57char CPL_DLL **CSLMerge(char **papszOrig,
59
60char CPL_DLL **CSLTokenizeString(const char *pszString) CPL_WARN_UNUSED_RESULT;
61char CPL_DLL **
62CSLTokenizeStringComplex(const char *pszString, const char *pszDelimiter,
63 int bHonourStrings,
64 int bAllowEmptyTokens) CPL_WARN_UNUSED_RESULT;
65char CPL_DLL **CSLTokenizeString2(const char *pszString,
66 const char *pszDelimiter,
67 int nCSLTFlags) CPL_WARN_UNUSED_RESULT;
68
70#define CSLT_HONOURSTRINGS 0x0001
72#define CSLT_ALLOWEMPTYTOKENS 0x0002
74#define CSLT_PRESERVEQUOTES 0x0004
76#define CSLT_PRESERVEESCAPES 0x0008
78#define CSLT_STRIPLEADSPACES 0x0010
80#define CSLT_STRIPENDSPACES 0x0020
81
82int CPL_DLL CSLPrint(CSLConstList papszStrList, FILE *fpOut);
83char CPL_DLL **CSLLoad(const char *pszFname) CPL_WARN_UNUSED_RESULT;
84char CPL_DLL **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols,
86int CPL_DLL CSLSave(CSLConstList papszStrList, const char *pszFname);
87
88char CPL_DLL **
89CSLInsertStrings(char **papszStrList, int nInsertAtLineNo,
91char CPL_DLL **CSLInsertString(char **papszStrList, int nInsertAtLineNo,
92 const char *pszNewLine) CPL_WARN_UNUSED_RESULT;
93char CPL_DLL **
94CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete, int nNumToRemove,
95 char ***ppapszRetStrings) CPL_WARN_UNUSED_RESULT;
96int CPL_DLL CSLFindString(CSLConstList papszList, const char *pszTarget);
97int CPL_DLL CSLFindStringCaseSensitive(CSLConstList papszList,
98 const char *pszTarget);
99int CPL_DLL CSLPartialFindString(CSLConstList papszHaystack,
100 const char *pszNeedle);
101int CPL_DLL CSLFindName(CSLConstList papszStrList, const char *pszName);
102int CPL_DLL CSLFetchBoolean(CSLConstList papszStrList, const char *pszKey,
103 int bDefault);
104
105/* TODO: Deprecate CSLTestBoolean. Remove in GDAL 3.x. */
106int CPL_DLL CSLTestBoolean(const char *pszValue);
107/* Do not use CPLTestBoolean in C++ code. Use CPLTestBool. */
108int CPL_DLL CPLTestBoolean(const char *pszValue);
109
110bool CPL_DLL CPLTestBool(const char *pszValue);
111bool CPL_DLL CPLFetchBool(CSLConstList papszStrList, const char *pszKey,
112 bool bDefault);
113#ifdef __cplusplus
115
117inline bool CPLFetchBool(CSLConstList papszStrList, const char *pszKey,
118 int bDefault)
119{
120 return CPLFetchBool(papszStrList, pszKey, bDefault != 0);
121}
122
123bool CPLFetchBool(CSLConstList papszStrList, const char *pszKey,
124 const char *) = delete;
125
128#endif
129CPLErr CPL_DLL CPLParseMemorySize(const char *pszValue, GIntBig *pnValue,
130 bool *pbUnitSpecified);
131
132const char CPL_DLL *CPLParseNameValue(const char *pszNameValue, char **ppszKey);
133const char CPL_DLL *CPLParseNameValueSep(const char *pszNameValue,
134 char **ppszKey, char chSep);
135
136const char CPL_DLL *CSLFetchNameValue(CSLConstList papszStrList,
137 const char *pszName);
138const char CPL_DLL *CSLFetchNameValueDef(CSLConstList papszStrList,
139 const char *pszName,
140 const char *pszDefault);
141char CPL_DLL **CSLFetchNameValueMultiple(CSLConstList papszStrList,
142 const char *pszName);
143char CPL_DLL **CSLAddNameValue(char **papszStrList, const char *pszName,
144 const char *pszValue) CPL_WARN_UNUSED_RESULT;
145char CPL_DLL **CSLSetNameValue(char **papszStrList, const char *pszName,
146 const char *pszValue) CPL_WARN_UNUSED_RESULT;
147void CPL_DLL CSLSetNameValueSeparator(char **papszStrList,
148 const char *pszSeparator);
149
150char CPL_DLL **CSLParseCommandLine(const char *pszCommandLine);
151
153#define CPLES_BackslashQuotable 0
155#define CPLES_XML 1
157#define CPLES_URL 2
159#define CPLES_SQL 3
161#define CPLES_CSV 4
164#define CPLES_XML_BUT_QUOTES 5
166#define CPLES_CSV_FORCE_QUOTING 6
168#define CPLES_SQLI 7
169
170char CPL_DLL *CPLEscapeString(const char *pszString, int nLength,
171 int nScheme) CPL_WARN_UNUSED_RESULT;
172char CPL_DLL *CPLUnescapeString(const char *pszString, int *pnLength,
173 int nScheme) CPL_WARN_UNUSED_RESULT;
174
175char CPL_DLL *CPLBinaryToHex(int nBytes,
176 const GByte *pabyData) CPL_WARN_UNUSED_RESULT;
177GByte CPL_DLL *CPLHexToBinary(const char *pszHex,
178 int *pnBytes) CPL_WARN_UNUSED_RESULT;
179
180char CPL_DLL *CPLBase64Encode(int nBytes,
181 const GByte *pabyData) CPL_WARN_UNUSED_RESULT;
183
191
192CPLValueType CPL_DLL CPLGetValueType(const char *pszValue);
193
194int CPL_DLL CPLToupper(int c);
195int CPL_DLL CPLTolower(int c);
196
197size_t CPL_DLL CPLStrlcpy(char *pszDest, const char *pszSrc, size_t nDestSize);
198size_t CPL_DLL CPLStrlcat(char *pszDest, const char *pszSrc, size_t nDestSize);
199size_t CPL_DLL CPLStrnlen(const char *pszStr, size_t nMaxLen);
200
201/* -------------------------------------------------------------------- */
202/* Locale independent formatting functions. */
203/* -------------------------------------------------------------------- */
204int CPL_DLL CPLvsnprintf(char *str, size_t size,
205 CPL_FORMAT_STRING(const char *fmt), va_list args)
207
208/* ALIAS_CPLSNPRINTF_AS_SNPRINTF might be defined to enable GCC 7 */
209/* -Wformat-truncation= warnings, but shouldn't be set for normal use */
210#if defined(ALIAS_CPLSNPRINTF_AS_SNPRINTF)
211#define CPLsnprintf snprintf
212#else
213int CPL_DLL CPLsnprintf(char *str, size_t size,
214 CPL_FORMAT_STRING(const char *fmt), ...)
216#endif
217
219#if defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
220int CPL_DLL CPLsprintf(char *str, CPL_FORMAT_STRING(const char *fmt), ...)
221 CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use CPLsnprintf instead");
222#else
223int CPL_DLL CPLsprintf(char *str, CPL_FORMAT_STRING(const char *fmt), ...)
225#endif
227int CPL_DLL CPLprintf(CPL_FORMAT_STRING(const char *fmt), ...)
229
230/* For some reason Doxygen_Suppress is needed to avoid warning. Not sure why */
232/* caution: only works with limited number of formats */
233int CPL_DLL CPLsscanf(const char *str, CPL_SCANF_FORMAT_STRING(const char *fmt),
234 ...) CPL_SCAN_FUNC_FORMAT(2, 3);
237const char CPL_DLL *CPLSPrintf(CPL_FORMAT_STRING(const char *fmt), ...)
239char CPL_DLL **CSLAppendPrintf(char **papszStrList,
240 CPL_FORMAT_STRING(const char *fmt), ...)
242int CPL_DLL CPLVASPrintf(char **buf, CPL_FORMAT_STRING(const char *fmt),
243 va_list args) CPL_PRINT_FUNC_FORMAT(2, 0);
244
245/* -------------------------------------------------------------------- */
246/* RFC 23 character set conversion/recoding API (cpl_recode.cpp). */
247/* -------------------------------------------------------------------- */
249#define CPL_ENC_LOCALE ""
251#define CPL_ENC_UTF8 "UTF-8"
253#define CPL_ENC_UTF16 "UTF-16"
255#define CPL_ENC_UCS2 "UCS-2"
257#define CPL_ENC_UCS4 "UCS-4"
259#define CPL_ENC_ASCII "ASCII"
261#define CPL_ENC_ISO8859_1 "ISO-8859-1"
262
263int CPL_DLL CPLEncodingCharSize(const char *pszEncoding);
265void CPL_DLL CPLClearRecodeWarningFlags(void);
267char CPL_DLL *CPLRecode(const char *pszSource, const char *pszSrcEncoding,
268 const char *pszDstEncoding)
270char CPL_DLL *
271CPLRecodeFromWChar(const wchar_t *pwszSource, const char *pszSrcEncoding,
272 const char *pszDstEncoding) CPL_WARN_UNUSED_RESULT;
273wchar_t CPL_DLL *
274CPLRecodeToWChar(const char *pszSource, const char *pszSrcEncoding,
275 const char *pszDstEncoding) CPL_WARN_UNUSED_RESULT;
276int CPL_DLL CPLIsUTF8(const char *pabyData, int nLen);
277bool CPL_DLL CPLIsASCII(const char *pabyData, size_t nLen);
278char CPL_DLL *CPLForceToASCII(const char *pabyData, int nLen,
279 char chReplacementChar) CPL_WARN_UNUSED_RESULT;
280char CPL_DLL *CPLUTF8ForceToASCII(const char *pszStr, char chReplacementChar)
282int CPL_DLL CPLStrlenUTF8(const char *pszUTF8Str)
284 CPL_WARN_DEPRECATED("Use CPLStrlenUTF8Ex() instead")
286 ;
287size_t CPL_DLL CPLStrlenUTF8Ex(const char *pszUTF8Str);
288int CPL_DLL CPLCanRecode(const char *pszTestStr, const char *pszSrcEncoding,
289 const char *pszDstEncoding) CPL_WARN_UNUSED_RESULT;
291
292#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
293
294extern "C++"
295{
296 std::string CPL_DLL CPLRemoveSQLComments(const std::string &osInput);
297}
298
299#endif
300
301/************************************************************************/
302/* CPLString */
303/************************************************************************/
304
305#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
306
307extern "C++"
308{
309#ifndef DOXYGEN_SKIP
310#include <string>
311#include <vector>
312#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
313#include <string_view>
314#endif
315#endif
316
317// VC++ implicitly applies __declspec(dllexport) to template base classes
318// of classes marked with __declspec(dllexport).
319// Hence, if marked with CPL_DLL, VC++ would export symbols for the
320// specialization of std::basic_string<char>, since it is a base class of
321// CPLString. As a result, if an application linked both gdal.dll and a static
322// library that (implicitly) instantiates std::string (almost all do!), then the
323// linker would emit an error concerning duplicate symbols for std::string. The
324// least intrusive solution is to not mark the whole class with
325// __declspec(dllexport) for VC++, but only its non-inline methods.
326#ifdef _MSC_VER
327#define CPLSTRING_CLASS_DLL
328#define CPLSTRING_METHOD_DLL CPL_DLL
329#else
331#define CPLSTRING_CLASS_DLL CPL_DLL
332#define CPLSTRING_METHOD_DLL
334#endif
335
337 class CPLSTRING_CLASS_DLL CPLString : public std::string
338 {
339 public:
342 {
343 }
344
346 // cppcheck-suppress noExplicitConstructor
347 CPLString(const std::string &oStr) : std::string(oStr)
348 {
349 }
350
352 // cppcheck-suppress noExplicitConstructor
353 CPLString(const char *pszStr) : std::string(pszStr)
354 {
355 }
356
358 CPLString(const char *pszStr, size_t n) : std::string(pszStr, n)
359 {
360 }
361
363 operator const char *(void) const
364 {
365 return c_str();
366 }
367
369 char &operator[](std::string::size_type i)
370 {
371 return std::string::operator[](i);
372 }
373
375 const char &operator[](std::string::size_type i) const
376 {
377 return std::string::operator[](i);
378 }
379
381 char &operator[](int i)
382 {
383 return std::string::operator[](
384 static_cast<std::string::size_type>(i));
385 }
386
388 const char &operator[](int i) const
389 {
390 return std::string::operator[](
391 static_cast<std::string::size_type>(i));
392 }
393
395 void Clear()
396 {
397 resize(0);
398 }
399
403 void Seize(char *pszValue)
404 {
405 if (pszValue == nullptr)
406 Clear();
407 else
408 {
409 *this = pszValue;
410 CPLFree(pszValue);
411 }
412 }
413
414 /* There seems to be a bug in the way the compiler count indices...
415 * Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
416 CPLSTRING_METHOD_DLL CPLString &
417 Printf(CPL_FORMAT_STRING(const char *pszFormat), ...)
419 CPLSTRING_METHOD_DLL CPLString &
420 vPrintf(CPL_FORMAT_STRING(const char *pszFormat), va_list args)
422 CPLSTRING_METHOD_DLL CPLString &
423 FormatC(double dfValue, const char *pszFormat = nullptr);
424 CPLSTRING_METHOD_DLL CPLString &Trim();
425 CPLSTRING_METHOD_DLL CPLString &Recode(const char *pszSrcEncoding,
426 const char *pszDstEncoding);
427 CPLSTRING_METHOD_DLL CPLString &replaceAll(const std::string &osBefore,
428 const std::string &osAfter);
429 CPLSTRING_METHOD_DLL CPLString &replaceAll(const std::string &osBefore,
430 char chAfter);
431 CPLSTRING_METHOD_DLL CPLString &replaceAll(char chBefore,
432 const std::string &osAfter);
433 CPLSTRING_METHOD_DLL CPLString &replaceAll(char chBefore, char chAfter);
434
435 /* case insensitive find alternates */
436 CPLSTRING_METHOD_DLL size_t ifind(const std::string &str,
437 size_t pos = 0) const;
438 CPLSTRING_METHOD_DLL size_t ifind(const char *s, size_t pos = 0) const;
439 CPLSTRING_METHOD_DLL CPLString &toupper(void);
440 CPLSTRING_METHOD_DLL CPLString &tolower(void);
441
442 CPLSTRING_METHOD_DLL bool endsWith(const std::string &osStr) const;
443
444 CPLSTRING_METHOD_DLL CPLString URLEncode() const;
445
446 CPLSTRING_METHOD_DLL CPLString SQLQuotedIdentifier() const;
447
448 CPLSTRING_METHOD_DLL CPLString SQLQuotedLiteral() const;
449
450 private:
451 operator void *(void) = delete;
452 };
453
454#undef CPLSTRING_CLASS_DLL
455#undef CPLSTRING_METHOD_DLL
456
457 CPLString CPL_DLL CPLOPrintf(CPL_FORMAT_STRING(const char *pszFormat), ...)
459 CPLString CPL_DLL CPLOvPrintf(CPL_FORMAT_STRING(const char *pszFormat),
460 va_list args) CPL_PRINT_FUNC_FORMAT(1, 0);
461 CPLString CPL_DLL CPLQuotedSQLIdentifier(const char *pszIdent);
462
463 /* -------------------------------------------------------------------- */
464 /* URL processing functions, here since they depend on CPLString. */
465 /* -------------------------------------------------------------------- */
466 CPLString CPL_DLL CPLURLGetValue(const char *pszURL, const char *pszKey);
467 CPLString CPL_DLL CPLURLAddKVP(const char *pszURL, const char *pszKey,
468 const char *pszValue);
469
470 /************************************************************************/
471 /* CPLStringList */
472 /************************************************************************/
473
475 class CPL_DLL CPLStringList
476 {
477 char **papszList = nullptr;
478 mutable int nCount = 0;
479 mutable int nAllocation = 0;
480 bool bOwnList = false;
481 bool bIsSorted = false;
482
483 bool MakeOurOwnCopy();
484 bool EnsureAllocation(int nMaxLength);
485 int FindSortedInsertionPoint(const char *pszLine);
486
487 public:
489 explicit CPLStringList(char **papszList, int bTakeOwnership = TRUE);
490 explicit CPLStringList(CSLConstList papszList);
491 explicit CPLStringList(const std::vector<std::string> &aosList);
492 explicit CPLStringList(std::initializer_list<const char *> oInitList);
493 CPLStringList(const CPLStringList &oOther);
496
497 static const CPLStringList BoundToConstList(CSLConstList papszList);
498
499 CPLStringList &Clear();
500
502 inline void clear()
503 {
504 Clear();
505 }
506
508 int size() const
509 {
510 return Count();
511 }
512
513 int Count() const;
514
516 bool empty() const
517 {
518 return Count() == 0;
519 }
520
521 CPLStringList &AddString(const char *pszNewString);
522 CPLStringList &AddString(const std::string &newString);
523 CPLStringList &AddStringDirectly(char *pszNewString);
524
526 void push_back(const char *pszNewString)
527 {
528 AddString(pszNewString);
529 }
530
532 void push_back(const std::string &osStr)
533 {
534 AddString(osStr.c_str());
535 }
536
537#if defined(DOXYGEN_SKIP) || __cplusplus >= 201703L || \
538 (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
539 void push_back(std::string_view svStr);
540#endif
541
542 CPLStringList &InsertString(int nInsertAtLineNo, const char *pszNewLine)
543 {
544 return InsertStringDirectly(nInsertAtLineNo, CPLStrdup(pszNewLine));
545 }
546
547 CPLStringList &InsertStringDirectly(int nInsertAtLineNo,
548 char *pszNewLine);
549
550 // CPLStringList &InsertStrings( int nInsertAtLineNo, char
551 // **papszNewLines );
552
553 CPLStringList &RemoveStrings(int nFirstLineToDelete,
554 int nNumToRemove = 1);
555
557 int FindString(const char *pszTarget) const
558 {
559 return CSLFindString(papszList, pszTarget);
560 }
561
564 int PartialFindString(const char *pszNeedle) const
565 {
566 return CSLPartialFindString(papszList, pszNeedle);
567 }
568
569 int FindName(const char *pszName) const;
570 bool FetchBool(const char *pszKey, bool bDefault) const;
571 // Deprecated.
572 int FetchBoolean(const char *pszKey, int bDefault) const;
573 const char *FetchNameValue(const char *pszKey) const;
574 const char *FetchNameValueDef(const char *pszKey,
575 const char *pszDefault) const;
576 CPLStringList &AddNameValue(const char *pszKey, const char *pszValue);
577 CPLStringList &SetNameValue(const char *pszKey, const char *pszValue);
578
579 CPLStringList &SetString(int pos, const char *pszString);
580 CPLStringList &SetString(int pos, const std::string &osString);
581 CPLStringList &SetStringDirectly(int pos, char *pszString);
582
583 CPLStringList &Assign(char **papszListIn, int bTakeOwnership = TRUE);
584
586 CPLStringList &operator=(char **papszListIn)
587 {
588 return Assign(papszListIn, TRUE);
589 }
590
592 CPLStringList &operator=(const CPLStringList &oOther);
596 CPLStringList &operator=(CPLStringList &&oOther);
597
599 char *operator[](int i);
600
602 char *operator[](size_t i)
603 {
604 return (*this)[static_cast<int>(i)];
605 }
606
608 const char *operator[](int i) const;
609
611 const char *operator[](size_t i) const
612 {
613 return (*this)[static_cast<int>(i)];
614 }
615
617 const char *operator[](const char *pszKey) const
618 {
619 return FetchNameValue(pszKey);
620 }
621
623 inline const char *front() const
624 {
625 return papszList[0];
626 }
627
629 inline const char *back() const
630 {
631 return papszList[size() - 1];
632 }
633
635 const char *const *begin() const
636 {
637 return papszList ? &papszList[0] : nullptr;
638 }
639
641 const char *const *end() const
642 {
643 return papszList ? &papszList[size()] : nullptr;
644 }
645
647 char **List()
648 {
649 return papszList;
650 }
651
654 {
655 return papszList;
656 }
657
658 char **StealList();
659
660 CPLStringList &Sort();
661
663 int IsSorted() const
664 {
665 return bIsSorted;
666 }
667
669 operator char **(void)
670 {
671 return List();
672 }
673
675 operator CSLConstList(void) const
676 {
677 return List();
678 }
679
681 operator std::vector<std::string>(void) const
682 {
683 return std::vector<std::string>{begin(), end()};
684 }
685
686 private:
687 operator void *(void) = delete;
688 };
689
690#ifdef GDAL_COMPILATION
691
692#include <iterator> // For std::input_iterator_tag
693#include <memory>
694#include <string_view>
695#include <utility> // For std::pair
696
698 struct CPL_DLL CSLDestroyReleaser
699 {
700 void operator()(char **papszStr) const
701 {
702 CSLDestroy(papszStr);
703 }
704 };
705
709 using CSLUniquePtr = std::unique_ptr<char *, CSLDestroyReleaser>;
710
713 using CPLCharUniquePtr = std::unique_ptr<char, VSIFreeReleaser>;
714
715 namespace cpl
716 {
717
721 template <class StringType>
722 inline bool starts_with(const StringType &str, const char *prefix)
723 {
724 const size_t prefixLen = strlen(prefix);
725 return str.size() >= prefixLen &&
726 str.compare(0, prefixLen, prefix, prefixLen) == 0;
727 }
728
730 template <class StringType>
731 inline bool starts_with(const StringType &str, const std::string &prefix)
732 {
733 return str.size() >= prefix.size() &&
734 str.compare(0, prefix.size(), prefix) == 0;
735 }
736
738 template <class StringType>
739 inline bool starts_with(const StringType &str, std::string_view prefix)
740 {
741 return str.size() >= prefix.size() &&
742 str.compare(0, prefix.size(), prefix) == 0;
743 }
744
746 template <class StringType>
747 inline bool ends_with(const StringType &str, const char *suffix)
748 {
749 const size_t suffixLen = strlen(suffix);
750 return str.size() >= suffixLen &&
751 str.compare(str.size() - suffixLen, suffixLen, suffix,
752 suffixLen) == 0;
753 }
754
756 template <class StringType>
757 inline bool ends_with(const StringType &str, const std::string &suffix)
758 {
759 return str.size() >= suffix.size() &&
760 str.compare(str.size() - suffix.size(), suffix.size(), suffix) ==
761 0;
762 }
763
765 template <class StringType>
766 inline bool ends_with(const StringType &str, std::string_view suffix)
767 {
768 return str.size() >= suffix.size() &&
769 str.compare(str.size() - suffix.size(), suffix.size(), suffix) ==
770 0;
771 }
772
774 struct CPL_DLL CSLIterator
775 {
776 using iterator_category = std::input_iterator_tag;
777 using difference_type = std::ptrdiff_t;
778 using value_type = const char *;
779 using pointer = value_type *;
780 using reference = value_type &;
781
782 CSLConstList m_papszList = nullptr;
783 bool m_bAtEnd = false;
784
785 inline const char *operator*() const
786 {
787 return *m_papszList;
788 }
789
790 inline CSLIterator &operator++()
791 {
792 if (m_papszList)
793 ++m_papszList;
794 return *this;
795 }
796
797 bool operator==(const CSLIterator &other) const;
798
799 inline bool operator!=(const CSLIterator &other) const
800 {
801 return !(operator==(other));
802 }
803 };
804
811 struct CPL_DLL CSLIteratorWrapper
812 {
813 public:
815 inline explicit CSLIteratorWrapper(CSLConstList papszList)
816 : m_papszList(papszList)
817 {
818 }
819
821 inline CSLIterator begin() const
822 {
823 return {m_papszList, false};
824 }
825
827 inline CSLIterator end() const
828 {
829 return {m_papszList, true};
830 }
831
832 private:
833 CSLConstList m_papszList;
834 };
835
840 inline CSLIteratorWrapper Iterate(CSLConstList papszList)
841 {
842 return CSLIteratorWrapper{papszList};
843 }
844
846 inline CSLIteratorWrapper Iterate(const CPLStringList &aosList)
847 {
848 return Iterate(aosList.List());
849 }
850
854 inline CSLIteratorWrapper Iterate(char **) = delete;
855
860 struct CPL_DLL CSLNameValueIterator
861 {
862 using iterator_category = std::input_iterator_tag;
863 using difference_type = std::ptrdiff_t;
864 using value_type = std::pair<const char *, const char *>;
865 using pointer = value_type *;
866 using reference = value_type &;
867
868 CSLConstList m_papszList = nullptr;
869 bool m_bReturnNullKeyIfNotNameValue = false;
870 std::string m_osKey{};
871
872 value_type operator*();
873
874 inline CSLNameValueIterator &operator++()
875 {
876 if (m_papszList)
877 ++m_papszList;
878 return *this;
879 }
880
881 inline bool operator==(const CSLNameValueIterator &other) const
882 {
883 return m_papszList == other.m_papszList;
884 }
885
886 inline bool operator!=(const CSLNameValueIterator &other) const
887 {
888 return !(operator==(other));
889 }
890 };
891
905 struct CPL_DLL CSLNameValueIteratorWrapper
906 {
907 public:
909 inline explicit CSLNameValueIteratorWrapper(
910 CSLConstList papszList, bool bReturnNullKeyIfNotNameValue)
911 : m_papszList(papszList),
912 m_bReturnNullKeyIfNotNameValue(bReturnNullKeyIfNotNameValue)
913 {
914 }
915
917 inline CSLNameValueIterator begin() const
918 {
919 return {m_papszList, m_bReturnNullKeyIfNotNameValue};
920 }
921
923 CSLNameValueIterator end() const;
924
925 private:
926 CSLConstList m_papszList;
927 const bool m_bReturnNullKeyIfNotNameValue;
928 };
929
946 inline CSLNameValueIteratorWrapper
947 IterateNameValue(CSLConstList papszList,
948 bool bReturnNullKeyIfNotNameValue = false)
949 {
950 return CSLNameValueIteratorWrapper{papszList,
951 bReturnNullKeyIfNotNameValue};
952 }
953
955 inline CSLNameValueIteratorWrapper
956 IterateNameValue(const CPLStringList &aosList,
957 bool bReturnNullKeyIfNotNameValue = false)
958 {
959 return IterateNameValue(aosList.List(), bReturnNullKeyIfNotNameValue);
960 }
961
965 inline CSLIteratorWrapper IterateNameValue(char **, bool = false) = delete;
966
970 inline std::vector<std::string> ToVector(CSLConstList papszList)
971 {
972 return CPLStringList::BoundToConstList(papszList);
973 }
974
975 inline std::vector<std::string> ToVector(char **) = delete;
976
977 } // namespace cpl
978
979#endif
980
981} // extern "C++"
982
983#endif /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
984
985#endif /* CPL_STRING_H_INCLUDED */
String list class designed around our use of C "char**" string lists.
Definition cpl_string.h:476
const char * operator[](size_t i) const
Return string at specified index.
Definition cpl_string.h:611
const char * operator[](const char *pszKey) const
Return value corresponding to pszKey, or nullptr.
Definition cpl_string.h:617
int IsSorted() const
Returns whether the list is sorted.
Definition cpl_string.h:663
bool empty() const
Return whether the list is empty.
Definition cpl_string.h:516
const char *const * end() const
end() implementation
Definition cpl_string.h:641
CPLStringList & operator=(CSLConstList papszListIn)
Assignment operator.
void push_back(const std::string &osStr)
Add a string to the list.
Definition cpl_string.h:532
char ** List()
Return list.
Definition cpl_string.h:647
void clear()
Clear the list.
Definition cpl_string.h:502
void push_back(const char *pszNewString)
Add a string to the list.
Definition cpl_string.h:526
CPLStringList & InsertString(int nInsertAtLineNo, const char *pszNewLine)
Insert into the list at identified location.
Definition cpl_string.h:542
CPLStringList & operator=(char **papszListIn)
Assignment operator.
Definition cpl_string.h:586
char * operator[](size_t i)
Return string at specified index.
Definition cpl_string.h:602
int size() const
Return size of list.
Definition cpl_string.h:508
CSLConstList List() const
Return list.
Definition cpl_string.h:653
const char * front() const
Return first element.
Definition cpl_string.h:623
const char *const * begin() const
begin() implementation
Definition cpl_string.h:635
int FindString(const char *pszTarget) const
Return index of pszTarget in the list, or -1.
Definition cpl_string.h:557
const char * back() const
Return last element.
Definition cpl_string.h:629
static const CPLStringList BoundToConstList(CSLConstList papszList)
Return a CPLStringList that wraps the passed list.
Definition cplstringlist.cpp:160
int PartialFindString(const char *pszNeedle) const
Return index of pszTarget in the list (using partial search), or -1.
Definition cpl_string.h:564
Convenient string class based on std::string.
Definition cpl_string.h:338
void Clear()
Clear the string.
Definition cpl_string.h:395
CPLString(void)
Constructor.
Definition cpl_string.h:341
const char & operator[](std::string::size_type i) const
Return character at specified index.
Definition cpl_string.h:375
CPLString(const std::string &oStr)
Constructor.
Definition cpl_string.h:347
void Seize(char *pszValue)
Assign specified string and take ownership of it (assumed to be allocated with CPLMalloc()).
Definition cpl_string.h:403
char & operator[](int i)
Return character at specified index.
Definition cpl_string.h:381
const char & operator[](int i) const
Return character at specified index.
Definition cpl_string.h:388
char & operator[](std::string::size_type i)
Return character at specified index.
Definition cpl_string.h:369
CPLString(const char *pszStr, size_t n)
Constructor.
Definition cpl_string.h:358
CPLString(const char *pszStr)
Constructor.
Definition cpl_string.h:353
Various convenience functions for CPL.
#define CPLFree
Alias of VSIFree()
Definition cpl_conv.h:97
CPL error handling services.
CPLErr
Error category / error level.
Definition cpl_error.h:45
#define CPL_SCAN_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have scanf() formatting.
Definition cpl_port.h:1011
#define CPL_C_END
Macro to end a block of C symbols.
Definition cpl_port.h:279
#define CPL_C_START
Macro to start a block of C symbols.
Definition cpl_port.h:275
#define CPL_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a printf-like function.
Definition cpl_port.h:1025
#define CPL_RETURNS_NONNULL
Qualifier for a function that does not return NULL.
Definition cpl_port.h:1073
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition cpl_port.h:1009
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition cpl_port.h:1252
#define CPL_WARN_UNUSED_RESULT
Qualifier to warn when the return value of a function is not used.
Definition cpl_port.h:1035
#define CPL_SCANF_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a sscanf-like function.
Definition cpl_port.h:1027
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:165
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:195
int CSLFindStringCaseSensitive(CSLConstList papszList, const char *pszTarget)
Find a string within a string list(case sensitive)
Definition cpl_string.cpp:687
int CPLvsnprintf(char *str, size_t size, const char *fmt, va_list args)
vsnprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition cpl_string.cpp:1128
char * CPLEscapeString(const char *pszString, int nLength, int nScheme)
Apply escaping to string to preserve special characters.
Definition cpl_string.cpp:2215
char * CPLBinaryToHex(int nBytes, const GByte *pabyData)
Binary to hexadecimal translation.
Definition cpl_string.cpp:2789
std::string CPLRemoveSQLComments(const std::string &osInput)
Remove SQL comments from a string.
Definition cpl_string.cpp:3181
char ** CSLTokenizeStringComplex(const char *pszString, const char *pszDelimiter, int bHonourStrings, int bAllowEmptyTokens)
Obsolete tokenizing api.
Definition cpl_string.cpp:750
int CPLprintf(const char *fmt,...)
printf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition cpl_string.cpp:1400
int CPLBase64DecodeInPlace(GByte *pszBase64)
Decode base64 string "pszBase64" (null terminated) in place.
Definition cpl_base64.cpp:90
char ** CSLAddNameValue(char **papszStrList, const char *pszName, const char *pszValue)
Add a new entry to a StringList of "Name=Value" pairs, ("Name:Value" pairs are also supported for bac...
Definition cpl_string.cpp:2016
size_t CPLStrlenUTF8Ex(const char *pszUTF8Str)
Return the number of UTF-8 characters of a nul-terminated string.
Definition cpl_recode.cpp:1202
CPLString CPLQuotedSQLIdentifier(const char *pszIdent)
Return a CPLString of the SQL quoted identifier.
Definition cplstring.cpp:613
CPLString CPLOPrintf(const char *pszFormat,...)
Return a CPLString with the content of sprintf()
Definition cplstring.cpp:581
size_t CPLStrlcat(char *pszDest, const char *pszSrc, size_t nDestSize)
Appends a source string to a destination buffer.
Definition cpl_string.cpp:3080
int CSLPrint(CSLConstList papszStrList, FILE *fpOut)
Print a StringList to fpOut.
Definition cpl_string.cpp:445
char * CPLForceToASCII(const char *pabyData, int nLen, char chReplacementChar)
Return a new string that is made only of ASCII characters.
Definition cpl_recode.cpp:297
char ** CSLTokenizeString2(const char *pszString, const char *pszDelimiter, int nCSLTFlags)
Tokenize a string.
Definition cpl_string.cpp:817
CPLString CPLURLGetValue(const char *pszURL, const char *pszKey)
Return the value matching a key from a key=value pair in a URL.
Definition cplstring.cpp:500
size_t CPLStrlcpy(char *pszDest, const char *pszSrc, size_t nDestSize)
Copy source string to a destination buffer.
Definition cpl_string.cpp:3024
const char * CSLFetchNameValue(CSLConstList papszStrList, const char *pszName)
In a StringList of "Name=Value" pairs, look for the first value associated with the specified name.
Definition cpl_string.cpp:1678
char * CPLRecodeFromWChar(const wchar_t *pwszSource, const char *pszSrcEncoding, const char *pszDstEncoding)
Convert wchar_t string to UTF-8.
Definition cpl_recode.cpp:163
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
snprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition cpl_string.cpp:1347
bool CPLIsASCII(const char *pabyData, size_t nLen)
Test if a string is encoded as ASCII.
Definition cpl_recode.cpp:260
CPLValueType
Type of value.
Definition cpl_string.h:186
@ CPL_VALUE_INTEGER
Integer.
Definition cpl_string.h:189
@ CPL_VALUE_STRING
String.
Definition cpl_string.h:187
@ CPL_VALUE_REAL
Real number.
Definition cpl_string.h:188
void CSLDestroy(char **papszStrList)
Free string list.
Definition cpl_string.cpp:185
bool CPLFetchBool(CSLConstList papszStrList, const char *pszKey, bool bDefault)
Check for boolean key value.
Definition cpl_string.cpp:1607
int CSLFindName(CSLConstList papszStrList, const char *pszName)
Find StringList entry with given key name.
Definition cpl_string.cpp:1710
CPLErr CPLParseMemorySize(const char *pszValue, GIntBig *pnValue, bool *pbUnitSpecified)
Parse a memory size from a string.
Definition cpl_string.cpp:1751
int CPLVASPrintf(char **buf, const char *fmt, va_list args)
This is intended to serve as an easy to use C callable vasprintf() alternative.
Definition cpl_string.cpp:1037
const char * CPLParseNameValueSep(const char *pszNameValue, char **ppszKey, char chSep)
Parse NAME<Sep>VALUE string into name and value components.
Definition cpl_string.cpp:1933
int CPLTolower(int c)
Converts a (ASCII) uppercase character to lowercase.
Definition cpl_string.cpp:3167
int CPLCanRecode(const char *pszTestStr, const char *pszSrcEncoding, const char *pszDstEncoding)
Checks if it is possible to recode a string from one encoding to another.
Definition cpl_recode.cpp:1230
const char * CSLGetField(CSLConstList, int)
Fetches the indicated field, being careful not to crash if the field doesn't exist within this string...
Definition cpl_string.cpp:158
int CSLFindString(CSLConstList papszList, const char *pszTarget)
Find a string within a string list (case insensitive).
Definition cpl_string.cpp:654
int CSLSave(CSLConstList papszStrList, const char *pszFname)
Write a StringList to a text file.
Definition cpl_string.cpp:396
bool CPLTestBool(const char *pszValue)
Test what boolean value contained in the string.
Definition cpl_string.cpp:1535
char ** CSLParseCommandLine(const char *pszCommandLine)
Tokenize command line arguments in a list of strings.
Definition cpl_string.cpp:3137
const char * CSLFetchNameValueDef(CSLConstList papszStrList, const char *pszName, const char *pszDefault)
Same as CSLFetchNameValue() but return pszDefault in case of no match.
Definition cpl_string.cpp:1651
int CSLCount(CSLConstList papszStrList)
Return number of items in a string list.
Definition cpl_string.cpp:132
char ** CSLAddString(char **papszStrList, const char *pszNewString)
Append a string to a StringList and return a pointer to the modified StringList.
Definition cpl_string.cpp:68
size_t CPLStrnlen(const char *pszStr, size_t nMaxLen)
Returns the length of a NUL terminated string by reading at most the specified number of bytes.
Definition cpl_string.cpp:3114
char * CPLUTF8ForceToASCII(const char *pszStr, char chReplacementChar)
Return a new string that is made only of ASCII characters.
Definition cpl_recode.cpp:351
char ** CSLInsertString(char **papszStrList, int nInsertAtLineNo, const char *pszNewLine)
Insert a string at a given line number inside a StringList.
Definition cpl_string.cpp:546
void CSLSetNameValueSeparator(char **papszStrList, const char *pszSeparator)
Replace the default separator (":" or "=") with the passed separator in the given name/value list.
Definition cpl_string.cpp:2136
int CPLIsUTF8(const char *pabyData, int nLen)
Test if a string is encoded as UTF-8.
Definition cpl_recode_stub.cpp:500
int CSLPartialFindString(CSLConstList papszHaystack, const char *pszNeedle)
Find a substring within a string list.
Definition cpl_string.cpp:719
char ** CSLTokenizeString(const char *pszString)
Tokenizes a string and returns a StringList with one string for each token.
Definition cpl_string.cpp:740
char ** CSLFetchNameValueMultiple(CSLConstList papszStrList, const char *pszName)
In a StringList of "Name=Value" pairs, look for all the values with the specified name.
Definition cpl_string.cpp:1981
CPLValueType CPLGetValueType(const char *pszValue)
Detect the type of the value contained in a string, whether it is a real, an integer or a string Lead...
Definition cpl_string.cpp:2889
char ** CSLSetNameValue(char **papszStrList, const char *pszName, const char *pszValue)
Assign value to name in StringList.
Definition cpl_string.cpp:2055
char ** CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete, int nNumToRemove, char ***ppapszRetStrings)
Remove strings inside a StringList.
Definition cpl_string.cpp:572
char ** CSLLoad(const char *pszFname)
Load a text file into a string list.
Definition cpl_string.cpp:381
char * CPLRecode(const char *pszSource, const char *pszSrcEncoding, const char *pszDstEncoding)
Convert a string from a source encoding to a destination encoding.
Definition cpl_recode.cpp:79
char ** CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, CSLConstList papszOptions)
Load a text file into a string list.
Definition cpl_string.cpp:305
CPLString CPLURLAddKVP(const char *pszURL, const char *pszKey, const char *pszValue)
Return a new URL with a new key=value pair.
Definition cplstring.cpp:532
int CSLFetchBoolean(CSLConstList papszStrList, const char *pszKey, int bDefault)
DEPRECATED.
Definition cpl_string.cpp:1640
int CPLStrlenUTF8(const char *pszUTF8Str)
Return the number of UTF-8 characters of a nul-terminated string.
Definition cpl_recode.cpp:1168
char ** CSLAppendPrintf(char **papszStrList, const char *fmt,...)
Use CPLSPrintf() to append a new line at the end of a StringList.
Definition cpl_string.cpp:1018
char ** CSLInsertStrings(char **papszStrList, int nInsertAtLineNo, CSLConstList papszNewLines)
Copies the contents of a StringList inside another StringList before the specified line.
Definition cpl_string.cpp:481
CPLString CPLOvPrintf(const char *pszFormat, va_list args)
Return a CPLString with the content of vsprintf()
Definition cplstring.cpp:600
char ** CSLDuplicate(CSLConstList papszStrList)
Clone a string list.
Definition cpl_string.cpp:213
const char * CPLParseNameValue(const char *pszNameValue, char **ppszKey)
Parse NAME=VALUE string into name and value components.
Definition cpl_string.cpp:1886
int CSLTestBoolean(const char *pszValue)
Test what boolean value contained in the string.
Definition cpl_string.cpp:1560
const char * CPLSPrintf(const char *fmt,...)
CPLSPrintf() that works with 10 static buffer.
Definition cpl_string.cpp:965
int CPLToupper(int c)
Converts a (ASCII) lowercase character to uppercase.
Definition cpl_string.cpp:3152
GByte * CPLHexToBinary(const char *pszHex, int *pnBytes)
Hexadecimal to binary translation.
Definition cpl_string.cpp:2850
char ** CSLMerge(char **papszOrig, CSLConstList papszOverride)
Merge two lists.
Definition cpl_string.cpp:258
char ** CSLAddStringMayFail(char **papszStrList, const char *pszNewString)
Same as CSLAddString() but may return NULL in case of (memory) failure.
Definition cpl_string.cpp:77
int CPLEncodingCharSize(const char *pszEncoding)
Return bytes per character for encoding.
Definition cpl_recode.cpp:1122
char * CPLUnescapeString(const char *pszString, int *pnLength, int nScheme)
Unescape a string.
Definition cpl_string.cpp:2565
char * CPLBase64Encode(int nBytes, const GByte *pabyData)
Base64 encode a buffer.
Definition cpl_base64.cpp:196
int CPLTestBoolean(const char *pszValue)
Test what boolean value contained in the string.
Definition cpl_string.cpp:1582
wchar_t * CPLRecodeToWChar(const char *pszSource, const char *pszSrcEncoding, const char *pszDstEncoding)
Convert UTF-8 string to a wchar_t string.
Definition cpl_recode.cpp:219
Standard C Covers.
OGRLayer::FeatureIterator begin(OGRLayer *poLayer)
Return begin of feature iterator.
Definition ogrsf_frmts.h:478
OGRLayer::FeatureIterator end(OGRLayer *poLayer)
Return end of feature iterator.
Definition ogrsf_frmts.h:486