Allow binding commands to class instance
[lsnes.git] / src / cmdhelp / mkstubsi.cpp
blob6f16f3264ee05ee997e3d77825096928474225d7
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, bool hiraw = false);
12 std::string quote_c_string(const std::string& raw, bool hiraw)
14 std::ostringstream out;
15 out << "\"";
16 for(auto i : raw) {
17 unsigned char ch = i;
18 if(ch == '\n')
19 out << "\\n";
20 else if(ch == '\"')
21 out << "\\\"";
22 else if(ch == '\\')
23 out << "\\\\";
24 else if(ch < 32 || ch == 127 || (ch > 127 && !hiraw))
25 out << "\\x" << hexes[ch / 16] << hexes[ch % 16];
26 else
27 out << ch;
29 out << "\"";
30 return out.str();
33 void process_command_inverse(JSON::node& n, std::ostream& imp)
35 if(!n.field_exists("__inverse"))
36 return;
37 auto& inv = n["__inverse"];
38 auto name = n["__name"].as_string8();
39 for(auto i = inv.begin(); i != inv.end(); i++) {
40 std::string args = i.key8();
41 std::string desc = i->as_string8();
42 if(args != "")
43 imp << "\t" << quote_c_string(name + " " + args) << ", " << quote_c_string(desc, true) << ","
44 << std::endl;
45 else
46 imp << "\t" << quote_c_string(name) << ", " << quote_c_string(desc, true) << "," << std::endl;
50 int main(int argc, char** argv)
52 std::ofstream impl("inverselist.cpp");
53 impl << "#include \"cmdhelp/inverselist.hpp\"" << std::endl;
54 impl << "namespace STUBS" << std::endl;
55 impl << "{" << std::endl;
56 impl << "const char* inverse_cmd_list[] = {" << std::endl;
58 for(int i = 1; i < argc; i++) {
59 //Hack, skip crap.
60 if(argv[i][0] == 0 || argv[i][strlen(argv[i])-1] == 'e')
61 continue;
62 std::ifstream infile(argv[i]);
63 std::string in_json;
64 std::string tmp;
65 while(infile) {
66 std::getline(infile, tmp);
67 in_json = in_json + tmp + "\n";
69 JSON::node n(in_json);
70 for(auto j = n.begin(); j != n.end(); j++)
71 process_command_inverse(*j, impl);
74 impl << "\t0\n};" << std::endl;
75 impl << "}" << std::endl;
76 return 0;