43 lines
1.3 KiB
C++
43 lines
1.3 KiB
C++
#include "game/data/strings.h"
|
|
|
|
#include "mars/text/csv.h"
|
|
|
|
namespace game::data {
|
|
|
|
std::optional<std::string_view> StringTable::get(std::string_view key) const {
|
|
auto it = by_key_.find(fold(key));
|
|
if (it == by_key_.end()) return std::nullopt;
|
|
return std::string_view(it->second);
|
|
}
|
|
|
|
std::optional<std::string_view> StringTable::resolve(std::string_view token) const {
|
|
if (!token.empty() && token[0] == '@') token.remove_prefix(1);
|
|
return get(token);
|
|
}
|
|
|
|
void StringTable::set(std::string key, std::string text) {
|
|
std::string k = fold(key);
|
|
auto it = by_key_.find(k);
|
|
if (it != by_key_.end()) {
|
|
duplicates_.emplace_back(key, it->second);
|
|
it->second = std::move(text);
|
|
return;
|
|
}
|
|
by_key_.emplace(std::move(k), std::move(text));
|
|
}
|
|
|
|
Loaded<StringTable> parse_string_table(std::string_view csv_text, std::string file) {
|
|
Loaded<StringTable> out;
|
|
StringTable t;
|
|
auto csv = mars::text::parse_csv(csv_text);
|
|
for (const auto& p : csv.problems)
|
|
out.problems.push_back({Problem::Kind::Unparsed, file, p.line, "", p.message});
|
|
for (const auto& row : csv.value.rows) {
|
|
if (row.cells.empty()) continue;
|
|
t.set(row.cells[0], row.cells.size() > 1 ? row.cells[1] : std::string());
|
|
}
|
|
out.value = std::move(t);
|
|
return out;
|
|
}
|
|
|
|
} // namespace game::data
|