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 
16 
18  open(path);
19 }
20 
21 RawFile* RawFile::open(const char* path) {
22 
23  //cout << " Full path: " << path << endl;
24 
25  std::unique_ptr<char[]> chaux{ new char[strlen(path)+1] };
26  strcpy(chaux.get(),path);
27  char* prefix = strtok(chaux.get(),":");
28  //cout << " Prefix: " << prefix << endl;
29 
30  char* filename = prefix;
31  if (strlen(prefix)<strlen(path)) filename = strtok(nullptr,":");
32  //cout << " Filename: " << filename << endl;
33 
34  if (strcmp(prefix,"root")==0) 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 }
52 
54  int flag = -1;
55  if (!inputFile) 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:21
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
#define nullptr
bool xrootdFlag
Definition: RawFile.h:64
int close()
Close file if necessary.
Definition: RawFile.cc:53
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:82
FILE * inputFile
Definition: RawFile.h:63
RawFile()
Default constructor.
Definition: RawFile.cc:15
bool isXROOTD()
XROOTD flag.
Definition: RawFile.cc:74