Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. static std::string read_contents(const std::string& filename) {
  2.     std::ifstream in(filename);
  3.     std::stringstream sstr;
  4.     sstr << in.rdbuf();
  5.     return sstr.str();
  6. }
  7.  
  8. static size_t nnpos(const std::string& buf, size_t pos) {
  9.     if (pos == std::string::npos) {
  10.         return buf.length();
  11.     }
  12.     return pos;
  13. }
  14.  
  15. static std::string_view next_token(const std::string& buf, size_t& pos) {
  16.     auto start = nnpos(buf, pos);
  17.     auto limit = nnpos(buf, buf.find_first_of("\r\n", start));
  18.     auto end = min(limit, nnpos(buf, buf.find_first_of("\t", start)));
  19.  
  20.     pos = buf.find_first_not_of("\t", end);
  21.  
  22.     return std::string_view(buf.data() + start, end - start);
  23. }
  24.  
  25. static void for_each_line(const std::string& buf, std::function<void(const std::string& buf, size_t pos)> fn) {
  26.     for (size_t pos = 0; pos <= buf.length(); pos = nnpos(buf, buf.find_first_of('\n', pos)) + 1) {
  27.         if (pos + 1 >= buf.length() || (buf[pos] == '/' && buf[pos + 1] == '/')) {
  28.             continue;
  29.         }
  30.  
  31.         fn(buf, pos);
  32.     }
  33. };
  34.  
  35. static std::unordered_map<std::string_view, std::string_view> parse_spec_item(const std::string& spec_item) {
  36.     std::unordered_map<std::string_view, std::string_view> item_id_to_loc;
  37.     for_each_line(spec_item, [&](const std::string& buf, size_t pos) {
  38.         next_token(buf, pos);
  39.         auto item_id = next_token(buf, pos);
  40.         auto item_id_loc = next_token(buf, pos);
  41.  
  42.         item_id_to_loc[item_id] = item_id_loc;
  43.     });
  44.     return item_id_to_loc;
  45. }
  46.  
  47. static std::unordered_map<std::string_view, std::string_view> parse_prop_item_txt_txt(const std::string& loc_item) {
  48.     std::unordered_map<std::string_view, std::string_view> loc_to_str;
  49.     for_each_line(loc_item, [&](const std::string& buf, size_t pos) {
  50.         auto loc_id = next_token(buf, pos);
  51.         auto loc_str = next_token(buf, pos);
  52.  
  53.         loc_to_str[loc_id] = loc_str;
  54.     });
  55.     return loc_to_str;
  56. }
  57.  
  58. static std::unordered_map<std::string_view, std::string_view> parse_define_item(const std::string& define_item) {
  59.     std::unordered_map<std::string_view, std::string_view> item_id_to_val;
  60.     for_each_line(define_item, [&](const std::string& buf, size_t pos) {
  61.         next_token(buf, pos);
  62.         auto define_id = next_token(buf, pos);
  63.         auto define_val = next_token(buf, pos);
  64.  
  65.         if (define_id.empty() || define_id == "#endif" || define_id == "#ifndef" || define_val == "__DEFINE_ITEM") {
  66.             return;
  67.         }
  68.  
  69.         item_id_to_val[define_id] = define_val;
  70.     });
  71.     return item_id_to_val;
  72. }
  73.  
  74. static std::unordered_map<std::string, int> populate_item_database() {
  75.     std::unordered_map<std::string, int> item_name_to_id;
  76.     if (!std::filesystem::exists("data/Spec_Item.txt") ||
  77.         !std::filesystem::exists("data/propItem.txt.txt") ||
  78.         !std::filesystem::exists("data/defineItem.h")) {
  79.         return item_name_to_id;
  80.     }
  81.  
  82.     std::string spec_item = read_contents("data/Spec_Item.txt");
  83.     std::string prop_item_txt_txt = read_contents("data/propItem.txt.txt");
  84.     std::string define_item = read_contents("data/defineItem.h");
  85.  
  86.     std::unordered_map<std::string_view, std::string_view> item_id_to_val = parse_define_item(define_item);
  87.     std::unordered_map<std::string_view, std::string_view> item_id_to_loc = parse_spec_item(spec_item);
  88.     std::unordered_map<std::string_view, std::string_view> loc_to_str = parse_prop_item_txt_txt(prop_item_txt_txt);
  89.  
  90.     for (auto& [define_id, define_val] : item_id_to_val) {
  91.         try {
  92.             item_name_to_id[std::string(loc_to_str[item_id_to_loc[define_id]])] = std::stoi(std::string(define_val));
  93.         } catch (...) {}
  94.     }
  95.  
  96.     return item_name_to_id;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement