lsnes rr2-β24
[lsnes.git] / src / cmdhelp / mkstubsi.cpp
blobd84b397ee20f20ff23a1cab4d8c7f38a1df9d0d1
1 #include "json.hpp"
2 #include <string>
3 #include <cstring>
4 #include <fstream>
5 #include <sstream>
6 #include <set>
8 const char* hexes = "0123456789abcdef";
10 std::string quote_c_string(const std::string& raw)
12 std::ostringstream out;
13 out << "\"";
14 for(auto i : raw) {
15 unsigned char ch = i;
16 if(ch == '\n')
17 out << "\\n";
18 else if(ch == '\"')
19 out << "\\\"";
20 else if(ch == '\\')
21 out << "\\\\";
22 else if(ch < 32 || ch == 127)
23 out << "\\x" << hexes[ch / 16] << hexes[ch % 16];
24 else
25 out << ch;
27 out << "\"";
28 return out.str();
31 void process_command_inverse(JSON::node& n, std::string name, std::ostream& imp)
33 if(name == "__mod" || n.index_count() < 4)
34 return;
35 auto& inv = n.index(3);
36 for(auto i = inv.begin(); i != inv.end(); i++) {
37 std::string args = i.key8();
38 std::string desc = i->as_string8();
39 if(args != "")
40 imp << "\t" << quote_c_string(name + " " + args) << ", " << quote_c_string(desc) << ","
41 << std::endl;
42 else
43 imp << "\t" << quote_c_string(name) << ", " << quote_c_string(desc) << "," << std::endl;
47 bool is_crap_filename(const std::string& arg)
49 if(arg.length() >= 4 && arg.substr(arg.length() - 4) == ".exe") return true;
50 if(arg.length() >= 9 && arg.substr(arg.length() - 9) == "/mkstubsi") return true;
51 if(arg == "mkstubsi") return true;
52 return false;
55 int main(int argc, char** argv)
57 std::ofstream impl("inverselist.cpp");
58 impl << "#include \"cmdhelp/inverselist.hpp\"" << std::endl;
59 impl << "namespace STUBS" << std::endl;
60 impl << "{" << std::endl;
61 impl << "const char* inverse_cmd_list[] = {" << std::endl;
63 for(int i = 1; i < argc; i++) {
64 //Hack, skip crap.
65 if(argv[i][0] == 0 || is_crap_filename(argv[i]))
66 continue;
67 std::ifstream infile(argv[i]);
68 std::string in_json;
69 std::string tmp;
70 while(infile) {
71 std::getline(infile, tmp);
72 in_json = in_json + tmp + "\n";
74 JSON::node n(in_json);
75 for(auto j = n.begin(); j != n.end(); j++)
76 process_command_inverse(*j, j.key8(), impl);
79 impl << "\t0\n};" << std::endl;
80 impl << "}" << std::endl;
81 return 0;