| 6 | namespace OD2::Common { |
| 7 | |
| 8 | void 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 | } |
| 59 | DataTable &DataTableManager::getDataTable(const std::string_view name) { |
| 60 | const auto it = dataTables.find(name); |
| 61 | if (it == dataTables.end()) |
no test coverage detected