Project
Loading...
Searching...
No Matches
IO.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include "IO.h"
13#include <stdexcept>
14#include <fmt/format.h>
15#include <iostream>
16#include "DigitFileFormat.h"
17
18namespace o2::mch::io::impl
19{
20int readNofItems(std::istream& in, const char* itemName)
21{
22 int nitems(-1);
23 in.read(reinterpret_cast<char*>(&nitems), sizeof(int));
24 if (in.fail() || nitems < 0) {
25 throw std::length_error(fmt::format("invalid input : cannot get number of {}", itemName));
26 }
27 return nitems;
28}
29
30void writeNofItems(std::ostream& out, uint32_t nofItems)
31{
32 out.write(reinterpret_cast<const char*>(&nofItems), sizeof(uint32_t));
33}
34
35int advance(std::istream& in, size_t itemByteSize, const char* itemName)
36{
37 if (in.peek() == EOF) {
38 return -1;
39 }
40 // get the number of items
41 int nitems = readNofItems(in, itemName);
42 // move forward of n items
43 auto current = in.tellg();
44 in.seekg(current + static_cast<decltype(current)>(nitems * itemByteSize));
45 return nitems;
46}
47
48std::pair<int, int> advanceOneEvent(std::istream& in, int fileFormatVersion)
49{
50 auto dff = digitFileFormats[fileFormatVersion];
51 int nrofs = advance(in, dff.rofSize, "rofs");
52 if (nrofs < 0) {
53 return std::make_pair(-1, -1);
54 }
55 int ndigits = advance(in, dff.digitSize, "digits");
56 return std::make_pair(nrofs, ndigits);
57}
58} // namespace o2::mch::io::impl
std::pair< int, int > advanceOneEvent(std::istream &in, int fileFormatVersion)
Definition IO.cxx:48
int advance(std::istream &in, size_t itemByteSize, const char *itemName)
Definition IO.cxx:35
void writeNofItems(std::ostream &out, uint32_t nofItems)
Definition IO.cxx:30
int readNofItems(std::istream &in, const char *itemName)
Definition IO.cxx:20
std::array< DigitFileFormat, 5 > digitFileFormats