LCOV - code coverage report
Current view: top level - port - cpl_string.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 107 113 94.7 %
Date: 2025-01-18 12:42:00 Functions: 47 48 97.9 %

          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    14592693 :         CPLString(void)
     319    14592693 :         {
     320    14592473 :         }
     321             : 
     322             :         /** Constructor */
     323             :         // cppcheck-suppress noExplicitConstructor
     324     4017449 :         CPLString(const std::string &oStr) : std::string(oStr)
     325             :         {
     326     4017443 :         }
     327             : 
     328             :         /** Constructor */
     329             :         // cppcheck-suppress noExplicitConstructor
     330     7525830 :         CPLString(const char *pszStr) : std::string(pszStr)
     331             :         {
     332     7525807 :         }
     333             : 
     334             :         /** Constructor */
     335       60654 :         CPLString(const char *pszStr, size_t n) : std::string(pszStr, n)
     336             :         {
     337       60653 :         }
     338             : 
     339             :         /** Return string as zero terminated character array */
     340    21955722 :         operator const char *(void) const
     341             :         {
     342    21955722 :             return c_str();
     343             :         }
     344             : 
     345             :         /** Return character at specified index */
     346    47917112 :         char &operator[](std::string::size_type i)
     347             :         {
     348    47917112 :             return std::string::operator[](i);
     349             :         }
     350             : 
     351             :         /** Return character at specified index */
     352      257846 :         const char &operator[](std::string::size_type i) const
     353             :         {
     354      257846 :             return std::string::operator[](i);
     355             :         }
     356             : 
     357             :         /** Return character at specified index */
     358      119919 :         char &operator[](int i)
     359             :         {
     360             :             return std::string::operator[](
     361      119919 :                 static_cast<std::string::size_type>(i));
     362             :         }
     363             : 
     364             :         /** Return character at specified index */
     365       21413 :         const char &operator[](int i) const
     366             :         {
     367             :             return std::string::operator[](
     368       21413 :                 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     9623780 :     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      946838 :         int size() const
     477             :         {
     478      946838 :             return Count();
     479             :         }
     480             : 
     481             :         int Count() const;
     482             : 
     483             :         /** Return whether the list is empty. */
     484       28639 :         bool empty() const
     485             :         {
     486       28639 :             return Count() == 0;
     487             :         }
     488             : 
     489             :         CPLStringList &AddString(const char *pszNewString);
     490             :         CPLStringList &AddStringDirectly(char *pszNewString);
     491             : 
     492             :         /** Add a string to the list */
     493          39 :         void push_back(const char *pszNewString)
     494             :         {
     495          39 :             AddString(pszNewString);
     496          39 :         }
     497             : 
     498             :         /** Add a string to the list */
     499        1304 :         void push_back(const std::string &osStr)
     500             :         {
     501        1304 :             AddString(osStr.c_str());
     502        1304 :         }
     503             : 
     504         218 :         CPLStringList &InsertString(int nInsertAtLineNo, const char *pszNewLine)
     505             :         {
     506         218 :             return InsertStringDirectly(nInsertAtLineNo, CPLStrdup(pszNewLine));
     507             :         }
     508             : 
     509             :         CPLStringList &InsertStringDirectly(int nInsertAtLineNo,
     510             :                                             char *pszNewLine);
     511             : 
     512             :         // CPLStringList &InsertStrings( int nInsertAtLineNo, char
     513             :         // **papszNewLines ); CPLStringList &RemoveStrings( int
     514             :         // nFirstLineToDelete, int nNumToRemove=1 );
     515             : 
     516             :         /** Return index of pszTarget in the list, or -1 */
     517       16691 :         int FindString(const char *pszTarget) const
     518             :         {
     519       16691 :             return CSLFindString(papszList, pszTarget);
     520             :         }
     521             : 
     522             :         /** Return index of pszTarget in the list (using partial search), or -1
     523             :          */
     524             :         int PartialFindString(const char *pszNeedle) const
     525             :         {
     526             :             return CSLPartialFindString(papszList, pszNeedle);
     527             :         }
     528             : 
     529             :         int FindName(const char *pszName) const;
     530             :         bool FetchBool(const char *pszKey, bool bDefault) const;
     531             :         // Deprecated.
     532             :         int FetchBoolean(const char *pszKey, int bDefault) const;
     533             :         const char *FetchNameValue(const char *pszKey) const;
     534             :         const char *FetchNameValueDef(const char *pszKey,
     535             :                                       const char *pszDefault) const;
     536             :         CPLStringList &AddNameValue(const char *pszKey, const char *pszValue);
     537             :         CPLStringList &SetNameValue(const char *pszKey, const char *pszValue);
     538             : 
     539             :         CPLStringList &Assign(char **papszListIn, int bTakeOwnership = TRUE);
     540             : 
     541             :         /** Assignment operator */
     542       34429 :         CPLStringList &operator=(char **papszListIn)
     543             :         {
     544       34429 :             return Assign(papszListIn, TRUE);
     545             :         }
     546             : 
     547             :         /** Assignment operator */
     548             :         CPLStringList &operator=(const CPLStringList &oOther);
     549             :         /** Assignment operator */
     550             :         CPLStringList &operator=(CSLConstList papszListIn);
     551             :         /** Move assignment operator */
     552             :         CPLStringList &operator=(CPLStringList &&oOther);
     553             : 
     554             :         /** Return string at specified index */
     555             :         char *operator[](int i);
     556             : 
     557             :         /** Return string at specified index */
     558        1188 :         char *operator[](size_t i)
     559             :         {
     560        1188 :             return (*this)[static_cast<int>(i)];
     561             :         }
     562             : 
     563             :         /** Return string at specified index */
     564             :         const char *operator[](int i) const;
     565             : 
     566             :         /** Return string at specified index */
     567         287 :         const char *operator[](size_t i) const
     568             :         {
     569         287 :             return (*this)[static_cast<int>(i)];
     570             :         }
     571             : 
     572             :         /** Return value corresponding to pszKey, or nullptr */
     573        4161 :         const char *operator[](const char *pszKey) const
     574             :         {
     575        4161 :             return FetchNameValue(pszKey);
     576             :         }
     577             : 
     578             :         /** Return first element */
     579             :         inline const char *front() const
     580             :         {
     581             :             return papszList[0];
     582             :         }
     583             : 
     584             :         /** Return last element */
     585      460631 :         inline const char *back() const
     586             :         {
     587      460631 :             return papszList[size() - 1];
     588             :         }
     589             : 
     590             :         /** begin() implementation */
     591        5111 :         const char *const *begin() const
     592             :         {
     593        5111 :             return papszList ? &papszList[0] : nullptr;
     594             :         }
     595             : 
     596             :         /** end() implementation */
     597        5111 :         const char *const *end() const
     598             :         {
     599        5111 :             return papszList ? &papszList[size()] : nullptr;
     600             :         }
     601             : 
     602             :         /** Return list. Ownership remains to the object */
     603     2839191 :         char **List()
     604             :         {
     605     2839191 :             return papszList;
     606             :         }
     607             : 
     608             :         /** Return list. Ownership remains to the object */
     609     1944112 :         CSLConstList List() const
     610             :         {
     611     1944112 :             return papszList;
     612             :         }
     613             : 
     614             :         char **StealList();
     615             : 
     616             :         CPLStringList &Sort();
     617             : 
     618             :         /** Returns whether the list is sorted */
     619    19930856 :         int IsSorted() const
     620             :         {
     621    19930856 :             return bIsSorted;
     622             :         }
     623             : 
     624             :         /** Return lists */
     625        1432 :         operator char **(void)
     626             :         {
     627        1432 :             return List();
     628             :         }
     629             : 
     630             :         /** Return lists */
     631         753 :         operator CSLConstList(void) const
     632             :         {
     633         753 :             return List();
     634             :         }
     635             : 
     636             :         /** Return the list as a vector of strings */
     637         625 :         operator std::vector<std::string>(void) const
     638             :         {
     639         625 :             return std::vector<std::string>{begin(), end()};
     640             :         }
     641             :     };
     642             : 
     643             : #ifdef GDAL_COMPILATION
     644             : 
     645             : #include <iterator>  // For std::input_iterator_tag
     646             : #include <memory>
     647             : #include <utility>  // For std::pair
     648             : 
     649             :     /*! @cond Doxygen_Suppress */
     650             :     struct CPL_DLL CSLDestroyReleaser
     651             :     {
     652             :         void operator()(char **papszStr) const
     653             :         {
     654             :             CSLDestroy(papszStr);
     655             :         }
     656             :     };
     657             : 
     658             :     /*! @endcond */
     659             : 
     660             :     /** Unique pointer type to use with CSL functions returning a char** */
     661             :     using CSLUniquePtr = std::unique_ptr<char *, CSLDestroyReleaser>;
     662             : 
     663             :     /** Unique pointer type to use with functions returning a char* to release
     664             :      * with VSIFree */
     665             :     using CPLCharUniquePtr = std::unique_ptr<char, VSIFreeReleaser>;
     666             : 
     667             :     namespace cpl
     668             :     {
     669             : 
     670             :     /*! @cond Doxygen_Suppress */
     671             :     /** Iterator for a CSLConstList */
     672             :     struct CPL_DLL CSLIterator
     673             :     {
     674             :         using iterator_category = std::input_iterator_tag;
     675             :         using difference_type = std::ptrdiff_t;
     676             :         using value_type = const char *;
     677             :         using pointer = value_type *;
     678             :         using reference = value_type &;
     679             : 
     680             :         CSLConstList m_papszList = nullptr;
     681             :         bool m_bAtEnd = false;
     682             : 
     683      143403 :         inline const char *operator*() const
     684             :         {
     685      143403 :             return *m_papszList;
     686             :         }
     687             : 
     688      143202 :         inline CSLIterator &operator++()
     689             :         {
     690      143202 :             if (m_papszList)
     691      143200 :                 ++m_papszList;
     692      143202 :             return *this;
     693             :         }
     694             : 
     695             :         bool operator==(const CSLIterator &other) const;
     696             : 
     697      263768 :         inline bool operator!=(const CSLIterator &other) const
     698             :         {
     699      263768 :             return !(operator==(other));
     700             :         }
     701             :     };
     702             : 
     703             :     /*! @endcond */
     704             : 
     705             :     /** Wrapper for a CSLConstList that can be used with C++ iterators.
     706             :      *
     707             :      * @since GDAL 3.9
     708             :      */
     709             :     struct CPL_DLL CSLIteratorWrapper
     710             :     {
     711             :       public:
     712             :         /** Constructor */
     713      120565 :         inline explicit CSLIteratorWrapper(CSLConstList papszList)
     714      120565 :             : m_papszList(papszList)
     715             :         {
     716      120565 :         }
     717             : 
     718             :         /** Get the begin of the list */
     719      120567 :         inline CSLIterator begin() const
     720             :         {
     721      120567 :             return {m_papszList, false};
     722             :         }
     723             : 
     724             :         /** Get the end of the list */
     725      120568 :         inline CSLIterator end() const
     726             :         {
     727      120568 :             return {m_papszList, true};
     728             :         }
     729             : 
     730             :       private:
     731             :         CSLConstList m_papszList;
     732             :     };
     733             : 
     734             :     /** Wraps a CSLConstList in a structure that can be used with C++ iterators.
     735             :      *
     736             :      * @since GDAL 3.9
     737             :      */
     738      120568 :     inline CSLIteratorWrapper Iterate(CSLConstList papszList)
     739             :     {
     740      120568 :         return CSLIteratorWrapper{papszList};
     741             :     }
     742             : 
     743             :     /*! @cond Doxygen_Suppress */
     744        2257 :     inline CSLIteratorWrapper Iterate(const CPLStringList &aosList)
     745             :     {
     746        2257 :         return Iterate(aosList.List());
     747             :     }
     748             : 
     749             :     /*! @endcond */
     750             : 
     751             :     /*! @cond Doxygen_Suppress */
     752             :     inline CSLIteratorWrapper Iterate(char **) = delete;
     753             : 
     754             :     /*! @endcond */
     755             : 
     756             :     /*! @cond Doxygen_Suppress */
     757             :     /** Iterator for a CSLConstList as (name, value) pairs. */
     758             :     struct CPL_DLL CSLNameValueIterator
     759             :     {
     760             :         using iterator_category = std::input_iterator_tag;
     761             :         using difference_type = std::ptrdiff_t;
     762             :         using value_type = std::pair<const char *, const char *>;
     763             :         using pointer = value_type *;
     764             :         using reference = value_type &;
     765             : 
     766             :         CSLConstList m_papszList = nullptr;
     767             :         bool m_bReturnNullKeyIfNotNameValue = false;
     768             :         std::string m_osKey{};
     769             : 
     770             :         value_type operator*();
     771             : 
     772        1269 :         inline CSLNameValueIterator &operator++()
     773             :         {
     774        1269 :             if (m_papszList)
     775        1269 :                 ++m_papszList;
     776        1269 :             return *this;
     777             :         }
     778             : 
     779        5100 :         inline bool operator==(const CSLNameValueIterator &other) const
     780             :         {
     781        5100 :             return m_papszList == other.m_papszList;
     782             :         }
     783             : 
     784        5098 :         inline bool operator!=(const CSLNameValueIterator &other) const
     785             :         {
     786        5098 :             return !(operator==(other));
     787             :         }
     788             :     };
     789             : 
     790             :     /*! @endcond */
     791             : 
     792             :     /** Wrapper for a CSLConstList that can be used with C++ iterators
     793             :      * to get (name, value) pairs.
     794             :      *
     795             :      * This can for example be used to do the following:
     796             :      * for (const auto& [name, value]: cpl::IterateNameValue(papszList)) {}
     797             :      *
     798             :      * Note that a (name, value) pair returned by dereferencing an iterator
     799             :      * is invalidated by the next iteration on the iterator.
     800             :      *
     801             :      * @since GDAL 3.9
     802             :      */
     803             :     struct CPL_DLL CSLNameValueIteratorWrapper
     804             :     {
     805             :       public:
     806             :         /** Constructor */
     807        3831 :         inline explicit CSLNameValueIteratorWrapper(
     808             :             CSLConstList papszList, bool bReturnNullKeyIfNotNameValue)
     809        3831 :             : m_papszList(papszList),
     810        3831 :               m_bReturnNullKeyIfNotNameValue(bReturnNullKeyIfNotNameValue)
     811             :         {
     812        3831 :         }
     813             : 
     814             :         /** Get the begin of the list */
     815        3831 :         inline CSLNameValueIterator begin() const
     816             :         {
     817        3831 :             return {m_papszList, m_bReturnNullKeyIfNotNameValue};
     818             :         }
     819             : 
     820             :         /** Get the end of the list */
     821             :         CSLNameValueIterator end() const;
     822             : 
     823             :       private:
     824             :         CSLConstList m_papszList;
     825             :         const bool m_bReturnNullKeyIfNotNameValue;
     826             :     };
     827             : 
     828             :     /** Wraps a CSLConstList in a structure that can be used with C++ iterators
     829             :      * to get (name, value) pairs.
     830             :      *
     831             :      * This can for example be used to do the following:
     832             :      * for (const auto& [name, value]: cpl::IterateNameValue(papszList)) {}
     833             :      *
     834             :      * Note that a (name, value) pair returned by dereferencing an iterator
     835             :      * is invalidated by the next iteration on the iterator.
     836             :      *
     837             :      * @param papszList List to iterate over.
     838             :      * @param bReturnNullKeyIfNotNameValue When this is set to true, if a string
     839             :      * contained in the list if not of the form name=value, then the value of
     840             :      * the iterator will be (nullptr, string).
     841             :      *
     842             :      * @since GDAL 3.9
     843             :      */
     844             :     inline CSLNameValueIteratorWrapper
     845        3831 :     IterateNameValue(CSLConstList papszList,
     846             :                      bool bReturnNullKeyIfNotNameValue = false)
     847             :     {
     848        3831 :         return CSLNameValueIteratorWrapper{papszList,
     849        3831 :                                            bReturnNullKeyIfNotNameValue};
     850             :     }
     851             : 
     852             :     /*! @cond Doxygen_Suppress */
     853             :     inline CSLNameValueIteratorWrapper
     854        2526 :     IterateNameValue(const CPLStringList &aosList,
     855             :                      bool bReturnNullKeyIfNotNameValue = false)
     856             :     {
     857        2526 :         return IterateNameValue(aosList.List(), bReturnNullKeyIfNotNameValue);
     858             :     }
     859             : 
     860             :     /*! @endcond */
     861             : 
     862             :     /*! @cond Doxygen_Suppress */
     863             :     inline CSLIteratorWrapper IterateNameValue(char **, bool = false) = delete;
     864             : 
     865             :     /*! @endcond */
     866             : 
     867             :     /** Converts a CSLConstList to a std::vector<std::string> */
     868          94 :     inline std::vector<std::string> ToVector(CSLConstList papszList)
     869             :     {
     870         188 :         return CPLStringList::BoundToConstList(papszList);
     871             :     }
     872             : 
     873             :     inline std::vector<std::string> ToVector(char **) = delete;
     874             : 
     875             :     }  // namespace cpl
     876             : 
     877             : #endif
     878             : 
     879             : }  // extern "C++"
     880             : 
     881             : #endif /* def __cplusplus && !CPL_SUPRESS_CPLUSPLUS */
     882             : 
     883             : #endif /* CPL_STRING_H_INCLUDED */

Generated by: LCOV version 1.14