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>");
28 if (arguments.missing() || name_space.empty())
30 arguments.print_help(std::cerr);
31 std::cerr <<
"output: <namespace>_<identifer>.{h,cpp}\n";
35 const std::string file_name = name_space.back() +
'_' + std::string(identifier);
39 cpp <<
"#include \"" << file_name <<
".h\"\n";
40 cpp <<
"#include \"" << name_space.back() <<
"/Readonly_Database.h\"\n";
41 cpp <<
"#include \"joedb/journal/Readonly_Memory_File.h\"\n";
42 if (mode == Mode::base64)
43 cpp <<
"#include \"joedb/ui/base64.h\"\n";
50 std::string file_content;
54 file_content.resize(
size_t(in.get_size()));
55 in.pread(file_content.data(), file_content.size(), 0);
58 if (mode != Mode::base64)
60 cpp <<
" const size_t " << identifier <<
"_size = ";
61 cpp << file_content.size() <<
";\n";
63 cpp <<
" char const * const " << identifier <<
"_data = ";
65 if (mode == Mode::base64)
69 else if (mode == Mode::raw)
71 char const *
const delimiter =
"glouglou";
73 cpp <<
"R\"" << delimiter <<
"(";
75 cpp <<
")" << delimiter <<
'\"';
77 else if (mode == Mode::utf8)
81 else if (mode == Mode::ascii)
84 for (
const uint8_t c: file_content)
92 cpp <<
" const Database &get_embedded_" << identifier <<
"()\n";
94 cpp <<
" static const Readonly_Database db(joedb::Readonly_Memory_File(";
96 if (mode == Mode::base64)
97 cpp <<
"joedb::base64_decode(" << identifier <<
"_data)";
99 cpp << identifier <<
"_data, " << identifier <<
"_size";
102 cpp <<
" return db;\n";
105 if (mode != Mode::base64)
107 cpp <<
"\n size_t get_embedded_" << identifier;
108 cpp <<
"_size() {return " << identifier <<
"_size;}\n";
110 cpp <<
" char const *get_embedded_" << identifier;
111 cpp <<
"_data() {return " << identifier <<
"_data;}\n";
121 h <<
"\n#include <stddef.h>\n\n";
125 h <<
"\n class Database;\n";
126 h <<
" const Database &get_embedded_" << identifier <<
"();\n";
128 if (mode != Mode::base64)
130 h <<
" size_t get_embedded_" << identifier <<
"_size();\n";
131 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_open(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)
void namespace_include_guard_close(std::ostream &out)
@ truncate
create new file, or truncate existing file, and locks the file
@ 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)