]> git.tdb.fi Git - libs/datafile.git/commitdiff
Use explicit lengths in builtin data initialization
authorMikko Rasa <tdb@tdb.fi>
Sun, 2 Jan 2022 10:14:47 +0000 (12:14 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 2 Jan 2022 10:14:47 +0000 (12:14 +0200)
This allows adding binary files which may contain embedded nul bytes.

tool/builtingenerator.cpp
tool/builtingenerator.h

index 45d5844f5fdbe9b7e2236b76e89e0eacff51587c..45465db93ff172f67ee7bd1dd1f48961de0ebe3a 100644 (file)
@@ -14,7 +14,7 @@ BuiltinGenerator::BuiltinGenerator(IO::Base &o):
 
 void BuiltinGenerator::begin(const std::string &ns)
 {
-       if(!namespc.empty() || !filenames.empty())
+       if(!namespc.empty() || !files.empty())
                throw logic_error("BuiltinGenerator::begin");
 
        if(!ns.empty())
@@ -30,7 +30,9 @@ void BuiltinGenerator::add_file(const std::string &fn)
        IO::BufferedFile in(fn);
 
        string base_fn = FS::basename(fn);
-       filenames.push_back(base_fn);
+       files.emplace_back();
+       File &file = files.back();
+       file.filename = base_fn;
 
        out.write(format("\nconst char %s_data[] =\n", mangle_filename(base_fn)));
        string line;
@@ -47,14 +49,15 @@ void BuiltinGenerator::add_file(const std::string &fn)
                                out.put(';');
                        out.put('\n');
                }
+               file.size += len;
        }
 }
 
 void BuiltinGenerator::end(const string &module_name)
 {
        out.write(format("\nvoid init_%s(DataFile::BuiltinSource &source)\n{\n", module_name));
-       for(vector<string>::const_iterator i=filenames.begin(); i!=filenames.end(); ++i)
-               out.write(format("  source.add_object(\"%s\", %s_data);\n", *i, mangle_filename(*i)));
+       for(vector<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
+               out.write(format("  source.add_object(\"%s\", %s_data, %d);\n", i->filename, mangle_filename(i->filename), i->size));
        out.write("}\n");
 
        if(!namespc.empty())
@@ -64,7 +67,7 @@ void BuiltinGenerator::end(const string &module_name)
                        out.write(format("} // namespace %s\n", *i));
 
                namespc.clear();
-               filenames.clear();
+               files.clear();
        }
 }
 
index d6a0886ae140ea241394e0e804a2a4b8d55099f0..ddae27495faaf18a7b4bb0d3532efe379049a2e9 100644 (file)
@@ -8,9 +8,15 @@
 class BuiltinGenerator
 {
 private:
+       struct File
+       {
+               std::string filename;
+               std::size_t size = 0;
+       };
+
        Msp::IO::Base &out;
        std::vector<std::string> namespc;
-       std::vector<std::string> filenames;
+       std::vector<File> files;
 
 public:
        BuiltinGenerator(Msp::IO::Base &);