📄 cpl_string.cpp
字号:
( (*ppszKey)[i] == ' ' || (*ppszKey)[i] == '\t') ) { (*ppszKey)[i] = '\0'; i--; } } return pszValue; } } return NULL;}/********************************************************************** * CSLFetchNameValueMultiple() * * In a StringList of "Name=Value" pairs, look for all the * values with the specified name. The search is not case * sensitive. * ("Name:Value" pairs are also supported for backward compatibility * with older stuff.) * * Returns stringlist with one entry for each occurence of the * specified name. The stringlist should eventually be destroyed * by calling CSLDestroy(). * * Returns NULL if the name is not found. **********************************************************************/char **CSLFetchNameValueMultiple(char **papszStrList, const char *pszName){ int nLen; char **papszValues = NULL; if (papszStrList == NULL || pszName == NULL) return NULL; nLen = strlen(pszName); while(*papszStrList != NULL) { if (EQUALN(*papszStrList, pszName, nLen) && ( (*papszStrList)[nLen] == '=' || (*papszStrList)[nLen] == ':' ) ) { papszValues = CSLAddString(papszValues, (*papszStrList)+nLen+1); } papszStrList++; } return papszValues;}/********************************************************************** * CSLAddNameValue() * * Add a new entry to a StringList of "Name=Value" pairs, * ("Name:Value" pairs are also supported for backward compatibility * with older stuff.) * * This function does not check if a "Name=Value" pair already exists * for that name and can generate multiple entryes for the same name. * Use CSLSetNameValue() if you want each name to have only one value. * * Returns the modified stringlist. **********************************************************************/char **CSLAddNameValue(char **papszStrList, const char *pszName, const char *pszValue){ char *pszLine; if (pszName == NULL || pszValue==NULL) return papszStrList; pszLine = (char *) CPLMalloc(strlen(pszName)+strlen(pszValue)+2); sprintf( pszLine, "%s=%s", pszName, pszValue ); papszStrList = CSLAddString(papszStrList, pszLine); CPLFree( pszLine ); return papszStrList;}/************************************************************************//* CSLSetNameValue() *//************************************************************************//** * 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. * * @param papszList the original list, the modified version is returned. * @param pszName the name to be assigned a value. This should be a well * formed token (no spaces or very special characters). * @param pszValue the value to assign to the name. This should not contain * any newlines (CR or LF) but is otherwise pretty much unconstrained. * * @return modified stringlist. */char **CSLSetNameValue(char **papszList, const char *pszName, const char *pszValue){ char **papszPtr; int nLen; if (pszName == NULL || pszValue==NULL) return papszList; nLen = strlen(pszName); papszPtr = papszList; while(papszPtr && *papszPtr != NULL) { if (EQUALN(*papszPtr, pszName, nLen) && ( (*papszPtr)[nLen] == '=' || (*papszPtr)[nLen] == ':' ) ) { /* Found it! * Change the value... make sure to keep the ':' or '=' */ char cSep; cSep = (*papszPtr)[nLen]; CPLFree(*papszPtr); *papszPtr = (char *) CPLMalloc(strlen(pszName)+strlen(pszValue)+2); sprintf( *papszPtr, "%s%c%s", pszName, cSep, pszValue ); return papszList; } papszPtr++; } /* The name does not exist yet... create a new entry */ return CSLAddNameValue(papszList, pszName, pszValue);}/************************************************************************//* CSLSetNameValueSeparator() *//************************************************************************//** * 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 " = ". * * @param papszList the list to update. Component strings may be freed * but the list array will remain at the same location. * * @param pszSeparator the new separator string to insert. * */void CSLSetNameValueSeparator( char ** papszList, const char *pszSeparator ){ int nLines = CSLCount(papszList), iLine; for( iLine = 0; iLine < nLines; iLine++ ) { char *pszKey = NULL; const char *pszValue; char *pszNewLine; pszValue = CPLParseNameValue( papszList[iLine], &pszKey ); pszNewLine = (char *) CPLMalloc( strlen(pszValue) + strlen(pszKey) + strlen(pszSeparator) + 1 ); strcpy( pszNewLine, pszKey ); strcat( pszNewLine, pszSeparator ); strcat( pszNewLine, pszValue ); CPLFree( papszList[iLine] ); papszList[iLine] = pszNewLine; }}/************************************************************************//* CPLEscapeString() *//************************************************************************//** * 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. * * @param pszInput the string to escape. * @param nLength The number of bytes of data to preserve. If this is -1 * the strlen(pszString) function will be used to compute the length. * @param nScheme the encoding scheme to use. * * @return an escaped, zero terminated string that should be freed with * CPLFree() when no longer needed. */char *CPLEscapeString( const char *pszInput, int nLength, int nScheme ){ char *pszOutput; char *pszShortOutput; if( nLength == -1 ) nLength = strlen(pszInput); pszOutput = (char *) CPLMalloc( nLength * 6 + 1 ); if( nScheme == CPLES_BackslashQuotable ) { int iOut = 0, iIn; for( iIn = 0; iIn < nLength; iIn++ ) { if( pszInput[iIn] == '\0' ) { pszOutput[iOut++] = '\\'; pszOutput[iOut++] = '0'; } else if( pszInput[iIn] == '"' ) { pszOutput[iOut++] = '\\'; pszOutput[iOut++] = 'n'; } else if( pszInput[iIn] == '\\' ) { pszOutput[iOut++] = '\\'; pszOutput[iOut++] = '\\'; } else pszOutput[iOut++] = pszInput[iIn]; } pszOutput[iOut] = '\0'; } else if( nScheme == CPLES_URL ) /* Untested at implementation */ { int iOut = 0, iIn; for( iIn = 0; iIn < nLength; iIn++ ) { if( (pszInput[iIn] >= 'a' && pszInput[iIn] <= 'z') || (pszInput[iIn] >= 'A' && pszInput[iIn] <= 'Z') || (pszInput[iIn] >= '0' && pszInput[iIn] <= '9') || pszInput[iIn] == '_' ) { pszOutput[iOut++] = pszInput[iIn]; } else { sprintf( pszOutput, "%%%02X", pszInput[iIn] ); iOut += 3; } } pszOutput[iOut] = '\0'; } else if( nScheme == CPLES_XML ) { int iOut = 0, iIn; for( iIn = 0; iIn < nLength; iIn++ ) { if( pszInput[iIn] == '<' ) { pszOutput[iOut++] = '&'; pszOutput[iOut++] = 'l'; pszOutput[iOut++] = 't'; pszOutput[iOut++] = ';'; } else if( pszInput[iIn] == '>' ) { pszOutput[iOut++] = '&'; pszOutput[iOut++] = 'g'; pszOutput[iOut++] = 't'; pszOutput[iOut++] = ';'; } else if( pszInput[iIn] == '&' ) { pszOutput[iOut++] = '&'; pszOutput[iOut++] = 'a'; pszOutput[iOut++] = 'm'; pszOutput[iOut++] = 'p'; pszOutput[iOut++] = ';'; } else if( pszInput[iIn] == '"' ) { pszOutput[iOut++] = '&'; pszOutput[iOut++] = 'q'; pszOutput[iOut++] = 'u'; pszOutput[iOut++] = 'o'; pszOutput[iOut++] = 't'; pszOutput[iOut++] = ';'; } else pszOutput[iOut++] = pszInput[iIn]; } pszOutput[iOut] = '\0'; } else { pszOutput[0] = '\0'; CPLError( CE_Failure, CPLE_AppDefined, "Undefined escaping scheme (%d) in CPLEscapeString()", nScheme ); } pszShortOutput = CPLStrdup( pszOutput ); CPLFree( pszOutput ); return pszShortOutput;}/************************************************************************//* CPLUnescapeString() *//************************************************************************//** * 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. * * @param pszInput the input string. This is a zero terminated string. * @param pnLength location to return the length of the unescaped string, * which may in some cases include embedded '\0' characters. * @param nScheme the escaped scheme to undo (see CPLEscapeString() for a * list). * * @return a copy of the unescaped string that should be freed by the * application using CPLFree() when no longer needed. */char *CPLUnescapeString( const char *pszInput, int *pnLength, int nScheme ){ char *pszOutput; int iOut=0, iIn; pszOutput = (char *) CPLMalloc(strlen(pszInput)+1); pszOutput[0] = '\0'; if( nScheme == CPLES_XML ) { for( iIn = 0; pszInput[iIn] != '\0'; iIn++ ) { if( EQUALN(pszInput+iIn,"<",4) ) { pszOutput[iOut++] = '<'; iIn += 3; } else if( EQUALN(pszInput+iIn,">",4) ) { pszOutput[iOut++] = '>'; iIn += 3; } else if( EQUALN(pszInput+iIn,"&",5) ) { pszOutput[iOut++] = '&'; iIn += 4; } else if( EQUALN(pszInput+iIn,""",6) ) { pszOutput[iOut++] = '"'; iIn += 5; } else { pszOutput[iOut++] = pszInput[iIn]; } } } else if( nScheme == CPLES_URL ) { for( iIn = 0; pszInput[iIn] != '\0'; iIn++ ) { if( pszInput[iIn] == '%' && pszInput[iIn+1] != '\0' && pszInput[iIn+2] != '\0' ) { int nHexChar = 0; if( pszInput[iIn+1] >= 'A' && pszInput[iIn+1] <= 'F' ) nHexChar += 16 * (pszInput[iIn+1] - 'A' + 10); else if( pszInput[iIn+1] >= 'a' && pszInput[iIn+1] <= 'f' ) nHexChar += 16 * (pszInput[iIn+1] - 'a' + 10); else if( pszInput[iIn+1] >= '0' && pszInput[iIn+1] <= '9' ) nHexChar += 16 * (pszInput[iIn+1] - '0'); else CPLDebug( "CPL", "Error unescaping CPLES_URL text, percent not " "followed by two hex digits." ); if( pszInput[iIn+2] >= 'A' && pszInput[iIn+2] <= 'F' ) nHexChar += pszInput[iIn+2] - 'A' + 10; else if( pszInput[iIn+2] >= 'a' && pszInput[iIn+2] <= 'f' ) nHexChar += pszInput[iIn+2] - 'a' + 10; else if( pszInput[iIn+2] >= '0' && pszInput[iIn+2] <= '9' ) nHexChar += pszInput[iIn+2] - '0'; else CPLDebug( "CPL", "Error unescaping CPLES_URL text, percent not " "followed by two hex digits." ); pszOutput[iOut++] = (char) nHexChar; iIn += 2; } else if( pszInput[iIn] == '+' ) { pszOutput[iOut++] = ' '; } else { pszOutput[iOut++] = pszInput[iIn]; } } } else /* if( nScheme == CPLES_BackslashQuoteable ) */ { for( iIn = 0; pszInput[iIn] != '\0'; iIn++ ) { if( pszInput[iIn] == '\\' ) { iIn++; if( pszInput[iIn] == 'n' ) pszOutput[iOut++] = '\n'; else if( pszInput[iIn] == '0' ) pszOutput[iOut++] = '\0'; else pszOutput[iOut++] = pszInput[iIn]; } else { pszOutput[iOut++] = pszInput[iIn]; } } } pszOutput[iOut] = '\0'; if( pnLength != NULL ) *pnLength = iOut; return pszOutput;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -