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