GDAL

cpl_string.h
Go to the documentation of this file.
1/**********************************************************************
2 * $Id$
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 spatialys.com>
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
32#ifndef CPL_STRING_H_INCLUDED
33#define CPL_STRING_H_INCLUDED
34
35#include "cpl_error.h"
36#include "cpl_conv.h"
37#include "cpl_vsi.h"
38
39#include <stdbool.h>
40
63 CPL_C_START
64
65char CPL_DLL **CSLAddString(char **papszStrList,
66 const char *pszNewString) CPL_WARN_UNUSED_RESULT;
67char CPL_DLL **
68 CSLAddStringMayFail(char **papszStrList,
69 const char *pszNewString) CPL_WARN_UNUSED_RESULT;
70int CPL_DLL CSLCount(CSLConstList papszStrList);
71const char CPL_DLL *CSLGetField(CSLConstList, int);
72void CPL_DLL CPL_STDCALL CSLDestroy(char **papszStrList);
73char CPL_DLL **CSLDuplicate(CSLConstList papszStrList) CPL_WARN_UNUSED_RESULT;
74char CPL_DLL **CSLMerge(char **papszOrig,
75 CSLConstList papszOverride) CPL_WARN_UNUSED_RESULT;
76
77char CPL_DLL **CSLTokenizeString(const char *pszString) CPL_WARN_UNUSED_RESULT;
78char CPL_DLL **
79 CSLTokenizeStringComplex(const char *pszString, const char *pszDelimiter,
80 int bHonourStrings,
81 int bAllowEmptyTokens) CPL_WARN_UNUSED_RESULT;
82char CPL_DLL **CSLTokenizeString2(const char *pszString,
83 const char *pszDelimiter,
84 int nCSLTFlags) CPL_WARN_UNUSED_RESULT;
85
87#define CSLT_HONOURSTRINGS 0x0001
89#define CSLT_ALLOWEMPTYTOKENS 0x0002
91#define CSLT_PRESERVEQUOTES 0x0004
93#define CSLT_PRESERVEESCAPES 0x0008
95#define CSLT_STRIPLEADSPACES 0x0010
97#define CSLT_STRIPENDSPACES 0x0020
98
99int CPL_DLL CSLPrint(CSLConstList papszStrList, FILE *fpOut);
100char CPL_DLL **CSLLoad(const char *pszFname) CPL_WARN_UNUSED_RESULT;
101char CPL_DLL **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols,
102 CSLConstList papszOptions) CPL_WARN_UNUSED_RESULT;
103int CPL_DLL CSLSave(CSLConstList papszStrList, const char *pszFname);
104
105char CPL_DLL **
106 CSLInsertStrings(char **papszStrList, int nInsertAtLineNo,
107 CSLConstList papszNewLines) CPL_WARN_UNUSED_RESULT;
108char CPL_DLL **CSLInsertString(char **papszStrList, int nInsertAtLineNo,
109 const char *pszNewLine) CPL_WARN_UNUSED_RESULT;
110char CPL_DLL **
111 CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete, int nNumToRemove,
112 char ***ppapszRetStrings) CPL_WARN_UNUSED_RESULT;
113int CPL_DLL CSLFindString(CSLConstList papszList, const char *pszTarget);
114int CPL_DLL CSLFindStringCaseSensitive(CSLConstList papszList,
115 const char *pszTarget);
116int CPL_DLL CSLPartialFindString(CSLConstList papszHaystack,
117 const char *pszNeedle);
118int CPL_DLL CSLFindName(CSLConstList papszStrList, const char *pszName);
119int CPL_DLL CSLFetchBoolean(CSLConstList papszStrList, const char *pszKey,
120 int bDefault);
121
122/* TODO: Deprecate CSLTestBoolean. Remove in GDAL 3.x. */
123int CPL_DLL CSLTestBoolean(const char *pszValue);
124/* Do not use CPLTestBoolean in C++ code. Use CPLTestBool. */
125int CPL_DLL CPLTestBoolean(const char *pszValue);
126
127bool CPL_DLL CPLTestBool(const char *pszValue);
128bool CPL_DLL CPLFetchBool(CSLConstList papszStrList, const char *pszKey,
129 bool bDefault);
130
131const char CPL_DLL *CPLParseNameValue(const char *pszNameValue, char **ppszKey);
132const char CPL_DLL *CPLParseNameValueSep(const char *pszNameValue,
133 char **ppszKey, char chSep);
134
135const char CPL_DLL *CSLFetchNameValue(CSLConstList papszStrList,
136 const char *pszName);
137const char CPL_DLL *CSLFetchNameValueDef(CSLConstList papszStrList,
138 const char *pszName,
139 const char *pszDefault);
140char CPL_DLL **CSLFetchNameValueMultiple(CSLConstList papszStrList,
141 const char *pszName);
142char CPL_DLL **CSLAddNameValue(char **papszStrList, const char *pszName,
143 const char *pszValue) CPL_WARN_UNUSED_RESULT;
144char CPL_DLL **CSLSetNameValue(char **papszStrList, const char *pszName,
145 const char *pszValue) CPL_WARN_UNUSED_RESULT;
146void CPL_DLL CSLSetNameValueSeparator(char **papszStrList,
147 const char *pszSeparator);
148
149char CPL_DLL **CSLParseCommandLine(const char *pszCommandLine);
150
152#define CPLES_BackslashQuotable 0
154#define CPLES_XML 1
156#define CPLES_URL 2
158#define CPLES_SQL 3
160#define CPLES_CSV 4
163#define CPLES_XML_BUT_QUOTES 5
165#define CPLES_CSV_FORCE_QUOTING 6
167#define CPLES_SQLI 7
168
169char CPL_DLL *CPLEscapeString(const char *pszString, int nLength,
170 int nScheme) CPL_WARN_UNUSED_RESULT;
171char CPL_DLL *CPLUnescapeString(const char *pszString, int *pnLength,
172 int nScheme) CPL_WARN_UNUSED_RESULT;
173
174char CPL_DLL *CPLBinaryToHex(int nBytes,
175 const GByte *pabyData) CPL_WARN_UNUSED_RESULT;
176 GByte CPL_DLL *CPLHexToBinary(const char *pszHex,
177 int *pnBytes) CPL_WARN_UNUSED_RESULT;
178
179char CPL_DLL *CPLBase64Encode(int nBytes,
180 const GByte *pabyData) CPL_WARN_UNUSED_RESULT;
181int CPL_DLL CPLBase64DecodeInPlace(GByte *pszBase64) CPL_WARN_UNUSED_RESULT;
182
184 typedef enum
185{
186 CPL_VALUE_STRING,
187 CPL_VALUE_REAL,
188 CPL_VALUE_INTEGER
189} CPLValueType;
190
191 CPLValueType CPL_DLL CPLGetValueType(const char *pszValue);
192
193int CPL_DLL CPLToupper(int c);
194int CPL_DLL CPLTolower(int c);
195
196size_t CPL_DLL CPLStrlcpy(char *pszDest, const char *pszSrc, size_t nDestSize);
197size_t CPL_DLL CPLStrlcat(char *pszDest, const char *pszSrc, size_t nDestSize);
198size_t CPL_DLL CPLStrnlen(const char *pszStr, size_t nMaxLen);
199
200/* -------------------------------------------------------------------- */
201/* Locale independent formatting functions. */
202/* -------------------------------------------------------------------- */
203int CPL_DLL CPLvsnprintf(char *str, size_t size,
204 CPL_FORMAT_STRING(const char *fmt), va_list args)
205 CPL_PRINT_FUNC_FORMAT(3, 0);
206
207/* ALIAS_CPLSNPRINTF_AS_SNPRINTF might be defined to enable GCC 7 */
208/* -Wformat-truncation= warnings, but shouldn't be set for normal use */
209#if defined(ALIAS_CPLSNPRINTF_AS_SNPRINTF)
210#define CPLsnprintf snprintf
211#else
212int CPL_DLL CPLsnprintf(char *str, size_t size,
213 CPL_FORMAT_STRING(const char *fmt), ...)
214 CPL_PRINT_FUNC_FORMAT(3, 4);
215#endif
216
218#if defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
219int CPL_DLL CPLsprintf(char *str, CPL_FORMAT_STRING(const char *fmt), ...)
220 CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use CPLsnprintf instead");
221#else
222int CPL_DLL CPLsprintf(char *str, CPL_FORMAT_STRING(const char *fmt), ...)
223 CPL_PRINT_FUNC_FORMAT(2, 3);
224#endif
226int CPL_DLL CPLprintf(CPL_FORMAT_STRING(const char *fmt), ...)
227 CPL_PRINT_FUNC_FORMAT(1, 2);
228
229/* For some reason Doxygen_Suppress is needed to avoid warning. Not sure why */
231/* caution: only works with limited number of formats */
232int CPL_DLL CPLsscanf(const char *str, CPL_SCANF_FORMAT_STRING(const char *fmt),
233 ...) CPL_SCAN_FUNC_FORMAT(2, 3);
236const char CPL_DLL *CPLSPrintf(CPL_FORMAT_STRING(const char *fmt), ...)
237 CPL_PRINT_FUNC_FORMAT(1, 2) CPL_WARN_UNUSED_RESULT;
238char CPL_DLL **CSLAppendPrintf(char **papszStrList,
239 CPL_FORMAT_STRING(const char *fmt), ...)
240 CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_UNUSED_RESULT;
241int CPL_DLL CPLVASPrintf(char **buf, CPL_FORMAT_STRING(const char *fmt),
242 va_list args) CPL_PRINT_FUNC_FORMAT(2, 0);
243
244/* -------------------------------------------------------------------- */
245/* RFC 23 character set conversion/recoding API (cpl_recode.cpp). */
246/* -------------------------------------------------------------------- */
248#define CPL_ENC_LOCALE ""
250#define CPL_ENC_UTF8 "UTF-8"
252#define CPL_ENC_UTF16 "UTF-16"
254#define CPL_ENC_UCS2 "UCS-2"
256#define CPL_ENC_UCS4 "UCS-4"
258#define CPL_ENC_ASCII "ASCII"
260#define CPL_ENC_ISO8859_1 "ISO-8859-1"
261
262int CPL_DLL CPLEncodingCharSize(const char *pszEncoding);
264void CPL_DLL CPLClearRecodeWarningFlags(void);
266char CPL_DLL *CPLRecode(const char *pszSource, const char *pszSrcEncoding,
267 const char *pszDstEncoding)
268 CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
269char CPL_DLL *
270 CPLRecodeFromWChar(const wchar_t *pwszSource, const char *pszSrcEncoding,
271 const char *pszDstEncoding) CPL_WARN_UNUSED_RESULT;
272wchar_t CPL_DLL *
273 CPLRecodeToWChar(const char *pszSource, const char *pszSrcEncoding,
274 const char *pszDstEncoding) CPL_WARN_UNUSED_RESULT;
275int CPL_DLL CPLIsUTF8(const char *pabyData, int nLen);
276bool CPL_DLL CPLIsASCII(const char *pabyData, size_t nLen);
277char CPL_DLL *CPLForceToASCII(const char *pabyData, int nLen,
278 char chReplacementChar) CPL_WARN_UNUSED_RESULT;
279char CPL_DLL *CPLUTF8ForceToASCII(const char *pszStr, char chReplacementChar)
280 CPL_WARN_UNUSED_RESULT;
281int CPL_DLL CPLStrlenUTF8(const char *pszUTF8Str);
282int CPL_DLL CPLCanRecode(const char *pszTestStr, const char *pszSrcEncoding,
283 const char *pszDstEncoding) CPL_WARN_UNUSED_RESULT;
284 CPL_C_END
285
286/************************************************************************/
287/* CPLString */
288/************************************************************************/
289
290#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
291
292extern "C++"
293{
294#ifndef DOXYGEN_SKIP
295#include <string>
296#include <vector>
297#endif
298
299// VC++ implicitly applies __declspec(dllexport) to template base classes
300// of classes marked with __declspec(dllexport).
301// Hence, if marked with CPL_DLL, VC++ would export symbols for the
302// specialization of std::basic_string<char>, since it is a base class of
303// CPLString. As a result, if an application linked both gdal.dll and a static
304// library that (implicitly) instantiates std::string (almost all do!), then the
305// linker would emit an error concerning duplicate symbols for std::string. The
306// least intrusive solution is to not mark the whole class with
307// __declspec(dllexport) for VC++, but only its non-inline methods.
308#ifdef _MSC_VER
309#define CPLSTRING_CLASS_DLL
310#define CPLSTRING_METHOD_DLL CPL_DLL
311#else
313#define CPLSTRING_CLASS_DLL CPL_DLL
314#define CPLSTRING_METHOD_DLL
316#endif
317
319 class CPLSTRING_CLASS_DLL CPLString : public std::string
320 {
321 public:
323 CPLString(void)
324 {
325 }
326
328 // cppcheck-suppress noExplicitConstructor
329 CPLString(const std::string &oStr) : std::string(oStr)
330 {
331 }
332
334 // cppcheck-suppress noExplicitConstructor
335 CPLString(const char *pszStr) : std::string(pszStr)
336 {
337 }
338
340 CPLString(const char *pszStr, size_t n) : std::string(pszStr, n)
341 {
342 }
343
345 operator const char *(void) const
346 {
347 return c_str();
348 }
349
351 char &operator[](std::string::size_type i)
352 {
353 return std::string::operator[](i);
354 }
355
357 const char &operator[](std::string::size_type i) const
358 {
359 return std::string::operator[](i);
360 }
361
363 char &operator[](int i)
364 {
365 return std::string::operator[](
366 static_cast<std::string::size_type>(i));
367 }
368
370 const char &operator[](int i) const
371 {
372 return std::string::operator[](
373 static_cast<std::string::size_type>(i));
374 }
375
377 void Clear()
378 {
379 resize(0);
380 }
381
385 void Seize(char *pszValue)
386 {
387 if (pszValue == nullptr)
388 Clear();
389 else
390 {
391 *this = pszValue;
392 CPLFree(pszValue);
393 }
394 }
395
396 /* There seems to be a bug in the way the compiler count indices...
397 * Should be CPL_PRINT_FUNC_FORMAT (1, 2) */
398 CPLSTRING_METHOD_DLL CPLString &
399 Printf(CPL_FORMAT_STRING(const char *pszFormat), ...)
400 CPL_PRINT_FUNC_FORMAT(2, 3);
401 CPLSTRING_METHOD_DLL CPLString &
402 vPrintf(CPL_FORMAT_STRING(const char *pszFormat), va_list args)
403 CPL_PRINT_FUNC_FORMAT(2, 0);
404 CPLSTRING_METHOD_DLL CPLString &
405 FormatC(double dfValue, const char *pszFormat = nullptr);
406 CPLSTRING_METHOD_DLL CPLString &Trim();
407 CPLSTRING_METHOD_DLL CPLString &Recode(const char *pszSrcEncoding,
408 const char *pszDstEncoding);
409 CPLSTRING_METHOD_DLL CPLString &replaceAll(const std::string &osBefore,
410 const std::string &osAfter);
411 CPLSTRING_METHOD_DLL CPLString &replaceAll(const std::string &osBefore,
412 char chAfter);
413 CPLSTRING_METHOD_DLL CPLString &replaceAll(char chBefore,
414 const std::string &osAfter);
415 CPLSTRING_METHOD_DLL CPLString &replaceAll(char chBefore, char chAfter);
416
417 /* case insensitive find alternates */
418 CPLSTRING_METHOD_DLL size_t ifind(const std::string &str,
419 size_t pos = 0) const;
420 CPLSTRING_METHOD_DLL size_t ifind(const char *s, size_t pos = 0) const;
421 CPLSTRING_METHOD_DLL CPLString &toupper(void);
422 CPLSTRING_METHOD_DLL CPLString &tolower(void);
423
424 CPLSTRING_METHOD_DLL bool endsWith(const std::string &osStr) const;
425 };
426
427#undef CPLSTRING_CLASS_DLL
428#undef CPLSTRING_METHOD_DLL
429
430 CPLString CPL_DLL CPLOPrintf(CPL_FORMAT_STRING(const char *pszFormat), ...)
431 CPL_PRINT_FUNC_FORMAT(1, 2);
432 CPLString CPL_DLL CPLOvPrintf(CPL_FORMAT_STRING(const char *pszFormat),
433 va_list args) CPL_PRINT_FUNC_FORMAT(1, 0);
434 CPLString CPL_DLL CPLQuotedSQLIdentifier(const char *pszIdent);
435
436 /* -------------------------------------------------------------------- */
437 /* URL processing functions, here since they depend on CPLString. */
438 /* -------------------------------------------------------------------- */
439 CPLString CPL_DLL CPLURLGetValue(const char *pszURL, const char *pszKey);
440 CPLString CPL_DLL CPLURLAddKVP(const char *pszURL, const char *pszKey,
441 const char *pszValue);
442
443 /************************************************************************/
444 /* CPLStringList */
445 /************************************************************************/
446
448 class CPL_DLL CPLStringList
449 {
450 char **papszList = nullptr;
451 mutable int nCount = 0;
452 mutable int nAllocation = 0;
453 bool bOwnList = false;
454 bool bIsSorted = false;
455
456 bool MakeOurOwnCopy();
457 bool EnsureAllocation(int nMaxLength);
458 int FindSortedInsertionPoint(const char *pszLine);
459
460 public:
461 CPLStringList();
462 explicit CPLStringList(char **papszList, int bTakeOwnership = TRUE);
463 explicit CPLStringList(CSLConstList papszList);
464 explicit CPLStringList(const std::vector<std::string> &aosList);
465 explicit CPLStringList(std::initializer_list<const char *> oInitList);
466 CPLStringList(const CPLStringList &oOther);
467 CPLStringList(CPLStringList &&oOther);
468 ~CPLStringList();
469
470 static const CPLStringList BoundToConstList(CSLConstList papszList);
471
472 CPLStringList &Clear();
473
475 inline void clear()
476 {
477 Clear();
478 }
479
481 int size() const
482 {
483 return Count();
484 }
485
486 int Count() const;
487
489 bool empty() const
490 {
491 return Count() == 0;
492 }
493
494 CPLStringList &AddString(const char *pszNewString);
495 CPLStringList &AddStringDirectly(char *pszNewString);
496
497 CPLStringList &InsertString(int nInsertAtLineNo, const char *pszNewLine)
498 {
499 return InsertStringDirectly(nInsertAtLineNo, CPLStrdup(pszNewLine));
500 }
501
502 CPLStringList &InsertStringDirectly(int nInsertAtLineNo,
503 char *pszNewLine);
504
505 // CPLStringList &InsertStrings( int nInsertAtLineNo, char
506 // **papszNewLines ); CPLStringList &RemoveStrings( int
507 // nFirstLineToDelete, int nNumToRemove=1 );
508
510 int FindString(const char *pszTarget) const
511 {
512 return CSLFindString(papszList, pszTarget);
513 }
514
517 int PartialFindString(const char *pszNeedle) const
518 {
519 return CSLPartialFindString(papszList, pszNeedle);
520 }
521
522 int FindName(const char *pszName) const;
523 bool FetchBool(const char *pszKey, bool bDefault) const;
524 // Deprecated.
525 int FetchBoolean(const char *pszKey, int bDefault) const;
526 const char *FetchNameValue(const char *pszKey) const;
527 const char *FetchNameValueDef(const char *pszKey,
528 const char *pszDefault) const;
529 CPLStringList &AddNameValue(const char *pszKey, const char *pszValue);
530 CPLStringList &SetNameValue(const char *pszKey, const char *pszValue);
531
532 CPLStringList &Assign(char **papszListIn, int bTakeOwnership = TRUE);
533
535 CPLStringList &operator=(char **papszListIn)
536 {
537 return Assign(papszListIn, TRUE);
538 }
539
541 CPLStringList &operator=(const CPLStringList &oOther);
543 CPLStringList &operator=(CSLConstList papszListIn);
545 CPLStringList &operator=(CPLStringList &&oOther);
546
548 char *operator[](int i);
549
551 char *operator[](size_t i)
552 {
553 return (*this)[static_cast<int>(i)];
554 }
555
557 const char *operator[](int i) const;
558
560 const char *operator[](size_t i) const
561 {
562 return (*this)[static_cast<int>(i)];
563 }
564
566 const char *operator[](const char *pszKey) const
567 {
568 return FetchNameValue(pszKey);
569 }
570
572 inline const char *front() const
573 {
574 return papszList[0];
575 }
576
578 inline const char *back() const
579 {
580 return papszList[size() - 1];
581 }
582
584 const char *const *begin() const
585 {
586 return papszList ? &papszList[0] : nullptr;
587 }
588
590 const char *const *end() const
591 {
592 return papszList ? &papszList[size()] : nullptr;
593 }
594
596 char **List()
597 {
598 return papszList;
599 }
600
602 CSLConstList List() const
603 {
604 return papszList;
605 }
606
607 char **StealList();
608
609 CPLStringList &Sort();
610
612 int IsSorted() const
613 {
614 return bIsSorted;
615 }
616
618 operator char **(void)
619 {
620 return List();
621 }
622
624 operator CSLConstList(void) const
625 {
626 return List();
627 }
628
630 operator std::vector<std::string>(void) const
631 {
632 return std::vector<std::string>{begin(), end()};
633 }
634 };
635
636#ifdef GDAL_COMPILATION
637
638#include <iterator> // For std::input_iterator_tag
639#include <memory>
640#include <utility> // For std::pair
641
643 struct CPL_DLL CSLDestroyReleaser
644 {
645 void operator()(char **papszStr) const
646 {
647 CSLDestroy(papszStr);
648 }
649 };
650
654 using CSLUniquePtr = std::unique_ptr<char *, CSLDestroyReleaser>;
655
658 using CPLCharUniquePtr = std::unique_ptr<char, VSIFreeReleaser>;
659
660 namespace cpl
661 {
662
665 struct CPL_DLL CSLIterator
666 {
667 using iterator_category = std::input_iterator_tag;
668 using difference_type = std::ptrdiff_t;
669 using value_type = const char *;
670 using pointer = value_type *;
671 using reference = value_type &;
672
673 CSLConstList m_papszList = nullptr;
674 bool m_bAtEnd = false;
675
676 inline const char *operator*() const
677 {
678 return *m_papszList;
679 }
680
681 inline CSLIterator &operator++()
682 {
683 if (m_papszList)
684 ++m_papszList;
685 return *this;
686 }
687
688 bool operator==(const CSLIterator &other) const;
689
690 inline bool operator!=(const CSLIterator &other) const
691 {
692 return !(operator==(other));
693 }
694 };
695
702 struct CPL_DLL CSLIteratorWrapper
703 {
704 public:
706 inline explicit CSLIteratorWrapper(CSLConstList papszList)
707 : m_papszList(papszList)
708 {
709 }
710
712 inline CSLIterator begin() const
713 {
714 return {m_papszList, false};
715 }
716
718 inline CSLIterator end() const
719 {
720 return {m_papszList, true};
721 }
722
723 private:
724 CSLConstList m_papszList;
725 };
726
731 inline CSLIteratorWrapper Iterate(CSLConstList papszList)
732 {
733 return CSLIteratorWrapper{papszList};
734 }
735
737 inline CSLIteratorWrapper Iterate(const CPLStringList &aosList)
738 {
739 return Iterate(aosList.List());
740 }
741
745 inline CSLIteratorWrapper Iterate(char **) = delete;
746
751 struct CPL_DLL CSLNameValueIterator
752 {
753 using iterator_category = std::input_iterator_tag;
754 using difference_type = std::ptrdiff_t;
755 using value_type = std::pair<const char *, const char *>;
756 using pointer = value_type *;
757 using reference = value_type &;
758
759 CSLConstList m_papszList = nullptr;
760 bool m_bReturnNullKeyIfNotNameValue = false;
761 std::string m_osKey{};
762
763 value_type operator*();
764
765 inline CSLNameValueIterator &operator++()
766 {
767 if (m_papszList)
768 ++m_papszList;
769 return *this;
770 }
771
772 inline bool operator==(const CSLNameValueIterator &other) const
773 {
774 return m_papszList == other.m_papszList;
775 }
776
777 inline bool operator!=(const CSLNameValueIterator &other) const
778 {
779 return !(operator==(other));
780 }
781 };
782
796 struct CPL_DLL CSLNameValueIteratorWrapper
797 {
798 public:
800 inline explicit CSLNameValueIteratorWrapper(
801 CSLConstList papszList, bool bReturnNullKeyIfNotNameValue)
802 : m_papszList(papszList),
803 m_bReturnNullKeyIfNotNameValue(bReturnNullKeyIfNotNameValue)
804 {
805 }
806
808 inline CSLNameValueIterator begin() const
809 {
810 return {m_papszList, m_bReturnNullKeyIfNotNameValue};
811 }
812
814 CSLNameValueIterator end() const;
815
816 private:
817 CSLConstList m_papszList;
818 const bool m_bReturnNullKeyIfNotNameValue;
819 };
820
837 inline CSLNameValueIteratorWrapper
838 IterateNameValue(CSLConstList papszList,
839 bool bReturnNullKeyIfNotNameValue = false)
840 {
841 return CSLNameValueIteratorWrapper{papszList,
842 bReturnNullKeyIfNotNameValue};
843 }
844
846 inline CSLNameValueIteratorWrapper
847 IterateNameValue(const CPLStringList &aosList,
848 bool bReturnNullKeyIfNotNameValue = false)
849 {
850 return IterateNameValue(aosList.List(), bReturnNullKeyIfNotNameValue);
851 }
852
856 inline CSLIteratorWrapper IterateNameValue(char **, bool = false) = delete;
857
861 inline std::vector<std::string> ToVector(CSLConstList papszList)
862 {
863 return CPLStringList::BoundToConstList(papszList);
864 }
865
866 inline std::vector<std::string> ToVector(char **) = delete;
867
868 } // namespace cpl
869
870#endif
871
872} // extern "C++"
873
874#endif /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
875
876#endif /* CPL_STRING_H_INCLUDED */
CPLStringList
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:449
CPLStringList::operator[]
const char * operator[](size_t i) const
Return string at specified index.
Definition: cpl_string.h:560
CPLStringList::operator[]
const char * operator[](const char *pszKey) const
Return value corresponding to pszKey, or nullptr.
Definition: cpl_string.h:566
CPLStringList::IsSorted
int IsSorted() const
Returns whether the list is sorted.
Definition: cpl_string.h:612
CPLStringList::empty
bool empty() const
Return whether the list is empty.
Definition: cpl_string.h:489
CPLStringList::end
const char *const * end() const
end() implementation
Definition: cpl_string.h:590
CPLStringList::operator=
CPLStringList & operator=(CSLConstList papszListIn)
Assignment operator.
CPLStringList::List
char ** List()
Return list.
Definition: cpl_string.h:596
CPLStringList::clear
void clear()
Clear the list.
Definition: cpl_string.h:475
CPLStringList::InsertString
CPLStringList & InsertString(int nInsertAtLineNo, const char *pszNewLine)
Insert into the list at identified location.
Definition: cpl_string.h:497
CPLStringList::operator=
CPLStringList & operator=(char **papszListIn)
Assignment operator.
Definition: cpl_string.h:535
CPLStringList::operator[]
char * operator[](size_t i)
Return string at specified index.
Definition: cpl_string.h:551
CPLStringList::size
int size() const
Return size of list.
Definition: cpl_string.h:481
CPLStringList::List
CSLConstList List() const
Return list.
Definition: cpl_string.h:602
CPLStringList::front
const char * front() const
Return first element.
Definition: cpl_string.h:572
CPLStringList::begin
const char *const * begin() const
begin() implementation
Definition: cpl_string.h:584
CPLStringList::FindString
int FindString(const char *pszTarget) const
Return index of pszTarget in the list, or -1.
Definition: cpl_string.h:510
CPLStringList::back
const char * back() const
Return last element.
Definition: cpl_string.h:578
CPLStringList::BoundToConstList
static const CPLStringList BoundToConstList(CSLConstList papszList)
Return a CPLStringList that wraps the passed list.
Definition: cplstringlist.cpp:174
CPLStringList::PartialFindString
int PartialFindString(const char *pszNeedle) const
Return index of pszTarget in the list (using partial search), or -1.
Definition: cpl_string.h:517
CPLString
Convenient string class based on std::string.
Definition: cpl_string.h:320
CPLString::Clear
void Clear()
Clear the string.
Definition: cpl_string.h:377
CPLString::CPLString
CPLString(void)
Constructor.
Definition: cpl_string.h:323
CPLString::operator[]
const char & operator[](std::string::size_type i) const
Return character at specified index.
Definition: cpl_string.h:357
CPLString::CPLString
CPLString(const std::string &oStr)
Constructor.
Definition: cpl_string.h:329
CPLString::Seize
void Seize(char *pszValue)
Assign specified string and take ownership of it (assumed to be allocated with CPLMalloc()).
Definition: cpl_string.h:385
CPLString::operator[]
char & operator[](int i)
Return character at specified index.
Definition: cpl_string.h:363
CPLString::operator[]
const char & operator[](int i) const
Return character at specified index.
Definition: cpl_string.h:370
CPLString::operator[]
char & operator[](std::string::size_type i)
Return character at specified index.
Definition: cpl_string.h:351
CPLString::CPLString
CPLString(const char *pszStr, size_t n)
Constructor.
Definition: cpl_string.h:340
CPLString::CPLString
CPLString(const char *pszStr)
Constructor.
Definition: cpl_string.h:335
cpl_conv.h
Various convenience functions for CPL.
CPLFree
#define CPLFree
Alias of VSIFree()
Definition: cpl_conv.h:98
CPLStrdup
char * CPLStrdup(const char *)
Safe version of strdup() function.
Definition: cpl_conv.cpp:310
cpl_error.h
CPL error handling services.
CPL_SCAN_FUNC_FORMAT
#define CPL_SCAN_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have scanf() formatting.
Definition: cpl_port.h:952
CPL_C_END
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:299
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:295
CPL_FORMAT_STRING
#define CPL_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a printf-like function.
Definition: cpl_port.h:966
CPL_RETURNS_NONNULL
#define CPL_RETURNS_NONNULL
Qualifier for a function that does not return NULL.
Definition: cpl_port.h:1014
CPL_PRINT_FUNC_FORMAT
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:950
CSLConstList
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1183
CPL_WARN_UNUSED_RESULT
#define CPL_WARN_UNUSED_RESULT
Qualifier to warn when the return value of a function is not used.
Definition: cpl_port.h:976
CPL_SCANF_FORMAT_STRING
#define CPL_SCANF_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a sscanf-like function.
Definition: cpl_port.h:968
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:185
CSLFindStringCaseSensitive
int CSLFindStringCaseSensitive(CSLConstList papszList, const char *pszTarget)
Find a string within a string list(case sensitive)
Definition: cpl_string.cpp:704
CPLvsnprintf
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:1146
CPLEscapeString
char * CPLEscapeString(const char *pszString, int nLength, int nScheme)
Apply escaping to string to preserve special characters.
Definition: cpl_string.cpp:2105
CPLBinaryToHex
char * CPLBinaryToHex(int nBytes, const GByte *pabyData)
Binary to hexadecimal translation.
Definition: cpl_string.cpp:2679
CSLTokenizeStringComplex
char ** CSLTokenizeStringComplex(const char *pszString, const char *pszDelimiter, int bHonourStrings, int bAllowEmptyTokens)
Obsolete tokenizing api.
Definition: cpl_string.cpp:767
CPLprintf
int CPLprintf(const char *fmt,...)
printf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1421
CPLBase64DecodeInPlace
int CPLBase64DecodeInPlace(GByte *pszBase64)
Decode base64 string "pszBase64" (null terminated) in place.
Definition: cpl_base64.cpp:90
CSLAddNameValue
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:1907
CPLQuotedSQLIdentifier
CPLString CPLQuotedSQLIdentifier(const char *pszIdent)
Return a CPLString of the SQL quoted identifier.
Definition: cplstring.cpp:528
CPLOPrintf
CPLString CPLOPrintf(const char *pszFormat,...)
Return a CPLString with the content of sprintf()
Definition: cplstring.cpp:496
CPLStrlcat
size_t CPLStrlcat(char *pszDest, const char *pszSrc, size_t nDestSize)
Appends a source string to a destination buffer.
Definition: cpl_string.cpp:2961
CSLPrint
int CSLPrint(CSLConstList papszStrList, FILE *fpOut)
Print a StringList to fpOut.
Definition: cpl_string.cpp:461
CPLForceToASCII
char * CPLForceToASCII(const char *pabyData, int nLen, char chReplacementChar)
Return a new string that is made only of ASCII characters.
Definition: cpl_recode.cpp:315
CSLTokenizeString2
char ** CSLTokenizeString2(const char *pszString, const char *pszDelimiter, int nCSLTFlags)
Tokenize a string.
Definition: cpl_string.cpp:834
CPLURLGetValue
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:414
CPLStrlcpy
size_t CPLStrlcpy(char *pszDest, const char *pszSrc, size_t nDestSize)
Copy source string to a destination buffer.
Definition: cpl_string.cpp:2904
CSLFetchNameValue
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:1700
CPLRecodeFromWChar
char * CPLRecodeFromWChar(const wchar_t *pwszSource, const char *pszSrcEncoding, const char *pszDstEncoding)
Convert wchar_t string to UTF-8.
Definition: cpl_recode.cpp:180
CPLsnprintf
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
snprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1366
CPLIsASCII
bool CPLIsASCII(const char *pabyData, size_t nLen)
Test if a string is encoded as ASCII.
Definition: cpl_recode.cpp:278
CPLValueType
CPLValueType
Type of value.
Definition: cpl_string.h:185
CPL_VALUE_INTEGER
@ CPL_VALUE_INTEGER
Integer.
Definition: cpl_string.h:188
CPL_VALUE_STRING
@ CPL_VALUE_STRING
String.
Definition: cpl_string.h:186
CPL_VALUE_REAL
@ CPL_VALUE_REAL
Real number.
Definition: cpl_string.h:187
CSLDestroy
void CSLDestroy(char **papszStrList)
Free string list.
Definition: cpl_string.cpp:200
CPLFetchBool
bool CPLFetchBool(CSLConstList papszStrList, const char *pszKey, bool bDefault)
Check for boolean key value.
Definition: cpl_string.cpp:1629
CSLFindName
int CSLFindName(CSLConstList papszStrList, const char *pszName)
Find StringList entry with given key name.
Definition: cpl_string.cpp:1732
CPLVASPrintf
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:1054
CPLParseNameValueSep
const char * CPLParseNameValueSep(const char *pszNameValue, char **ppszKey, char chSep)
Parse NAME<Sep>VALUE string into name and value components.
Definition: cpl_string.cpp:1824
CPLTolower
int CPLTolower(int c)
Converts a (ASCII) uppercase character to lowercase.
Definition: cpl_string.cpp:3050
CPLCanRecode
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:1206
CSLGetField
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:173
CSLFindString
int CSLFindString(CSLConstList papszList, const char *pszTarget)
Find a string within a string list (case insensitive).
Definition: cpl_string.cpp:670
CSLSave
int CSLSave(CSLConstList papszStrList, const char *pszFname)
Write a StringList to a text file.
Definition: cpl_string.cpp:412
CPLTestBool
bool CPLTestBool(const char *pszValue)
Test what boolean value contained in the string.
Definition: cpl_string.cpp:1557
CSLParseCommandLine
char ** CSLParseCommandLine(const char *pszCommandLine)
Tokenize command line arguments in a list of strings.
Definition: cpl_string.cpp:3020
CSLFetchNameValueDef
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:1673
CSLCount
int CSLCount(CSLConstList papszStrList)
Return number of items in a string list.
Definition: cpl_string.cpp:147
CSLAddString
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:83
CPLStrnlen
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:2996
CPLUTF8ForceToASCII
char * CPLUTF8ForceToASCII(const char *pszStr, char chReplacementChar)
Return a new string that is made only of ASCII characters.
Definition: cpl_recode.cpp:366
CSLInsertString
char ** CSLInsertString(char **papszStrList, int nInsertAtLineNo, const char *pszNewLine)
Insert a string at a given line number inside a StringList.
Definition: cpl_string.cpp:562
CSLSetNameValueSeparator
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:2027
CPLIsUTF8
int CPLIsUTF8(const char *pabyData, int nLen)
Test if a string is encoded as UTF-8.
Definition: cpl_recode_stub.cpp:446
CSLPartialFindString
int CSLPartialFindString(CSLConstList papszHaystack, const char *pszNeedle)
Find a substring within a string list.
Definition: cpl_string.cpp:736
CSLTokenizeString
char ** CSLTokenizeString(const char *pszString)
Tokenizes a string and returns a StringList with one string for each token.
Definition: cpl_string.cpp:757
CSLFetchNameValueMultiple
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:1872
CPLGetValueType
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:2772
CSLSetNameValue
char ** CSLSetNameValue(char **papszStrList, const char *pszName, const char *pszValue)
Assign value to name in StringList.
Definition: cpl_string.cpp:1946
CSLRemoveStrings
char ** CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete, int nNumToRemove, char ***ppapszRetStrings)
Remove strings inside a StringList.
Definition: cpl_string.cpp:588
CSLLoad
char ** CSLLoad(const char *pszFname)
Load a text file into a string list.
Definition: cpl_string.cpp:397
CPLRecode
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
CSLLoad2
char ** CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, CSLConstList papszOptions)
Load a text file into a string list.
Definition: cpl_string.cpp:321
CPLURLAddKVP
CPLString CPLURLAddKVP(const char *pszURL, const char *pszKey, const char *pszValue)
Return a new URL with a new key=value pair.
Definition: cplstring.cpp:447
CSLFetchBoolean
int CSLFetchBoolean(CSLConstList papszStrList, const char *pszKey, int bDefault)
DEPRECATED.
Definition: cpl_string.cpp:1662
CPLStrlenUTF8
int CPLStrlenUTF8(const char *pszUTF8Str)
Return the number of UTF-8 characters of a nul-terminated string.
Definition: cpl_recode.cpp:1180
CSLAppendPrintf
char ** CSLAppendPrintf(char **papszStrList, const char *fmt,...)
Use CPLSPrintf() to append a new line at the end of a StringList.
Definition: cpl_string.cpp:1035
CSLInsertStrings
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:497
CPLOvPrintf
CPLString CPLOvPrintf(const char *pszFormat, va_list args)
Return a CPLString with the content of vsprintf()
Definition: cplstring.cpp:515
CSLDuplicate
char ** CSLDuplicate(CSLConstList papszStrList)
Clone a string list.
Definition: cpl_string.cpp:228
CPLParseNameValue
const char * CPLParseNameValue(const char *pszNameValue, char **ppszKey)
Parse NAME=VALUE string into name and value components.
Definition: cpl_string.cpp:1777
CSLTestBoolean
int CSLTestBoolean(const char *pszValue)
Test what boolean value contained in the string.
Definition: cpl_string.cpp:1582
CPLSPrintf
const char * CPLSPrintf(const char *fmt,...)
CPLSPrintf() that works with 10 static buffer.
Definition: cpl_string.cpp:982
CPLToupper
int CPLToupper(int c)
Converts a (ASCII) lowercase character to uppercase.
Definition: cpl_string.cpp:3035
CPLHexToBinary
GByte * CPLHexToBinary(const char *pszHex, int *pnBytes)
Hexadecimal to binary translation.
Definition: cpl_string.cpp:2733
CSLMerge
char ** CSLMerge(char **papszOrig, CSLConstList papszOverride)
Merge two lists.
Definition: cpl_string.cpp:273
CSLAddStringMayFail
char ** CSLAddStringMayFail(char **papszStrList, const char *pszNewString)
Same as CSLAddString() but may return NULL in case of (memory) failure.
Definition: cpl_string.cpp:92
CPLEncodingCharSize
int CPLEncodingCharSize(const char *pszEncoding)
Return bytes per character for encoding.
Definition: cpl_recode.cpp:1134
CPLUnescapeString
char * CPLUnescapeString(const char *pszString, int *pnLength, int nScheme)
Unescape a string.
Definition: cpl_string.cpp:2455
CPLBase64Encode
char * CPLBase64Encode(int nBytes, const GByte *pabyData)
Base64 encode a buffer.
Definition: cpl_base64.cpp:196
CPLTestBoolean
int CPLTestBoolean(const char *pszValue)
Test what boolean value contained in the string.
Definition: cpl_string.cpp:1604
CPLRecodeToWChar
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:237
cpl_vsi.h
Standard C Covers.
begin
OGRLayer::FeatureIterator begin(OGRLayer *poLayer)
Return begin of feature iterator.
Definition: ogrsf_frmts.h:426
end
OGRLayer::FeatureIterator end(OGRLayer *poLayer)
Return end of feature iterator.
Definition: ogrsf_frmts.h:434