Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Implementation of plugin spec
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include <pluginspec.hpp>
11 :
12 : #include <toolexcept.hpp>
13 :
14 : #include <iostream>
15 :
16 : using namespace std;
17 :
18 : namespace kdb
19 : {
20 :
21 : namespace tools
22 : {
23 :
24 : /**
25 : * @brief Construct a plugin spec with a given module name
26 : *
27 : * @param pluginName the fullname (modulename # refname)
28 : * @param pluginConfig the plugins config
29 : *
30 : * @throw BadPluginName if name not a-z
31 : *
32 : * @see setFullName()
33 : */
34 80435 : PluginSpec::PluginSpec (std::string pluginName, KeySet pluginConfig) : name (pluginName), refname (pluginName), config (pluginConfig)
35 : {
36 20045 : setFullName (pluginName);
37 19994 : }
38 :
39 : /**
40 : * @brief Construct a plugin spec with a given module and ref name
41 : *
42 : * @param pluginName the module this plugin is instantiated from
43 : * @param refName for uniqueness for more plugins from one module
44 : * @param pluginConfig the plugins config
45 : *
46 : * @throw BadPluginName if name not a-z
47 : *
48 : * @see setName()
49 : * @see setRefName()
50 : */
51 156 : PluginSpec::PluginSpec (std::string pluginName, std::string refName, KeySet pluginConfig)
52 624 : : name (pluginName), refname (refName), config (pluginConfig)
53 : {
54 156 : validate (pluginName);
55 156 : validate (refname);
56 156 : }
57 :
58 : /**
59 : * @brief Construct a plugin spec with a given module name and ref number
60 : *
61 : * @param pluginName the module this plugin is instantiated from
62 : * @param refNumber for uniqueness for more plugins from one module
63 : * @param pluginConfig the plugins config
64 : *
65 : * @throw BadPluginName if name not a-z
66 : *
67 : * @see setName()
68 : * @see setRefName()
69 : */
70 18 : PluginSpec::PluginSpec (std::string pluginName, size_t refNumber, KeySet pluginConfig)
71 72 : : name (pluginName), refname (), config (pluginConfig)
72 : {
73 18 : validate (pluginName);
74 18 : setRefNumber (refNumber);
75 18 : }
76 :
77 :
78 : /**
79 : * @note if no reference name is given its equal to the module name
80 : *
81 : * @return the module name, then #, and then the reference name
82 : */
83 7651 : std::string PluginSpec::getFullName () const
84 : {
85 15302 : return name + "#" + refname;
86 : }
87 :
88 : /**
89 : * @return the reference name
90 : */
91 9619 : std::string PluginSpec::getRefName () const
92 : {
93 19238 : return refname;
94 : }
95 :
96 : /**
97 : * @brief Checks if reference name contains only numbers
98 : *
99 : * @retval true if only numbers
100 : * @retval true if a refname was given
101 : */
102 832 : bool PluginSpec::isRefNumber () const
103 : {
104 1664 : auto it = refname.find_first_not_of ("0123456789");
105 832 : return it == std::string::npos;
106 : }
107 :
108 : /**
109 : * @return the module name
110 : */
111 243543 : std::string PluginSpec::getName () const
112 : {
113 487086 : return name;
114 : }
115 :
116 : /**
117 : * @return the config
118 : */
119 32518 : KeySet PluginSpec::getConfig () const
120 : {
121 65036 : return config;
122 : }
123 :
124 : /**
125 : * @brief Set the full name with # or only the name
126 : *
127 : * @throw BadPluginName if name not a-z (no change then)
128 : *
129 : * @param n the string to set
130 : */
131 20051 : void PluginSpec::setFullName (std::string const & n)
132 : {
133 20051 : auto it = n.find ('#');
134 20051 : if (it != std::string::npos)
135 : {
136 440 : std::string nn = n.substr (0, it);
137 440 : std::string rn = n.substr (it + 1);
138 220 : validate (nn);
139 204 : validate (rn);
140 356 : name = nn;
141 356 : refname = rn;
142 : }
143 : else
144 : {
145 19831 : setName (n);
146 : }
147 19997 : }
148 :
149 : /**
150 : * @brief Set the reference name of the plugin
151 : *
152 : * @throw BadPluginName if name not a-z (no change then)
153 : *
154 : * Makes different plugins based from the same module
155 : * unique.
156 : *
157 : * @param n the string to set
158 : */
159 4216 : void PluginSpec::setRefName (std::string const & n)
160 : {
161 4216 : validate (n);
162 8428 : refname = n;
163 4214 : }
164 :
165 : /**
166 : * @brief Set a number for automatic references during parsing
167 : *
168 : * @param refnumber the number to set
169 : */
170 444 : void PluginSpec::setRefNumber (size_t refnumber)
171 : {
172 888 : refname = to_string (refnumber);
173 444 : }
174 :
175 : /**
176 : * @brief Set the module name of the plugin
177 : *
178 : * @throw BadPluginName if name not a-z (no change then)
179 : *
180 : * @param n the string to set
181 : */
182 23756 : void PluginSpec::setName (std::string const & n)
183 : {
184 23756 : validate (n);
185 47480 : name = n;
186 23740 : }
187 :
188 : /**
189 : * @brief Append to config
190 : *
191 : * @param c config to append
192 : */
193 616 : void PluginSpec::appendConfig (KeySet c)
194 : {
195 1232 : config.append (c);
196 616 : }
197 :
198 : /**
199 : * @brief Set plugin config
200 : *
201 : * @param c new config to be used as plugin config
202 : */
203 22 : void PluginSpec::setConfig (KeySet c)
204 : {
205 44 : config.clear ();
206 44 : config.append (c);
207 22 : }
208 :
209 : /**
210 : * @brief Check if str starts with a-z and then only has chars a-z, 0-9 or underscore (_)
211 : *
212 : * @param n the string to check
213 : */
214 28726 : void PluginSpec::validate (std::string const & n) const
215 : {
216 28756 : if (n.empty ()) throw BadPluginName ("<empty>");
217 28716 : auto begin = n.find_first_of ("abcdefghijklmnopqrstuvwxyz");
218 28716 : if (begin != 0)
219 : {
220 : // must start a-z
221 120 : throw BadPluginName (n);
222 : }
223 :
224 28676 : auto it = n.find_first_not_of ("abcdefghijklmnopqrstuvwxyz0123456789_");
225 28676 : if (it != std::string::npos)
226 : {
227 30 : throw BadPluginName (n);
228 : }
229 28666 : }
230 :
231 :
232 : /**
233 : * @brief Compare two pluginspec if their value is equal
234 : * @note the content of getConfig() will be only compared with keynames, not content!
235 : */
236 468 : bool operator== (PluginSpec const & self, PluginSpec const & other)
237 : {
238 3236 : return self.getName () == other.getName () && self.getRefName () == other.getRefName () && self.getConfig () == other.getConfig ();
239 : }
240 :
241 : /**
242 : * @brief Compare two pluginspec if their value is not equal
243 : * @note the content of getConfig() will be only compared with keynames, not content!
244 : */
245 20 : bool operator!= (PluginSpec const & self, PluginSpec const & other)
246 : {
247 20 : return !(self == other);
248 : }
249 :
250 : /**
251 : * @brief Output the name, refname and size of config
252 : */
253 0 : std::ostream & operator<< (std::ostream & os, PluginSpec const & spec)
254 : {
255 0 : os << "name: " << spec.getName () << " refname: " << spec.getRefName () << " configsize: " << spec.getConfig ().size ();
256 0 : return os;
257 : }
258 : } // namespace tools
259 7272 : } // namespace kdb
|