📄 cpl_string.cpp
字号:
* 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. * * @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] == '\n' ) { pszOutput[iOut++] = '\\'; pszOutput[iOut++] = 'n'; } else if( pszInput[iIn] == '"' ) { pszOutput[iOut++] = '\\'; pszOutput[iOut++] = '\"'; } 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 if( nScheme == CPLES_SQL ) { int iOut = 0, iIn; for( iIn = 0; iIn < nLength; iIn++ ) { if( pszInput[iIn] == '\'' ) { pszOutput[iOut++] = '\''; pszOutput[iOut++] = '\''; } else pszOutput[iOut++] = pszInput[iIn]; } pszOutput[iOut] = '\0'; } else if( nScheme == CPLES_CSV ) { if( strchr( pszInput, '\"' ) == NULL && strchr( pszInput, ',') == NULL && strchr( pszInput, 10) == NULL && strchr( pszInput, 13) == NULL ) { strcpy( pszOutput, pszInput ); } else { int iOut = 1, iIn; pszOutput[0] = '\"'; for( iIn = 0; iIn < nLength; iIn++ ) { if( pszInput[iIn] == '\"' ) { pszOutput[iOut++] = '\"'; pszOutput[iOut++] = '\"'; } else if( pszInput[iIn] == 13 ) /* drop DOS LF's in strings. */; else pszOutput[iOut++] = pszInput[iIn]; } pszOutput[iOut++] = '\"'; 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_SQL ) { for( iIn = 0; pszInput[iIn] != '\0'; iIn++ ) { if( pszInput[iIn] == '\'' && pszInput[iIn+1] == '\'' ) { iIn++; pszOutput[iOut++] = pszInput[iIn]; } 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;}/************************************************************************//* CPLBinaryToHex() *//************************************************************************//** * Binary to hexadecimal translation. * * @param nBytes number of bytes of binary data in pabyData. * @param pabyData array of data bytes to translate. * * @return hexadecimal translation, zero terminated. Free with CPLFree(). */char *CPLBinaryToHex( int nBytes, const GByte *pabyData ){ char *pszHex = (char *) CPLMalloc(nBytes * 2 + 1 ); int i; static const char achHex[] = "0123456789ABCDEF"; pszHex[nBytes*2] = '\0'; for( i = 0; i < nBytes; i++ ) { int nLow = pabyData[i] & 0x0f; int nHigh = (pabyData[i] & 0xf0) >> 4; pszHex[i*2] = achHex[nHigh]; pszHex[i*2+1] = achHex[nLow]; } return pszHex;}/************************************************************************//* CPLHexToBinary() *//************************************************************************//** * Hexadecimal to binary translation * * @param */GByte *CPLHexToBinary( const char *pszHex, int *pnBytes ){ int iSrc = 0, iDst = 0; size_t nHexLen = strlen(pszHex); GByte *pabyWKB; pabyWKB = (GByte *) CPLMalloc(nHexLen / 2 + 2); while( pszHex[iSrc] != '\0' ) { if( pszHex[iSrc] >= '0' && pszHex[iSrc] <= '9' ) pabyWKB[iDst] = pszHex[iSrc] - '0'; else if( pszHex[iSrc] >= 'A' && pszHex[iSrc] <= 'F' ) pabyWKB[iDst] = pszHex[iSrc] - 'A' + 10; else if( pszHex[iSrc] >= 'a' && pszHex[iSrc] <= 'f' ) pabyWKB[iDst] = pszHex[iSrc] - 'a' + 10; else break; pabyWKB[iDst] *= 16; iSrc++; if( pszHex[iSrc] >= '0' && pszHex[iSrc] <= '9' ) pabyWKB[iDst] += pszHex[iSrc] - '0'; else if( pszHex[iSrc] >= 'A' && pszHex[iSrc] <= 'F' ) pabyWKB[iDst] += pszHex[iSrc] - 'A' + 10; else if( pszHex[iSrc] >= 'a' && pszHex[iSrc] <= 'f' ) pabyWKB[iDst] += pszHex[iSrc] - 'a' + 10; else break; iSrc++; iDst++; } pabyWKB[iDst] = 0; *pnBytes = iDst; return pabyWKB;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -