Belle II Software  release-05-01-25
Reader.cc
1 #include "daq/slc/base/Reader.h"
2 
3 #include "daq/slc/base/Serializable.h"
4 
5 #include <unistd.h>
6 
7 #define __ENDIAN_L__
8 
9 using namespace Belle2;
10 
11 void Reader::reverse(void* buf, size_t len)
12 {
13  char tmp;
14  for (int c = 0 ; c < (int)len / 2 ; c ++) {
15  tmp = *((char*)buf + c);
16  *((char*)buf + c) = *((char*)buf + len - 1 - c);
17  *((char*)buf + len - 1 - c) = tmp;
18  }
19 }
20 
21 bool Reader::readBool()
22 {
23  bool v;
24  read(&v, sizeof(bool));
25  return v;
26 }
27 
28 char Reader::readChar()
29 {
30  char v;
31  read(&v, sizeof(char));
32  return v;
33 }
34 
35 unsigned char Reader::readUChar()
36 {
37  unsigned char v;
38  read(&v, sizeof(unsigned char));
39  return v;
40 }
41 
42 short Reader::readShort()
43 {
44  short v;
45  read(&v, sizeof(short));
46 #if defined(__ENDIAN_L__)
47  reverse(&v, sizeof(short));
48 #endif
49  return v;
50 }
51 
52 unsigned short Reader::readUShort()
53 {
54  unsigned short v;
55  read(&v, sizeof(unsigned short));
56 #if defined(__ENDIAN_L__)
57  reverse(&v, sizeof(unsigned short));
58 #endif
59  return v;
60 }
61 
62 int Reader::readInt()
63 {
64  int v;
65  read(&v, sizeof(int));
66 #if defined(__ENDIAN_L__)
67  reverse(&v, sizeof(int));
68 #endif
69  return v;
70 }
71 
72 unsigned int Reader::readUInt()
73 {
74  unsigned int v;
75  read(&v, sizeof(unsigned int));
76 #if defined(__ENDIAN_L__)
77  reverse(&v, sizeof(unsigned int));
78 #endif
79  return v;
80 }
81 
82 long long Reader::readLong()
83 {
84  long long v;
85  read(&v, sizeof(long long));
86 #if defined(__ENDIAN_L__)
87  reverse(&v, sizeof(long long));
88 #endif
89  return v;
90 }
91 
92 unsigned long long Reader::readULong()
93 {
94  unsigned long long v;
95  read(&v, sizeof(unsigned long long));
96 #if defined(__ENDIAN_L__)
97  reverse(&v, sizeof(unsigned long long));
98 #endif
99  return v;
100 }
101 
102 float Reader::readFloat()
103 {
104  float v;
105  read(&v, sizeof(float));
106 #if defined(__ENDIAN_L__)
107  reverse(&v, sizeof(float));
108 #endif
109  return v;
110 }
111 
112 double Reader::readDouble()
113 {
114  double v;
115  read(&v, sizeof(double));
116 #if defined(__ENDIAN_L__)
117  reverse(&v, sizeof(double));
118 #endif
119  return v;
120 }
121 
122 const std::string Reader::readString()
123 {
124  int count = readInt();
125  char str[1025];
126  std::string v;
127  while (count > 0) {
128  int c = (count > 1024) ? 1024 : count;
129  read(str, c);
130  v.append(str, 0, c);
131  count -= c;
132  }
133  return v;
134 }
135 
136 void Reader::readObject(Serializable& v)
137 {
138  v.readObject(*this);
139 }
Belle2::Serializable
Definition: Serializable.h:13
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19