EUDAQ
 All Classes Namespaces Files Functions Variables Pages
Utils.hh
Go to the documentation of this file.
1 #ifndef EUDAQ_INCLUDED_Utils
2 #define EUDAQ_INCLUDED_Utils
3 
9 #include <string>
10 #include <vector>
11 #include <sstream>
12 #include <iomanip>
13 #include <stdexcept>
14 #include <fstream>
15 #include <sys/types.h>
16 #include "eudaq/Platform.hh"
17 
18 #if ((defined WIN32) && (defined __CINT__))
19 typedef unsigned long long uint64_t typedef long long
20  int64_t typedef unsigned int uint32_t typedef int int32_t
21 #else
22 #include <cstdint>
23 #endif
24 
25  namespace eudaq {
26 
27  std::string DLLEXPORT ucase(const std::string &);
28  std::string DLLEXPORT lcase(const std::string &);
29  std::string DLLEXPORT trim(const std::string &s);
30  std::string DLLEXPORT firstline(const std::string &s);
31  std::string DLLEXPORT escape(const std::string &);
32  std::vector<std::string> DLLEXPORT
33  split(const std::string &str, const std::string &delim = "\t");
34  std::vector<std::string> DLLEXPORT
35  split(const std::string &str, const std::string &delim, bool dotrim);
36 
37  void DLLEXPORT bool2uchar(const bool *inBegin, const bool *inEnd,
38  std::vector<unsigned char> &out);
39  void DLLEXPORT uchar2bool(const unsigned char *inBegin,
40  const unsigned char *inEnd, std::vector<bool> &out);
41 
45  void DLLEXPORT mSleep(unsigned ms);
46 
53  template <typename T>
54  inline std::string to_string(const T &x, int digits = 0) {
55  std::ostringstream s;
56  s << std::setfill('0') << std::setw(digits) << x;
57  return s.str();
58  }
59 
60  template <typename T>
61  inline std::string to_string(const std::vector<T> &x, const std::string &sep,
62  int digits = 0) {
63  std::ostringstream s;
64  if (x.size() > 0)
65  s << to_string(x[0], digits);
66  for (size_t i = 1; i < x.size(); ++i) {
67  s << sep << to_string(x[i], digits);
68  }
69  return s.str();
70  }
71 
72  template <typename T>
73  inline std::string to_string(const std::vector<T> &x, int digits = 0) {
74  return to_string(x, ",", digits);
75  }
76 
77  inline std::string to_string(const std::string &x, int /*digits*/ = 0) {
78  return x;
79  }
80  inline std::string to_string(const char *x, int /*digits*/ = 0) { return x; }
81 
88  template <typename T> inline std::string to_hex(const T &x, int digits = 0) {
89  std::ostringstream s;
90  s << std::hex << std::setfill('0') << std::setw(digits) << x;
91  return s.str();
92  }
93 
94  template <> inline std::string to_hex(const unsigned char &x, int digits) {
95  return to_hex((int)x, digits);
96  }
97 
98  template <> inline std::string to_hex(const signed char &x, int digits) {
99  return to_hex((int)x, digits);
100  }
101 
102  template <> inline std::string to_hex(const char &x, int digits) {
103  return to_hex((unsigned char)x, digits);
104  }
105 
114  template <typename T>
115  inline T DLLEXPORT from_string(const std::string &x, const T &def = 0) {
116  if (x == "")
117  return def;
118  T ret = def;
119  std::istringstream s(x);
120  s >> ret;
121  char remain = '\0';
122  s >> remain;
123  if (remain)
124  throw std::invalid_argument("Invalid argument: " + x);
125  return ret;
126  }
127 
128  template <>
129  inline std::string DLLEXPORT
130  from_string(const std::string &x, const std::string &def) {
131  return x == "" ? def : x;
132  }
133 
134  template <>
135  int64_t DLLEXPORT from_string(const std::string &x, const int64_t &def);
136  template <>
137  uint64_t DLLEXPORT from_string(const std::string &x, const uint64_t &def);
138  template <>
139  inline int32_t DLLEXPORT
140  from_string(const std::string &x, const int32_t &def) {
141  return static_cast<int32_t>(from_string(x, (int64_t)def));
142  }
143  template <>
144  inline uint32_t from_string(const std::string &x, const uint32_t &def) {
145  return static_cast<uint32_t>(from_string(x, (uint64_t)def));
146  }
147 
148  template <typename T> struct Holder {
149  Holder(T val) : m_val(val) {}
150  T m_val;
151  };
152 
153  template <typename T> struct hexdec_t {
154  enum { DIGITS = 2 * sizeof(T) };
155  hexdec_t(T val, unsigned hexdigits) : m_val(val), m_dig(hexdigits) {}
156  T m_val;
157  unsigned m_dig;
158  };
159 
160  template <typename T>
161  inline hexdec_t<T> hexdec(T val, unsigned hexdigits = hexdec_t<T>::DIGITS) {
162  return hexdec_t<T>(val, hexdigits);
163  }
164 
165  template <typename T>
166  inline std::ostream &operator<<(std::ostream & os, const hexdec_t<T> &h) {
167  return os << "0x" << to_hex(h.m_val, h.m_dig) << " (" << h.m_val << ")";
168  }
169 
170  template <>
171  inline std::ostream &operator<<(std::ostream & os,
172  const hexdec_t<unsigned char> &h) {
173  return os << (int)h.m_val << " (0x" << to_hex(h.m_val, h.m_dig) << ")";
174  }
175 
176  template <>
177  inline std::ostream &operator<<(std::ostream & os,
178  const hexdec_t<signed char> &h) {
179  return os << (int)h.m_val << " (0x" << to_hex(h.m_val, h.m_dig) << ")";
180  }
181 
182  template <>
183  inline std::ostream &operator<<(std::ostream & os, const hexdec_t<char> &h) {
184  return os << (int)(unsigned char)h.m_val << " (0x"
185  << to_hex(h.m_val, h.m_dig) << ")";
186  }
187 
188  template <typename T> unsigned char *uchar_cast(T * x) {
189  return reinterpret_cast<unsigned char *>(x);
190  }
191 
192  template <typename T> unsigned char *uchar_cast(std::vector<T> & x) {
193  return uchar_cast(&x[0]);
194  }
195 
196  template <typename T> const unsigned char *constuchar_cast(const T *x) {
197  return reinterpret_cast<const unsigned char *>(x);
198  }
199 
200  template <typename T>
201  const unsigned char *constuchar_cast(const std::vector<T> &x) {
202  return constuchar_cast(&x[0]);
203  }
204 
205  template <typename T> inline T getbigendian(const unsigned char *ptr) {
206 #if (defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN) || \
207  (defined(__DARWIN_BYTE_ORDER) && \
208  __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN)
209  return *reinterpret_cast<const T *>(ptr);
210 #else
211  T result = 0;
212  for (size_t i = 0; i < sizeof(T); ++i) {
213  result <<= 8;
214  result += *ptr++;
215  }
216  return result;
217 #endif
218  }
219 
220  template <typename T> inline T getlittleendian(const unsigned char *ptr) {
221 #if (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
222  (defined(__DARWIN_BYTE_ORDER) && \
223  __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
224  return *reinterpret_cast<const T *>(ptr);
225 #else
226  T result = 0;
227  for (size_t i = 0; i < sizeof(T); ++i) {
228  result += *ptr++ << (8 * i);
229  }
230  return result;
231 #endif
232  }
233 
234  template <typename T>
235  inline void setbigendian(unsigned char *ptr, const T &val) {
236 #if (defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN) || \
237  (defined(__DARWIN_BYTE_ORDER) && \
238  __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN)
239  *reinterpret_cast<T *>(ptr) = val;
240 #else
241  T tmp = val;
242  ptr += sizeof(T);
243  for (size_t i = 0; i < sizeof(T); ++i) {
244  *--ptr = tmp & 0xff;
245  tmp >>= 8;
246  }
247 #endif
248  }
249 
250  template <typename T>
251  inline void setlittleendian(unsigned char *ptr, const T &val) {
252 #if (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
253  (defined(__DARWIN_BYTE_ORDER) && \
254  __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
255  *reinterpret_cast<T *>(ptr) = val;
256 #else
257  T tmp = val;
258  for (size_t i = 0; i < sizeof(T); ++i) {
259  *ptr++ = tmp & 0xff;
260  tmp >>= 8;
261  }
262 #endif
263  }
264 
265  std::string DLLEXPORT ReadLineFromFile(const std::string &fname);
266 
267  template <typename T>
268  inline T ReadFromFile(const std::string &fname, const T &def = 0) {
269  return from_string(ReadLineFromFile(fname), def);
270  }
271 
272  void DLLEXPORT
273  WriteStringToFile(const std::string &fname, const std::string &val);
274 
275  template <typename T>
276  inline void WriteToFile(const std::string &fname, const T &val) {
277  WriteStringToFile(fname, to_string(val));
278  }
279 }
280 
281 #endif // EUDAQ_INCLUDED_Utils
void DLLEXPORT mSleep(unsigned ms)
Definition: Utils.cc:95
Definition: Utils.hh:153
std::string DLLEXPORT trim(const std::string &s)
Definition: Utils.cc:46
T DLLEXPORT from_string(const std::string &x, const T &def=0)
Definition: Utils.hh:115
std::string to_string(const T &x, int digits=0)
Definition: Utils.hh:54
std::string to_hex(const T &x, int digits=0)
Definition: Utils.hh:88
Definition: Utils.hh:148