1 | // **************************************************************************
|
---|
2 | /** @class Converter
|
---|
3 |
|
---|
4 | @brief A compiler for the DIM data format string
|
---|
5 |
|
---|
6 | The Converter class interprets arguments in a string accoring to the
|
---|
7 | given format definition and produces a corresponding memory block from it
|
---|
8 | which can be attached to an event later.
|
---|
9 |
|
---|
10 | The format is given according to the Dim format description:
|
---|
11 |
|
---|
12 | The format parameter specifies the contents of the structure in the
|
---|
13 | form T:N[;T:N]*[;T] where T is the item type: (I)nteger, (C)haracter,
|
---|
14 | (L)ong, (S)hort, (F)loat, (D)ouble, X(tra long) and N is the
|
---|
15 | number of such items. The type alone at the end means all following items
|
---|
16 | are of the same type. Example: "I:3;F:2;C" means 3 Integers, 2 Floats and
|
---|
17 | characters until the end. The format parameter is used for
|
---|
18 | communicating between different platforms.
|
---|
19 |
|
---|
20 | Note, that the strange notation T:N[;T:N]*[;T] is meant to be a regular
|
---|
21 | expression. An Xtra-long is a 'long long'.
|
---|
22 |
|
---|
23 | Since Dim itself never really interpretes the format string, the programmer
|
---|
24 | is responsible to make sure that the delivered data and the interpretation
|
---|
25 | is consistent. Therefore the provided class can be of some help.
|
---|
26 |
|
---|
27 | For example:
|
---|
28 |
|
---|
29 | \code
|
---|
30 | Converter c(cout, "I:1;F:2;I:2", );
|
---|
31 | vector<char> v = c.GetVector("COMMAND 1 2.5 4.2 3 4");
|
---|
32 | \endcode
|
---|
33 |
|
---|
34 | would produce a 20 byte data block with the integers 1, the floats
|
---|
35 | 2.5 and 4.2, and the intergers 3 and 4, in this order.
|
---|
36 |
|
---|
37 | The opposite direction is also possible
|
---|
38 |
|
---|
39 | \code
|
---|
40 | Converter c(cout, "I:1;F:2;I:2");
|
---|
41 | cout << c.GetString(pointer, size) << endl;
|
---|
42 | \endcode
|
---|
43 |
|
---|
44 | Other conversion functions also exist.
|
---|
45 |
|
---|
46 | To check if the compilation of the format string was successfull
|
---|
47 | the valid() member functio is provided.
|
---|
48 |
|
---|
49 | The format parameter \b W(ord) is dedicated to this kind of conversion and
|
---|
50 | not understood by Dim. In addition there are \b O(ptions) which are like
|
---|
51 | Words but can be omitted. They should only be used at the end of the string.
|
---|
52 | Both can be encapsulated in quotationmarks '"'. Nested quotationmarks
|
---|
53 | are not supported. \b B(ool) is also special. It evaluates true/false,
|
---|
54 | yes/no, on/off, 1/0.
|
---|
55 |
|
---|
56 | The non-DIM like format options can be switched on and off by using the
|
---|
57 | strict argument in the constructor. In general DimCommands can use these
|
---|
58 | options, but DimServices not.
|
---|
59 |
|
---|
60 | @remark Note that all values are interpreted as signed, except the single
|
---|
61 | char (e.g. C:5)
|
---|
62 |
|
---|
63 | */
|
---|
64 | // **************************************************************************
|
---|
65 | #include "Converter.h"
|
---|
66 |
|
---|
67 | #include <iostream>
|
---|
68 | #include <iomanip>
|
---|
69 | #include <sstream>
|
---|
70 |
|
---|
71 | #include <cctype> // std::tolower
|
---|
72 | #include <algorithm> // std::transform
|
---|
73 |
|
---|
74 | #include <boost/regex.hpp>
|
---|
75 |
|
---|
76 | #include "Readline.h"
|
---|
77 | #include "WindowLog.h"
|
---|
78 |
|
---|
79 | using namespace std;
|
---|
80 |
|
---|
81 | // --------------------------------------------------------------------------
|
---|
82 | //
|
---|
83 | //! This function is supposed to remove all whitespaces from the format
|
---|
84 | //! string to allow easier regular expressions later.
|
---|
85 | //!
|
---|
86 | //! @param s
|
---|
87 | //! string to be cleaned
|
---|
88 | //!
|
---|
89 | //! @returns
|
---|
90 | //! string cleaned from whitespaces
|
---|
91 | //
|
---|
92 | std::string Converter::Clean(std::string s)
|
---|
93 | {
|
---|
94 | while (1)
|
---|
95 | {
|
---|
96 | const size_t pos = s.find_last_of(' ');
|
---|
97 | if (pos==string::npos)
|
---|
98 | break;
|
---|
99 | s.erase(pos, pos+1);
|
---|
100 | }
|
---|
101 |
|
---|
102 | return s;
|
---|
103 | }
|
---|
104 |
|
---|
105 | // --------------------------------------------------------------------------
|
---|
106 | //
|
---|
107 | //! This is just a simplification. For the time being it is used to output
|
---|
108 | //! the interpreted contents to the logging stream. Its main purpose
|
---|
109 | //! is to add the contents of val in a binary representation to the
|
---|
110 | //! vector v
|
---|
111 | //!
|
---|
112 | //! @tparam
|
---|
113 | //! data type of the variable which should be added
|
---|
114 | //!
|
---|
115 | //! @param val
|
---|
116 | //! reference to the data
|
---|
117 | //!
|
---|
118 | //! @param v
|
---|
119 | //! vector<char> to which the binary copy should be added
|
---|
120 | //!
|
---|
121 | template <class T>
|
---|
122 | void Converter::GetBinImp(std::vector<char> &v, const T &val) const
|
---|
123 | {
|
---|
124 | wout << " (" << val << ")";
|
---|
125 |
|
---|
126 | v.insert(v.end(),
|
---|
127 | reinterpret_cast<const char*>(&val),
|
---|
128 | reinterpret_cast<const char*>(&val+1));
|
---|
129 | }
|
---|
130 |
|
---|
131 | // --------------------------------------------------------------------------
|
---|
132 | //
|
---|
133 | //! This is just a simplification. For the time being it is used to output
|
---|
134 | //! the interpreted contents to the logging stream. Its main purpose
|
---|
135 | //! is to add the contents of val as a boost::any object to the
|
---|
136 | //! vector v
|
---|
137 | //!
|
---|
138 | //! @tparam
|
---|
139 | //! data type of the variable which should be added
|
---|
140 | //!
|
---|
141 | //! @param val
|
---|
142 | //! reference to the data
|
---|
143 | //!
|
---|
144 | //! @param v
|
---|
145 | //! vector<boost::any> to which the value should be added
|
---|
146 | //!
|
---|
147 | template <class T>
|
---|
148 | void Converter::GetBinImp(std::vector<boost::any> &v, const T &val) const
|
---|
149 | {
|
---|
150 | wout << " (" << val << ")";
|
---|
151 |
|
---|
152 | v.push_back(val);
|
---|
153 | }
|
---|
154 |
|
---|
155 | // --------------------------------------------------------------------------
|
---|
156 | //
|
---|
157 | //! This is just a simplification. For the time being it is used to output
|
---|
158 | //! the interpreted contents to the logging stream. Its main purpose
|
---|
159 | //! is to add the contents of the provided string at the end of the vector v.
|
---|
160 | //! vector v
|
---|
161 | //!
|
---|
162 | //! @param val
|
---|
163 | //! reference to the string
|
---|
164 | //!
|
---|
165 | //! @param v
|
---|
166 | //! vector<char> to which the value should be added
|
---|
167 | //!
|
---|
168 | void Converter::GetBinString(std::vector<char> &v, const string &val) const
|
---|
169 | {
|
---|
170 | wout << " (" << val << ")";
|
---|
171 |
|
---|
172 | v.insert(v.end(), val.begin(), val.end()+1);
|
---|
173 | }
|
---|
174 |
|
---|
175 | // --------------------------------------------------------------------------
|
---|
176 | //
|
---|
177 | //! This is just a simplification. For the time being it is used to output
|
---|
178 | //! the interpreted contents to the logging stream. Its main purpose
|
---|
179 | //! is to add the contents of the provided string at the end of the vector v.
|
---|
180 | //! vector v
|
---|
181 | //!
|
---|
182 | //! @param val
|
---|
183 | //! reference to the string
|
---|
184 | //!
|
---|
185 | //! @param v
|
---|
186 | //! vector<boost::any> to which the value should be added
|
---|
187 | //!
|
---|
188 | void Converter::GetBinString(std::vector<boost::any> &v, const string &val) const
|
---|
189 | {
|
---|
190 | wout << " (" << val << ")";
|
---|
191 |
|
---|
192 | v.push_back(val);
|
---|
193 | v.push_back('\n');
|
---|
194 | }
|
---|
195 |
|
---|
196 | // --------------------------------------------------------------------------
|
---|
197 | //
|
---|
198 | //! Converts from the stringstream into the provided type.
|
---|
199 | //!
|
---|
200 | //! @param line
|
---|
201 | //! reference to the stringstream from which the data should be
|
---|
202 | //! interpreted
|
---|
203 | //!
|
---|
204 | //! @tparam
|
---|
205 | //! Type of the data to be returned
|
---|
206 | //!
|
---|
207 | //! @returns
|
---|
208 | //! The interpreted data
|
---|
209 | //!
|
---|
210 | template <class T>
|
---|
211 | T Converter::Get(std::stringstream &line) const
|
---|
212 | {
|
---|
213 | char c;
|
---|
214 | line >> c;
|
---|
215 |
|
---|
216 | if (c=='0')
|
---|
217 | {
|
---|
218 | if (line.peek()==-1)
|
---|
219 | {
|
---|
220 | line.clear(ios::eofbit);
|
---|
221 | return 0;
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (line.peek()=='x')
|
---|
225 | {
|
---|
226 | line >> c;
|
---|
227 | line >> hex;
|
---|
228 | }
|
---|
229 | else
|
---|
230 | {
|
---|
231 | line.unget();
|
---|
232 | line >> oct;
|
---|
233 | }
|
---|
234 | }
|
---|
235 | else
|
---|
236 | {
|
---|
237 | line.unget();
|
---|
238 | line >> dec;
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | T val;
|
---|
243 | line >> val;
|
---|
244 | return val;
|
---|
245 | }
|
---|
246 |
|
---|
247 | // --------------------------------------------------------------------------
|
---|
248 | //
|
---|
249 | //! Converts from the stringstream into bool. It allows to use lexical
|
---|
250 | //! boolean representations like yes/no, on/off, true/false and of
|
---|
251 | //! course 0/1. If the conversion fails the failbit is set.
|
---|
252 | //!
|
---|
253 | //! @param line
|
---|
254 | //! reference to the stringstream from which the data should be
|
---|
255 | //! interpreted
|
---|
256 | //!
|
---|
257 | //! @returns
|
---|
258 | //! The boolean. 0 in case of failure
|
---|
259 | //!
|
---|
260 | bool Converter::GetBool(std::stringstream &line) const
|
---|
261 | {
|
---|
262 | string buf;
|
---|
263 | line >> buf;
|
---|
264 | transform(buf.begin(), buf.end(), buf.begin(), (int(*)(int)) std::tolower);
|
---|
265 |
|
---|
266 | if (buf=="yes" || buf=="true" || buf=="on" || buf=="1")
|
---|
267 | return true;
|
---|
268 |
|
---|
269 | if (buf=="no" || buf=="false" || buf=="off" || buf=="0")
|
---|
270 | return false;
|
---|
271 |
|
---|
272 | line.clear(ios::failbit);
|
---|
273 |
|
---|
274 | return false;
|
---|
275 | }
|
---|
276 |
|
---|
277 | // --------------------------------------------------------------------------
|
---|
278 | //
|
---|
279 | //! Converts from the stringstream into a string. Leading whitespaces are
|
---|
280 | //! skipped. Everything up to the next whitespace is returned.
|
---|
281 | //! strings can be encapsulated into escape characters ("). Note, that
|
---|
282 | //! they cannot be nested.
|
---|
283 | //!
|
---|
284 | //! @param line
|
---|
285 | //! reference to the stringstream from which the data should be
|
---|
286 | //! interpreted
|
---|
287 | //!
|
---|
288 | //! @returns
|
---|
289 | //! The string
|
---|
290 | //!
|
---|
291 | string Converter::GetString(std::stringstream &line) const
|
---|
292 | {
|
---|
293 | while (line.peek()==' ')
|
---|
294 | line.get();
|
---|
295 |
|
---|
296 | string buf;
|
---|
297 | if (line.peek()=='\"')
|
---|
298 | {
|
---|
299 | line.get();
|
---|
300 | getline(line, buf, '\"');
|
---|
301 | if (line.peek()==-1)
|
---|
302 | line.clear(ios::eofbit);
|
---|
303 | }
|
---|
304 | else
|
---|
305 | line >> buf;
|
---|
306 |
|
---|
307 | return buf;
|
---|
308 | }
|
---|
309 |
|
---|
310 | // --------------------------------------------------------------------------
|
---|
311 | //
|
---|
312 | //! Converts from the stringstream into a string. Leading whitespaces are
|
---|
313 | //! skipped. Everything until the end-of-line is returned. A trailing
|
---|
314 | //! \0 is added.
|
---|
315 | //!
|
---|
316 | //! @param line
|
---|
317 | //! reference to the stringstream from which the data should be
|
---|
318 | //! interpreted
|
---|
319 | //!
|
---|
320 | //! @returns
|
---|
321 | //! The string
|
---|
322 | //!
|
---|
323 | string Converter::GetStringEol(stringstream &line) const
|
---|
324 | {
|
---|
325 | // Remove leading whitespaces
|
---|
326 | while (line.peek()==' ')
|
---|
327 | line.get();
|
---|
328 |
|
---|
329 | line >> noskipws;
|
---|
330 |
|
---|
331 | const istream_iterator<char> eol; // end-of-line iterator
|
---|
332 | const string s(istream_iterator<char>(line), eol);
|
---|
333 | return s + '\0';
|
---|
334 | }
|
---|
335 |
|
---|
336 | // --------------------------------------------------------------------------
|
---|
337 | //
|
---|
338 | //! Converts from a binary block into a string. The type of the expected
|
---|
339 | //! value is defined by the template parameter.
|
---|
340 | //!
|
---|
341 | //! @param ptr
|
---|
342 | //! A refrenece to the pointer of the binary representation to be
|
---|
343 | //! interpreted. The pointer is incremented by the sizeof the type.
|
---|
344 | //!
|
---|
345 | //! @tparam T
|
---|
346 | //! Expected type
|
---|
347 | //!
|
---|
348 | //! @returns
|
---|
349 | //! The string
|
---|
350 | //!
|
---|
351 | template<class T>
|
---|
352 | string Converter::GetString(const char* &ptr) const
|
---|
353 | {
|
---|
354 | const T &t = *reinterpret_cast<const T*>(ptr);
|
---|
355 |
|
---|
356 | ostringstream stream;
|
---|
357 | stream << t;
|
---|
358 | ptr += sizeof(T);
|
---|
359 |
|
---|
360 | return stream.str();
|
---|
361 | }
|
---|
362 |
|
---|
363 | // --------------------------------------------------------------------------
|
---|
364 | //
|
---|
365 | //! Convert the pointer using GetString into a string and add it (prefixed
|
---|
366 | //! by a whaitespace) to the given string.
|
---|
367 | //!
|
---|
368 | //! @param str
|
---|
369 | //! Reference to the string to which the ptr should be added
|
---|
370 | //!
|
---|
371 | //! @param ptr
|
---|
372 | //! Pointer to the binary representation. It will be incremented
|
---|
373 | //! according to the sze of the template argument
|
---|
374 | //!
|
---|
375 | //! @tparam T
|
---|
376 | //! Type as which the binary data should be interpreted
|
---|
377 | //!
|
---|
378 | template<class T>
|
---|
379 | void Converter::Add(string &str, const char* &ptr) const
|
---|
380 | {
|
---|
381 | str += ' ' + GetString<T>(ptr);
|
---|
382 | }
|
---|
383 |
|
---|
384 | // --------------------------------------------------------------------------
|
---|
385 | //
|
---|
386 | //! Convert the pointer into a boost::any object and add it to the
|
---|
387 | //! provided vector
|
---|
388 | //!
|
---|
389 | //! @param vec
|
---|
390 | //! Vector to which the boost::any object should be added
|
---|
391 | //!
|
---|
392 | //! @param ptr
|
---|
393 | //! Pointer to the binary representation. It will be incremented
|
---|
394 | //! according to the size of the template argument
|
---|
395 | //!
|
---|
396 | //! @tparam T
|
---|
397 | //! Type as which the binary data should be interpreted
|
---|
398 | //!
|
---|
399 | template<class T>
|
---|
400 | void Converter::Add(vector<boost::any> &vec, const char* &ptr) const
|
---|
401 | {
|
---|
402 | vec.push_back(*reinterpret_cast<const T*>(ptr));
|
---|
403 | ptr += sizeof(T);
|
---|
404 | }
|
---|
405 |
|
---|
406 | // --------------------------------------------------------------------------
|
---|
407 | //
|
---|
408 | //! Add the string pointed to by ptr to the given string.
|
---|
409 | //!
|
---|
410 | //! @param str
|
---|
411 | //! Reference to the string to which the ptr should be added
|
---|
412 | //!
|
---|
413 | //! @param ptr
|
---|
414 | //! Pointer to the binary representation. It will be incremented
|
---|
415 | //! according to the size of the template argument
|
---|
416 | //!
|
---|
417 | void Converter::AddString(string &str, const char* &ptr) const
|
---|
418 | {
|
---|
419 | const string txt(ptr);
|
---|
420 | str += ' '+txt;
|
---|
421 | ptr += txt.length()+1;
|
---|
422 | }
|
---|
423 |
|
---|
424 | // --------------------------------------------------------------------------
|
---|
425 | //
|
---|
426 | //! Add the string pointed to by ptr as boost::any to the provided vector
|
---|
427 | //!
|
---|
428 | //! @param vec
|
---|
429 | //! Vector to which the boost::any object should be added
|
---|
430 | //!
|
---|
431 | //! @param ptr
|
---|
432 | //! Pointer to the binary representation. It will be incremented
|
---|
433 | //! according to the size of the template argument
|
---|
434 | //!
|
---|
435 | void Converter::AddString(vector<boost::any> &vec, const char* &ptr) const
|
---|
436 | {
|
---|
437 | const string txt(ptr);
|
---|
438 | vec.push_back(txt);
|
---|
439 | ptr += txt.length()+1;
|
---|
440 | }
|
---|
441 |
|
---|
442 | // --------------------------------------------------------------------------
|
---|
443 | //
|
---|
444 | //! Compiles the format string into fList. See Compile() for more details.
|
---|
445 | //!
|
---|
446 | //! @param out
|
---|
447 | //! Output stream to which possible logging is redirected
|
---|
448 | //!
|
---|
449 | //! @param fmt
|
---|
450 | //! Format to be compiled. For details see class reference
|
---|
451 | //!
|
---|
452 | //! @param strict
|
---|
453 | //! Setting this to true allows non DIM options, whiel false
|
---|
454 | //! will restrict the possible format strings to the ones also
|
---|
455 | //! understood by DIM.
|
---|
456 | //!
|
---|
457 | Converter::Converter(std::ostream &out, const std::string &fmt, bool strict)
|
---|
458 | : wout(out), fFormat(Clean(fmt)), fList(Compile(out, fmt, strict))
|
---|
459 | {
|
---|
460 | }
|
---|
461 |
|
---|
462 | // --------------------------------------------------------------------------
|
---|
463 | //
|
---|
464 | //! Compiles the format string into fList.
|
---|
465 | //!
|
---|
466 | //! Output by default is redirected to cout.
|
---|
467 | //!
|
---|
468 | //! @param fmt
|
---|
469 | //! Format to be compiled. For details see class reference
|
---|
470 | //!
|
---|
471 | //! @param strict
|
---|
472 | //! Setting this to true allows non DIM options, whiel false
|
---|
473 | //! will restrict the possible format strings to the ones also
|
---|
474 | //! understood by DIM.
|
---|
475 | //!
|
---|
476 | Converter::Converter(const std::string &fmt, bool strict)
|
---|
477 | : wout(cout), fFormat(Clean(fmt)), fList(Compile(fmt, strict))
|
---|
478 | {
|
---|
479 | }
|
---|
480 |
|
---|
481 | // --------------------------------------------------------------------------
|
---|
482 | //
|
---|
483 | //! Converts the provided format string into a vector.
|
---|
484 | //!
|
---|
485 | //! @tparam T
|
---|
486 | //! Kind of data to be returned. This can either be boost::any objects
|
---|
487 | //! or a bnary data-block (char).
|
---|
488 | //!
|
---|
489 | //! @param str
|
---|
490 | //! Data to be converted. For details see class reference
|
---|
491 | //!
|
---|
492 | //! @returns
|
---|
493 | //! A vector of the given template type containing the arguments. In
|
---|
494 | //! case of failure an empty vector is returned.
|
---|
495 | //!
|
---|
496 | //! @throws
|
---|
497 | //! std::runtime_error if the conversion was not successfull
|
---|
498 | //!
|
---|
499 | template <class T>
|
---|
500 | vector<T> Converter::Get(const std::string &str) const
|
---|
501 | {
|
---|
502 | if (!valid())
|
---|
503 | throw runtime_error("Compiled format invalid!");
|
---|
504 |
|
---|
505 | // If the format is empty we are already done
|
---|
506 | if (empty() && str.empty())
|
---|
507 | {
|
---|
508 | wout << endl;
|
---|
509 | return vector<T>();
|
---|
510 | }
|
---|
511 |
|
---|
512 | int arg = 0;
|
---|
513 | stringstream line(str);
|
---|
514 |
|
---|
515 | vector<T> data;
|
---|
516 |
|
---|
517 | for (Converter::FormatList::const_iterator i=fList.begin(); i<fList.end()-1; i++)
|
---|
518 | {
|
---|
519 | if (*i->first.first == typeid(string))
|
---|
520 | {
|
---|
521 | GetBinString(data, GetStringEol(line));
|
---|
522 | line.clear(ios::eofbit);
|
---|
523 | continue;
|
---|
524 | }
|
---|
525 |
|
---|
526 | // Get as many items from the input line as requested
|
---|
527 | for (int j=0; j<i->second.first; j++)
|
---|
528 | {
|
---|
529 | switch (i->first.first->name()[0])
|
---|
530 | {
|
---|
531 | case 'b': GetBinImp(data, GetBool(line)); break;
|
---|
532 | case 's': GetBinImp(data, Get<short> (line)); break;
|
---|
533 | case 'i': GetBinImp(data, Get<int> (line)); break;
|
---|
534 | case 'l': GetBinImp(data, Get<long> (line)); break;
|
---|
535 | case 'f': GetBinImp(data, Get<float> (line)); break;
|
---|
536 | case 'd': GetBinImp(data, Get<double> (line)); break;
|
---|
537 | case 'x': GetBinImp(data, Get<long long>(line)); break;
|
---|
538 | case 'c':
|
---|
539 | if (line.peek()==-1)
|
---|
540 | {
|
---|
541 | line.clear(ios::failbit|ios::eofbit);
|
---|
542 | break;
|
---|
543 | }
|
---|
544 | GetBinImp(data, Get<unsigned char>(line));
|
---|
545 | if (line.peek()==-1)
|
---|
546 | line.clear(ios::eofbit);
|
---|
547 | break;
|
---|
548 | case 'N':
|
---|
549 | GetBinString(data, GetString(line));
|
---|
550 | if (*i->first.first == typeid(O))
|
---|
551 | line.clear(ios::goodbit|(line.rdstate()&ios::eofbit));
|
---|
552 | break;
|
---|
553 | default:
|
---|
554 | // This should never happen!
|
---|
555 | throw runtime_error("Format '"+string(i->first.first->name())+" not supported!");
|
---|
556 | }
|
---|
557 |
|
---|
558 | arg++;
|
---|
559 | }
|
---|
560 |
|
---|
561 | if (!line)
|
---|
562 | break;
|
---|
563 | }
|
---|
564 | wout << endl;
|
---|
565 |
|
---|
566 | // Something wrong with the conversion (e.g. 5.5 for an int)
|
---|
567 | if (line.fail() && !line.eof())
|
---|
568 | {
|
---|
569 | line.clear(); // This is necesasary to get a proper response from tellg()
|
---|
570 |
|
---|
571 | ostringstream err;
|
---|
572 | err << "Error converting argument at " << arg << " [fmt=" << fFormat << "]!\n";
|
---|
573 | err << line.str() << "\n";
|
---|
574 | err << setw(int(line.tellg())) << " " << "^\n";
|
---|
575 | throw runtime_error(err.str());
|
---|
576 | }
|
---|
577 |
|
---|
578 | // Not enough arguments, we have not reached the end
|
---|
579 | if (line.fail() && line.eof())
|
---|
580 | {
|
---|
581 | line.clear();
|
---|
582 |
|
---|
583 | ostringstream err;
|
---|
584 | err << "Not enough arguments [fmt=" << fFormat << "]!\n";
|
---|
585 | err << line.str() << "\n";
|
---|
586 | err << setw(int(line.tellg())+1) << " " << "^\n";
|
---|
587 | throw runtime_error(err.str());
|
---|
588 | }
|
---|
589 |
|
---|
590 | // Too many arguments, we have not reached the end
|
---|
591 | // Unfortunately, this can also mean that there is something
|
---|
592 | // wrong with the last argument
|
---|
593 | if (line.good() && !line.eof())
|
---|
594 | {
|
---|
595 | ostringstream err;
|
---|
596 | err << "More arguments available than expected [fmt=" << fFormat << "]!\n";
|
---|
597 | err << line.str() << "\n";
|
---|
598 | err << setw(int(line.tellg())+1) << " " << "^\n";
|
---|
599 | throw runtime_error(err.str());
|
---|
600 | }
|
---|
601 |
|
---|
602 | return data;
|
---|
603 |
|
---|
604 | }
|
---|
605 |
|
---|
606 | std::vector<boost::any> Converter::GetAny(const std::string &str) const
|
---|
607 | {
|
---|
608 | return Get<boost::any>(str);
|
---|
609 | }
|
---|
610 |
|
---|
611 | std::vector<char> Converter::GetVector(const std::string &str) const
|
---|
612 | {
|
---|
613 | return Get<char>(str);
|
---|
614 | }
|
---|
615 |
|
---|
616 | // --------------------------------------------------------------------------
|
---|
617 | //
|
---|
618 | //! Converts the provided data block into a vector of boost::any or
|
---|
619 | //! a string.
|
---|
620 | //!
|
---|
621 | //! @tparam T
|
---|
622 | //! Kind of data to be returned. This can either be boost::any objects
|
---|
623 | //! or a string
|
---|
624 | //!
|
---|
625 | //! @returns
|
---|
626 | //! A vector of the given template type containing the arguments. In
|
---|
627 | //! case of failure an empty vector is returned.
|
---|
628 | //!
|
---|
629 | //! @throws
|
---|
630 | //! std::runtime_error if the conversion was not successfull
|
---|
631 | //!
|
---|
632 | template<class T>
|
---|
633 | T Converter::Get(const void *dat, size_t size) const
|
---|
634 | {
|
---|
635 | if (!valid())
|
---|
636 | throw runtime_error("Compiled format invalid!");
|
---|
637 |
|
---|
638 | if (dat==0)
|
---|
639 | throw runtime_error("Data pointer == NULL!");
|
---|
640 |
|
---|
641 | const char *ptr = reinterpret_cast<const char *>(dat);
|
---|
642 |
|
---|
643 | T text;
|
---|
644 | for (Converter::FormatList::const_iterator i=fList.begin(); i<fList.end()-1; i++)
|
---|
645 | {
|
---|
646 | if (ptr-size>dat)
|
---|
647 | {
|
---|
648 | ostringstream err;
|
---|
649 | err << "Format description [fmt=" << fFormat << "] exceeds available data size (" << size << ")";
|
---|
650 | throw runtime_error(err.str());
|
---|
651 | }
|
---|
652 |
|
---|
653 | if (*i->first.first == typeid(string))
|
---|
654 | {
|
---|
655 | if (size>0)
|
---|
656 | AddString(text, ptr);
|
---|
657 | if (ptr-size<=dat)
|
---|
658 | return text;
|
---|
659 | break;
|
---|
660 | }
|
---|
661 |
|
---|
662 | // Get as many items from the input line as requested
|
---|
663 | for (int j=0; j<i->second.first; j++)
|
---|
664 | {
|
---|
665 | switch (i->first.first->name()[0])
|
---|
666 | {
|
---|
667 | case 'b': Add<bool> (text, ptr); break;
|
---|
668 | case 'c': Add<char> (text, ptr); break;
|
---|
669 | case 's': Add<short> (text, ptr); break;
|
---|
670 | case 'i': Add<int> (text, ptr); break;
|
---|
671 | case 'l': Add<long> (text, ptr); break;
|
---|
672 | case 'f': Add<float> (text, ptr); break;
|
---|
673 | case 'd': Add<double> (text, ptr); break;
|
---|
674 | case 'x': Add<long long>(text, ptr); break;
|
---|
675 | case 'N': AddString(text, ptr); break;
|
---|
676 |
|
---|
677 | case 'v':
|
---|
678 | // This should never happen!
|
---|
679 | throw runtime_error("Type 'void' not supported!");
|
---|
680 | default:
|
---|
681 | throw runtime_error("TypeId '"+string(i->first.first->name())+"' not known!");
|
---|
682 | }
|
---|
683 | }
|
---|
684 | }
|
---|
685 |
|
---|
686 | if (ptr-size!=dat)
|
---|
687 | {
|
---|
688 | ostringstream err;
|
---|
689 | err << "Data block size (" << size << ") doesn't fit format description [fmt=" << fFormat << "]";
|
---|
690 | throw runtime_error(err.str());
|
---|
691 | }
|
---|
692 |
|
---|
693 | return text;
|
---|
694 | }
|
---|
695 |
|
---|
696 | std::vector<boost::any> Converter::GetAny(const void *dat, size_t size) const
|
---|
697 | {
|
---|
698 | return Get<vector<boost::any>>(dat, size);
|
---|
699 | }
|
---|
700 |
|
---|
701 | std::vector<char> Converter::GetVector(const void *dat, size_t size) const
|
---|
702 | {
|
---|
703 | const string ref = GetString(dat, size);
|
---|
704 |
|
---|
705 | vector<char> data;
|
---|
706 | data.insert(data.begin(), ref.begin()+1, ref.end());
|
---|
707 | data.push_back(0);
|
---|
708 |
|
---|
709 | return data;
|
---|
710 | }
|
---|
711 |
|
---|
712 | string Converter::GetString(const void *dat, size_t size) const
|
---|
713 | {
|
---|
714 | const string s = Get<string>(dat, size);
|
---|
715 | return s.empty() ? s : s.substr(1);
|
---|
716 | }
|
---|
717 |
|
---|
718 | template<class T>
|
---|
719 | Converter::Type Converter::GetType()
|
---|
720 | {
|
---|
721 | Type t;
|
---|
722 | t.first = &typeid(T);
|
---|
723 | t.second = sizeof(T);
|
---|
724 | return t;
|
---|
725 | }
|
---|
726 |
|
---|
727 | template<class T>
|
---|
728 | Converter::Type Converter::GetVoid()
|
---|
729 | {
|
---|
730 | Type t;
|
---|
731 | t.first = &typeid(T);
|
---|
732 | t.second = 0;
|
---|
733 | return t;
|
---|
734 | }
|
---|
735 |
|
---|
736 | // --------------------------------------------------------------------------
|
---|
737 | //
|
---|
738 | //! static function to compile a format string.
|
---|
739 | //!
|
---|
740 | //! @param out
|
---|
741 | //! Output stream to which possible logging is redirected
|
---|
742 | //!
|
---|
743 | //! @param fmt
|
---|
744 | //! Format to be compiled. For details see class reference
|
---|
745 | //!
|
---|
746 | //! @param strict
|
---|
747 | //! Setting this to true allows non DIM options, whiel false
|
---|
748 | //! will restrict the possible format strings to the ones also
|
---|
749 | //! understood by DIM.
|
---|
750 | //!
|
---|
751 | Converter::FormatList Converter::Compile(std::ostream &out, const std::string &fmt, bool strict)
|
---|
752 | {
|
---|
753 | ostringstream text;
|
---|
754 |
|
---|
755 | // Access both, the data and the format through a stringstream
|
---|
756 | stringstream stream(fmt);
|
---|
757 |
|
---|
758 | // For better performance we could use sregex
|
---|
759 | static const boost::regex expr1("^([CSILFDXBOW])(:([1-9]+[0-9]*))?$");
|
---|
760 | static const boost::regex expr2("^([CSILFDX])(:([1-9]+[0-9]*))?$");
|
---|
761 |
|
---|
762 | FormatList list;
|
---|
763 | Format format;
|
---|
764 |
|
---|
765 | // Tokenize the format
|
---|
766 | string buffer;
|
---|
767 | while (getline(stream, buffer, ';'))
|
---|
768 | {
|
---|
769 | boost::smatch what;
|
---|
770 | if (!boost::regex_match(buffer, what, strict?expr2:expr1))
|
---|
771 | {
|
---|
772 | out << kRed << "Wrong format string '" << buffer << "'!" << endl;
|
---|
773 | return FormatList();
|
---|
774 | }
|
---|
775 |
|
---|
776 | const string t = what[1]; // type id
|
---|
777 | const string n = what[3]; // counter
|
---|
778 |
|
---|
779 | const int cnt = atoi(n.c_str());
|
---|
780 |
|
---|
781 | // if the :N part was not given assume 1
|
---|
782 | format.second.first = cnt == 0 ? 1 : cnt;
|
---|
783 |
|
---|
784 | /*
|
---|
785 | if (strict && t[0]=='C' && cnt>0)
|
---|
786 | {
|
---|
787 | out << kRed << "Dim doesn't support the format C with N>0!" << endl;
|
---|
788 | return FormatList();
|
---|
789 | }*/
|
---|
790 |
|
---|
791 | // Check if the format is just C (without a number)
|
---|
792 | // That would mean that it is a \0 terminated string
|
---|
793 | if (t[0]=='C' && cnt==0)
|
---|
794 | {
|
---|
795 | format.first = GetType<string>();
|
---|
796 | list.push_back(format);
|
---|
797 | format.second.second = 0; // end position not known
|
---|
798 | break;
|
---|
799 | }
|
---|
800 |
|
---|
801 | // Get as many items from the input line as requested
|
---|
802 | switch (t[0])
|
---|
803 | {
|
---|
804 | case 'B': format.first = GetType<bool>(); break;
|
---|
805 | case 'C': format.first = GetType<char>(); break;
|
---|
806 | case 'S': format.first = GetType<short>(); break;
|
---|
807 | case 'I': format.first = GetType<int>(); break;
|
---|
808 | case 'L': format.first = GetType<long>(); break;
|
---|
809 | case 'F': format.first = GetType<float>(); break;
|
---|
810 | case 'D': format.first = GetType<double>(); break;
|
---|
811 | case 'X': format.first = GetType<long long>(); break;
|
---|
812 | case 'O': format.first = GetVoid<O>(); break;
|
---|
813 | case 'W': format.first = GetVoid<W>(); break;
|
---|
814 | default:
|
---|
815 | // This should never happen!
|
---|
816 | out << kRed << "Format '" << t[0] << " not known!" << endl;
|
---|
817 | return list;
|
---|
818 | }
|
---|
819 |
|
---|
820 | list.push_back(format);
|
---|
821 | format.second.second += format.first.second * format.second.first;
|
---|
822 | }
|
---|
823 |
|
---|
824 | format.first = GetVoid<void>();
|
---|
825 | format.second.first = 0;
|
---|
826 |
|
---|
827 | list.push_back(format);
|
---|
828 |
|
---|
829 | return list;
|
---|
830 | }
|
---|
831 |
|
---|
832 | // --------------------------------------------------------------------------
|
---|
833 | //
|
---|
834 | //! Same as Compile(ostream&,string&,bool) but cout is used as the default
|
---|
835 | //! output stream.
|
---|
836 | //!
|
---|
837 | //!
|
---|
838 | Converter::FormatList Converter::Compile(const std::string &fmt, bool strict)
|
---|
839 | {
|
---|
840 | return Compile(cout, fmt, strict);
|
---|
841 | }
|
---|
842 |
|
---|
843 | vector<string> Converter::Regex(const string &expr, const string &line)
|
---|
844 | {
|
---|
845 | const boost::regex reg(expr);
|
---|
846 |
|
---|
847 | boost::smatch what;
|
---|
848 | if (!boost::regex_match(line, what, reg, boost::match_extra))
|
---|
849 | return vector<string>();
|
---|
850 |
|
---|
851 | vector<string> ret;
|
---|
852 | for (unsigned int i=0; i<what.size(); i++)
|
---|
853 | ret.push_back(what[i]);
|
---|
854 |
|
---|
855 | return ret;
|
---|
856 | }
|
---|
857 |
|
---|
858 |
|
---|
859 | void Converter::ToFits(void *dest, const void *src, size_t size) const
|
---|
860 | {
|
---|
861 | // crawl through the src buffer and copy the data appropriately to the
|
---|
862 | // destination buffer
|
---|
863 | // Assumption: the string is always last. This way we
|
---|
864 | // use the provided size to determine the number
|
---|
865 | // of character to copy
|
---|
866 |
|
---|
867 | char *charDest = static_cast<char*>(dest);
|
---|
868 | const char *charSrc = static_cast<const char*>(src);
|
---|
869 |
|
---|
870 | for (Converter::FormatList::const_iterator i=fList.begin(); i!=fList.end(); i++)
|
---|
871 | {
|
---|
872 | if (charDest-size>dest)
|
---|
873 | {
|
---|
874 | ostringstream err;
|
---|
875 | err << "Format description [fmt=" << fFormat << "] exceeds available data size (" << size << ")";
|
---|
876 | throw runtime_error(err.str());
|
---|
877 | }
|
---|
878 |
|
---|
879 | const char type = i->first.first->name()[0];
|
---|
880 | if (type=='v')
|
---|
881 | break;
|
---|
882 |
|
---|
883 | // string types
|
---|
884 | if (string("bsilfdx").find_first_of(type)==string::npos)
|
---|
885 | throw runtime_error(string("Type '")+type+"' not supported converting to FITS.");
|
---|
886 |
|
---|
887 | const int s = i->first.second; // size of element
|
---|
888 | const int n = i->second.first; // number of elements
|
---|
889 |
|
---|
890 | // for all elements of this column
|
---|
891 | for (int j=0; j<n; j++)
|
---|
892 | {
|
---|
893 | reverse_copy(charSrc, charSrc+s, charDest);
|
---|
894 |
|
---|
895 | charSrc += s;
|
---|
896 | charDest += s;
|
---|
897 | }
|
---|
898 | }
|
---|
899 |
|
---|
900 | if (charDest-size!=dest)
|
---|
901 | {
|
---|
902 | ostringstream err;
|
---|
903 | err << "Data block size (" << size << ") doesn't fit format description [fmt=" << fFormat << "]";
|
---|
904 | throw runtime_error(err.str());
|
---|
905 | }
|
---|
906 | }
|
---|
907 |
|
---|
908 | vector<char> Converter::ToFits(const void *src, size_t size) const
|
---|
909 | {
|
---|
910 | vector<char> dest(size);
|
---|
911 | ToFits(dest.data(), src, size);
|
---|
912 | return dest;
|
---|
913 | }
|
---|
914 |
|
---|
915 |
|
---|
916 | void Converter::Print(std::ostream &out) const
|
---|
917 | {
|
---|
918 | for (FormatList::const_iterator i=fList.begin(); i!=fList.end(); i++)
|
---|
919 | {
|
---|
920 | out << "Type=" << i->first.first->name() << "[" << i->first.second << "] ";
|
---|
921 | out << "N=" << i->second.first << " ";
|
---|
922 | out << "offset=" << i->second.second << endl;
|
---|
923 | }
|
---|
924 | }
|
---|
925 |
|
---|
926 | void Converter::Print() const
|
---|
927 | {
|
---|
928 | return Print(cout);
|
---|
929 | }
|
---|