xcdat/tools/xcdat_lookup.cpp

65 lines
1.6 KiB
C++
Raw Normal View History

2021-06-27 17:54:58 +00:00
#include <xcdat.hpp>
#include "cmd_line_parser/parser.hpp"
2021-06-29 01:45:02 +00:00
#include "mm_file/mm_file.hpp"
2021-06-27 17:54:58 +00:00
#include "tinyformat/tinyformat.h"
cmd_line_parser::parser make_parser(int argc, char** argv) {
cmd_line_parser::parser p(argc, argv);
p.add("input_idx", "Input filepath of trie index");
return p;
}
2021-06-29 03:02:51 +00:00
xcdat::flag_type get_flag(std::string_view filepath) {
std::ifstream ifs(filepath);
XCDAT_THROW_IF(!ifs.good(), "Cannot open the input file");
xcdat::flag_type flag;
ifs.read(reinterpret_cast<char*>(&flag), sizeof(flag));
return flag;
}
2021-06-27 17:54:58 +00:00
template <class Trie>
int lookup(const cmd_line_parser::parser& p) {
const auto input_idx = p.get<std::string>("input_idx");
2021-06-29 01:45:02 +00:00
const mm::file_source<char> fin(input_idx.c_str(), mm::advice::sequential);
const auto trie = xcdat::mmap<Trie>(fin.data());
2021-06-27 17:54:58 +00:00
for (std::string str; std::getline(std::cin, str);) {
const auto id = trie.lookup(str);
if (id.has_value()) {
tfm::printfln("%d\t%s", id.value(), str);
} else {
tfm::printfln("-1\t%s", str);
}
}
return 0;
}
int main(int argc, char** argv) {
#ifndef NDEBUG
tfm::warnfln("The code is running in debug mode.");
#endif
std::ios::sync_with_stdio(false);
auto p = make_parser(argc, argv);
if (!p.parse()) {
return 1;
}
2021-06-29 03:02:51 +00:00
const auto input_idx = p.get<std::string>("input_idx");
const auto flag = get_flag(input_idx);
2021-06-27 17:54:58 +00:00
2021-06-29 03:02:51 +00:00
switch (flag) {
2021-06-27 17:54:58 +00:00
case 7:
return lookup<xcdat::trie_7_type>(p);
case 8:
return lookup<xcdat::trie_8_type>(p);
default:
break;
}
p.help();
return 1;
}