13 static int embed(Arguments &arguments)
15 enum class Mode {base64, raw,
utf8, ascii};
16 const std::vector<const char *> labels{
"base64",
"raw",
"utf8",
"ascii"};
18 const Mode mode = Mode
20 arguments.get_enum_option(
"mode", labels,
int(Mode::ascii))
22 const std::string_view joedb_file_name = arguments.get_next(
"<file.joedb>");
24 const std::string_view identifier = arguments.get_next(
"<identifier>");
27 if (arguments.missing() || name_space.empty())
29 arguments.print_help(std::cerr);
30 std::cerr <<
"output: <namespace>_<identifer>.{h,cpp}\n";
34 const std::string file_name = name_space.back() +
'_' + std::string(identifier);
37 std::ofstream cpp(file_name +
".cpp", std::ios::binary | std::ios::out);
39 std::string file_content;
43 file_content.resize(
size_t(in.get_size()));
44 in.pread(file_content.data(), file_content.size(), 0);
47 cpp <<
"#include \"" << file_name <<
".h\"\n";
48 cpp <<
"#include \"" << name_space.back() <<
"/Readonly_Database.h\"\n";
49 cpp <<
"#include \"joedb/journal/Readonly_Memory_File.h\"\n";
50 if (mode == Mode::base64)
51 cpp <<
"#include \"joedb/ui/base64.h\"\n";
57 if (mode != Mode::base64)
59 cpp <<
" const size_t " << identifier <<
"_size = ";
60 cpp << file_content.size() <<
";\n";
62 cpp <<
" char const * const " << identifier <<
"_data = ";
64 if (mode == Mode::base64)
68 else if (mode == Mode::raw)
70 char const *
const delimiter =
"glouglou";
72 cpp <<
"R\"" << delimiter <<
"(";
74 cpp <<
")" << delimiter <<
'\"';
76 else if (mode == Mode::utf8)
80 else if (mode == Mode::ascii)
83 for (
const uint8_t c: file_content)
90 cpp <<
" const Database &get_embedded_" << identifier <<
"()\n";
92 cpp <<
" static const Readonly_Database db(joedb::Readonly_Memory_File(";
94 if (mode == Mode::base64)
95 cpp <<
"joedb::base64_decode(" << identifier <<
"_data)";
97 cpp << identifier <<
"_data, " << identifier <<
"_size";
100 cpp <<
" return db;\n";
103 if (mode != Mode::base64)
105 cpp <<
"\n size_t get_embedded_" << identifier;
106 cpp <<
"_size() {return " << identifier <<
"_size;}\n";
108 cpp <<
" char const *get_embedded_" << identifier;
109 cpp <<
"_data() {return " << identifier <<
"_data;}\n";
116 std::ofstream h(file_name +
".h", std::ios::binary | std::ios::out);
120 h <<
"\n#include <stddef.h>\n\n";
124 h <<
"\n class Database;\n";
125 h <<
" const Database &get_embedded_" << identifier <<
"();\n";
127 if (mode != Mode::base64)
129 h <<
" size_t get_embedded_" << identifier <<
"_size();\n";
130 h <<
" char const *get_embedded_" << identifier <<
"_data();\n";
std::vector< std::string > split_namespace(std::string_view s)
void namespace_open(std::ostream &out, const std::vector< std::string > &n)
void namespace_close(std::ostream &out, const std::vector< std::string > &n)
void namespace_include_guard(std::ostream &out, const char *name, const std::vector< std::string > &n)
std::string namespace_string(const std::vector< std::string > &n, const char *delimiter)
@ read_existing
fails if does not exist
void write_octal_character(std::ostream &out, uint8_t c)
void write_string(std::ostream &out, const std::string &s, bool json)
int main_wrapper(int(*main)(Arguments &), int argc, char **argv)
Process command-line arguments and catch exceptions from main.
std::string base64_encode(const std::string &input)