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