📄 cplconv.cpp
字号:
pszBuffer = (char *)CPLMalloc( nMaxLength + 1 ); if ( !pszBuffer ) return NULL; strncpy( pszBuffer, pszString, nMaxLength ); pszBuffer[nMaxLength] = '\0'; if ( bTrimSpaces ) { size_t i = strlen( pszBuffer ); while ( i-- > 0 && isspace(pszBuffer[i]) ) pszBuffer[i] = '\0'; } if ( bNormalize ) { size_t i = strlen( pszBuffer ); while ( i-- > 0 ) { if ( pszBuffer[i] == ':' ) pszBuffer[i] = '_'; } } return pszBuffer;}/************************************************************************//* CPLScanLong() *//************************************************************************//** * Scan up to a maximum number of characters from a string and convert * the result to a long. * * @param pszString String containing characters to be scanned. It may be * terminated with a null character. * * @param nMaxLength The maximum number of character to consider as part * of the number. Less characters will be considered if a null character * is encountered. * * @return Long value, converted from its ASCII form. */long CPLScanLong( char *pszString, int nMaxLength ){ char szTemp[32];/* -------------------------------------------------------------------- *//* Compute string into local buffer, and terminate it. *//* -------------------------------------------------------------------- */ strncpy( szTemp, pszString, nMaxLength ); szTemp[nMaxLength] = '\0';/* -------------------------------------------------------------------- *//* Use atol() to fetch out the result *//* -------------------------------------------------------------------- */ return atol( szTemp );}/************************************************************************//* CPLScanDouble() *//************************************************************************//** * Scan up to a maximum number of characters from a string and convert * the result to a double. * * @param pszString String containing characters to be scanned. It may be * terminated with a null character. * * @param nMaxLength The maximum number of character to consider as part * of the number. Less characters will be considered if a null character * is encountered. * * @param pszLocale Pointer to a character string containing locale name * ("C", "POSIX", "us_US", "ru_RU.KOI8-R" etc.). If NULL, we will not * manipulate with locale settings and current process locale will be used for * printing. Wee need this setting because in different locales decimal * delimiter represented with the different characters. With the pszLocale * option we can control what exact locale will be used for scanning a numeric * value from the string (in most cases it should be C/POSIX). * * @return Double value, converted from its ASCII form. */double CPLScanDouble( char *pszString, int nMaxLength, char *pszLocale ){ char szTemp[64]; int i; double dfValue;/* -------------------------------------------------------------------- *//* Compute string into local buffer, and terminate it. *//* -------------------------------------------------------------------- */ strncpy( szTemp, pszString, nMaxLength ); szTemp[nMaxLength] = '\0';/* -------------------------------------------------------------------- *//* Make a pass through converting 'D's to 'E's. *//* -------------------------------------------------------------------- */ for( i = 0; i < nMaxLength; i++ ) if ( szTemp[i] == 'd' || szTemp[i] == 'D' ) szTemp[i] = 'E';/* -------------------------------------------------------------------- *//* Use atof() to fetch out the result *//* -------------------------------------------------------------------- */#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE) char *pszCurLocale = NULL; if ( pszLocale || EQUAL( pszLocale, "" ) ) { // Save the current locale pszCurLocale = setlocale(LC_ALL, NULL ); // Set locale to the specified value setlocale(LC_ALL, pszLocale ); }#endif dfValue = atof( szTemp );#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE) // Restore stored locale back if ( pszCurLocale ) setlocale(LC_ALL, pszCurLocale );#endif return dfValue;}/************************************************************************//* CPLPrintString() *//************************************************************************//** * Copy the string pointed to by pszSrc, _not_ including the terminating * `\0' character, to the array pointed to by pszDest. * * @param pszDest Pointer to the destination string buffer. Should be * large enough to hold the resulting string. * * @param pszDest Pointer to the source buffer. * * @param nMaxLen Maximum length of the resulting string. If string length * is greater than nMaxLen, it will be truncated. * * @return Pointer to the destination string pszDest. */char *CPLPrintString( char *pszDest, const char *pszSrc, int nMaxLen ){ char *pszTemp = pszDest; if ( !pszDest ) return NULL; if ( !pszSrc ) { memset( pszDest, '\0', nMaxLen ); return pszDest; } while ( nMaxLen && *pszSrc ) { *pszTemp++ = *pszSrc++; nMaxLen--; } return pszDest;}/************************************************************************//* CPLPrintStringFill() *//************************************************************************//** * Copy the string pointed to by pszSrc, _not_ including the terminating * `\0' character, to the array pointed to by pszDest. Remainder of the * destination string will be filled with space characters. This is only * difference from the PrintString(). * * @param pszDest Pointer to the destination string buffer. Should be * large enough to hold the resulting string. * * @param pszDest Pointer to the source buffer. * * @param nMaxLen Maximum length of the resulting string. If string length * is greater than nMaxLen, it will be truncated. * * @return Pointer to the destination string pszDest. */char *CPLPrintStringFill( char *pszDest, const char *pszSrc, int nMaxLen ){ char *pszTemp = pszDest; if ( !pszDest ) return NULL; if ( !pszSrc ) { memset( pszDest, '\0', nMaxLen ); return pszDest; } while ( nMaxLen && *pszSrc ) { *pszTemp++ = *pszSrc++; nMaxLen--; } if ( nMaxLen ) memset( pszTemp, ' ', nMaxLen ); return pszDest;}/************************************************************************//* CPLPrintInt32() *//************************************************************************//** * Print GInt32 value into specified string buffer. This string will not * be NULL-terminated. * * @param Pointer to the destination string buffer. Should be * large enough to hold the resulting string. Note, that the string will * not be NULL-terminated, so user should do this himself, if needed. * * @param iValue Numerical value to print. * * @param nMaxLen Maximum length of the resulting string. If string length * is greater than nMaxLen, it will be truncated. * * @return Pointer to the destination string buffer. */char *CPLPrintInt32( char *pszBuffer, GInt32 iValue, int nMaxLen ){ char szTemp[64]; if ( !pszBuffer ) return NULL; if ( nMaxLen >= 64 ) nMaxLen = 63;#if UINT_MAX == 65535 sprintf( szTemp, "%*ld", nMaxLen, iValue );#else sprintf( szTemp, "%*d", nMaxLen, iValue );#endif return CPLPrintString( pszBuffer, szTemp, nMaxLen );}/************************************************************************//* CPLPrintUIntBig() *//************************************************************************//** * Print GUIntBig value into specified string buffer. This string will not * be NULL-terminated. * * @param Pointer to the destination string buffer. Should be * large enough to hold the resulting string. Note, that the string will * not be NULL-terminated, so user should do this himself, if needed. * * @param iValue Numerical value to print. * * @param nMaxLen Maximum length of the resulting string. If string length * is greater than nMaxLen, it will be truncated. * * @return Pointer to the destination string buffer. */char *CPLPrintUIntBig( char *pszBuffer, GUIntBig iValue, int nMaxLen ){ char szTemp[64]; if ( !pszBuffer ) return NULL; if ( nMaxLen >= 64 ) nMaxLen = 63;#if defined(WIN32) && defined(_MSC_VER) sprintf( szTemp, "%*I64d", nMaxLen, iValue );# elif HAVE_LONG_LONG sprintf( szTemp, "%*Ld", nMaxLen, iValue );#else sprintf( szTemp, "%*ld", nMaxLen, iValue );#endif return CPLPrintString( pszBuffer, szTemp, nMaxLen );}/************************************************************************//* CPLPrintDouble() *//************************************************************************//** * Print double value into specified string buffer. Exponential character * flag 'E' (or 'e') will be replaced with 'D', as in Fortran. Resulting * string will not to be NULL-terminated. * * @param Pointer to the destination string buffer. Should be * large enough to hold the resulting string. Note, that the string will * not be NULL-terminated, so user should do this himself, if needed. * * @param Format specifier (for example, "%16.9E"). * * @param dfValue Numerical value to print. * * @param pszLocale Pointer to a character string containing locale name * ("C", "POSIX", "us_US", "ru_RU.KOI8-R" etc.). If NULL we will not * manipulate with locale settings and current process locale will be used for * printing. With the pszLocale option we can control what exact locale * will be used for printing a numeric value to the string (in most cases * it should be C/POSIX). * * @return Pointer to the destination string buffer. */char *CPLPrintDouble( char *pszBuffer, const char *pszFormat, double dfValue, char *pszLocale ){ char szTemp[64]; int i; if ( !pszBuffer ) return NULL;#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE) char *pszCurLocale = NULL; if ( pszLocale || EQUAL( pszLocale, "" ) ) { // Save the current locale pszCurLocale = setlocale(LC_ALL, NULL ); // Set locale to the specified value setlocale(LC_ALL, pszLocale ); }#endif sprintf( szTemp, pszFormat, dfValue ); for( i = 0; szTemp[i] != '\0'; i++ ) { if( szTemp[i] == 'E' || szTemp[i] == 'e' ) szTemp[i] = 'D'; }#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE) // Restore stored locale back if ( pszCurLocale ) setlocale(LC_ALL, pszCurLocale );#endif return CPLPrintString( pszBuffer, szTemp, 64 );}/************************************************************************//* CPLPrintTime() *//************************************************************************//** * Print specified time value accordingly to the format options and * specified locale name. This function does following: * * - if locale parameter is not NULL, the current locale setting will be * stored and replaced with the specified one; * - format time value with the strftime(3) function; * - restore back current locale, if was saved. * * @param pszBuffer Pointer to the destination string buffer. Should be * large enough to hold the resulting string. Note, that the string will * not be NULL-terminated, so user should do this himself, if needed. * * @param nMaxLen Maximum length of the resulting string. If string length is * greater than nMaxLen, it will be truncated. * * @param pszFormat Controls the output format. Options are the same as * for strftime(3) function. * * @param poBrokenTime Pointer to the broken-down time structure. May be * requested with the VSIGMTime() and VSILocalTime() functions. * * @param pszLocale Pointer to a character string containing locale name * ("C", "POSIX", "us_US", "ru_RU.KOI8-R" etc.). If NULL we will not * manipulate with locale settings and current process locale will be used for * printing. Be aware that it may be unsuitable to use current locale for * printing time, because all names will be printed in your native language, * as well as time format settings also may be ajusted differently from the * C/POSIX defaults. To solve these problems this option was introdiced. * * @return Pointer to the destination not NULL terminated buffer. */char *CPLPrintTime( char *pszBuffer, int nMaxLen, const char *pszFormat, const struct tm *poBrokenTime, char *pszLocale )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -