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() || !files.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);
34 File &file = files.back();
35 file.filename = base_fn;
37 out.write(format("\nconst char %s_data[] =\n", mangle_filename(base_fn)));
42 unsigned len = in.read(buf, (79-line.size())/4);
43 line += c_escape(string(buf, len));
44 if(line.size()>=76 || in.eof())
46 out.write(format(" \"%s\"", line));
56 void BuiltinGenerator::end(const string &module_name)
58 out.write(format("\nvoid init_%s(DataFile::BuiltinSource &source)\n{\n", module_name));
59 for(vector<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
60 out.write(format(" source.add_object(\"%s\", %s_data, %d);\n", i->filename, mangle_filename(i->filename), i->size));
66 for(vector<string>::const_iterator i=namespc.begin(); i!=namespc.end(); ++i)
67 out.write(format("} // namespace %s\n", *i));
74 string BuiltinGenerator::mangle_filename(const string &fn)
77 for(string::iterator i=mangled.begin(); i!=mangled.end(); ++i)