Joedb 9.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Connection_Parser.cpp
Go to the documentation of this file.
6
7#ifdef JOEDB_HAS_NETWORKING
9#endif
10
11#ifdef JOEDB_HAS_SSH
13#endif
14
15#include <cstring>
16#include <sstream>
17
18namespace joedb
19{
20 //////////////////////////////////////////////////////////////////////////
22 //////////////////////////////////////////////////////////////////////////
23 {
24 builders.emplace_back(new Dummy_Connection_Builder());
25 builders.emplace_back(new File_Connection_Builder());
26
27#ifdef JOEDB_HAS_NETWORKING
28 builders.emplace_back(new Local_Connection_Builder());
29#endif
30
31#ifdef JOEDB_HAS_SSH
32 builders.emplace_back(new SSH_Connection_Builder());
33#endif
34 }
35
36 //////////////////////////////////////////////////////////////////////////
37 void Connection_Parser::print_help(std::ostream &out) const
38 //////////////////////////////////////////////////////////////////////////
39 {
40 out << "\n<connection> is one of:\n";
41 for (size_t i = 0; i < builders.size(); i++)
42 {
43 out << ' ';
44
45 if (i == 0)
46 out << '[';
47
48 out<< builders[i]->get_name();
49
50 if (i == 0)
51 out << "] (default)";
52
53 out << ' ' << builders[i]->get_parameters_description();
54 out << '\n';
55 }
56 }
57
58 //////////////////////////////////////////////////////////////////////////
59 Connection_Builder &Connection_Parser::get_builder(const char *name) const
60 //////////////////////////////////////////////////////////////////////////
61 {
62 for (const auto &b: builders)
63 {
64 if (std::strcmp(b->get_name(), name) == 0)
65 return *b;
66 }
67
68 std::ostringstream message;
69 message << "Unknown connection type: " << name << '\n';
70 print_help(message);
71 throw Exception(message.str());
72 }
73
74 //////////////////////////////////////////////////////////////////////////
75 Connection &Connection_Parser::build
76 //////////////////////////////////////////////////////////////////////////
77 (
78 Connection_Builder &builder,
79 const int argc,
80 const char * const * const argv,
81 Buffered_File *file
82 )
83 {
84 if
85 (
86 argc < builder.get_min_parameters() ||
87 argc > builder.get_max_parameters()
88 )
89 {
90 const char * description = builder.get_parameters_description();
91 if (!*description)
92 description = "no parameters";
93 throw Exception
94 (
95 std::string("Wrong number of connection arguments. Expected: ") +
96 std::string(description)
97 );
98 }
99
100 return builder.build(argc, argv, file);
101 }
102
103 //////////////////////////////////////////////////////////////////////////
104 Connection &Connection_Parser::build
105 //////////////////////////////////////////////////////////////////////////
106 (
107 const int argc,
108 const char * const * argv,
109 Buffered_File *file
110 ) const
111 {
112 const char * connection_name;
113 int arg_index = 0;
114
115 if (argc <= 0)
116 connection_name = builders[0]->get_name();
117 else
118 connection_name = argv[arg_index++];
119
120 std::cerr << "Creating connection (" << connection_name << ") ... ";
121
122 Connection &result = build
123 (
124 get_builder(connection_name),
125 argc - arg_index,
126 argv + arg_index,
127 file
128 );
129
130 std::cerr << "OK\n";
131
132 return result;
133 }
134}
void print_help(std::ostream &out) const
Definition Blob.h:7