cpl_string.h File Reference

#include "cpl_vsi.h"
#include "cpl_error.h"
#include "cpl_conv.h"
#include <string>

Go to the source code of this file.

Classes

class  CPLString

Defines

#define CSLT_HONOURSTRINGS   0x0001
#define CSLT_ALLOWEMPTYTOKENS   0x0002
#define CSLT_PRESERVEQUOTES   0x0004
#define CSLT_PRESERVEESCAPES   0x0008
#define CSLT_STRIPLEADSPACES   0x0010
#define CSLT_STRIPENDSPACES   0x0020
#define CPLES_BackslashQuotable   0
#define CPLES_XML   1
#define CPLES_URL   2
#define CPLES_SQL   3
#define CPLES_CSV   4
#define CPL_ENC_LOCALE   ""
#define CPL_ENC_UTF8   "UTF-8"
#define CPL_ENC_UTF16   "UTF-16"
#define CPL_ENC_UCS2   "UCS-2"
#define CPL_ENC_UCS4   "UCS-4"
#define CPL_ENC_ASCII   "ASCII"
#define CPL_ENC_ISO8859_1   "ISO-8859-1"
#define std_string   std::string

Enumerations

enum  CPLValueType { CPL_VALUE_STRING, CPL_VALUE_REAL, CPL_VALUE_INTEGER }

Functions

char ** CSLAddString (char **papszStrList, const char *pszNewString)
int CSLCount (char **papszStrList)
const char * CSLGetField (char **, int)
void CSLDestroy (char **papszStrList)
char ** CSLDuplicate (char **papszStrList)
char ** CSLMerge (char **papszOrig, char **papszOverride)
 Merge two lists.
