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); }
__LLBC_NS_BEGIN LLBC_String LLBC_GetHomeDirectory(bool appendSlash) { #if LLBC_TARGET_PLATFORM_NON_WIN32 char *valStr = getenv("HOME"); #else char *valStr = getenv("HOMEPATH"); #endif if (!valStr) { LLBC_SetLastError(LLBC_ERROR_CLIB); return ""; } LLBC_String path = valStr; if (appendSlash) { #if LLBC_TARGET_PLATFORM_NON_WIN32 path.append(1, LLBC_SLASH_A); #else path.append(1, LLBC_BACKLASH_A); #endif } return path; }
void TestCase_Com_DataType::StringBaseTest() { LLBC_PrintLine("String base functions test:"); LLBC_String testStr; testStr.format("%s", "hello world!"); testStr.append_format("%s", "hello world!"); LLBC_PrintLine("LLBC_String::format/append_format test: %s", testStr.c_str()); LLBC_String testStr2; for(int i = 0; i < 1000; i++) { testStr.append("hello world!"); } testStr2.append_format("%s", testStr.c_str()); LLBC_PrintLine("LLBC_String:format large string test: %s", testStr2.c_str()); // tolower/toupper test. testStr = "Hello WoRlD!"; LLBC_PrintLine("'%s' to lower: '%s'", testStr.c_str(), testStr.tolower().c_str()); LLBC_PrintLine("'%s' to upper: '%s'", testStr.c_str(), testStr.toupper().c_str()); LLBC_PrintLine("\n"); }
LLBC_String LLBC_GetCacheDirectory(bool appendSlash) { #if LLBC_TARGET_PLATFORM_NON_WIN32 if (appendSlash) { return "/tmp/"; } else { return "/tmp"; } #else CHAR buf[MAX_PATH]; memset(buf, 0, sizeof(CHAR) * MAX_PATH); if (::SHGetSpecialFolderPathA(NULL, buf, CSIDL_INTERNET_CACHE, FALSE) == FALSE) { LLBC_SetLastError(LLBC_ERROR_OSAPI); return ""; } LLBC_String path = buf; if (appendSlash) { path.append(1, LLBC_BACKLASH_A); } return path; #endif }
void LLBC_LogTagToken::Format(const LLBC_LogData &data, LLBC_String &formattedData) const { int index = static_cast<int>(formattedData.size()); if (data.tagLen) formattedData.append(data.others + data.tagBeg, data.tagLen); LLBC_LogFormattingInfo *formatter = GetFormatter(); formatter->Format(formattedData, index); }
void LLBC_LogThreadIdToken::Format(const LLBC_LogData &data, LLBC_String &formattedData) const { int index = static_cast<int>(formattedData.size()); #if LLBC_TARGET_PLATFORM_LINUX formattedData.append(LLBC_Num2Str((uint32)(data.threadHandle))); #elif LLBC_TARGET_PLATFORM_WIN32 formattedData.append(LLBC_Num2Str((uint32)(data.threadHandle))); #elif LLBC_TARGET_PLATFORM_IPHONE formattedData.append(LLBC_Ptr2Str(data.threadHandle)); #elif LLBC_TARGET_PLATFORM_MAC formattedData.append(LLBC_Ptr2Str(data.threadHandle)); #elif LLBC_TARGET_PLATFORM_ANDROID formattedData.append(LLBC_Ptr2Str(data.threadHandle)); #endif LLBC_LogFormattingInfo *formatter = this->GetFormatter(); formatter->Format(formattedData, index); }
void TestCase_Com_DataType::StringBaseTest() { LLBC_PrintLine("String base functions test:"); LLBC_String testStr; testStr.format("%s", "hello world!"); testStr.append_format("%s", "hello world!"); LLBC_PrintLine("LLBC_String::format/append_format test: %s", testStr.c_str()); LLBC_String testStr2; for(int i = 0; i < 1000; i++) { testStr.append("hello world!"); } testStr2.append_format("%s", testStr.c_str()); LLBC_PrintLine("LLBC_String:format large string test: %s", testStr2.c_str()); // tolower/toupper test. testStr = "Hello WoRlD!"; LLBC_PrintLine("'%s' to lower: '%s'", testStr.c_str(), testStr.tolower().c_str()); LLBC_PrintLine("'%s' to upper: '%s'", testStr.c_str(), testStr.toupper().c_str()); // isalpha/isupper/islower. LLBC_String str("HELLO"); LLBC_PrintLine("%s islower?%d, isupper?%d, isalpha?%d", str.c_str(), str.islower(), str.isupper(), str.isalpha()); str = "hello"; LLBC_PrintLine("%s islower?%d, isupper?%d, isalpha?%d", str.c_str(), str.islower(), str.isupper(), str.isalpha()); str = "HeLlO"; LLBC_PrintLine("%s islower?%d, isupper?%d, isalpha?%d", str.c_str(), str.islower(), str.isupper(), str.isalpha()); str = "hello123"; LLBC_PrintLine("%s islower?%d, isupper?%d, isalpha?%d", str.c_str(), str.islower(), str.isupper(), str.isalpha()); str = "HELLO123"; LLBC_PrintLine("%s islower?%d, isupper?%d, isalpha?%d", str.c_str(), str.islower(), str.isupper(), str.isalpha()); str = "Hello123"; LLBC_PrintLine("%s islower?%d, isupper?%d, isalpha?%d", str.c_str(), str.islower(), str.isupper(), str.isalpha()); str = "H"; LLBC_PrintLine("%s islower?%d, isupper?%d, isalpha?%d", str.c_str(), str.islower(), str.isupper(), str.isalpha()); str = "h"; LLBC_PrintLine("%s islower?%d, isupper?%d, isalpha?%d", str.c_str(), str.islower(), str.isupper(), str.isalpha()); str = "3"; LLBC_PrintLine("%s islower?%d, isupper?%d, isalpha?%d", str.c_str(), str.islower(), str.isupper(), str.isalpha()); LLBC_PrintLine("\n"); }
LLBC_String LLBC_GetDocumentDirectory(bool appendSlash) { #if LLBC_TARGET_PLATFORM_NON_WIN32 return LLBC_GetHomeDirectory(appendSlash); #else CHAR buf[MAX_PATH]; memset(buf, 0, sizeof(CHAR) * MAX_PATH); if (::SHGetSpecialFolderPathA(NULL, buf, CSIDL_COMMON_DOCUMENTS, FALSE) == FALSE) { LLBC_SetLastError(LLBC_ERROR_OSAPI); return ""; } LLBC_String path = buf; if (appendSlash) { path.append(1, LLBC_BACKLASH_A); } return path; #endif }
LLBC_String LLBC_Variant::ValueToString() const { if (IsStr()) { return _holder.str; } else if (IsDict()) { LLBC_String content; content.append("{"); if (_holder.dict) { for (DictConstIter it = _holder.dict->begin(); it != _holder.dict->end(); ) { content.append(it->first.ValueToString()); content.append(":"); content.append(it->second.ValueToString()); if (++ it != _holder.dict->end()) { content.append("|"); } } } content.append("}"); return content; } else if (IsNil()) { return "nil"; } // RAW type var data. switch(_holder.type) { case LLBC_VariantType::VT_RAW_BOOL: return _holder.raw.int64Val != 0 ? "true" : "false"; case LLBC_VariantType::VT_RAW_SINT8: case LLBC_VariantType::VT_RAW_SINT16: case LLBC_VariantType::VT_RAW_SINT32: case LLBC_VariantType::VT_RAW_LONG: case LLBC_VariantType::VT_RAW_SINT64: return LLBC_Num2Str(_holder.raw.int64Val); case LLBC_VariantType::VT_RAW_UINT8: case LLBC_VariantType::VT_RAW_UINT16: case LLBC_VariantType::VT_RAW_UINT32: case LLBC_VariantType::VT_RAW_ULONG: case LLBC_VariantType::VT_RAW_UINT64: return LLBC_Num2Str(_holder.raw.uint64Val); case LLBC_VariantType::VT_RAW_FLOAT: case LLBC_VariantType::VT_RAW_DOUBLE: return LLBC_Num2Str(_holder.raw.doubleVal); default: break; } return ""; }
LLBC_String LLBC_Object::ToString() const { LLBC_String str; return str.append("Object: ") .append_format("%x", this); }
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; }
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; }