CMS 3D CMS Logo

RawFile.cc
Go to the documentation of this file.
1 
7 #include <cstring>
8 #include <cstdio>
9 #include <memory>
10 
11 #include <XrdPosix/XrdPosixExtern.hh>
12 
13 using namespace std;
14 
15 RawFile::RawFile() : inputFile(nullptr), xrootdFlag(false) {}
16 
17 RawFile::RawFile(const char* path) : inputFile(nullptr), xrootdFlag(false) { open(path); }
18 
19 RawFile* RawFile::open(const char* path) {
20  //cout << " Full path: " << path << endl;
21 
22  std::unique_ptr<char[]> chaux{new char[strlen(path) + 1]};
23  strcpy(chaux.get(), path);
24  char* saveptr;
25  char* prefix = strtok_r(chaux.get(), ":", &saveptr);
26  //cout << " Prefix: " << prefix << endl;
27 
28  char* filename = prefix;
29  if (strlen(prefix) < strlen(path))
30  filename = strtok_r(nullptr, ":", &saveptr);
31  //cout << " Filename: " << filename << endl;
32 
33  if (strcmp(prefix, "root") == 0)
34  xrootdFlag = true;
35 
36  if (xrootdFlag) {
37  char chopt[] = "rb";
38  inputFile = XrdPosix_Fopen(path, chopt);
39  } else {
40  char chopt[] = "rb";
41  inputFile = fopen(filename, chopt);
42  }
43  if (!inputFile) {
44  cout << "RawFile: the input file '" << path << "' is not present" << endl;
45  } else {
46  cout << "RawFile: DAQ file '" << path << "' was succesfully opened" << endl;
47  }
48 
49  return this;
50 }
51 
53  int flag = -1;
54  if (!inputFile)
55  return flag;
56 
57  if (xrootdFlag) {
58  flag = XrdPosix_Fclose(inputFile);
59  } else {
60  flag = fclose(inputFile);
61  }
62  inputFile = nullptr;
63  return flag;
64 }
65 
67 
68 FILE* RawFile::GetPointer() { return inputFile; }
69 
70 bool RawFile::ok() { return (inputFile != nullptr); }
71 
72 bool RawFile::fail() { return !ok(); }
73 
74 bool RawFile::isXROOTD() { return xrootdFlag; }
75 
76 int RawFile::read(void* data, size_t nbytes) {
77  if (xrootdFlag) {
78  return XrdPosix_Fread(data, nbytes, 1, inputFile);
79  } else {
80  return fread(data, nbytes, 1, inputFile);
81  }
82 }
83 
84 int RawFile::seek(long offset, int whence) {
85  if (xrootdFlag) {
86  return XrdPosix_Fseek(inputFile, offset, whence);
87  } else {
88  return fseek(inputFile, offset, whence);
89  }
90 }
91 
92 int RawFile::ignore(long offset) { return seek(offset, SEEK_CUR); }
93 
94 int RawFile::eof() {
95  return feof(inputFile); // Also for XROOTD
96 }
97 
98 long RawFile::tell() {
99  if (xrootdFlag) {
100  return XrdPosix_Ftell(inputFile);
101  } else {
102  return ftell(inputFile);
103  }
104 }
bool ok()
It is OK (i.e. file was correctly opened)
Definition: RawFile.cc:70
RawFile * open(const char *path)
Open file.
Definition: RawFile.cc:19
virtual ~RawFile()
Destructor.
Definition: RawFile.cc:66
bool fail()
It is not OK.
Definition: RawFile.cc:72
int read(void *data, size_t nbytes)
Read from file.
Definition: RawFile.cc:76
long tell()
Tell instruction.
Definition: RawFile.cc:98
FILE * GetPointer()
Get file pointer.
Definition: RawFile.cc:68
bool xrootdFlag
Definition: RawFile.h:63
int close()
Close file if necessary.
Definition: RawFile.cc:52
int seek(long offset, int whence)
Go somewhere.
Definition: RawFile.cc:84
int eof()
Check end of file.
Definition: RawFile.cc:94
int ignore(long offset)
Ignore some bytes.
Definition: RawFile.cc:92
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
FILE * inputFile
Definition: RawFile.h:62
RawFile()
Default constructor.
Definition: RawFile.cc:15
bool isXROOTD()
XROOTD flag.
Definition: RawFile.cc:74