MCPcopy Create free account
hub / github.com/AbyssEngine/AbyssEngineOld / addDataTable

Method addDataTable

src/OD2/Common/DataTableManager.cpp:8–58  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6namespace OD2::Common {
7
8void DataTableManager::addDataTable(const std::string_view name, const std::string_view fileName) {
9 Abyss::Common::Log::debug("Loading data table: {} ({})", name, fileName);
10 auto splitLine = [](const std::string_view line) {
11 std::vector<std::string> result;
12 std::string current;
13 for (const auto c : line) {
14 if (c == '\t') {
15 result.push_back(std::move(current));
16 current.clear();
17 } else {
18 current += c;
19 }
20 }
21 result.push_back(std::move(current));
22 return result;
23 };
24
25 std::string text;
26 {
27 std::lock_guard lock(_readMutex);
28 text = Abyss::AbyssEngine::getInstance().loadString(fileName);
29 }
30 std::vector<std::string> lines{};
31 std::istringstream stream(text);
32 std::string line;
33 while (std::getline(stream, line)) {
34 if (line.empty()) {
35 continue;
36 }
37 lines.push_back(std::move(line));
38 }
39 DataTable result;
40 result.reserve(lines.size());
41
42 // Grab first row and remove from lines
43 auto header = splitLine(lines.front());
44
45 for (int i = 1; i < lines.size(); ++i) {
46 auto row = splitLine(lines[i]);
47 absl::flat_hash_map<std::string, std::string> rowMap;
48 for (size_t j = 0; j < header.size(); j++) {
49 rowMap.emplace(header[j], std::move(row[j]));
50 }
51 result.push_back(std::move(rowMap));
52 }
53
54 {
55 std::lock_guard lock(_writeMutex);
56 dataTables.emplace(name, result);
57 }
58}
59DataTable &DataTableManager::getDataTable(const std::string_view name) {
60 const auto it = dataTables.find(name);
61 if (it == dataTables.end())

Callers 1

loadDataTablesFunction · 0.80

Calls 5

debugFunction · 0.85
clearMethod · 0.80
loadStringMethod · 0.80
emptyMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected