Joedb 10.2.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 << " [interpreted] [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#ifdef JOEDB_HAS_SSH
58 out << " sftp [--port p] [--verbosity v] <user> <host> <path>\n";
59#endif
60
61#ifdef JOEDB_HAS_CURL
62 out << " curl [--verbose] <URL>\n";
63#endif
64
65#ifdef JOEDB_HAS_BROTLI
66 out << " brotli ";
67 if (!default_only)
68 out << "[--read] ";
69 out << "<file_name>\n";
70#endif
71
72#if defined(JOEDB_HAS_ASIO) || defined(JOEDB_HAS_SSH) || defined(JOEDB_HAS_WEBSOCKETS)
73 if (include_server)
74 out << " server (client must use a connection to a server)\n";
75#endif
76 }
77
78 ////////////////////////////////////////////////////////////////////////////
79 Abstract_File *File_Parser::parse(std::ostream &out, Arguments &arguments)
80 ////////////////////////////////////////////////////////////////////////////
81 {
82 if (arguments.peek("memory"))
83 file.reset(new Memory_File());
84 else if (arguments.peek("server"))
85 file.reset();
86#ifdef JOEDB_HAS_SSH
87 else if (arguments.peek("sftp"))
88 {
89 const auto port = arguments.next_option<unsigned>("port", "p", 22);
90 const auto verbosity = arguments.next_option<int>("verbosity", "v", 0);
91 const std::string_view user = arguments.get_next();
92 const std::string_view host = arguments.get_next();
93 const std::string_view path = arguments.get_next();
94
95 if (arguments.missing())
96 return nullptr;
97
98 out << "Creating ssh Session... ";
99 out.flush();
100 ssh_session.emplace(user.data(), host.data(), port, verbosity);
101
102 out << "OK\nInitializing sftp... ";
103 out.flush();
104 sftp.emplace(*ssh_session);
105
106 out << "OK\nOpening file... ";
107 out.flush();
108 file.reset(new SFTP_File(*sftp, path.data()));
109
110 out << "OK\n";
111 }
112#endif
113#ifdef JOEDB_HAS_CURL
114 else if (arguments.peek("curl"))
115 {
116 const bool verbose = arguments.peek("--verbose");
117 const std::string_view url = arguments.get_next();
118
119 if (arguments.missing())
120 return nullptr;
121
122 file.reset(new CURL_File(url.data(), verbose));
123 }
124#endif
125#ifdef JOEDB_HAS_BROTLI
126 else if (arguments.peek("brotli"))
127 {
128 bool readonly = false;
129
130 if (default_only)
131 readonly = default_open_mode == Open_Mode::read_existing;
132 else if (arguments.peek("--read"))
133 readonly = true;
134
135 const std::string_view file_name = arguments.get_next();
136
137 if (arguments.missing())
138 return nullptr;
139
140 out << "Opening brotli file... ";
141 out.flush();
142
143 if (readonly)
144 file.reset(new Readonly_Brotli_File(file_name.data()));
145 else
146 file.reset(new Brotli_File(file_name.data()));
147
148 out << "OK\n";
149 }
150#endif
151 else
152 {
153 const bool interpreted = arguments.peek("interpreted");
154 arguments.peek("file");
155
156 Open_Mode open_mode = default_open_mode;
157
158 if (!default_only)
159 {
160 for (size_t i = 0; i < open_modes; i++)
161 {
162 const Open_Mode mode = Open_Mode(i);
163 if (!include_shared && mode == Open_Mode::shared_write)
164 continue;
165 const std::string option = std::string("--") + open_mode_strings[i];
166 if (arguments.peek(option.data()))
167 open_mode = mode;
168 }
169 }
170
171 const std::string_view file_name = arguments.get_next();
172
173 if (arguments.missing())
174 return nullptr;
175
176 out << "Opening local file (open_mode = ";
177 out << open_mode_strings[size_t(open_mode)] << ") ... ";
178 out.flush();
179
180 if (interpreted)
181 file.reset(new Interpreted_File(file_name.data(), open_mode));
182 else
183 file.reset(new File(file_name.data(), open_mode));
184 }
185
186 return file.get();
187 }
188}
Class for conveniently parsing command-line arguments.
Definition Arguments.h:19
bool peek(const char *s)
bool missing() const
Definition Arguments.h:146
std::string_view get_next()
T next_option(const char *name, const char *description, T default_value)
Definition Arguments.h:117
Abstract_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
JOEDB_FILE File
Definition File.h:25