CMS 3D CMS Logo

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