static void BencTestParseRawStrings() { BencArray array; array.AddRaw("a\x82"); array.AddRaw("a\x82", 1); BencString *raw = array.GetString(0); assert(raw && str::Eq(raw->RawValue(), "a\x82")); BencTestSerialization(raw, "2:a\x82"); raw = array.GetString(1); assert(raw && str::Eq(raw->RawValue(), "a")); BencTestSerialization(raw, "1:a"); BencDict dict; dict.AddRaw("1", "a\x82"); dict.AddRaw("2", "a\x82", 1); raw = dict.GetString("1"); assert(raw && str::Eq(raw->RawValue(), "a\x82")); BencTestSerialization(raw, "2:a\x82"); raw = dict.GetString("2"); assert(raw && str::Eq(raw->RawValue(), "a")); BencTestSerialization(raw, "1:a"); }
static void BencTestDictAppend() { /* test insertion in ascending order */ BencDict *dict = new BencDict(); for (size_t i = 1; i <= ITERATION_COUNT; i++) { ScopedMem<char> key(str::Format("%04u", i)); utassert(str::Len(key) == 4); dict->Add(key, i); utassert(dict->Length() == i); utassert(dict->GetInt(key)); utassert(!dict->GetString(key)); utassert(!dict->GetArray(key)); utassert(!dict->GetDict(key)); } BencInt *intObj = dict->GetInt("0123"); utassert(intObj && intObj->Value() == 123); BencTestRoundtrip(dict); delete dict; /* test insertion in descending order */ dict = new BencDict(); for (size_t i = ITERATION_COUNT; i > 0; i--) { ScopedMem<char> key(str::Format("%04u", i)); utassert(str::Len(key) == 4); BencObj *obj = new BencInt(i); dict->Add(key, obj); utassert(dict->Length() == ITERATION_COUNT + 1 - i); utassert(dict->GetInt(key)); } intObj = dict->GetInt("0123"); utassert(intObj && intObj->Value() == 123); BencTestRoundtrip(dict); delete dict; dict = new BencDict(); dict->Add("ab", 1); dict->Add("KL", 2); dict->Add("gh", 3); dict->Add("YZ", 4); dict->Add("ab", 5); BencTestSerialization(dict, "d2:KLi2e2:YZi4e2:abi5e2:ghi3ee"); delete dict->Remove("gh"); delete dict->Remove("YZ"); delete dict->Remove("missing"); BencTestSerialization(dict, "d2:KLi2e2:abi5ee"); delete dict; }
void SelectTranslation(const WCHAR *exePath=NULL) { LANGID langId = GetUserDefaultUILanguage(); int idx = GetLanguageIndex(langId); if (-1 == idx) { // try a neutral language if the specific sublanguage isn't available langId = MAKELANGID(PRIMARYLANGID(langId), SUBLANG_NEUTRAL); idx = GetLanguageIndex(langId); } if (-1 != idx) { gTranslationIdx = idx; plogf("sp: Detected language %s (%d)", gLanguages[idx / gTranslationsCount], idx); } // try to extract the language used by SumatraPDF ScopedMem<WCHAR> path; if (exePath) { path.Set(path::GetDir(exePath)); path.Set(path::Join(path, PREFS_FILE_NAME)); } if (!file::Exists(path)) { path.Set(GetSpecialFolder(CSIDL_APPDATA)); path.Set(path::Join(path, L"SumatraPDF\\" PREFS_FILE_NAME)); } if (!file::Exists(path)) return; plogf("sp: Found preferences at %S", path); #ifndef USE_INI_SETTINGS ScopedMem<char> data(file::ReadAll(path, NULL)); if (data) { BencObj *root = BencObj::Decode(data); if (root && root->Type() == BT_DICT) { BencDict *global = static_cast<BencDict *>(root)->GetDict("gp"); BencString *string = global ? global->GetString("UILanguage") : NULL; if (string) { plogf("sp: UILanguage from preferences: %s", string->RawValue()); for (int i = 0; gLanguages[i]; i++) { if (str::Eq(gLanguages[i], string->RawValue())) { gTranslationIdx = i * gTranslationsCount; break; } } } } delete root; } #else IniFile ini(path); IniSection *section = ini.FindSection(NULL); IniLine *line = section ? section->FindLine("CurrLangCode") : NULL; if (line) { plogf("sp: UILanguage from preferences: %s", line->value); for (int i = 0; gLanguages[i]; i++) { if (str::Eq(gLanguages[i], line->value)) { gTranslationIdx = i * gTranslationsCount; break; } } } #endif }