⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpl_conv.cpp

📁 用于读取TAB、MIF、SHP文件的类
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        }        if( bBreak )            break;/* -------------------------------------------------------------------- *//*      If there is a remaining character and it is not a newline       *//*      consume it.  If it is a newline, but we are clearly at the      *//*      end of the file then consume it.                                *//* -------------------------------------------------------------------- */        if( nChunkBytesConsumed == nChunkBytesRead-1             && nChunkBytesRead < nChunkSize )        {            if( szChunk[nChunkBytesConsumed] == 10                || szChunk[nChunkBytesConsumed] == 13 )            {                nChunkBytesConsumed++;                break;            }            pszRLBuffer[nBufLength++] = szChunk[nChunkBytesConsumed++];            break;        }    }/* -------------------------------------------------------------------- *//*      If we have left over bytes after breaking out, seek back to     *//*      ensure they remain to be read next time.                        *//* -------------------------------------------------------------------- */    if( nChunkBytesConsumed < nChunkBytesRead )    {        size_t nBytesToPush = nChunkBytesRead - nChunkBytesConsumed;                VSIFSeekL( fp, VSIFTellL( fp ) - nBytesToPush, SEEK_SET );    }    pszRLBuffer[nBufLength] = '\0';    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( const 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( const char *pszString, int nMaxLength ){    long    iValue;    char    *pszValue = (char *)CPLMalloc( nMaxLength + 1);/* -------------------------------------------------------------------- *//*      Compute string into local buffer, and terminate it.             *//* -------------------------------------------------------------------- */    strncpy( pszValue, pszString, nMaxLength );    pszValue[nMaxLength] = '\0';/* -------------------------------------------------------------------- *//*      Use atol() to fetch out the result                              *//* -------------------------------------------------------------------- */    iValue = atol( pszValue );    CPLFree( pszValue );    return iValue;}/************************************************************************//*                           CPLScanUIntBig()                           *//************************************************************************//** * Extract big integer from string. * * Scan up to a maximum number of characters from a string and convert * the result to a GUIntBig.  * * @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 GUIntBig value, converted from its ASCII form. */GUIntBig CPLScanUIntBig( const char *pszString, int nMaxLength ){    GUIntBig    iValue;    char        *pszValue = (char *)CPLMalloc( nMaxLength + 1);/* -------------------------------------------------------------------- *//*      Compute string into local buffer, and terminate it.             *//* -------------------------------------------------------------------- */    strncpy( pszValue, pszString, nMaxLength );    pszValue[nMaxLength] = '\0';/* -------------------------------------------------------------------- *//*      Fetch out the result                                            *//* -------------------------------------------------------------------- */#if defined(WIN32) && defined(_MSC_VER)    iValue = (GUIntBig)_atoi64( pszValue );# elif HAVE_ATOLL    iValue = atoll( pszValue );#else    iValue = atol( pszValue );#endif    CPLFree( pszValue );    return iValue;}/************************************************************************//*                           CPLScanPointer()                           *//************************************************************************//** * Extract pointer from string. * * Scan up to a maximum number of characters from a string and convert * the result to a GUIntBig.  * * @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 pointer value, converted from its ASCII form. */void *CPLScanPointer( const char *pszString, int nMaxLength ){    void  *pResult;    char  szTemp[128];/* -------------------------------------------------------------------- *//*      Compute string into local buffer, and terminate it.             *//* -------------------------------------------------------------------- */    if( nMaxLength > (int) sizeof(szTemp)-1 )        nMaxLength = sizeof(szTemp)-1;    strncpy( szTemp, pszString, nMaxLength );    szTemp[nMaxLength] = '\0';/* -------------------------------------------------------------------- *//*      On MSVC we have to scanf pointer values without the 0x          *//*      prefix.                                                         *//* -------------------------------------------------------------------- */    if( EQUALN(szTemp,"0x",2) )    {        pResult = NULL;#if defined(WIN32) && defined(_MSC_VER)        sscanf( szTemp+2, "%p", &pResult );#else        sscanf( szTemp, "%p", &pResult );#endif    }        else    {        pResult = (void *) CPLScanUIntBig( szTemp, nMaxLength );    }    return pResult;}/************************************************************************//*                             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( const char *pszString, int nMaxLength, char *pszLocale ){    int     i;    double  dfValue;    char    *pszValue = (char *)CPLMalloc( nMaxLength + 1);/* -------------------------------------------------------------------- *//*      Compute string into local buffer, and terminate it.             *//* -------------------------------------------------------------------- */    strncpy( pszValue, pszString, nMaxLength );    pszValue[nMaxLength] = '\0';/* -------------------------------------------------------------------- *//*      Make a pass through converting 'D's to 'E's.                    *//* -------------------------------------------------------------------- */    for( i = 0; i < nMaxLength; i++ )        if ( pszValue[i] == 'd' || pszValue[i] == 'D' )            pszValue[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( pszValue );#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)    // Restore stored locale back    if ( pszCurLocale )        setlocale(LC_ALL, pszCurLocale );#endif    CPLFree( pszValue );    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 Number of characters printed. */int CPLPrintString( char *pszDest, const char *pszSrc, int nMaxLen ){    char    *pszTemp = pszDest;    int     nChars = 0;    if ( !pszDest )        return 0;    if ( !pszSrc )    {        *pszDest = '\0';        return 1;    }    while ( nChars < nMaxLen && *pszSrc )    {        *pszTemp++ = *pszSrc++;        nChars++;    }    return nChars;}/************************************************************************//*                         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 Number of characters printed. */int CPLPrintStringFill( char *pszDest, const char *pszSrc, int nMaxLen ){    char    *pszTemp = pszDest;    if ( !pszDest )        return 0;    if ( !pszSrc )    {        memset( pszDest, ' ', nMaxLen );        return nMaxLen;    }    while ( nMaxLen && *pszSrc )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -