// dictionary load/contains/find test void Test::Test9() { const size_t dict_size = 10; ofstream f("dict"); for (size_t i = 0; i < dict_size; ++i) f << i << endl; f.close(); Dictionary dict; string msg; bool res = dict.Load("dict", 1, msg); for (size_t i = 0; res && i < dict_size; ++i) { string str = to_string(i); res = dict.Contains(str); Dictionary::DictionaryData::iterator di; res = res && dict.Find(str, di); res = res && di->first.compare(str) == 0; res = res && di->second == false; } printf(res ? "Test9:\tpassed\r\n" : "Test9:\tfailed\r\n"); remove("dict"); }
std::string QueryMap::Apply(const VariableMap & vm, const Dictionary & dict , SiteCharset charset) { using namespace std; // split to vector // find value in dict stringstream ss; UrlQueryEscape uqe; vector<wstring> v; boost::split(v, text_, boost::is_any_of(L"&")); vector<pair<wstring, wstring> > w; int i; for (i=0; i<v.size(); ++i) { vector<wstring> eq; boost::split(eq, v[i], boost::is_any_of(L"=")); if (eq.size() == 2) w.push_back(make_pair(eq[0], eq[1])); } for (i=0; i<w.size(); ++i) { const wstring & key = w[i].first; wstring domain = w[i].second, val; // 1 find in vm, get value // 2 find in dict, get value's value if (boost::starts_with(domain, L"{") && boost::ends_with(domain, L"}") ) { domain = domain.substr(1); domain.resize(domain.size() - 1); VariableMap::const_iterator i_vm = vm.find(domain); ASSERT(i_vm != vm.end()); const VariableMap & vm_domain = dict.Find(domain); if (!vm_domain.empty()) { VariableMap::const_iterator j_vm = vm_domain.find(i_vm->second); ASSERT(j_vm != vm_domain.end()); if (j_vm != vm_domain.end()) val = j_vm->second; } else val = i_vm->second; } else val = domain; if (charset == SC_ANSI) ss << w2string(key) << "=" << uqe(w2string(val)) << "&"; else ss << string2utf8(key) << "=" << uqe(string2utf8(val)) << "&"; } std::string ret = ss.str(); // remove last & ret.resize(ret.size() - 1); return ret; }