Ejemplo n.º 1
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.º 2
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.º 3
0
LLBC_String LLBC_Trim(const LLBC_String &str, const char *targets)
{
    if (UNLIKELY(str.empty()))
    {
        return LLBC_String();
    }

    return LLBC_TrimRight(LLBC_TrimLeft(str, targets), targets);
}
Ejemplo n.º 4
0
LLBC_String LLBC_Trim(const LLBC_String &str)
{
    if (UNLIKELY(str.empty()))
    {
        return LLBC_String(); 
    }

    return LLBC_TrimRight(LLBC_TrimLeft(str));
}
Ejemplo n.º 5
0
void LLBC_SockAddr_IN::SetIp(const LLBC_String &ip)
{
    if (ip.empty())
    {
        SetIp("127.0.0.1");
        return;
    }

    _ip = ::inet_addr(ip.c_str());
}
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_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.º 8
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.º 9
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.º 10
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.º 11
0
int LLBC_SamplerGroup::AddSampler(int type, const LLBC_String &name)
{
    if (!LLBC_SamplerType::IsValid(type) || name.empty())
    {
        LLBC_SetLastError(LLBC_ERROR_ARG);
        return LLBC_RTN_FAILED;
    }

    if (_samplers->find(name) != _samplers->end())
    {
        LLBC_SetLastError(LLBC_ERROR_EXIST);
        return LLBC_RTN_FAILED;
    }

    LLBC_ISampler *sampler = NULL;
    switch (type)
    {
    case LLBC_SamplerType::CountSampler:
        sampler = new LLBC_CountSampler;
        break;

    case LLBC_SamplerType::LimitSampler:
        sampler = new LLBC_LimitSampler;
        break;

    case LLBC_SamplerType::IntervalSampler:
        sampler = new LLBC_IntervalSampler;
        break;

    default:
        ASSERT(false && "llbc library internal error, unknown sampler type!");
        break;
    }

    _samplers->insert(std::make_pair(name, sampler));

    return true;
}
Ejemplo n.º 12
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);
}
Ejemplo n.º 13
0
LLBC_PacketHeaderParts *pyllbc_Service::BuildCLayerParts(PyObject *pyLayerParts)
{
    // Python layer parts(dict type) convert rules describe:
    //   python type       c++ type
    // --------------------------
    //   int/long/bool -->   sint64
    //     float4/8    -->  float/double
    //   str/bytearray -->  LLBC_String

    if (!PyDict_Check(pyLayerParts))
    {
        pyllbc_SetError("parts instance not dict type");
        return NULL;
    }

    LLBC_PacketHeaderParts *cLayerParts = LLBC_New(LLBC_PacketHeaderParts);

    Py_ssize_t pos = 0;
    PyObject *key, *value;
    while (PyDict_Next(pyLayerParts, &pos, &key, &value)) // key & value are borrowed.
    {
        const int serialNo = static_cast<int>(PyInt_AsLong(key));
        if (UNLIKELY(serialNo == -1 && PyErr_Occurred()))
        {
            pyllbc_TransferPyError("When fetch header part serial no");
            LLBC_Delete(cLayerParts);

            return NULL;
        }

        // Value type check order:
        //   int->
        //     str->
        //       float->
        //         long->
        //           bool->
        //             bytearray->
        //               other objects
        if (PyInt_CheckExact(value))
        {
            const sint64 cValue = PyInt_AS_LONG(value);
            cLayerParts->SetPart<sint64>(serialNo, cValue);
        }
        else if (PyString_CheckExact(value))
        {
            char *strBeg;
            Py_ssize_t strLen;
            if (UNLIKELY(PyString_AsStringAndSize(value, &strBeg, &strLen) == -1))
            {
                pyllbc_TransferPyError("When fetch header part value");
                LLBC_Delete(cLayerParts);

                return NULL;
            }

            cLayerParts->SetPart(serialNo, strBeg, strLen);

        }
        else if (PyFloat_CheckExact(value))
        {
            const double cValue = PyFloat_AS_DOUBLE(value);
            cLayerParts->SetPart<double>(serialNo, cValue);
        }
        else if (PyLong_CheckExact(value))
        {
            const sint64 cValue = PyLong_AsLongLong(value);
            cLayerParts->SetPart<sint64>(serialNo, cValue);
        }
        else if (PyBool_Check(value))
        {
            const int pyBoolCheck = PyObject_IsTrue(value);
            if (UNLIKELY(pyBoolCheck == -1))
            {
                pyllbc_TransferPyError("when fetch header part value");
                LLBC_Delete(cLayerParts);

                return NULL;
            }

            cLayerParts->SetPart<uint8>(serialNo, pyBoolCheck);
        }
        else if (PyByteArray_CheckExact(value))
        {
            char *bytesBeg = PyByteArray_AS_STRING(value);
            Py_ssize_t bytesLen = PyByteArray_GET_SIZE(value);

            cLayerParts->SetPart(serialNo, bytesBeg, bytesLen);
        }
        else // Other types, we simple get the object string representations.
        {
            LLBC_String strRepr = pyllbc_ObjUtil::GetObjStr(value);
            if (UNLIKELY(strRepr.empty() && PyErr_Occurred()))
            {
                LLBC_Delete(cLayerParts);
                return NULL;
            }

            cLayerParts->SetPart(serialNo, strRepr.data(), strRepr.size());
        }
    }

    return cLayerParts;
}
Ejemplo n.º 14
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.º 15
0
int LLBC_LogTokenChain::Build(const LLBC_String &pattern)
{
    if (_head)
    {
        LLBC_SetLastError(LLBC_ERROR_REENTRY);
        return LLBC_RTN_FAILED;
    }

    char ch = '\0';
    const char *curPattern = NULL;
    LLBC_String::size_type patternLength = 0;
    int state = LLBC_INTERNAL_NS __g_literal_state;

    LLBC_ILogToken *token = NULL;
    LLBC_LogFormattingInfo *formatter = NULL;

    LLBC_String buf;
    if (pattern.empty())
    {
        curPattern = LLBC_INTERNAL_NS __g_default_pattern;
        patternLength = LLBC_StrLenA(LLBC_INTERNAL_NS __g_default_pattern);
    }
    else
    {
        curPattern = pattern.data();
        patternLength = pattern.size();
    }

    for (size_t i = 0; i < patternLength;)
    {
        ch = curPattern[i ++];
        switch(state)
        {
        case LLBC_INTERNAL_NS __g_literal_state:
            if (i == patternLength)
            {
                buf.append(1, ch);
                break;
            }

            if (ch == LLBC_LogTokenType::EscapeToken)
            {
                if (curPattern[i] == LLBC_LogTokenType::EscapeToken)
                {
                    buf.append(1, ch);
                    i ++;
                }
                else
                {
                    if (!buf.empty())
                    {
                        token = LLBC_LogTokenBuilderSingleton->BuildLogToken(LLBC_LogTokenType::StrToken);
                        token->Initialize(formatter, buf);
                        this->AppendToken(token);

                        buf.clear();
                        formatter = NULL;
                    }

                    buf.append(1, ch);
                    LLBC_XDelete(formatter);
                    state = LLBC_INTERNAL_NS __g_converter_state;
                }
            }
            else
            {
                buf.append(1, ch);
            }

            break;

        case LLBC_INTERNAL_NS __g_converter_state:
            buf.append(1, ch);
            
            if ((ch >= 0x30 && ch <= 0x39) || ch == '-')
            {
                state = LLBC_INTERNAL_NS __g_number_state;
                break;
            }

            buf.erase(buf.rfind(LLBC_LogTokenType::EscapeToken));
            token = LLBC_LogTokenBuilderSingleton->BuildLogToken(ch);
            if (!formatter)
            {
                formatter = new LLBC_LogFormattingInfo;
            }

            token->Initialize(formatter, "");
            this->AppendToken(token);

            formatter = NULL;
            state = LLBC_INTERNAL_NS __g_literal_state;

            break;

        case LLBC_INTERNAL_NS __g_number_state:
            if ((ch < 0x30 || ch > 0x39) && ch != '-')
            {
                int minLength = LLBC_Str2Int32(&buf[buf.rfind(LLBC_LogTokenType::EscapeToken) + 1]);
                if (!formatter)
                {
                    formatter = new LLBC_LogFormattingInfo(minLength < 0 ? true : false, ::abs(minLength), INT_MAX);
                }
                else
                {
                    formatter->SetLeftAligh(minLength < 0 ? true : false);
                    formatter->SetMinLen(minLength);
                    formatter->SetMaxLen(INT_MAX);
                }

                i --;
                state = LLBC_INTERNAL_NS __g_converter_state;

                break;
            }

            buf.append(1, ch);
            break;

        default:
            LLBC_XDelete(formatter);
            this->Cleanup();

            LLBC_SetLastError(LLBC_ERROR_FORMAT);
            return LLBC_RTN_FAILED;

            break;
        }
    }

    if (!buf.empty())
    {
        token = LLBC_LogTokenBuilderSingleton->BuildLogToken(LLBC_LogTokenType::StrToken);
        token->Initialize(NULL, buf);
        this->AppendToken(token);
    }

    return LLBC_RTN_OK;
}