FACT++  1.0
Converter.h
Go to the documentation of this file.
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>
15 
16 class Converter
17 {
18 public:
19  typedef std::pair<const std::type_info *, int> Type;
20  typedef std::pair<int, int> Offset;
21  typedef std::pair<Type, Offset> Format;
22  typedef std::vector<Format> FormatList;
23 
24  struct O { };
25  struct W { };
26 
27  static std::string Clean(std::string s);
28 
29 private:
30  std::ostream &wout;
31 
32  const std::string fFormat;
33  const FormatList fList;
34 
35  template <class T>
36  T Get(std::stringstream &line) const;
37 
38  bool GetBool(std::stringstream &line) const;
39  std::string GetString(std::stringstream &line) const;
40  std::string GetStringEol(std::stringstream &line) const;
41 
42  template<class T>
43  void GetBinImp(std::vector<char> &v, const T &val) const;
44  template<class T>
45  void GetBinImp(std::vector<boost::any> &v, const T &val) const;
46 
47  void GetBinString(std::vector<char> &v, const std::string &val) const;
48  void GetBinString(std::vector<boost::any> &v, const std::string &val) const;
49 
50  template<class T>
51  std::string GetString(const char *&data) const;
52  template<char>
53  std::string GetString(const char* &ptr) const;
54 
55  template<class T>
56  static Type GetType();
57  template<class T>
58  static Type GetVoid();
59 
60  template <class T>
61  std::vector<T> Get(const std::string &str) const;
62  template <class T>
63  T Get(const void *d, size_t size) const;
64 
65 
66 
67  template<class T>
68  void Add(std::string &str, const char* &ptr) const;
69  void AddString(std::string &str, const char* &ptr) const;
70  template<class T>
71  void Add(std::vector<boost::any> &vec, const char* &ptr) const;
72  void AddString(std::vector<boost::any> &vec, const char* &ptr) const;
73 
74 
75 public:
76  Converter(std::ostream &out, const std::string &fmt, bool strict=true);
77  Converter(const std::string &fmt, bool strict=true);
78 
80  bool empty() const { return fList.size()==1 && fList.back().first.second==0; }
81 
83  bool valid() const { return !fList.empty() && fList.back().first.second==0; }
84 
86  bool operator!() const { return !valid(); }
87 
88  const FormatList &GetList() const { return fList; }
89  size_t GetSize() const { return fList.size()==0 ? 0 : fList.back().second.second; }
90 
91  static FormatList Compile(std::ostream &out, const std::string &fmt, bool strict=false);
92  static FormatList Compile(const std::string &fmt, bool strict=false);
93 
94  std::string GetString(const void *d, size_t size) const;
95  std::vector<char> GetVector(const void *d, size_t size) const;
96  std::vector<boost::any> GetAny(const void *d, size_t size) const;
97 
98  std::vector<boost::any> GetAny(const std::string &str) const;
99  std::vector<char> GetVector(const std::string &str) const;
100 
101  std::vector<std::string> ToStrings(const void *src/*, size_t size*/) const;
102  void ToFits(void* dest, const void* src, size_t size) const;
103 
104  std::vector<char> ToFits(const void* src, size_t size) const;
105  std::vector<std::string> GetFitsFormat() const;
106 
107  static std::string ToFormat(const std::vector<std::string> &fits);
108 
109  template<typename T>
110  static std::string GetHex(const void *dat, size_t size, size_t col=0, bool prefix=true)
111  {
112  if (size%sizeof(T)!=0)
113  throw std::runtime_error("GetHex: Total not dividable by typesize.");
114 
115  const T *ptr = reinterpret_cast<const T*>(dat);
116 
117  std::ostringstream text;
118  text << std::hex;
119 
120  const size_t w = nearbyint(ceil(log2(size+1)))/4+1;
121 
122  for (size_t i=0; i<size/sizeof(T); i++)
123  {
124  if (prefix && col!=0 && i%col==0)
125  text << std::setfill('0') << std::setw(w) << i << "| ";
126 
127  text << std::setfill('0') << std::setw(2*sizeof(T));
128  text << (unsigned int)ptr[i] << ':';
129 
130  if (col!=0 && i%col==col-1)
131  text << '\n';
132 
133  }
134 
135  return text.str();
136  }
137 
138  template<typename T, typename S>
139  static std::string GetHex(const S &s, size_t col=0, bool prefix=true)
140  {
141  return GetHex<T>(&s, sizeof(S), col, prefix);
142  }
143 
144  void Print(std::ostream &out) const;
145  void Print() const;
146 
147  static std::vector<std::string> Regex(const std::string &expr, const std::string &line);
148 };
149 
150 #endif
151 
152 // ***************************************************************************
179 // ***************************************************************************
static std::string GetHex(const S &s, size_t col=0, bool prefix=true)
Definition: Converter.h:139
std::vector< Format > FormatList
Definition: Converter.h:22
T Get(std::stringstream &line) const
Compiled format description.
Definition: Converter.cc:212
int i
Definition: db_dim_client.c:21
void Add(std::string &str, const char *&ptr) const
Definition: Converter.cc:404
char str[80]
Definition: test_client.c:7
bool valid() const
Definition: Converter.h:83
std::vector< boost::any > GetAny(const void *d, size_t size) const
Definition: Converter.cc:719
const FormatList & GetList() const
Definition: Converter.h:88
std::string GetString(std::stringstream &line) const
Definition: Converter.cc:294
std::ostream & wout
Definition: Converter.h:30
static std::string Clean(std::string s)
Definition: Converter.cc:93
Definition: fits.h:54
void GetBinImp(std::vector< char > &v, const T &val) const
Definition: Converter.cc:123
static FormatList Compile(std::ostream &out, const std::string &fmt, bool strict=false)
Definition: Converter.cc:774
static std::vector< std::string > Regex(const std::string &expr, const std::string &line)
Definition: Converter.cc:866
void GetBinString(std::vector< char > &v, const std::string &val) const
Definition: Converter.cc:169
static Type GetVoid()
Definition: Converter.cc:751
static Type GetType()
Definition: Converter.cc:742
bool GetBool(std::stringstream &line) const
Definition: Converter.cc:263
std::pair< const std::type_info *, int > Type
Definition: Converter.h:19
int size
Definition: db_dim_server.c:17
static std::string GetHex(const void *dat, size_t size, size_t col=0, bool prefix=true)
Definition: Converter.h:110
float data[4 *1440]
std::pair< Type, Offset > Format
Definition: Converter.h:21
bool operator!() const
Definition: Converter.h:86
static std::string ToFormat(const std::vector< std::string > &fits)
Definition: Converter.cc:1004
std::pair< int, int > Offset
Definition: Converter.h:20
std::string GetStringEol(std::stringstream &line) const
Definition: Converter.cc:326
Converter(std::ostream &out, const std::string &fmt, bool strict=true)
Definition: Converter.cc:482
const FormatList fList
Original format string.
Definition: Converter.h:33
void Print() const
Definition: Converter.cc:1086
A compiler for the DIM data format string.
Definition: Converter.h:16
bool empty() const
Definition: Converter.h:80
void AddString(std::string &str, const char *&ptr) const
Definition: Converter.cc:442
size_t GetSize() const
Definition: Converter.h:89
std::vector< std::string > ToStrings(const void *src) const
Definition: Converter.cc:948
const std::string fFormat
ostream to which output is redirected
Definition: Converter.h:32
std::vector< std::string > GetFitsFormat() const
Definition: Converter.cc:1042
std::vector< char > GetVector(const void *d, size_t size) const
Definition: Converter.cc:724
void ToFits(void *dest, const void *src, size_t size) const
Definition: Converter.cc:891