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

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