This source file includes following definitions.
- RemoveLanguageCode
- CCKey
- InitData
- GetData
#include "fake_downloader.h"
#include <cassert>
#include <fstream>
#include <map>
#include <string>
#include <utility>
namespace i18n {
namespace addressinput {
const char FakeDownloader::kFakeDataUrl[] = "test:///";
namespace {
const char kDataFileName[] = TEST_DATA_DIR "/countryinfo.txt";
const size_t kFakeDataUrlLength = sizeof FakeDownloader::kFakeDataUrl - 1;
std::string RemoveLanguageCode(const std::string& key) {
std::string::size_type language_code_pos = key.find("--");
return language_code_pos == std::string::npos
? key
: key.substr(0, language_code_pos);
}
std::string CCKey(const std::string& key) {
const char kSplitChar = '/';
std::string::size_type split = key.find(kSplitChar);
if (split == std::string::npos) {
return key;
}
split = key.find(kSplitChar, split + 1);
if (split == std::string::npos) {
return key;
}
return key.substr(0, split);
}
std::map<std::string, std::string> InitData() {
std::map<std::string, std::string> data;
std::ifstream file(kDataFileName);
assert(file.is_open());
std::string line;
while (file.good()) {
std::getline(file, line);
std::string::size_type divider = line.find('=');
if (divider == std::string::npos) {
continue;
}
std::string key = line.substr(0, divider);
std::string cc_key = RemoveLanguageCode(CCKey(key));
std::string value = line.substr(divider + 1);
std::string url = FakeDownloader::kFakeDataUrl + cc_key;
std::map<std::string, std::string>::iterator data_it = data.find(url);
if (data_it != data.end()) {
data_it->second += ", \"" + key + "\": " + value;
} else {
data.insert(std::make_pair(url, "{\"" + key + "\": " + value));
}
}
file.close();
for (std::map<std::string, std::string>::iterator data_it = data.begin();
data_it != data.end(); ++data_it) {
data_it->second += "}";
}
return data;
}
const std::map<std::string, std::string>& GetData() {
static const std::map<std::string, std::string> kData(InitData());
return kData;
}
}
FakeDownloader::FakeDownloader() {}
FakeDownloader::~FakeDownloader() {}
void FakeDownloader::Download(const std::string& url,
scoped_ptr<Callback> downloaded) {
std::map<std::string, std::string>::const_iterator data_it =
GetData().find(url);
bool success = data_it != GetData().end();
std::string data = success ? data_it->second : std::string();
if (!success &&
url.compare(
0, kFakeDataUrlLength, kFakeDataUrl, kFakeDataUrlLength) == 0) {
success = true;
data = "{}";
}
(*downloaded)(success, url, make_scoped_ptr(new std::string(data)));
}
}
}