Joedb 10.0.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
joedb_embed.cpp
Go to the documentation of this file.
1#include "joedb/ui/type_io.h"
2#include "joedb/ui/base64.h"
6
7#include <iostream>
8#include <fstream>
9#include <string>
10
11namespace joedb
12{
13 static int embed(Arguments &arguments)
14 {
15 enum class Mode {base64, raw, utf8, ascii};
16 const std::vector<const char *> labels{"base64", "raw", "utf8", "ascii"};
17
18 const Mode mode = Mode
19 (
20 arguments.get_enum_option("mode", labels, int(Mode::ascii))
21 );
22 const std::string_view joedb_file_name = arguments.get_next("<file.joedb>");
23 const std::string_view namespace_string = arguments.get_next("<namespace>");
24 const std::string_view identifier = arguments.get_next("<identifier>");
25 const std::vector<std::string> name_space = split_namespace(namespace_string);
26
27 if (arguments.missing() || name_space.empty())
28 {
29 arguments.print_help(std::cerr);
30 std::cerr << "output: <namespace>_<identifer>.{h,cpp}\n";
31 return 1;
32 }
33
34 const std::string file_name = name_space.back() + '_' + std::string(identifier);
35
36 {
37 std::ofstream cpp(file_name + ".cpp", std::ios::binary | std::ios::out);
38
39 std::string file_content;
40
41 {
42 const File in(joedb_file_name.data(), Open_Mode::read_existing);
43 file_content.resize(size_t(in.get_size()));
44 in.pread(file_content.data(), file_content.size(), 0);
45 }
46
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";
52 cpp << '\n';
53
54 namespace_open(cpp, name_space);
55 cpp << '\n';
56
57 if (mode != Mode::base64)
58 {
59 cpp << " const size_t " << identifier << "_size = ";
60 cpp << file_content.size() << ";\n";
61 }
62 cpp << " char const * const " << identifier << "_data = ";
63
64 if (mode == Mode::base64)
65 {
66 cpp << '"' << base64_encode(file_content) << '"';
67 }
68 else if (mode == Mode::raw)
69 {
70 char const * const delimiter = "glouglou";
71 // TODO: generate delimiter from data
72 cpp << "R\"" << delimiter << "(";
73 cpp << file_content;
74 cpp << ")" << delimiter << '\"';
75 }
76 else if (mode == Mode::utf8)
77 {
78 write_string(cpp, file_content);
79 }
80 else if (mode == Mode::ascii)
81 {
82 cpp << '"';
83 for (const uint8_t c: file_content)
85 cpp << '"';
86 }
87
88 cpp << ";\n\n";
89
90 cpp << " const Database &get_embedded_" << identifier << "()\n";
91 cpp << " {\n";
92 cpp << " static const Readonly_Database db(joedb::Readonly_Memory_File(";
93
94 if (mode == Mode::base64)
95 cpp << "joedb::base64_decode(" << identifier << "_data)";
96 else
97 cpp << identifier << "_data, " << identifier << "_size";
98
99 cpp << "));\n";
100 cpp << " return db;\n";
101 cpp << " }\n";
102
103 if (mode != Mode::base64)
104 {
105 cpp << "\n size_t get_embedded_" << identifier;
106 cpp << "_size() {return " << identifier << "_size;}\n";
107
108 cpp << " char const *get_embedded_" << identifier;
109 cpp << "_data() {return " << identifier << "_data;}\n";
110 }
111
112 namespace_close(cpp, name_space);
113 }
114
115 {
116 std::ofstream h(file_name + ".h", std::ios::binary | std::ios::out);
117
118 namespace_include_guard(h, identifier.data(), name_space);
119
120 h << "\n#include <stddef.h>\n\n";
121
122 namespace_open(h, name_space);
123
124 h << "\n class Database;\n";
125 h << " const Database &get_embedded_" << identifier << "();\n";
126
127 if (mode != Mode::base64)
128 {
129 h << " size_t get_embedded_" << identifier << "_size();\n";
130 h << " char const *get_embedded_" << identifier << "_data();\n";
131 }
132
133 namespace_close(h, name_space);
134
135 h << "#endif\n";
136 }
137
138 return 0;
139 }
140}
141
142int main(int argc, char **argv)
143{
144 return joedb::main_wrapper(joedb::embed, argc, argv);
145}
int main()
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)
Definition base64.cpp:11
@ utf8
Definition json.h:12
Definition Blob.h:7
JOEDB_FILE File
Definition File.h:25