Belle II Software  release-05-01-25
HLTFile.cc
1 #include <daq/rfarm/event/hltsocket/HLTFile.h>
2 #include <framework/logging/Logger.h>
3 
4 using namespace Belle2;
5 
6 bool HLTFile::open(const std::string& fileName, bool raw, const char* mode)
7 {
8  if (m_rfile) {
9  fclose(m_rfile);
10  }
11 
12  if (raw) {
13  m_rfile = std::fopen(fileName.c_str(), mode);
14  if (!m_rfile) {
15  B2ERROR("File opening failed! " << strerror(errno));
16  return false;
17  }
18  } else {
19  m_sfile.reset(new SeqFile(fileName, mode));
20  if (m_sfile->status() <= 0) {
21  B2ERROR("File opening failed! " << strerror(errno));
22  return false;
23  }
24  }
25 
26  return true;
27 }
28 
29 HLTFile::~HLTFile()
30 {
31  if (m_rfile) {
32  fclose(m_rfile);
33  }
34 }
35 
36 int HLTFile::put(char* data, int len)
37 {
38  if (not m_sfile) {
39  B2ERROR("Trying to write to a closed file");
40  }
41  const int bcount = m_sfile->write(data);
42  if (bcount <= 0) {
43  B2ERROR("Error in sending the data: " << strerror(errno));
44  return bcount;
45  }
46  B2ASSERT("Written buffer size != buffer size in data!", bcount == len);
47  return bcount;
48 }
49 
50 int HLTFile::put_wordbuf(int* data, int len)
51 {
52  if (not m_rfile) {
53  B2ERROR("Trying to write to a closed file!");
54  return -1;
55  }
56 
57  const int gcount = data[0];
58  B2ASSERT("The first entry in the data must be the buffer size!", gcount == len);
59 
60  int bcount = std::fwrite(data, sizeof(char), len * sizeof(int), m_rfile);
61  if (bcount <= 0) {
62  B2ERROR("Error in writing the data: " << strerror(errno));
63  return bcount;
64  }
65  bcount = ((bcount - 1) / sizeof(int) + 1);
66 
67  B2ASSERT("Written buffer size != buffer size in data!" << bcount << " " << len, bcount == len);
68 
69  // ATTENTION: the returned size is size / 4
70  return bcount;
71 }
72 
73 int HLTFile::get(char* data, int len)
74 {
75  const int bcount = m_sfile->read(data, len);
76  if (bcount <= 0) {
77  B2ERROR("Error in getting the data: " << strerror(errno));
78  return bcount;
79  }
80  return bcount;
81 }
82 
83 int HLTFile::get_wordbuf(int* wrdbuf, int len)
84 {
85  if (not m_rfile) {
86  B2ERROR("Trying to read from a closed file!");
87  return -1;
88  }
89  int br = std::fread(wrdbuf, sizeof(int), 1, m_rfile);
90  if (br <= 0) {
91  if (std::feof(m_rfile)) {
92  return 0;
93  }
94  B2ERROR("Error in getting the size: " << strerror(errno));
95  return br;
96  }
97 
98  const int gcount = (wrdbuf[0] - 1);
99  if (gcount > len) {
100  B2ERROR("buffer too small! " << gcount << " < " << len);
101  return -1;
102  }
103  // ATTENTION: the send size is size / 4
104  const int bcount = std::fread(&wrdbuf[1], sizeof(int), gcount, m_rfile);
105  if (bcount <= 0) {
106  if (std::feof(m_rfile)) {
107  return 0;
108  }
109  B2ERROR("Error in getting the data: " << strerror(errno));
110  return bcount;
111  }
112 
113  B2ASSERT("Read buffer size != buffer size in data: " << bcount << " != " << gcount, bcount == gcount);
114  return (wrdbuf[0]);
115 }
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::SeqFile
A class to manage I/O for a chain of blocked files.
Definition: SeqFile.h:22