source: trunk/FACT++/src/Converter.cc@ 10354

Last change on this file since 10354 was 10354, checked in by tbretz, 10 years ago
Added some comment.
File size: 24.0 KB
Line 
1// **************************************************************************
2/** @class Converter
3
4@brief A compiler for the DIM data format string
5
6The Converter class interprets arguments in a string accoring to the
7given format definition and produces a corresponding memory block from it
8which can be attached to an event later.
9
10The 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
20Note, that the strange notation T:N[;T:N]*[;T] is meant to be a regular
21expression. An Xtra-long is a 'long long'.
22
23Since Dim itself never really interpretes the format string, the programmer
24is responsible to make sure that the delivered data and the interpretation
25is consistent. Therefore the provided class can be of some help.
26
27For 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
34would produce a 20 byte data block with the integers 1, the floats
352.5 and 4.2, and the intergers 3 and 4, in this order.
36
37The 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
44Other conversion functions also exist.
45
46To check if the compilation of the format string was successfull
47the valid() member functio is provided.
48
49The format parameter \b W(ord) is dedicated to this kind of conversion and
50not understood by Dim. In addition there are \b O(ptions) which are like
51Words but can be omitted. They should only be used at the end of the string.
52Both can be encapsulated in quotationmarks '"'. Nested quotationmarks
53are not supported. \b B(ool) is also special. It evaluates true/false,
54yes/no, on/off, 1/0.
55
56The non-DIM like format options can be switched on and off by using the
57strict argument in the constructor. In general DimCommands can use these
58options, but DimServices not.
59
60@remark Note that all values are interpreted as signed, except the single
61char (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
79using 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//
92std::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//!
121template <class T>
122void 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//!
147template <class T>
148void 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//!
168void 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//!
188void 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//!
210template <class T>
211T Converter::Get(std::stringstream &line) const
212{
213 T val;
214 line >> val;
215 return val;
216}
217
218// --------------------------------------------------------------------------
219//
220//! Converts from the stringstream into bool. It allows to use lexical
221//! boolean representations like yes/no, on/off, true/false and of
222//! course 0/1. If the conversion fails the failbit is set.
223//!
224//! @param line
225//! reference to the stringstream from which the data should be
226//! interpreted
227//!
228//! @returns
229//! The boolean. 0 in case of failure
230//!
231bool Converter::GetBool(std::stringstream &line) const
232{
233 string buf;
234 line >> buf;
235 transform(buf.begin(), buf.end(), buf.begin(), (int(*)(int)) std::tolower);
236
237 if (buf=="yes" || buf=="true" || buf=="on" || buf=="1")
238 return true;
239
240 if (buf=="no" || buf=="false" || buf=="off" || buf=="0")
241 return false;
242
243 line.clear(ios::failbit);
244
245 return false;
246}
247
248// --------------------------------------------------------------------------
249//
250//! Converts from the stringstream into a string. Leading whitespaces are
251//! skipped. Everything up to the next whitespace is returned.
252//! strings can be encapsulated into escape characters ("). Note, that
253//! they cannot be nested.
254//!
255//! @param line
256//! reference to the stringstream from which the data should be
257//! interpreted
258//!
259//! @returns
260//! The string
261//!
262string Converter::GetString(std::stringstream &line) const
263{
264 while (line.peek()==' ')
265 line.get();
266
267 string buf;
268 if (line.peek()=='\"')
269 {
270 line.get();
271 getline(line, buf, '\"');
272 if (line.peek()==-1)
273 line.clear(ios::eofbit);
274 }
275 else
276 line >> buf;
277
278 return buf;
279}
280
281// --------------------------------------------------------------------------
282//
283//! Converts from the stringstream into a string. Leading whitespaces are
284//! skipped. Everything until the end-of-line is returned. A trailing
285//! \0 is added.
286//!
287//! @param line
288//! reference to the stringstream from which the data should be
289//! interpreted
290//!
291//! @returns
292//! The string
293//!
294string Converter::GetStringEol(stringstream &line) const
295{
296 // Remove leading whitespaces
297 while (line.peek()==' ')
298 line.get();
299
300 line >> noskipws;
301
302 const istream_iterator<char> eol; // end-of-line iterator
303 const string s(istream_iterator<char>(line), eol);
304 return s + '\0';
305}
306
307// --------------------------------------------------------------------------
308//
309//! Converts from a binary block into a string. The type of the expected
310//! value is defined by the template parameter.
311//!
312//! @param ptr
313//! A refrenece to the pointer of the binary representation to be
314//! interpreted. The pointer is incremented by the sizeof the type.
315//!
316//! @tparam T
317//! Expected type
318//!
319//! @returns
320//! The string
321//!
322template<class T>
323string Converter::GetString(const char* &ptr) const
324{
325 const T &t = *reinterpret_cast<const T*>(ptr);
326
327 ostringstream stream;
328 stream << t;
329 ptr += sizeof(T);
330
331 return stream.str();
332}
333
334// --------------------------------------------------------------------------
335//
336//! Converts from a binary block into a hex representation.
337//!
338//! @param dat
339//! Pointer to the data block
340//!
341//! @param size
342//! Size of the data block
343//!
344//! @param chunk
345//! Size of the size of hex chunks seperted by a ':' in bytes
346//!
347//! @returns
348//! The string
349//!
350string Converter::GetHex(const void *dat, size_t size, size_t chunk)
351{
352 const unsigned char *ptr = reinterpret_cast<const unsigned char *>(dat);
353
354 ostringstream text;
355
356 text << hex;
357
358 for (size_t i=0; i<size; i++)
359 {
360 text << setw(2) << setfill('0') << (unsigned int)ptr[i];
361 if ((i%chunk)==chunk-1)
362 text << ":";
363 }
364
365 return text.str();
366}
367
368
369// --------------------------------------------------------------------------
370//
371//! Convert the pointer using GetString into a string and add it (prefixed
372//! by a whaitespace) to the given string.
373//!
374//! @param str
375//! Reference to the string to which the ptr should be added
376//!
377//! @param ptr
378//! Pointer to the binary representation. It will be incremented
379//! according to the sze of the template argument
380//!
381//! @tparam T
382//! Type as which the binary data should be interpreted
383//!
384template<class T>
385void Converter::Add(string &str, const char* &ptr) const
386{
387 str += ' ' + GetString<T>(ptr);
388}
389
390// --------------------------------------------------------------------------
391//
392//! Convert the pointer into a boost::any object and add it to the
393//! provided vector
394//!
395//! @param vec
396//! Vector to which the boost::any object should be added
397//!
398//! @param ptr
399//! Pointer to the binary representation. It will be incremented
400//! according to the size of the template argument
401//!
402//! @tparam T
403//! Type as which the binary data should be interpreted
404//!
405template<class T>
406void Converter::Add(vector<boost::any> &vec, const char* &ptr) const
407{
408 vec.push_back(*reinterpret_cast<const T*>(ptr));
409 ptr += sizeof(T);
410}
411
412// --------------------------------------------------------------------------
413//
414//! Add the string pointed to by ptr to the given string.
415//!
416//! @param str
417//! Reference to the string to which the ptr should be added
418//!
419//! @param ptr
420//! Pointer to the binary representation. It will be incremented
421//! according to the size of the template argument
422//!
423void Converter::AddString(string &str, const char* &ptr) const
424{
425 const string txt(ptr);
426 str += " [" + txt + "]";
427 ptr += txt.length()+1;
428}
429
430// --------------------------------------------------------------------------
431//
432//! Add the string pointed to by ptr as boost::any to the provided vector
433//!
434//! @param vec
435//! Vector to which the boost::any object should be added
436//!
437//! @param ptr
438//! Pointer to the binary representation. It will be incremented
439//! according to the size of the template argument
440//!
441void Converter::AddString(vector<boost::any> &vec, const char* &ptr) const
442{
443 const string txt(ptr);
444 vec.push_back(txt);
445 ptr += txt.length()+1;
446}
447
448// --------------------------------------------------------------------------
449//
450//! Compiles the format string into fList. See Compile() for more details.
451//!
452//! @param out
453//! Output stream to which possible logging is redirected
454//!
455//! @param fmt
456//! Format to be compiled. For details see class reference
457//!
458//! @param strict
459//! Setting this to true allows non DIM options, whiel false
460//! will restrict the possible format strings to the ones also
461//! understood by DIM.
462//!
463Converter::Converter(std::ostream &out, const std::string &fmt, bool strict)
464: wout(out), fFormat(Clean(fmt)), fList(Compile(out, fmt, strict))
465{
466}
467
468// --------------------------------------------------------------------------
469//
470//! Compiles the format string into fList.
471//!
472//! Output by default is redirected to cout.
473//!
474//! @param fmt
475//! Format to be compiled. For details see class reference
476//!
477//! @param strict
478//! Setting this to true allows non DIM options, whiel false
479//! will restrict the possible format strings to the ones also
480//! understood by DIM.
481//!
482Converter::Converter(const std::string &fmt, bool strict)
483: wout(cout), fFormat(Clean(fmt)), fList(Compile(fmt, strict))
484{
485}
486
487// --------------------------------------------------------------------------
488//
489//! Converts the provided format string into a vector.
490//!
491//! @tparam T
492//! Kind of data to be returned. This can either be boost::any objects
493//! or a bnary data-block (char).
494//!
495//! @param str
496//! Data to be converted. For details see class reference
497//!
498//! @returns
499//! A vector of the given template type containing the arguments. In
500//! case of failure an empty vector is returned.
501//!
502//! @throws
503//! std::runtime_error if the conversion was not successfull
504//!
505template <class T>
506vector<T> Converter::Get(const std::string &str) const
507{
508 if (!valid())
509 throw runtime_error("Compiled format invalid!");
510
511 // If the format is empty we are already done
512 if (empty() && str.empty())
513 {
514 wout << endl;
515 return vector<T>();
516 }
517
518 int arg = 0;
519 stringstream line(str);
520
521 vector<T> data;
522
523 for (Converter::FormatList::const_iterator i=fList.begin(); i<fList.end()-1; i++)
524 {
525 if (*i->first.first == typeid(string))
526 {
527 GetBinString(data, GetStringEol(line));
528 line.clear(ios::eofbit);
529 continue;
530 }
531
532 // Get as many items from the input line as requested
533 for (int j=0; j<i->second.first; j++)
534 {
535 switch (i->first.first->name()[0])
536 {
537 case 'b': GetBinImp(data, GetBool(line)); break;
538 case 's': GetBinImp(data, Get<short> (line)); break;
539 case 'i': GetBinImp(data, Get<int> (line)); break;
540 case 'l': GetBinImp(data, Get<long> (line)); break;
541 case 'f': GetBinImp(data, Get<float> (line)); break;
542 case 'd': GetBinImp(data, Get<double> (line)); break;
543 case 'x': GetBinImp(data, Get<long long>(line)); break;
544 case 'c':
545 if (line.peek()==-1)
546 {
547 line.clear(ios::failbit|ios::eofbit);
548 break;
549 }
550 GetBinImp(data, Get<unsigned char>(line));
551 if (line.peek()==-1)
552 line.clear(ios::eofbit);
553 break;
554 case 'N':
555 GetBinString(data, GetString(line));
556 if (*i->first.first == typeid(O))
557 line.clear(ios::goodbit|(line.rdstate()&ios::eofbit));
558 break;
559 default:
560 // This should never happen!
561 throw runtime_error("Format '"+string(i->first.first->name())+" not supported!");
562 }
563
564 arg++;
565 }
566
567 if (!line)
568 break;
569 }
570 wout << endl;
571
572 // Something wrong with the conversion (e.g. 5.5 for an int)
573 if (line.fail() && !line.eof())
574 {
575 line.clear(); // This is necesasary to get a proper response from tellg()
576
577 ostringstream err;
578 err << "Error converting argument at " << arg << " [fmt=" << fFormat << "]!\n";
579 err << line.str() << "\n";
580 err << setw(int(line.tellg())) << " " << "^\n";
581 throw runtime_error(err.str());
582 }
583
584 // Not enough arguments, we have not reached the end
585 if (line.fail() && line.eof())
586 {
587 line.clear();
588
589 ostringstream err;
590 err << "Not enough arguments [fmt=" << fFormat << "]!\n";
591 err << line.str() << "\n";
592 err << setw(int(line.tellg())+1) << " " << "^\n";
593 throw runtime_error(err.str());
594 }
595
596 // Too many arguments, we have not reached the end
597 // Unfortunately, this can also mean that there is something
598 // wrong with the last argument
599 if (line.good() && !line.eof())
600 {
601 ostringstream err;
602 err << "More arguments available than expected [fmt=" << fFormat << "]!\n";
603 err << line.str() << "\n";
604 err << setw(int(line.tellg())+1) << " " << "^\n";
605 throw runtime_error(err.str());
606 }
607
608 return data;
609
610}
611
612std::vector<boost::any> Converter::GetAny(const std::string &str) const
613{
614 return Get<boost::any>(str);
615}
616
617std::vector<char> Converter::GetVector(const std::string &str) const
618{
619 return Get<char>(str);
620}
621
622// --------------------------------------------------------------------------
623//
624//! Converts the provided data block into a vector of boost::any or
625//! a string.
626//!
627//! @tparam T
628//! Kind of data to be returned. This can either be boost::any objects
629//! or a string
630//!
631//! @returns
632//! A vector of the given template type containing the arguments. In
633//! case of failure an empty vector is returned.
634//!
635//! @throws
636//! std::runtime_error if the conversion was not successfull
637//!
638template<class T>
639T Converter::Get(const void *dat, size_t size) const
640{
641 if (!valid())
642 throw runtime_error("Compiled format invalid!");
643
644 const char *ptr = reinterpret_cast<const char *>(dat);
645
646 T text;
647 for (Converter::FormatList::const_iterator i=fList.begin(); i<fList.end()-1; i++)
648 {
649 if (ptr-size>=dat)
650 {
651 ostringstream err;
652 err << "Format description [fmt=" << fFormat << "] exceeds available data size (" << size << ")";
653 throw runtime_error(err.str());
654 }
655
656 if (*i->first.first == typeid(string))
657 {
658 AddString(text, ptr);
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
696std::vector<boost::any> Converter::GetAny(const void *dat, size_t size) const
697{
698 return Get<vector<boost::any>>(dat, size);
699}
700
701std::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
712string 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
718template<class T>
719Converter::Type Converter::GetType()
720{
721 Type t;
722 t.first = &typeid(T);
723 t.second = sizeof(T);
724 return t;
725}
726
727template<class T>
728Converter::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//!
751Converter::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 // Check if the format is just C (without a number)
785 // That would mean that it is a \0 terminated string
786 if (t[0]=='C' && cnt==0)
787 {
788 format.first = GetType<string>();
789 list.push_back(format);
790 format.second.second = 0; // end position not known
791 break;
792 }
793
794 // Get as many items from the input line as requested
795 switch (t[0])
796 {
797 case 'B': format.first = GetType<bool>(); break;
798 case 'C': format.first = GetType<char>(); break;
799 case 'S': format.first = GetType<short>(); break;
800 case 'I': format.first = GetType<int>(); break;
801 case 'L': format.first = GetType<long>(); break;
802 case 'F': format.first = GetType<float>(); break;
803 case 'D': format.first = GetType<double>(); break;
804 case 'X': format.first = GetType<long long>(); break;
805 case 'O': format.first = GetVoid<O>(); break;
806 case 'W': format.first = GetVoid<W>(); break;
807 default:
808 // This should never happen!
809 out << kRed << "Format '" << t[0] << " not known!" << endl;
810 return list;
811 }
812
813 list.push_back(format);
814 format.second.second += format.first.second * format.second.first;
815 }
816
817 format.first = GetVoid<void>();
818 format.second.first = 0;
819
820 list.push_back(format);
821
822 return list;
823}
824
825// --------------------------------------------------------------------------
826//
827//! Same as Compile(ostream&,string&,bool) but cout is used as the default
828//! output stream.
829//!
830//!
831Converter::FormatList Converter::Compile(const std::string &fmt, bool strict)
832{
833 return Compile(cout, fmt, strict);
834}
835
836vector<string> Converter::Regex(const string &expr, const string &line)
837{
838 const boost::regex reg(expr);
839
840 boost::smatch what;
841 if (!boost::regex_match(line, what, reg, boost::match_extra))
842 return vector<string>();
843
844 vector<string> ret;
845 for (unsigned int i=0; i<what.size(); i++)
846 ret.push_back(what[i]);
847
848 return ret;
849}
Note: See TracBrowser for help on using the repository browser.