Ejemplo n.º 1
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.º 2
0
void TestCase_Com_DataType::StringStripTest()
{
    LLBC_PrintLine("Strip test:");
    LLBC_String str = "\t \t Hello World! \t \t";
    LLBC_PrintLine("Before strip, str: %s, len: %ld", str.c_str(), str.length());

    str.strip();
    LLBC_PrintLine("After strip, str: %s, len: %ld", str.c_str(), str.length());
}
Ejemplo n.º 3
0
LLBC_BundleHandle LLBC_CreateBundle(const LLBC_String &path)
{
    LLBC_String realPath = LLBC_GetMainBundlePath();
    if (UNLIKELY(realPath.empty()))
        return LLBC_INVALID_BUNDLE_HANDLE;

    // Main bundle-path + /(\\) + path.
    // Trim right(/(\\)).
    if (!path.empty())
    {
#if LLBC_TARGET_PLATFORM_NON_WIN32
        realPath.append(1, LLBC_SLASH_A);
#else
        realPath.append(1, LLBC_BACKLASH_A);
#endif

        realPath.append(path);

        const LLBC_String::size_type len = realPath.length();
        if (realPath[len - 1] == LLBC_SLASH_A || realPath[len - 1] == LLBC_BACKLASH_A)
            realPath.erase(len - 1, 1);
    }

    // Check path.
    if (!LLBC_DirectoryExist(realPath))
        return LLBC_INVALID_BUNDLE_HANDLE;

    return new LLBC_String(realPath);
}
Ejemplo n.º 4
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.º 5
0
LLBC_String LLBC_GetBundleResPath(LLBC_BundleHandle bundle, const LLBC_String &name, const LLBC_String &ext, const LLBC_String &inDir)
{
    if (UNLIKELY(name.empty()))
    {
        LLBC_SetLastError(LLBC_ERROR_ARG);
        return "";
    }

    // Get bundle path.
    LLBC_String path = LLBC_GetBundlePath(bundle);
    if (UNLIKELY(path.empty()))
        return "";

    // Append intermediate directory.
    if (!inDir.empty())
    {
        if (inDir[0] != LLBC_SLASH_A && inDir[0] != LLBC_BACKLASH_A)
        {
#if LLBC_TARGET_PLATFORM_NON_WIN32
            path.append(1, LLBC_SLASH_A);
#else
            path.append(1, LLBC_BACKLASH_A);
#endif
        }

        path.append(inDir);

        if (inDir.size() > 1)
        {
            const LLBC_String::size_type endPos = inDir.length();
            if (inDir[endPos - 1] != LLBC_SLASH_A && inDir[endPos - 1] != LLBC_BACKLASH_A)
            {
#if LLBC_TARGET_PLATFORM_NON_WIN32
                path.append(1, LLBC_SLASH_A);
#else
                path.append(1, LLBC_BACKLASH_A);
#endif
            }
        }
    }
    else
    {
    // Append slash/backlash.
#if LLBC_TARGET_PLATFORM_NON_WIN32
        path.append(1, LLBC_SLASH_A);
#else
        path.append(1, LLBC_BACKLASH_A);
#endif
    }

    // Append file name.
    path.append(name);

    // Append extension.
    if (!ext.empty())
    {
        if (ext[0] != '.')
            path.append(1, '.');

        path.append(ext);
    }

    if (!LLBC_FileExist(path))
    {
        LLBC_SetLastError(LLBC_ERROR_NOT_FOUND);
        return "";
    }

    return path;
}
Ejemplo n.º 6
0
int LLBC_HashString(const LLBC_String &str)
{
    return LLBC_HashString(str.c_str(), str.length());
}