Joedb 10.0.1
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 ////////////////////////////////////////////////////////////////////////////
81 Buffered_File *File_Parser::parse(std::ostream &out, Arguments &arguments)
82 ////////////////////////////////////////////////////////////////////////////
83 {
84 if (arguments.peek("memory"))
85 file.reset(new Memory_File());
86 else if (arguments.peek("server"))
87 file.reset();
88 else if (arguments.peek("interpreted"))
89 {
90 bool readonly = false;
91
92 if (default_only)
93 readonly = default_open_mode == Open_Mode::read_existing;
94 else if (arguments.peek("--read"))
95 readonly = true;
96
97 const std::string_view file_name = arguments.get_next();
98 if (arguments.missing())
99 return nullptr;
100
101 out << "Opening interpreted file... ";
102 out.flush();
103
104 if (readonly)
105 file.reset(new Readonly_Interpreted_File(file_name.data()));
106 else
107 file.reset(new Interpreted_File(file_name.data()));
108
109 out << "OK\n";
110 }
111#ifdef JOEDB_HAS_SSH
112 else if (arguments.peek("sftp"))
113 {
114 std::string_view port_string;
115 if (arguments.peek("--port"))
116 port_string = arguments.get_next();
117
118 std::string_view verbosity_string;
119 if (arguments.peek("--verbosity"))
120 verbosity_string = arguments.get_next();
121
122 const std::string_view user = arguments.get_next();
123 const std::string_view host = arguments.get_next();
124 const std::string_view file_name = arguments.get_next();
125
126 if (arguments.missing())
127 return nullptr;
128
129 unsigned port = 22;
130 if (port_string.data())
131 port = uint16_t(std::atoi(port_string.data()));
132
133 int verbosity = 0;
134 if (verbosity_string.data())
135 verbosity = std::atoi(verbosity_string.data());
136
137 out << "Creating ssh Session... ";
138 out.flush();
139 ssh_session.emplace(user.data(), host.data(), port, verbosity);
140
141 out << "OK\nInitializing sftp... ";
142 out.flush();
143 sftp.emplace(*ssh_session);
144
145 out << "OK\nOpening file... ";
146 out.flush();
147 file.reset(new SFTP_File(*sftp, file_name.data()));
148
149 out << "OK\n";
150 }
151#endif
152#ifdef JOEDB_HAS_CURL
153 else if (arguments.peek("curl"))
154 {
155 const bool verbose = arguments.peek("--verbose");
156 const std::string_view url = arguments.get_next();
157
158 if (arguments.missing())
159 return nullptr;
160
161 file.reset(new CURL_File(url.data(), verbose));
162 }
163#endif
164#ifdef JOEDB_HAS_BROTLI
165 else if (arguments.peek("brotli"))
166 {
167 bool readonly = false;
168
169 if (default_only)
170 readonly = default_open_mode == Open_Mode::read_existing;
171 else if (arguments.peek("--read"))
172 readonly = true;
173
174 const std::string_view file_name = arguments.get_next();
175
176 if (arguments.missing())
177 return nullptr;
178
179 out << "Opening brotli file... ";
180 out.flush();
181
182 if (readonly)
183 file.reset(new Readonly_Brotli_File(file_name.data()));
184 else
185 file.reset(new Brotli_File(file_name.data()));
186
187 out << "OK\n";
188 }
189#endif
190 else
191 {
192 arguments.peek("file");
193
194 Open_Mode open_mode = default_open_mode;
195
196 if (!default_only)
197 {
198 for (size_t i = 0; i < open_modes; i++)
199 {
200 const Open_Mode mode = Open_Mode(i);
201 if (!include_shared && mode == Open_Mode::shared_write)
202 continue;
203 const std::string option = std::string("--") + open_mode_strings[i];
204 if (arguments.peek(option.data()))
205 open_mode = mode;
206 }
207 }
208
209 const std::string_view file_name = arguments.get_next();
210
211 if (arguments.missing())
212 return nullptr;
213
214 out << "Opening local file (open_mode = ";
215 out << open_mode_strings[size_t(open_mode)] << ") ... ";
216 out.flush();
217 file.reset(new File(file_name.data(), open_mode));
218 }
219
220 return file.get();
221 }
222}
Class for conveniently parsing command-line arguments.
Definition Arguments.h:19
bool peek(const char *s)
bool missing() const
Definition Arguments.h:124
std::string_view get_next()
Buffered_File * parse(std::ostream &out, Arguments &arguments)
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