?? cpl_conv.cpp
字號:
#endif
return CPLPrintString( pszBuffer, szTemp, nMaxLen );
}
/************************************************************************/
/* CPLPrintPointer() */
/************************************************************************/
/**
* Print pointer 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 pValue Pointer to ASCII encode.
*
* @param nMaxLen Maximum length of the resulting string. If string length
* is greater than nMaxLen, it will be truncated.
*
* @return Number of characters printed.
*/
int CPLPrintPointer( char *pszBuffer, void *pValue, int nMaxLen )
{
char szTemp[64];
if ( !pszBuffer )
return 0;
if ( nMaxLen >= 64 )
nMaxLen = 63;
sprintf( szTemp, "%p", pValue );
// On windows, and possibly some other platforms the sprintf("%p")
// does not prefix things with 0x so it is hard to know later if the
// value is hex encoded. Fix this up here.
if( !EQUALN(szTemp,"0x",2) )
sprintf( szTemp, "0x%p", pValue );
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 Number of characters printed.
*/
int CPLPrintDouble( char *pszBuffer, const char *pszFormat,
double dfValue, char *pszLocale )
{
#define DOUBLE_BUFFER_SIZE 64
char szTemp[DOUBLE_BUFFER_SIZE];
int i;
if ( !pszBuffer )
return 0;
#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
#if defined(HAVE_SNPRINTF)
snprintf( szTemp, DOUBLE_BUFFER_SIZE, pszFormat, dfValue );
#else
sprintf( szTemp, pszFormat, dfValue );
#endif
szTemp[DOUBLE_BUFFER_SIZE - 1] = '\0';
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 );
#undef DOUBLE_BUFFER_SIZE
}
/************************************************************************/
/* 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 Number of characters printed.
*/
#ifndef WIN32CE /* XXX - mloskot - strftime is not available yet. */
int CPLPrintTime( char *pszBuffer, int nMaxLen, const char *pszFormat,
const struct tm *poBrokenTime, char *pszLocale )
{
char *pszTemp = (char *)CPLMalloc( (nMaxLen + 1) * sizeof(char) );
int nChars;
#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
if ( !strftime( pszTemp, nMaxLen + 1, pszFormat, poBrokenTime ) )
memset( pszTemp, 0, nMaxLen + 1);
#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
// Restore stored locale back
if ( pszCurLocale )
setlocale( LC_ALL, pszCurLocale );
#endif
nChars = CPLPrintString( pszBuffer, pszTemp, nMaxLen );
CPLFree( pszTemp );
return nChars;
}
#endif
/************************************************************************/
/* CPLVerifyConfiguration() */
/************************************************************************/
void CPLVerifyConfiguration()
{
/* -------------------------------------------------------------------- */
/* Verify data types. */
/* -------------------------------------------------------------------- */
CPLAssert( sizeof(GInt32) == 4 );
CPLAssert( sizeof(GInt16) == 2 );
CPLAssert( sizeof(GByte) == 1 );
if( sizeof(GInt32) != 4 )
CPLError( CE_Fatal, CPLE_AppDefined,
"sizeof(GInt32) == %d ... yow!\n",
sizeof(GInt32) );
/* -------------------------------------------------------------------- */
/* Verify byte order */
/* -------------------------------------------------------------------- */
GInt32 nTest;
nTest = 1;
#ifdef CPL_LSB
if( ((GByte *) &nTest)[0] != 1 )
#endif
#ifdef CPL_MSB
if( ((GByte *) &nTest)[3] != 1 )
#endif
CPLError( CE_Fatal, CPLE_AppDefined,
"CPLVerifyConfiguration(): byte order set wrong.\n" );
}
/************************************************************************/
/* CPLGetConfigOption() */
/************************************************************************/
const char * CPL_STDCALL
CPLGetConfigOption( const char *pszKey, const char *pszDefault )
{
const char *pszResult = NULL;
{
CPLMutexHolderD( &hConfigMutex );
pszResult = CSLFetchNameValue( (char **) papszConfigOptions, pszKey );
}
#if !defined(WIN32CE)
if( pszResult == NULL )
pszResult = getenv( pszKey );
#endif
if( pszResult == NULL )
return pszDefault;
else
return pszResult;
}
/************************************************************************/
/* CPLSetConfigOption() */
/************************************************************************/
void CPL_STDCALL
CPLSetConfigOption( const char *pszKey, const char *pszValue )
{
CPLMutexHolderD( &hConfigMutex );
papszConfigOptions = (volatile char **)
CSLSetNameValue( (char **) papszConfigOptions, pszKey, pszValue );
}
/************************************************************************/
/* CPLFreeConfig() */
/************************************************************************/
void CPL_STDCALL CPLFreeConfig()
{
CPLMutexHolderD( &hConfigMutex );
CSLDestroy( (char **) papszConfigOptions);
papszConfigOptions = NULL;
}
/************************************************************************/
/* CPLStat() */
/* */
/* Same as VSIStat() except it works on "C:" as if it were */
/* "C:\". */
/************************************************************************/
int CPLStat( const char *pszPath, VSIStatBuf *psStatBuf )
{
if( strlen(pszPath) == 2 && pszPath[1] == ':' )
{
char szAltPath[10];
strncpy( szAltPath, pszPath, 10 );
strcat( szAltPath, "\\" );
return VSIStat( szAltPath, psStatBuf );
}
else
return VSIStat( pszPath, psStatBuf );
}
/************************************************************************/
/* proj_strtod() */
/************************************************************************/
static double
proj_strtod(char *nptr, char **endptr)
{
char c, *cp = nptr;
double result;
/*
* Scan for characters which cause problems with VC++ strtod()
*/
while ((c = *cp) != '\0') {
if (c == 'd' || c == 'D') {
/*
* Found one, so NUL it out, call strtod(),
* then restore it and return
*/
*cp = '\0';
result = strtod(nptr, endptr);
*cp = c;
return result;
}
++cp;
}
/* no offending characters, just handle normally */
return strtod(nptr, endptr);
}
/************************************************************************/
/* CPLDMSToDec() */
/************************************************************************/
static const char*sym = "NnEeSsWw";
static const double vm[] = { 1.0, 0.0166666666667, 0.00027777778 };
double CPLDMSToDec( const char *is )
{
int sign, n, nl;
char *p, *s, work[64];
double v, tv;
/* copy sting into work space */
while (isspace(sign = *is)) ++is;
for (n = sizeof(work), s = work, p = (char *)is; isgraph(*p) && --n ; )
*s++ = *p++;
*s = '\0';
/* it is possible that a really odd input (like lots of leading
zeros) could be truncated in copying into work. But ... */
sign = *(s = work);
if (sign == '+' || sign == '-') s++;
else sign = '+';
for (v = 0., nl = 0 ; nl < 3 ; nl = n + 1 ) {
if (!(isdigit(*s) || *s == '.')) break;
if ((tv = proj_strtod(s, &s)) == HUGE_VAL)
return tv;
switch (*s) {
case 'D': case 'd':
n = 0; break;
case '\'':
n = 1; break;
case '"':
n = 2; break;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -