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