Go to the documentation of this file.
32 #ifndef CPL_STRING_H_INCLUDED
33 #define CPL_STRING_H_INCLUDED
71 void CPL_DLL CPL_STDCALL
CSLDestroy(
char **papszStrList);
73 char CPL_DLL **
CSLMerge(
char **papszOrig,
78 const char *pszString,
const char *pszDelimiter,
int bHonourStrings,
81 const char *pszDelimiter,
85 #define CSLT_HONOURSTRINGS 0x0001
87 #define CSLT_ALLOWEMPTYTOKENS 0x0002
89 #define CSLT_PRESERVEQUOTES 0x0004
91 #define CSLT_PRESERVEESCAPES 0x0008
93 #define CSLT_STRIPLEADSPACES 0x0010
95 #define CSLT_STRIPENDSPACES 0x0020
100 const char *pszFname,
int nMaxLines,
int nMaxCols,
106 char CPL_DLL **
CSLInsertString(
char **papszStrList,
int nInsertAtLineNo,
109 char **papszStrList,
int nFirstLineToDelete,
113 const char *pszTarget );
115 const char *pszNeedle );
137 const char *pszDefault );
149 const char *pszSeparator );
154 #define CPLES_BackslashQuotable 0
164 #define CPLES_XML_BUT_QUOTES 5
166 #define CPLES_CSV_FORCE_QUOTING 6
194 size_t CPL_DLL
CPLStrlcpy(
char* pszDest,
const char* pszSrc,
size_t nDestSize);
195 size_t CPL_DLL
CPLStrlcat(
char* pszDest,
const char* pszSrc,
size_t nDestSize);
196 size_t CPL_DLL
CPLStrnlen(
const char *pszStr,
size_t nMaxLen);
208 #if defined(ALIAS_CPLSNPRINTF_AS_SNPRINTF)
209 #define CPLsnprintf snprintf
217 #if defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
232 int CPL_DLL CPLsscanf( const
char* str,
250 #define CPL_ENC_LOCALE ""
252 #define CPL_ENC_UTF8 "UTF-8"
254 #define CPL_ENC_UTF16 "UTF-16"
256 #define CPL_ENC_UCS2 "UCS-2"
258 #define CPL_ENC_UCS4 "UCS-4"
260 #define CPL_ENC_ASCII "ASCII"
262 #define CPL_ENC_ISO8859_1 "ISO-8859-1"
266 void CPL_DLL CPLClearRecodeWarningFlags(
void );
269 const char *pszSource,
const char *pszSrcEncoding,
272 const wchar_t *pwszSource,
const char *pszSrcEncoding,
275 const char *pszSource,
const char *pszSrcEncoding,
277 int CPL_DLL
CPLIsUTF8(
const char* pabyData,
int nLen );
278 bool CPL_DLL
CPLIsASCII(
const char* pabyData,
size_t nLen );
280 const char* pabyData,
int nLen,
284 const char *pszTestStr,
const char *pszSrcEncoding,
292 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
310 # define CPLSTRING_CLASS_DLL
311 # define CPLSTRING_METHOD_DLL CPL_DLL
314 # define CPLSTRING_CLASS_DLL CPL_DLL
315 # define CPLSTRING_METHOD_DLL
320 class CPLSTRING_CLASS_DLL
CPLString :
public std::string
328 CPLString(
const std::string &oStr ) : std::string( oStr ) {}
331 CPLString(
const char *pszStr ) : std::string( pszStr ) {}
333 CPLString(
const char *pszStr,
size_t n ) : std::string( pszStr, n ) {}
336 operator const char* (void)
const {
return c_str(); }
341 return std::string::operator[](i);
347 return std::string::operator[](i);
353 return std::string::operator[](
354 static_cast<std::string::size_type
>(i));
360 return std::string::operator[](
361 static_cast<std::string::size_type
>(i));
372 if (pszValue ==
nullptr )
389 CPLSTRING_METHOD_DLL
CPLString &FormatC(
double dfValue, const
char *pszFormat =
nullptr );
391 CPLSTRING_METHOD_DLL
CPLString &Recode( const
char *pszSrcEncoding, const
char *pszDstEncoding );
392 CPLSTRING_METHOD_DLL
CPLString &replaceAll(
393 const std::
string &osBefore, const std::
string& osAfter );
394 CPLSTRING_METHOD_DLL
CPLString &replaceAll( const std::
string &osBefore,
char chAfter );
395 CPLSTRING_METHOD_DLL
CPLString &replaceAll(
char chBefore, const std::
string &osAfter );
396 CPLSTRING_METHOD_DLL
CPLString &replaceAll(
char chBefore,
char chAfter );
399 CPLSTRING_METHOD_DLL
size_t ifind( const std::
string & str,
size_t pos = 0 ) const;
400 CPLSTRING_METHOD_DLL
size_t ifind( const
char * s,
size_t pos = 0 ) const;
401 CPLSTRING_METHOD_DLL
CPLString &toupper(
void );
402 CPLSTRING_METHOD_DLL
CPLString &tolower(
void );
404 CPLSTRING_METHOD_DLL
bool endsWith( const std::
string& osStr ) const;
407 #undef CPLSTRING_CLASS_DLL
408 #undef CPLSTRING_METHOD_DLL
422 const
char* pszValue);
431 char **papszList =
nullptr;
432 mutable int nCount = 0;
433 mutable int nAllocation = 0;
434 bool bOwnList =
false;
435 bool bIsSorted =
false;
437 bool MakeOurOwnCopy();
438 bool EnsureAllocation(
int nMaxLength );
439 int FindSortedInsertionPoint(
const char *pszLine );
443 explicit CPLStringList(
char **papszList,
int bTakeOwnership=TRUE );
452 int size()
const {
return Count(); }
456 bool empty()
const {
return Count() == 0; }
462 {
return InsertStringDirectly( nInsertAtLineNo,
CPLStrdup(pszNewLine) ); }
463 CPLStringList &InsertStringDirectly(
int nInsertAtLineNo,
char *pszNewLine);
475 int FindName(
const char *pszName )
const;
476 bool FetchBool(
const char *pszKey,
bool bDefault )
const;
478 int FetchBoolean(
const char *pszKey,
int bDefault )
const;
479 const char *FetchNameValue(
const char *pszKey )
const;
480 const char *FetchNameValueDef(
481 const char *pszKey,
const char *pszDefault )
const;
482 CPLStringList &AddNameValue(
const char *pszKey,
const char *pszValue );
483 CPLStringList &SetNameValue(
const char *pszKey,
const char *pszValue );
485 CPLStringList &Assign(
char **papszListIn,
int bTakeOwnership=TRUE );
488 return Assign( papszListIn, TRUE ); }
497 char * operator[](
int i);
499 char *
operator[](
size_t i) {
return (*
this)[
static_cast<int>(i)]; }
501 const char * operator[](
int i)
const;
504 return (*
this)[
static_cast<int>(i)]; }
507 return FetchNameValue(pszKey); }
510 char**
List() {
return papszList; }
520 operator char**(void) {
return List(); }
525 #ifdef GDAL_COMPILATION
530 struct CPL_DLL CSLDestroyReleaser
532 void operator()(
char** papszStr)
const {
CSLDestroy(papszStr); }
537 using CSLUniquePtr = std::unique_ptr< char*, CSLDestroyReleaser>;
540 struct CPL_DLL CPLFreeReleaser
542 void operator()(
void* p)
const {
CPLFree(p); }
547 using CPLCharUniquePtr = std::unique_ptr<char, CPLFreeReleaser>;
char ** CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, CSLConstList papszOptions)
Load a text file into a string list.
Definition: cpl_string.cpp:322
CPLStringList & operator=(char **papszListIn)
Assignment operator.
Definition: cpl_string.h:487
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:844
char * CPLRecodeFromWChar(const wchar_t *pwszSource, const char *pszSrcEncoding, const char *pszDstEncoding)
Convert wchar_t string to UTF-8.
Definition: cpl_recode.cpp:182
char ** CSLInsertString(char **papszStrList, int nInsertAtLineNo, const char *pszNewLine)
Insert a string at a given line number inside a StringList.
Definition: cpl_string.cpp:568
char ** CSLTokenizeString2(const char *pszString, const char *pszDelimiter, int nCSLTFlags)
Tokenize a string.
Definition: cpl_string.cpp:842
int CSLFindStringCaseSensitive(CSLConstList papszList, const char *pszTarget)
Find a string within a string list(case sensitive)
Definition: cpl_string.cpp:710
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:203
CPLString(const char *pszStr, size_t n)
Constructor.
Definition: cpl_string.h:333
CPLString CPLURLAddKVP(const char *pszURL, const char *pszKey, const char *pszValue)
Return a new URL with a new key=value pair.
Definition: cplstring.cpp:454
char ** CSLMerge(char **papszOrig, CSLConstList papszOverride)
Merge two lists.
Definition: cpl_string.cpp:274
int CPLIsUTF8(const char *pabyData, int nLen)
Test if a string is encoded as UTF-8.
Definition: cpl_recode.cpp:284
char * CPLEscapeString(const char *pszString, int nLength, int nScheme)
Apply escaping to string to preserve special characters.
Definition: cpl_string.cpp:2056
char ** CSLLoad(const char *pszFname)
Load a text file into a string list.
Definition: cpl_string.cpp:403
CPLString CPLOvPrintf(const char *pszFormat, va_list args)
Return a CPLString with the content of vsprintf()
Definition: cplstring.cpp:523
CPLStringList & InsertString(int nInsertAtLineNo, const char *pszNewLine)
Insert into the list at identified location.
Definition: cpl_string.h:461
size_t CPLStrlcat(char *pszDest, const char *pszSrc, size_t nDestSize)
Appends a source string to a destination buffer.
Definition: cpl_string.cpp:2927
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:429
int CSLFindName(CSLConstList papszStrList, const char *pszName)
Find StringList entry with given key name.
Definition: cpl_string.cpp:1728
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:2738
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:1666
char ** CSLSetNameValue(char **papszStrList, const char *pszName, const char *pszValue)
Assign value to name in StringList.
Definition: cpl_string.cpp:1897
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
snprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1353
char ** CSLAppendPrintf(char **papszStrList, const char *fmt,...)
Use CPLSPrintf() to append a new line at the end of a StringList.
Definition: cpl_string.cpp:1045
int CSLCount(CSLConstList papszStrList)
Return number of items in a string list.
Definition: cpl_string.cpp:148
CSLConstList List() const
Return list.
Definition: cpl_string.h:512
Convenient string class based on std::string.
Definition: cpl_string.h:320
int size() const
Return size of list.
Definition: cpl_string.h:452
bool CPLFetchBool(CSLConstList papszStrList, const char *pszKey, bool bDefault)
Check for boolean key value.
Definition: cpl_string.cpp:1620
@ CPL_VALUE_STRING
String.
Definition: cpl_string.h:187
int CSLPartialFindString(CSLConstList papszHaystack, const char *pszNeedle)
Find a substring within a string list.
Definition: cpl_string.cpp:743
int FindString(const char *pszTarget) const
Return index of pszTarget in the list, or -1.
Definition: cpl_string.h:469
CPLString(const std::string &oStr)
Constructor.
Definition: cpl_string.h:328
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:174
const char & operator[](std::string::size_type i) const
Return character at specified index.
Definition: cpl_string.h:345
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:304
int CSLTestBoolean(const char *pszValue)
Test what boolean value contained in the string.
Definition: cpl_string.cpp:1573
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:241
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:503
bool CPLTestBool(const char *pszValue)
Test what boolean value contained in the string.
Definition: cpl_string.cpp:1546
char & operator[](int i)
Return character at specified index.
Definition: cpl_string.h:351
int CPLprintf(const char *fmt,...)
printf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1409
void Seize(char *pszValue)
Assign specified string and take ownership of it (assumed to be allocated with CPLMalloc()).
Definition: cpl_string.h:370
const char & operator[](int i) const
Return character at specified index.
Definition: cpl_string.h:358
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1056
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:1064
char * operator[](size_t i)
Return string at specified index.
Definition: cpl_string.h:499
char * CPLBase64Encode(int nBytes, const GByte *pabyData)
Base64 encode a buffer.
Definition: cpl_base64.cpp:198
size_t CPLStrlcpy(char *pszDest, const char *pszSrc, size_t nDestSize)
Copy source string to a destination buffer.
Definition: cpl_string.cpp:2870
bool empty() const
Return whether the list is empty.
Definition: cpl_string.h:456
char & operator[](std::string::size_type i)
Return character at specified index.
Definition: cpl_string.h:339
#define CPL_SCAN_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have scanf() formatting.
Definition: cpl_port.h:846
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:421
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:306
int CSLPrint(CSLConstList papszStrList, FILE *fpOut)
Print a StringList to fpOut.
Definition: cpl_string.cpp:467
char ** List()
Return list.
Definition: cpl_string.h:510
int CSLFindString(CSLConstList papszList, const char *pszTarget)
Find a string within a string list (case insensitive).
Definition: cpl_string.cpp:676
@ CPL_VALUE_INTEGER
Integer.
Definition: cpl_string.h:189
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:80
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:1156
const char * CPLSPrintf(const char *fmt,...)
CPLSPrintf() that works with 10 static buffer.
Definition: cpl_string.cpp:993
int IsSorted() const
Returns whether the list is sorted.
Definition: cpl_string.h:517
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:1858
char ** CSLAddStringMayFail(char **papszStrList, const char *pszNewString)
Same as CSLAddString() but may return NULL in case of (memory) failure.
Definition: cpl_string.cpp:93
int CPLStrlenUTF8(const char *pszUTF8Str)
Return the number of UTF-8 characters of a nul-terminated string.
Definition: cpl_recode.cpp:424
const char * operator[](size_t i) const
Return string at specified index.
Definition: cpl_string.h:503
const char * CPLParseNameValue(const char *pszNameValue, char **ppszKey)
Parse NAME=VALUE string into name and value components.
Definition: cpl_string.cpp:1774
char ** CSLTokenizeStringComplex(const char *pszString, const char *pszDelimiter, int bHonourStrings, int bAllowEmptyTokens)
Obsolete tokenizing api.
Definition: cpl_string.cpp:775
char * CPLUnescapeString(const char *pszString, int *pnLength, int nScheme)
Unescape a string.
Definition: cpl_string.cpp:2414
int CPLEncodingCharSize(const char *pszEncoding)
Return bytes per character for encoding.
Definition: cpl_recode.cpp:377
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:2962
int CSLSave(CSLConstList papszStrList, const char *pszFname)
Write a StringList to a text file.
Definition: cpl_string.cpp:418
bool CPLIsASCII(const char *pabyData, size_t nLen)
Test if a string is encoded as ASCII.
Definition: cpl_recode.cpp:303
char ** CSLDuplicate(CSLConstList papszStrList)
Clone a string list.
Definition: cpl_string.cpp:229
int PartialFindString(const char *pszNeedle) const
Return index of pszTarget in the list (using partial search), or -1.
Definition: cpl_string.h:472
#define CPL_SCANF_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a sscanf-like function.
Definition: cpl_port.h:861
void CSLDestroy(char **papszStrList)
Free string list.
Definition: cpl_string.cpp:201
@ CPL_VALUE_REAL
Real number.
Definition: cpl_string.h:188
CPLValueType
Type of value.
Definition: cpl_string.h:185
#define CPL_WARN_UNUSED_RESULT
Qualifier to warn when the return value of a function is not used.
Definition: cpl_port.h:869
#define CPL_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a printf-like function.
Definition: cpl_port.h:859
CPLString(const char *pszStr)
Constructor.
Definition: cpl_string.h:331
char ** CSLTokenizeString(const char *pszString)
Tokenizes a string and returns a StringList with one string for each token.
Definition: cpl_string.cpp:765
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:1977
char * CPLForceToASCII(const char *pabyData, int nLen, char chReplacementChar)
Return a new string that is made only of ASCII characters.
Definition: cpl_recode.cpp:335
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:1821
int CPLTestBoolean(const char *pszValue)
Test what boolean value contained in the string.
Definition: cpl_string.cpp:1595
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:1694
char ** CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete, int nNumToRemove, char ***ppapszRetStrings)
Remove strings inside a StringList.
Definition: cpl_string.cpp:594
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:450
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:84
void Clear()
Clear the string.
Definition: cpl_string.h:365
CPLString CPLOPrintf(const char *pszFormat,...)
Return a CPLString with the content of sprintf()
Definition: cplstring.cpp:504
char * CPLStrdup(const char *)
Safe version of strdup() function.
Definition: cpl_conv.cpp:301
char * CPLBinaryToHex(int nBytes, const GByte *pabyData)
Binary to hexadecimal translation.
Definition: cpl_string.cpp:2641
CPLString(void)
Constructor.
Definition: cpl_string.h:325
const char * operator[](const char *pszKey) const
Return value corresponding to pszKey, or nullptr.
Definition: cpl_string.h:506
char ** CSLParseCommandLine(const char *pszCommandLine)
Tokenize command line arguments in a list of strings.
Definition: cpl_string.cpp:2986
int CPLBase64DecodeInPlace(GByte *pszBase64)
Decode base64 string "pszBase64" (null terminated) in place.
Definition: cpl_base64.cpp:92
int CSLFetchBoolean(CSLConstList papszStrList, const char *pszKey, int bDefault)
DEPRECATED.
Definition: cpl_string.cpp:1655
CPLString CPLQuotedSQLIdentifier(const char *pszIdent)
Return a CPLString of the SQL quoted identifier.
Definition: cplstring.cpp:536
#define CPLFree
Alias of VSIFree()
Definition: cpl_conv.h:83
#define CPL_RETURNS_NONNULL
Qualifier for a function that does not return NULL.
Definition: cpl_port.h:902
GByte * CPLHexToBinary(const char *pszHex, int *pnBytes)
Hexadecimal to binary translation.
Definition: cpl_string.cpp:2699