Joedb 9.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
File_Parser.cpp
Go to the documentation of this file.
6
7#ifdef JOEDB_HAS_SSH
9#endif
10
11#ifdef JOEDB_HAS_CURL
13#endif
14
15#ifdef JOEDB_HAS_BROTLI
18#endif
19
20#include <cstring>
21#include <iostream>
22
23namespace joedb
24{
25 static constexpr size_t open_modes = open_mode_strings.size() -
26 (File::lockable ? 0 : 2);
27
28 ////////////////////////////////////////////////////////////////////////////
29 void File_Parser::print_help(std::ostream &out) const
30 ////////////////////////////////////////////////////////////////////////////
31 {
32 out << "<file> is one of:\n";
33
34 if (default_only)
35 {
36 out << " [file] <file_name>\n";
37 }
38 else
39 {
40 out << " [file] [--<open_mode>] <file_name>\n";
41 out << " <open_mode> is one of:\n";
42
43 for (size_t i = 0; i < open_modes; i++)
44 {
45 const Open_Mode mode = Open_Mode(i);
46 if (!include_shared && mode == Open_Mode::shared_write)
47 continue;
48 out << " " << open_mode_strings[i];
49 if (mode == default_open_mode)
50 out << '*';
51 out << '\n';
52 }
53
54 out << " memory\n";
55 }
56
57 out << " interpreted [--read] <file_name>\n";
58
59#ifdef JOEDB_HAS_SSH
60 out << " sftp [--port p] [--verbosity v] <user> <host> <file_name>\n";
61#endif
62
63#ifdef JOEDB_HAS_CURL
64 out << " curl [--verbose] <URL>\n";
65#endif
66
67#ifdef JOEDB_HAS_BROTLI
68 out << " brotli ";
69 if (!default_only)
70 out << "[--read] ";
71 out << "<file_name>\n";
72#endif
73
74#if defined(JOEDB_HAS_NETWORKING) || defined(JOEDB_HAS_SSH)
75 if (include_server)
76 out << " server (client must use a connection to a server)\n";
77#endif
78 }
79
80 ////////////////////////////////////////////////////////////////////////////
82 ////////////////////////////////////////////////////////////////////////////
83 (
84 std::ostream &out,
85 const int argc,
86 const char * const * const argv,
87 int &arg_index
88 )
89 {
90 if (arg_index < argc && std::strcmp(argv[arg_index], "memory") == 0)
91 {
92 file.reset(new Memory_File());
93 arg_index++;
94 }
95 else if (arg_index < argc && std::strcmp(argv[arg_index], "server") == 0)
96 {
97 arg_index++;
98 }
99 else if (arg_index + 1 < argc && std::strcmp(argv[arg_index], "interpreted") == 0)
100 {
101 arg_index++;
102
103 bool readonly;
104
105 if (default_only)
106 {
107 readonly = default_open_mode == Open_Mode::read_existing;
108 }
109 else if (arg_index < argc + 1 && std::strcmp(argv[arg_index], "--read") == 0)
110 {
111 readonly = true;
112 arg_index++;
113 }
114 else
115 readonly = false;
116
117 const char * const file_name = argv[arg_index];
118 arg_index++;
119
120 out << "Opening interpreted file... ";
121 out.flush();
122
123 if (readonly)
124 file.reset(new Readonly_Interpreted_File(file_name));
125 else
126 file.reset(new Interpreted_File(file_name));
127
128 out << "OK\n";
129 }
130#ifdef JOEDB_HAS_SSH
131 else if (arg_index + 3 < argc && std::strcmp(argv[arg_index], "sftp") == 0)
132 {
133 arg_index++;
134
135 unsigned port = 22;
136 if (arg_index + 4 < argc && std::strcmp(argv[arg_index], "--port") == 0)
137 {
138 arg_index++;
139 port = uint16_t(std::atoi(argv[arg_index++]));
140 }
141
142 int verbosity = 0;
143 if (arg_index + 4 < argc && std::strcmp(argv[arg_index], "--verbosity") == 0)
144 {
145 arg_index++;
146 verbosity = std::atoi(argv[arg_index++]);
147 }
148
149 const char * const user = argv[arg_index++];
150 const char * const host = argv[arg_index++];
151
152 out << "Creating ssh Session... ";
153 out.flush();
154
155 ssh_session.emplace(user, host, port, verbosity);
156
157 out << "OK\n";
158
159 out << "Initializing sftp... ";
160 out.flush();
161
162 sftp.emplace(*ssh_session);
163
164 out << "OK\n";
165
166 const char * const file_name = argv[arg_index++];
167
168 out << "Opening file... ";
169
170 file.reset(new SFTP_File(*sftp, file_name));
171
172 out << "OK\n";
173 }
174#endif
175#ifdef JOEDB_HAS_CURL
176 else if (arg_index < argc && std::strcmp(argv[arg_index], "curl") == 0)
177 {
178 arg_index++;
179
180 bool verbose = false;
181 if (arg_index < argc && std::strcmp(argv[arg_index], "--verbose") == 0)
182 {
183 verbose = true;
184 arg_index++;
185 }
186
187 const char * url = nullptr;
188
189 if (arg_index < argc)
190 {
191 url = argv[arg_index];
192 arg_index++;
193 }
194
195 if (url && *url)
196 {
197 file.reset(new CURL_File(url, verbose));
198 out << "OK\n";
199 }
200 else
201 throw Exception("missing URL");
202 }
203#endif
204#ifdef JOEDB_HAS_BROTLI
205 else if (arg_index < argc + 1 && std::strcmp(argv[arg_index], "brotli") == 0)
206 {
207 arg_index++;
208
209 bool readonly;
210
211 if (default_only)
212 {
213 readonly = default_open_mode == Open_Mode::read_existing;
214 }
215 else if (arg_index < argc + 1 && std::strcmp(argv[arg_index], "--read") == 0)
216 {
217 readonly = true;
218 arg_index++;
219 }
220 else
221 readonly = false;
222
223 const char * const file_name = argv[arg_index];
224 arg_index++;
225
226 out << "Opening brotli file... ";
227 out.flush();
228
229 if (readonly)
230 file.reset(new Readonly_Brotli_File(file_name));
231 else
232 file.reset(new Brotli_File(file_name));
233
234 out << "OK\n";
235 }
236#endif
237 else
238 {
239 if (arg_index < argc && std::strcmp(argv[arg_index], "file") == 0)
240 arg_index++;
241
242 Open_Mode open_mode = default_open_mode;
243
244 if (arg_index < argc && !default_only)
245 {
246 for (size_t i = 0; i < open_modes; i++)
247 {
248 const Open_Mode mode = Open_Mode(i);
249 if (!include_shared && mode == Open_Mode::shared_write)
250 continue;
251 const std::string option = std::string("--") + open_mode_strings[i];
252 if (option == argv[arg_index])
253 {
254 open_mode = mode;
255 arg_index++;
256 }
257 }
258 }
259
260 const char *file_name = nullptr;
261 if (arg_index < argc)
262 {
263 file_name = argv[arg_index];
264 arg_index++;
265 }
266
267 out << "Opening local file (open_mode = ";
268 out << open_mode_strings[size_t(open_mode)] << ") ... ";
269 out.flush();
270
271 if (file_name && *file_name)
272 {
273 file.reset(new File(file_name, open_mode));
274 out << "OK\n";
275 }
276 else
277 throw Exception("missing file name");
278 }
279
280 return file.get();
281 }
282}
Buffered_File * parse(std::ostream &out, int argc, const char *const *argv, int &arg_index)
void print_help(std::ostream &out) const
Open_Mode
Definition Open_Mode.h:8
@ shared_write
like write_existing_or_create_new, but does not lock the file, and does not fail if locked
@ read_existing
fails if does not exist
const std::array< const char *, size_t(Open_Mode::mode_count)> open_mode_strings
Definition Blob.h:7
JOEDB_FILE File
Definition File.h:25