]> git.tdb.fi Git - libs/datafile.git/blob - tool/builtingenerator.cpp
Remove unnecessary destructor declarations
[libs/datafile.git] / tool / builtingenerator.cpp
1 #include <msp/fs/utils.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "builtingenerator.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 BuiltinGenerator::BuiltinGenerator(IO::Base &o):
10         out(o)
11 {
12         out.write("#include <msp/datafile/builtinsource.h>\n\n");
13 }
14
15 void BuiltinGenerator::begin(const std::string &ns)
16 {
17         if(!namespc.empty() || !files.empty())
18                 throw logic_error("BuiltinGenerator::begin");
19
20         if(!ns.empty())
21         {
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));
25         }
26 }
27
28 void BuiltinGenerator::add_file(const std::string &fn)
29 {
30         IO::BufferedFile in(fn);
31
32         string base_fn = FS::basename(fn);
33         files.emplace_back();
34         File &file = files.back();
35         file.filename = base_fn;
36
37         out.write(format("\nconst char %s_data[] =\n", mangle_filename(base_fn)));
38         string line;
39         while(!in.eof())
40         {
41                 char buf[19];
42                 unsigned len = in.read(buf, (79-line.size())/4);
43                 line += c_escape(string(buf, len));
44                 if(line.size()>=76 || in.eof())
45                 {
46                         out.write(format("  \"%s\"", line));
47                         line.clear();
48                         if(in.eof())
49                                 out.put(';');
50                         out.put('\n');
51                 }
52                 file.size += len;
53         }
54 }
55
56 void BuiltinGenerator::end(const string &module_name)
57 {
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));
61         out.write("}\n");
62
63         if(!namespc.empty())
64         {
65                 out.put('\n');
66                 for(vector<string>::const_iterator i=namespc.begin(); i!=namespc.end(); ++i)
67                         out.write(format("} // namespace %s\n", *i));
68
69                 namespc.clear();
70                 files.clear();
71         }
72 }
73
74 string BuiltinGenerator::mangle_filename(const string &fn)
75 {
76         string mangled = fn;
77         for(string::iterator i=mangled.begin(); i!=mangled.end(); ++i)
78                 if(!isalnum(*i))
79                         *i = '_';
80         return mangled;
81 }