source: trunk/FACT++/src/Converter.h@ 10560

Last change on this file since 10560 was 10449, checked in by tbretz, 14 years ago
Added Print member function; fixed interpretation of hex and oct values
File size: 4.8 KB
Line 
1#ifndef FACT_Converter
2#define FACT_Converter
3
4#include <math.h>
5
6#include <vector>
7#include <iomanip>
8#include <sstream>
9
10#include <boost/any.hpp>
11
12#include <stdexcept>
13
14class Converter
15{
16public:
17 typedef std::pair<const std::type_info *, int> Type;
18 typedef std::pair<int, int> Offset;
19 typedef std::pair<Type, Offset> Format;
20 typedef std::vector<Format> FormatList;
21
22 struct O { };
23 struct W { };
24
25 static std::string Clean(std::string s);
26
27private:
28 std::ostream &wout; /// ostream to which output is redirected
29
30 const std::string fFormat; /// Original format string
31 const FormatList fList; /// Compiled format description
32
33 template <class T>
34 T Get(std::stringstream &line) const;
35
36 bool GetBool(std::stringstream &line) const;
37 std::string GetString(std::stringstream &line) const;
38 std::string GetStringEol(std::stringstream &line) const;
39
40 template<class T>
41 void GetBinImp(std::vector<char> &v, const T &val) const;
42 template<class T>
43 void GetBinImp(std::vector<boost::any> &v, const T &val) const;
44
45 void GetBinString(std::vector<char> &v, const std::string &val) const;
46 void GetBinString(std::vector<boost::any> &v, const std::string &val) const;
47
48 template<class T>
49 std::string GetString(const char *&data) const;
50
51 template<class T>
52 static Type GetType();
53 template<class T>
54 static Type GetVoid();
55
56 template <class T>
57 std::vector<T> Get(const std::string &str) const;
58 template <class T>
59 T Get(const void *d, size_t size) const;
60
61
62
63 template<class T>
64 void Add(std::string &str, const char* &ptr) const;
65 void AddString(std::string &str, const char* &ptr) const;
66 template<class T>
67 void Add(std::vector<boost::any> &vec, const char* &ptr) const;
68 void AddString(std::vector<boost::any> &vec, const char* &ptr) const;
69
70
71public:
72 Converter(std::ostream &out, const std::string &fmt, bool strict=true);
73 Converter(const std::string &fmt, bool strict=true);
74
75 /// @returns whether the interpreted format was valid but empty ("")
76 bool empty() const { return fList.size()==1 && fList.back().first.second==0; }
77
78 /// @returns whether the compilation was successfull
79 bool valid() const { return !fList.empty() && fList.back().first.second==0; }
80
81 /// @returns true if the compilation failed
82 bool operator!() const { return !valid(); }
83
84 const FormatList &GetList() const { return fList; }
85
86 static FormatList Compile(std::ostream &out, const std::string &fmt, bool strict=false);
87 static FormatList Compile(const std::string &fmt, bool strict=false);
88
89 std::string GetString(const void *d, size_t size) const;
90 std::vector<char> GetVector(const void *d, size_t size) const;
91 std::vector<boost::any> GetAny(const void *d, size_t size) const;
92
93 std::vector<boost::any> GetAny(const std::string &str) const;
94 std::vector<char> GetVector(const std::string &str) const;
95
96 void ToFits(void* dest, const void* src, size_t size) const;
97
98 std::vector<char> ToFits(const void* src, size_t size) const;
99
100 template<typename T>
101 static std::string GetHex(const void *dat, size_t size, size_t col=0, bool prefix=true)
102 {
103 if (size%sizeof(T)!=0)
104 throw std::runtime_error("Total not dividable by typesize.");
105
106 const T *ptr = reinterpret_cast<const T*>(dat);
107
108 std::ostringstream text;
109 text << std::hex;
110
111 const size_t w = log2l(size+1)/4+1;
112
113 for (size_t i=0; i<size/sizeof(T); i++)
114 {
115 if (prefix && i%col==0)
116 text << std::setfill('0') << std::setw(w) << i << "| ";
117
118 text << std::setfill('0') << std::setw(2*sizeof(T)) << (unsigned int)ptr[i];
119 text << ':';
120
121 if (i%col==col-1)
122 text << '\n';
123 }
124
125 return text.str();
126 }
127
128 template<typename T, typename S>
129 static std::string GetHex(const S &s, size_t col=0, bool prefix=true)
130 {
131 return GetHex<T>(&s, sizeof(S), col, prefix);
132 }
133
134 void Print(std::ostream &out) const;
135 void Print() const;
136
137 static std::vector<std::string> Regex(const std::string &expr, const std::string &line);
138};
139
140#endif
141
142// ***************************************************************************
143/** @fn GetHex(const void *dat, size_t size)
144
145Converts from a binary block into a hex representation.
146
147@param dat
148 Pointer to the data block
149
150@param size
151 Size of the data block (in bytes)
152
153@param col
154 Number of columns before new line
155
156@tparam T
157 type to which the data should be converted. Most usefull types are
158 unsigned byte, unsigned short, unsigned int, uint8_t, uint16_t, ...
159
160@returns
161 The string
162
163
164**/
165// ***************************************************************************
Note: See TracBrowser for help on using the repository browser.