1 #ifndef EUDAQ_INCLUDED_Utils
2 #define EUDAQ_INCLUDED_Utils
15 #include <sys/types.h>
16 #include "eudaq/Platform.hh"
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
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);
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);
45 void DLLEXPORT
mSleep(
unsigned ms);
54 inline std::string
to_string(
const T &x,
int digits = 0) {
56 s << std::setfill(
'0') << std::setw(digits) << x;
61 inline std::string
to_string(
const std::vector<T> &x,
const std::string &sep,
66 for (
size_t i = 1; i < x.size(); ++i) {
73 inline std::string
to_string(
const std::vector<T> &x,
int digits = 0) {
77 inline std::string
to_string(
const std::string &x,
int = 0) {
80 inline std::string
to_string(
const char *x,
int = 0) {
return x; }
88 template <
typename T>
inline std::string
to_hex(
const T &x,
int digits = 0) {
90 s << std::hex << std::setfill(
'0') << std::setw(digits) << x;
94 template <>
inline std::string
to_hex(
const unsigned char &x,
int digits) {
95 return to_hex((
int)x, digits);
98 template <>
inline std::string
to_hex(
const signed char &x,
int digits) {
99 return to_hex((
int)x, digits);
102 template <>
inline std::string
to_hex(
const char &x,
int digits) {
103 return to_hex((
unsigned char)x, digits);
114 template <
typename T>
115 inline T DLLEXPORT
from_string(
const std::string &x,
const T &def = 0) {
119 std::istringstream s(x);
124 throw std::invalid_argument(
"Invalid argument: " + x);
129 inline std::string DLLEXPORT
130 from_string(
const std::string &x,
const std::string &def) {
131 return x ==
"" ? def : x;
135 int64_t DLLEXPORT
from_string(
const std::string &x,
const int64_t &def);
137 uint64_t DLLEXPORT
from_string(
const std::string &x,
const uint64_t &def);
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));
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));
149 Holder(T val) : m_val(val) {}
154 enum { DIGITS = 2 *
sizeof(T) };
155 hexdec_t(T val,
unsigned hexdigits) : m_val(val), m_dig(hexdigits) {}
160 template <
typename T>
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 <<
")";
171 inline std::ostream &operator<<(std::ostream & os,
173 return os << (int)h.m_val <<
" (0x" <<
to_hex(h.m_val, h.m_dig) <<
")";
177 inline std::ostream &operator<<(std::ostream & os,
179 return os << (int)h.m_val <<
" (0x" <<
to_hex(h.m_val, h.m_dig) <<
")";
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) <<
")";
188 template <
typename T>
unsigned char *uchar_cast(T * x) {
189 return reinterpret_cast<unsigned char *
>(x);
192 template <
typename T>
unsigned char *uchar_cast(std::vector<T> & x) {
193 return uchar_cast(&x[0]);
196 template <
typename T>
const unsigned char *constuchar_cast(
const T *x) {
197 return reinterpret_cast<const unsigned char *
>(x);
200 template <
typename T>
201 const unsigned char *constuchar_cast(
const std::vector<T> &x) {
202 return constuchar_cast(&x[0]);
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);
212 for (
size_t i = 0; i <
sizeof(T); ++i) {
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);
227 for (
size_t i = 0; i <
sizeof(T); ++i) {
228 result += *ptr++ << (8 * i);
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;
243 for (
size_t i = 0; i <
sizeof(T); ++i) {
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;
258 for (
size_t i = 0; i <
sizeof(T); ++i) {
265 std::string DLLEXPORT ReadLineFromFile(
const std::string &fname);
267 template <
typename T>
268 inline T ReadFromFile(
const std::string &fname,
const T &def = 0) {
273 WriteStringToFile(
const std::string &fname,
const std::string &val);
275 template <
typename T>
276 inline void WriteToFile(
const std::string &fname,
const T &val) {
277 WriteStringToFile(fname,
to_string(val));
281 #endif // EUDAQ_INCLUDED_Utils
void DLLEXPORT mSleep(unsigned ms)
Definition: Utils.cc:95
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