Joedb 10.2.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"
7
8#include <iostream>
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
26 const std::vector<std::string> name_space = split_namespace(namespace_string);
27
28 if (arguments.missing() || name_space.empty())
29 {
30 arguments.print_help(std::cerr);
31 std::cerr << "output: <namespace>_<identifer>.{h,cpp}\n";
32 return 1;
33 }
34
35 const std::string file_name = name_space.back() + '_' + std::string(identifier);
36
37 joedb::ofstream cpp(file_name + ".cpp", Open_Mode::truncate);
38
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";
44 cpp << '\n';
45
46 namespace_open(cpp, name_space);
47 cpp << '\n';
48
49 {
50 std::string file_content;
51
52 {
53 const File in(joedb_file_name.data(), Open_Mode::read_existing);
54 file_content.resize(size_t(in.get_size()));
55 in.pread(file_content.data(), file_content.size(), 0);
56 }
57
58 if (mode != Mode::base64)
59 {
60 cpp << " const size_t " << identifier << "_size = ";
61 cpp << file_content.size() << ";\n";
62 }
63 cpp << " char const * const " << identifier << "_data = ";
64
65 if (mode == Mode::base64)
66 {
67 cpp << '"' << base64_encode(file_content) << '"';
68 }
69 else if (mode == Mode::raw)
70 {
71 char const * const delimiter = "glouglou";
72 // TODO: generate delimiter from data
73 cpp << "R\"" << delimiter << "(";
74 cpp << file_content;
75 cpp << ")" << delimiter << '\"';
76 }
77 else if (mode == Mode::utf8)
78 {
79 write_string(cpp, file_content);
80 }
81 else if (mode == Mode::ascii)
82 {
83 cpp << '"';
84 for (const uint8_t c: file_content)
86 cpp << '"';
87 }
88 }
89
90 cpp << ";\n\n";
91
92 cpp << " const Database &get_embedded_" << identifier << "()\n";
93 cpp << " {\n";
94 cpp << " static const Readonly_Database db(joedb::Readonly_Memory_File(";
95
96 if (mode == Mode::base64)
97 cpp << "joedb::base64_decode(" << identifier << "_data)";
98 else
99 cpp << identifier << "_data, " << identifier << "_size";
100
101 cpp << "));\n";
102 cpp << " return db;\n";
103 cpp << " }\n";
104
105 if (mode != Mode::base64)
106 {
107 cpp << "\n size_t get_embedded_" << identifier;
108 cpp << "_size() {return " << identifier << "_size;}\n";
109
110 cpp << " char const *get_embedded_" << identifier;
111 cpp << "_data() {return " << identifier << "_data;}\n";
112 }
113
114 namespace_close(cpp, name_space);
115
116 {
117 joedb::ofstream h(file_name + ".h", Open_Mode::truncate);
118
119 namespace_include_guard_open(h, identifier.data(), name_space);
120
121 h << "\n#include <stddef.h>\n\n";
122
123 namespace_open(h, name_space);
124
125 h << "\n class Database;\n";
126 h << " const Database &get_embedded_" << identifier << "();\n";
127
128 if (mode != Mode::base64)
129 {
130 h << " size_t get_embedded_" << identifier << "_size();\n";
131 h << " char const *get_embedded_" << identifier << "_data();\n";
132 }
133
134 namespace_close(h, name_space);
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_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)
Definition base64.cpp:11
@ utf8
Definition json.h:12
JOEDB_FILE File
Definition File.h:25