📄 cpl_conv.cpp
字号:
{ static char *pszRLBuffer = NULL; static int nRLBufferSize = 0; int nReadSoFar = 0;/* -------------------------------------------------------------------- *//* Cleanup case. *//* -------------------------------------------------------------------- */ if( fp == NULL ) { CPLFree( pszRLBuffer ); pszRLBuffer = NULL; nRLBufferSize = 0; return NULL; }/* -------------------------------------------------------------------- *//* Loop reading chunks of the line till we get to the end of *//* the line. *//* -------------------------------------------------------------------- */ do {/* -------------------------------------------------------------------- *//* Grow the working buffer if we have it nearly full. Fail out *//* of read line if we can't reallocate it big enough (for *//* instance for a _very large_ file with no newlines). *//* -------------------------------------------------------------------- */ if( nRLBufferSize-nReadSoFar < 128 ) { nRLBufferSize = nRLBufferSize*2 + 128; pszRLBuffer = (char *) VSIRealloc(pszRLBuffer, nRLBufferSize); if( pszRLBuffer == NULL ) { nRLBufferSize = 0; return NULL; } }/* -------------------------------------------------------------------- *//* Do the actual read. *//* -------------------------------------------------------------------- */ if( CPLFGets( pszRLBuffer+nReadSoFar, nRLBufferSize-nReadSoFar, fp ) == NULL ) { CPLFree( pszRLBuffer ); pszRLBuffer = NULL; nRLBufferSize = 0; return NULL; } nReadSoFar = strlen(pszRLBuffer); } while( nReadSoFar == nRLBufferSize - 1 && pszRLBuffer[nRLBufferSize-2] != 13 && pszRLBuffer[nRLBufferSize-2] != 10 ); return( pszRLBuffer );}/************************************************************************//* CPLScanString() *//************************************************************************//** * Scan up to a maximum number of characters from a given string, * allocate a buffer for a new string and fill it with scanned characters. * * @param pszString String containing characters to be scanned. It may be * terminated with a null character. * * @param nMaxLength The maximum number of character to read. Less * characters will be read if a null character is encountered. * * @param bTrimSpaces If TRUE, trim ending spaces from the input string. * Character considered as empty using isspace(3) function. * * @param bNormalize If TRUE, replace ':' symbol with the '_'. It is needed if * resulting string will be used in CPL dictionaries. * * @return Pointer to the resulting string buffer. Caller responsible to free * this buffer with CPLFree(). */char *CPLScanString( char *pszString, int nMaxLength, int bTrimSpaces, int bNormalize ){ char *pszBuffer; if ( !pszString ) return NULL; if ( !nMaxLength ) return CPLStrdup( "" ); 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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -