1 #include <msp/fs/utils.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "builtingenerator.h"
9 BuiltinGenerator::BuiltinGenerator(IO::Base &o):
12 out.write("#include <msp/datafile/builtinsource.h>\n\n");
15 void BuiltinGenerator::begin(const std::string &ns)
17 if(!namespc.empty() || !filenames.empty())
18 throw logic_error("BuiltinGenerator::begin");
22 namespc = split(ns, "::");
23 for(vector<string>::const_iterator i=namespc.begin(); i!=namespc.end(); ++i)
24 out.write(format("namespace %s {\n", *i));
28 void BuiltinGenerator::add_file(const std::string &fn)
30 IO::BufferedFile in(fn);
32 string base_fn = FS::basename(fn);
33 filenames.push_back(base_fn);
35 out.write(format("\nconst char %s_data[] =\n", mangle_filename(base_fn)));
40 unsigned len = in.read(buf, (79-line.size())/4);
41 line += c_escape(string(buf, len));
42 if(line.size()>=76 || in.eof())
44 out.write(format(" \"%s\"", line));
53 void BuiltinGenerator::end(const string &module_name)
55 out.write(format("\nvoid init_%s(DataFile::BuiltinSource &source)\n{\n", module_name));
56 for(vector<string>::const_iterator i=filenames.begin(); i!=filenames.end(); ++i)
57 out.write(format(" source.add_object(\"%s\", %s_data);\n", *i, mangle_filename(*i)));
63 for(vector<string>::const_iterator i=namespc.begin(); i!=namespc.end(); ++i)
64 out.write(format("} // namespace %s\n", *i));
71 string BuiltinGenerator::mangle_filename(const string &fn)
74 for(string::iterator i=mangled.begin(); i!=mangled.end(); ++i)