char ** CSLTokenizeString (const char *pszString)
char ** CSLTokenizeStringComplex (const char *pszString, const char *pszDelimiter, int bHonourStrings, int bAllowEmptyTokens)
char ** CSLTokenizeString2 (const char *pszString, const char *pszDelimeter, int nCSLTFlags)
int CSLPrint (char **papszStrList, FILE *fpOut)
char ** CSLLoad (const char *pszFname)
int CSLSave (char **papszStrList, const char *pszFname)
char ** CSLInsertStrings (char **papszStrList, int nInsertAtLineNo, char **papszNewLines)
char ** CSLInsertString (char **papszStrList, int nInsertAtLineNo, const char *pszNewLine)
char ** CSLRemoveStrings (char **papszStrList, int nFirstLineToDelete, int nNumToRemove, char ***ppapszRetStrings)
int CSLFindString (char **, const char *)
int CSLPartialFindString (char **papszHaystack, const char *pszNeedle)
int CSLFindName (char **papszStrList, const char *pszName)
int CSLTestBoolean (const char *pszValue)
int CSLFetchBoolean (char **papszStrList, const char *pszKey, int bDefault)
const char * CPLSPrintf (const char *fmt,...) CPL_PRINT_FUNC_FORMAT(1
const char *char ** CSLAppendPrintf (char **papszStrList, const char *fmt,...) CPL_PRINT_FUNC_FORMAT(2
const char *char **int CPLVASPrintf (char **buf, const char *fmt, va_list args)
const char * CPLParseNameValue (const char *pszNameValue, char **ppszKey)
const char * CSLFetchNameValue (char **papszStrList, const char *pszName)
char ** CSLFetchNameValueMultiple (char **papszStrList, const char *pszName)
char ** CSLAddNameValue (char **papszStrList, const char *pszName, const char *pszValue)
char ** CSLSetNameValue (char **papszStrList, const char *pszName, const char *pszValue)
void CSLSetNameValueSeparator (char **papszStrList, const char *pszSeparator)
char * CPLEscapeString (const char *pszString, int nLength, int nScheme)
char * CPLUnescapeString (const char *pszString, int *pnLength, int nScheme)
char * CPLBinaryToHex (int nBytes, const GByte *pabyData)
GByte * CPLHexToBinary (const char *pszHex, int *pnBytes)
CPLValueType CPLGetValueType (const char *pszValue)
char * CPLRecode (const char *pszSource, const char *pszSrcEncoding, const char *pszDstEncoding)
char * CPLRecodeFromWChar (const wchar_t *pwszSource, const char *pszSrcEncoding, const char *pszDstEncoding)
wchar_t * CPLRecodeToWChar (const char *pszSource, const char *pszSrcEncoding, const char *pszDstEncoding)

Detailed Description

Various convenience functions for working with strings and string lists.

A StringList is just an array of strings with the last pointer being NULL. An empty StringList may be either a NULL pointer, or a pointer to a pointer memory location with a NULL value.

A common convention for StringLists is to use them to store name/value lists. In this case the contents are treated like a dictionary of name/value pairs. The actual data is formatted with each string having the format "<name>:<value>" (though "=" is also an acceptable separator). A number of the functions in the file operate on name/value style string lists (such as CSLSetNameValue(), and CSLFetchNameValue()).


Function Documentation

char* CPLBinaryToHex ( int  nBytes,
const GByte *  pabyData 
)

Binary to hexadecimal translation.

Parameters:
nBytes number of bytes of binary data in pabyData.
pabyData array of data bytes to translate.
Returns:
hexadecimal translation, zero terminated. Free with CPLFree().
char* CPLEscapeString ( const char *  pszInput,
int  nLength,
int  nScheme 
)

Apply escaping to string to preserve special characters.

This function will "escape" a variety of special characters to make the string suitable to embed within a string constant or to write within a text stream but in a form that can be reconstitued to it's original form. The escaping will even preserve zero bytes allowing preservation of raw binary data.

CPLES_BackslashQuotable(0): This scheme turns a binary string into a form suitable to be placed within double quotes as a string constant. The backslash, quote, '\0' and newline characters are all escaped in the usual C style.

CPLES_XML(1): This scheme converts the '<', '<' and '&' characters into their XML/HTML equivelent (>, < and &) making a string safe to embed as CDATA within an XML element. The '\0' is not escaped and should not be included in the input.

CPLES_URL(2): Everything except alphanumerics and the underscore are converted to a percent followed by a two digit hex encoding of the character (leading zero supplied if needed). This is the mechanism used for encoding values to be passed in URLs.

CPLES_SQL(3): All single quotes are replaced with two single quotes. Suitable for use when constructing literal values for SQL commands where the literal will be enclosed in single quotes.

CPLES_CSV(4): If the values contains commas, double quotes, or newlines it placed in double quotes, and double quotes in the value are doubled. Suitable for use when constructing field values for .csv files. Note that CPLUnescapeString() currently does not support this format, only CPLEscapeString(). See cpl_csv.cpp for csv parsing support.

Parameters:
pszInput the string to escape.
nLength The number of bytes of data to preserve. If this is -1 the strlen(pszString) function will be used to compute the length.
nScheme the encoding scheme to use.
Returns:
an escaped, zero terminated string that should be freed with CPLFree() when no longer needed.
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 Leading and trailing spaces are skipped in the analysis.

Parameters:
pszValue the string to analyze
Returns:
returns the type of the value contained in the string.
GByte* CPLHexToBinary ( const char *  pszHex,
int *  pnBytes 
)

Hexadecimal to binary translation

Parameters:
pszHex the input hex encoded string.
pnBytes the returned count of decoded bytes placed here.
Returns:
returns binary buffer of data - free with CPLFree().
const char* CPLParseNameValue ( const char *  pszNameValue,
char **  ppszKey 
)

Parse NAME=VALUE string into name and value components.

Note that if ppszKey is non-NULL, the key (or name) portion will be allocated using VSIMalloc(), and returned in that pointer. It is the applications responsibility to free this string, but the application should not modify or free the returned value portion.

This function also support "NAME:VALUE" strings and will strip white space from around the delimeter when forming name and value strings.

Eventually CSLFetchNameValue() and friends may be modified to use CPLParseNameValue().

Parameters:
pszNameValue string in "NAME=VALUE" format.
ppszKey optional pointer though which to return the name portion.
Returns:
the value portion (pointing into original string).
char* CPLRecodeFromWChar ( const wchar_t *  pwszSource,
const char *  pszSrcEncoding,
const char *  pszDstEncoding 
)

Convert wchar_t string to UTF-8.

Convert a wchar_t string into a multibyte utf-8 string. The only guaranteed supported source encoding is CPL_ENC_UCS2, and the only guaranteed supported destination encodings are CPL_ENC_UTF8, CPL_ENC_ASCII and CPL_ENC_ISO8859_1. In some cases (ie. using iconv()) other encodings may also be supported.

Note that the wchar_t type varies in size on different systems. On win32 it is normally 2 bytes, and on unix 4 bytes.

If an error occurs an error may, or may not be posted with CPLError().

Parameters:
pwszSource the source wchar_t string, terminated with a 0 wchar_t.
pszSrcEncoding the source encoding, typically CPL_ENC_UCS2.
pszDstEncoding the destination encoding, typically CPL_ENC_UTF8.
Returns:
a zero terminated multi-byte string which should be freed with CPLFree(), or NULL if an error occurs.
wchar_t* CPLRecodeToWChar ( const char *  pszSource,
const char *  pszSrcEncoding,
const char *  pszDstEncoding 
)

Convert UTF-8 string to a wchar_t string.

Convert a 8bit, multi-byte per character input string into a wide character (wchar_t) string. The only guaranteed supported source encodings are CPL_ENC_UTF8, CPL_ENC_ASCII and CPL_ENC_ISO8869_1 (LATIN1). The only guaranteed supported destination encoding is CPL_ENC_UCS2. Other source and destination encodings may be supported depending on the underlying implementation.

Note that the wchar_t type varies in size on different systems. On win32 it is normally 2 bytes, and on unix 4 bytes.

If an error occurs an error may, or may not be posted with CPLError().

Parameters:
pszSource input multi-byte character string.
pszSrcEncoding source encoding, typically CPL_ENC_UTF8.
pszDstEncoding destination encoding, typically CPL_ENC_UCS2.
Returns:
the zero terminated wchar_t string (to be freed with CPLFree()) or NULL on error.
char* CPLUnescapeString ( const char *  pszInput,
int *  pnLength,
int  nScheme 
)

Unescape a string.

This function does the opposite of CPLEscapeString(). Given a string with special values escaped according to some scheme, it will return a new copy of the string returned to it's original form.

Parameters:
pszInput the input string. This is a zero terminated string.
pnLength location to return the length of the unescaped string, which may in some cases include embedded '\0' characters.
nScheme the escaped scheme to undo (see CPLEscapeString() for a list).
Returns:
a copy of the unescaped string that should be freed by the application using CPLFree() when no longer needed.
int CSLCount ( char **  papszStrList  ) 

Return number of items in a string list.

Returns the number of items in a string list, not counting the terminating NULL. Passing in NULL is safe, and will result in a count of zero.

Lists are counted by iterating through them so long lists will take more time than short lists. Care should be taken to avoid using CSLCount() as an end condition for loops as it will result in O(n^2) behavior.

Parameters:
papszStrList the string list to count.
Returns:
the number of entries.
void CSLDestroy ( char **  papszStrList  ) 

Free string list.

Frees the passed string list (null terminated array of strings). It is safe to pass NULL.

Parameters:
papszStrList the list to free.
char** CSLDuplicate ( char **  papszStrList  ) 

Clone a string list.

Efficiently allocates a copy of a string list. The returned list is owned by the caller and should be freed with CSLDestroy().

Parameters:
papszStrList the input string list.
Returns:
newly allocated copy.
int CSLFindName ( char **  papszStrList,
const char *  pszName 
)

Find StringList entry with given key name.

Parameters:
papszStrList the string list to search.
pszName the key value to look for (case insensitive).
Returns:
-1 on failure or the list index of the first occurance matching the given key.
int CSLFindString ( char **  papszList,
const char *  pszTarget 
)

Find a string within a string list.

Returns the index of the entry in the string list that contains the target string. The string in the string list must be a full match for the target, but the search is case insensitive.

Parameters:
papszList the string list to be searched.
pszTarget the string to be searched for.
Returns:
the index of the string within the list or -1 on failure.
char** CSLLoad ( const char *  pszFname  ) 

Load a text file into a string list.

The VSI*L API is used, so VSIFOpenL() supported objects that aren't physical files can also be accessed. Files are returned as a string list, with one item in the string list per line. End of line markers are stripped (by CPLReadLineL()).

If reading the file fails a CPLError() will be issued and NULL returned.

Parameters:
pszFname the name of the file to read.
Returns:
a string list with the files lines, now owned by caller.

References VSIFCloseL(), VSIFEofL(), and VSIFOpenL().

char** CSLMerge ( char **  papszOrig,
char **  papszOverride 
)

Merge two lists.

The two lists are merged, ensuring that if any keys appear in both that the value from the second (papszOverride) list take precidence.

Parameters:
papszOrig the original list, being modified.
papszOverride the list of items being merged in. This list is unaltered and remains owned by the caller.
Returns:
updated list.
int CSLPartialFindString ( char **  papszHaystack,
const char *  pszNeedle 
)

Find a substring within a string list.

Returns the index of the entry in the string list that contains the target string as a substring. The search is case sensitive (unlike CSLFindString()).

Parameters:
papszHaystack the string list to be searched.
pszNeedle the substring to be searched for.
Returns:
the index of the string within the list or -1 on failure.
char** CSLSetNameValue ( char **  papszList,
const char *  pszName,
const char *  pszValue 
)

Assign value to name in StringList.

Set the value for a given name in a StringList of "Name=Value" pairs ("Name:Value" pairs are also supported for backward compatibility with older stuff.)

If there is already a value for that name in the list then the value is changed, otherwise a new "Name=Value" pair is added.

Parameters:
papszList the original list, the modified version is returned.
pszName the name to be assigned a value. This should be a well formed token (no spaces or very special characters).
pszValue the value to assign to the name. This should not contain any newlines (CR or LF) but is otherwise pretty much unconstrained. If NULL any corresponding value will be removed.
Returns:
modified stringlist.
void CSLSetNameValueSeparator ( char **  papszList,
const char *  pszSeparator 
)

Replace the default separator (":" or "=") with the passed separator in the given name/value list.

Note that if a separator other than ":" or "=" is used, the resulting list will not be manipulatable by the CSL name/value functions any more.

The CPLParseNameValue() function is used to break the existing lines, and it also strips white space from around the existing delimiter, thus the old separator, and any white space will be replaced by the new separator. For formatting purposes it may be desireable to include some white space in the new separator. eg. ": " or " = ".

Parameters:
papszList the list to update. Component strings may be freed but the list array will remain at the same location.
pszSeparator the new separator string to insert.
int CSLTestBoolean ( const char *  pszValue  ) 

Test what boolean value contained in the string.

If pszValue is "NO", "FALSE", "OFF" or "0" will be returned FALSE. Otherwise, TRUE will be returned.

Parameters:
pszValue the string should be tested.
Returns:
TRUE or FALSE.
char** CSLTokenizeString2 ( const char *  pszString,
const char *  pszDelimiters,
int  nCSLTFlags 
)

Tokenize a string.

This function will split a string into tokens based on specified' delimeter(s) with a variety of options. The returned result is a string list that should be freed with CSLDestroy() when no longer needed.

The available parsing options are:

  • CSLT_ALLOWEMPTYTOKENS: allow the return of empty tokens when two delimiters in a row occur with no other text between them. If not set, empty tokens will be discarded;
  • CSLT_STRIPLEADSPACES: strip leading space characters from the token (as reported by isspace());
  • CSLT_STRIPENDSPACES: strip ending space characters from the token (as reported by isspace());
  • CSLT_HONOURSTRINGS: double quotes can be used to hold values that should not be broken into multiple tokens;
  • CSLT_PRESERVEQUOTES: string quotes are carried into the tokens when this is set, otherwise they are removed;
  • CSLT_PRESERVEESCAPES: if set backslash escapes (for backslash itself, and for literal double quotes) will be preserved in the tokens, otherwise the backslashes will be removed in processing.

Example:

Parse a string into tokens based on various white space (space, newline, tab) and then print out results and cleanup. Quotes may be used to hold white space in tokens.

    char **papszTokens;
    int i;

    papszTokens = 
        CSLTokenizeString2( pszCommand, " \t\n", 
                            CSLT_HONOURSTRINGS | CSLT_ALLOWEMPTYTOKENS );

    for( i = 0; papszTokens != NULL && papszTokens[i] != NULL; i++ )
        printf( "arg %d: '%s'", papszTokens[i] );
    CSLDestroy( papszTokens );
Parameters:
pszString the string to be split into tokens.
pszDelimiters one or more characters to be used as token delimeters.
nCSLTFlags an ORing of one or more of the CSLT_ flag values.
Returns:
a string list of tokens owned by the caller.

Generated for GDAL by doxygen 1.6.2-20100208.