Ejemplo n.º 1
0
__LLBC_NS_BEGIN

void LLBC_SplitString(const LLBC_String &str,
                      const LLBC_String &separator,
                      std::vector<LLBC_String> &destStrList,
                      bool justSplitFirst,
                      char escapeChar)
{
    if (UNLIKELY(str.empty()))
    {
        return;
    }

    if (UNLIKELY(separator.empty()))
    {
        destStrList.push_back(str);
    }

    LLBC_String::size_type curPos = 0;
    LLBC_String::size_type prevPos = 0;

    LLBC_String strInternal = str;
    while ((curPos = strInternal.find(separator, curPos)) != LLBC_String::npos)
    {
        if (curPos != 0 && strInternal[curPos - 1] == escapeChar)
        {
            strInternal.erase(-- curPos, 1);
            curPos += separator.size();
            continue;
        }

        LLBC_String temp = strInternal.substr(prevPos, curPos - prevPos);
        destStrList.push_back(temp);

        if (justSplitFirst)
        {
            destStrList.push_back(strInternal.substr(curPos + separator.size()));
            return;
        }

        curPos += separator.size();
        prevPos = curPos;
    }

    LLBC_String temp = strInternal.substr(prevPos);
    if (!temp.empty())
    {   
        destStrList.push_back(temp);
    }
}
Ejemplo n.º 2
0
LLBC_String LLBC_BaseName(const LLBC_String &path, bool incExtension)
{
    if (UNLIKELY(path.empty()))
    {
        return LLBC_String();
    }

    LLBC_String baseName;
#if LLBC_TARGET_PLATFORM_NON_WIN32
    baseName = ::basename(const_cast<char *>(path.c_str()));
#else
    LLBC_String::size_type slashPos = path.rfind(LLBC_SLASH_A);
    LLBC_String::size_type backlashPos = path.rfind(LLBC_BACKLASH_A);

    if (slashPos == LLBC_String::npos)
    {
        if (backlashPos == LLBC_String::npos)
        {
            baseName = path;
        }
        else
        {
            baseName = path.substr(backlashPos + 1);
        }
    }
    else
    {
        if (backlashPos == LLBC_String::npos)
        {
            baseName = path.substr(slashPos + 1);
        }
        else
        {
            baseName = path.substr(MAX(slashPos, backlashPos) + 1);
        }
    }
#endif

    if (!incExtension)
    {
        LLBC_String::size_type dotPos = baseName.rfind('.');
        if (dotPos != LLBC_String::npos && dotPos != 0)
        {
            baseName.erase(dotPos);
        }
    }

    return baseName;
}
Ejemplo n.º 3
0
LLBC_String LLBC_Directory::TempDir()
{
#if LLBC_TARGET_PLATFORM_NON_WIN32
    return "/tmp";
#else // Win32
    DWORD bufLen = 0;
    bufLen = ::GetTempPathA(0, NULL);
    bufLen += 1;

    LPSTR buf = reinterpret_cast<LPSTR>(::malloc(sizeof(CHAR) * bufLen));
    if (::GetTempPathA(bufLen, buf) == 0)
    {
        LLBC_SetLastError(LLBC_ERROR_OSAPI);
        ::free(buf);
        return "";
    }

    LLBC_String path = buf;
    ::free(buf);

    if (path[path.length() - 1] == LLBC_BACKLASH_A)
        return path.substr(0, path.length() - 1);
    
    return path;
#endif // Non-Win32
}
Ejemplo n.º 4
0
LLBC_String LLBC_GetTemporaryDirectory(bool appendSlash)
{
 #if LLBC_TARGET_PLATFORM_NON_WIN32
    if (appendSlash)
    {
        return "/tmp/";
    }
    else
    {
        return "/tmp";
    }
 #else
    DWORD bufLen = 0;
    bufLen = ::GetTempPathA(0, NULL);
    bufLen += 1;

    LPSTR buf = reinterpret_cast<LPSTR>(::malloc(sizeof(CHAR) * bufLen));
    if (::GetTempPathA(bufLen, buf) == 0)
    {
        LLBC_SetLastError(LLBC_ERROR_OSAPI);
        ::free(buf);
        return "";
    }

    LLBC_String path = buf;
    ::free(buf);
    
    if (!appendSlash)
    {
        path = path.substr(0, path.size() - 1);
    }
    
    return path;
 #endif
}
Ejemplo n.º 5
0
LLBC_String LLBC_DirName(const LLBC_String &path)
{
    if (UNLIKELY(path.empty()))
    {
        return LLBC_String();
    }

#if LLBC_TARGET_PLATFORM_NON_WIN32
    char *buf = reinterpret_cast<char *>(::malloc(path.size() + 1));
    ::memcpy(buf,  path.data(), path.size());
    buf[path.size()] = '\0';

    ::dirname(buf);

    LLBC_String dirName = buf;
    ::free(buf);

    return dirName;
#else
    if (path[path.length() - 1] == ':')
    {
        return path;
    }

    LLBC_String::size_type slashPos = path.rfind(LLBC_SLASH_A);
    LLBC_String::size_type backlashPos = path.rfind(LLBC_BACKLASH_A);

    if (slashPos == LLBC_String::npos)
    {
        if (backlashPos == LLBC_String::npos)
        {
            return LLBC_String();
        }

        return path.substr(0, backlashPos);
    }
    else
    {
        if (backlashPos == LLBC_String::npos)
        {
            return path.substr(0, slashPos);
        }
    }

    return path.substr(0, MAX(slashPos, backlashPos));
#endif
}
Ejemplo n.º 6
0
LLBC_String LLBC_TrimRight(const LLBC_String &str, char target)
{
    if (UNLIKELY(str.empty()))
    {
        return LLBC_String();
    }

    const LLBC_String::size_type length = str.size();
    register LLBC_String::size_type rightPos = length - 1;
    for (; str[rightPos] == target && rightPos != 0; rightPos --);

    return str.substr(0, rightPos + 1);
}
Ejemplo n.º 7
0
LLBC_String LLBC_ExtensionName(const LLBC_String &path)
{
    LLBC_String basename = LLBC_BaseName(path);
    if (UNLIKELY(basename.empty()))
    {
        return LLBC_String();
    }

    LLBC_String::size_type pos = basename.rfind(".");
    if (pos == LLBC_String::npos)
    {
        return LLBC_String();
    }

    return basename.substr(pos + 1);
}
Ejemplo n.º 8
0
LLBC_String LLBC_TrimLeft(const LLBC_String &str, char target)
{
    if (UNLIKELY(str.empty()))
    {
        return LLBC_String();
    }

    const LLBC_String::size_type length = str.size();
    register LLBC_String::size_type leftPos = 0;
    for (; str[leftPos] == target && leftPos < length; leftPos ++);

    if (leftPos >= length)
    {
        return LLBC_String();
    }

    return str.substr(leftPos, LLBC_String::npos);
}
Ejemplo n.º 9
0
void *LLBC_Str2Ptr(const char *str)
{
    if (UNLIKELY(!str))
    {
        LLBC_SetLastError(LLBC_ERROR_ARG);
        return NULL;
    }

    LLBC_SetLastError(LLBC_ERROR_SUCCESS);

    bool hexFormat = false;
    LLBC_String lowerStr = LLBC_ToLower(str);
    lowerStr = LLBC_Trim(lowerStr);
    if (lowerStr.size() >= 2 && (lowerStr[0] == '0' && lowerStr[1] == 'x'))
    {
        hexFormat = true;
        lowerStr = lowerStr.substr(2);
    }

    if (lowerStr.empty())
    {
        return NULL;
    }

    for (LLBC_String::size_type i = 0; i < lowerStr.size(); i ++)
    {
        if (hexFormat)
        {
            if (!((lowerStr[i] >= '0' && lowerStr[i] <= '9') ||
                (lowerStr[i] >= 'a' && lowerStr[i] <= 'f')))
            {
                LLBC_SetLastError(LLBC_ERROR_ARG);
                return NULL;
            }
        }
        else
        {
            if (lowerStr[i] < '0' || lowerStr[i] > '9')
            {
                LLBC_SetLastError(LLBC_ERROR_ARG);
                return NULL;
            }
        }
    }

    ulong ptrVal = 0;
    ulong baseVal = hexFormat ? 16 : 10;
    for (LLBC_String::size_type i = 0; i < lowerStr.size(); i ++)
    {
        ptrVal *= baseVal;
        if (lowerStr[i] >= '0' && lowerStr[i] <= '9')
        {
            ptrVal += (uint8)(lowerStr[i] - '0');
        }
        else
        {
            ptrVal += (uint8)(lowerStr[i] - 'a' + (char)10);
        }
    }

    return reinterpret_cast<void *>(ptrVal);
}