test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 extern "C" {
15  extern FILE* rfio_fopen(char *path, char *mode);
16  extern int rfio_fread(void*, size_t, size_t, void*);
17  extern int rfio_fclose(FILE *fd);
18  extern int rfio_fseek(FILE *fp, long offset, int whence);
19  extern int rfio_feof(FILE *fp);
20  extern long rfio_ftell(FILE *fp);
21 }
22 
23 RawFile::RawFile() : inputFile(0), rfioFlag(false), xrootdFlag(false) {}
24 
25 RawFile::RawFile(const char* path) : inputFile(0), rfioFlag(false), xrootdFlag(false) {
26  open(path);
27 }
28 
29 RawFile* RawFile::open(const char* path) {
30 
31  //cout << " Full path: " << path << endl;
32 
33  char* chaux = new char[strlen(path)+1];
34  strcpy(chaux,path);
35  char* prefix = strtok(chaux,":");
36  //cout << " Prefix: " << prefix << endl;
37 
38  char* filename = prefix;
39  if (strlen(prefix)<strlen(path)) filename = strtok(0,":");
40  //cout << " Filename: " << filename << endl;
41 
42  if (strcmp(prefix,"rfio")==0) rfioFlag = true;
43  if (strcmp(prefix,"castor")==0) rfioFlag = true;
44  if (strcmp(prefix,"root")==0) xrootdFlag = true;
45 
46  if (xrootdFlag) {
47  char chopt[] = "rb";
48  inputFile = XrdPosix_Fopen(path,chopt);
49  } else if (rfioFlag) {
50  char chopt[] = "r";
51  inputFile = rfio_fopen(filename,chopt);
52  } else {
53  char chopt[] = "rb";
54  inputFile = fopen(filename,chopt);
55  }
56  if( !inputFile ) {
57  cout << "RawFile: the input file '" << path << "' is not present" << endl;
58  } else {
59  cout << "RawFile: DAQ file '" << path << "' was succesfully opened" << endl;
60  }
61 
62  return this;
63 
64 }
65 
67  int flag = -1;
68  if (!inputFile) return flag;
69 
70  if (xrootdFlag) {
71  flag = XrdPosix_Fclose(inputFile);
72  }
73  else if (rfioFlag) {
74  flag = rfio_fclose(inputFile);
75  } else {
76  flag = fclose(inputFile);
77  }
78  inputFile = 0;
79  return flag;
80 }
81 
83 
84 FILE* RawFile::GetPointer(){ return inputFile;}
85 
86 bool RawFile::ok(){ return (inputFile!=0);}
87 
88 bool RawFile::fail(){ return !ok();}
89 
90 bool RawFile::isRFIO() { return rfioFlag;}
91 
92 bool RawFile::isXROOTD() { return xrootdFlag;}
93 
94 int RawFile::read(void* data, size_t nbytes) {
95  if (xrootdFlag) {
96  return XrdPosix_Fread(data,nbytes,1,inputFile);
97  }
98  else if (rfioFlag) {
99  return rfio_fread(data, nbytes, 1, inputFile);
100  } else {
101  return fread(data, nbytes, 1, inputFile);
102  }
103 }
104 
105 int RawFile::seek(long offset, int whence) {
106  if (xrootdFlag) {
107  return XrdPosix_Fseek(inputFile, offset, whence);
108  }
109  else if (rfioFlag) {
110  return rfio_fseek(inputFile, offset, whence);
111  } else {
112  return fseek(inputFile, offset, whence);
113  }
114 }
115 
116 int RawFile::ignore(long offset) { return seek(offset, SEEK_CUR);}
117 
119  if (rfioFlag) {
120  return rfio_feof(inputFile);
121  } else {
122  return feof(inputFile); // Also for XROOTD
123  }
124 }
125 
127  if (xrootdFlag) {
128  return XrdPosix_Ftell(inputFile);
129  }
130  else if (rfioFlag) {
131  return rfio_ftell(inputFile);
132  } else {
133  return ftell(inputFile);
134  }
135 }
bool ok()
It is OK (i.e. file was correctly opened)
Definition: RawFile.cc:86
int rfio_fread(void *, size_t, size_t, void *)
long rfio_ftell(FILE *fp)
RawFile * open(const char *path)
Open file.
Definition: RawFile.cc:29
virtual ~RawFile()
Destructor.
Definition: RawFile.cc:82
bool fail()
It is not OK.
Definition: RawFile.cc:88
int read(void *data, size_t nbytes)
Read from file.
Definition: RawFile.cc:94
long tell()
Tell instruction.
Definition: RawFile.cc:126
FILE * GetPointer()
Get file pointer.
Definition: RawFile.cc:84
bool rfioFlag
Definition: RawFile.h:64
int rfio_fclose(FILE *fd)
bool xrootdFlag
Definition: RawFile.h:65
bool isRFIO()
Castor flag.
Definition: RawFile.cc:90
int close()
Close file if necessary.
Definition: RawFile.cc:66
int seek(long offset, int whence)
Go somewhere.
Definition: RawFile.cc:105
int rfio_feof(FILE *fp)
tuple fd
Definition: ztee.py:136
FILE * rfio_fopen(char *path, char *mode)
int eof()
Check end of file.
Definition: RawFile.cc:118
int ignore(long offset)
Ignore some bytes.
Definition: RawFile.cc:116
int rfio_fseek(FILE *fp, long offset, int whence)
FILE * inputFile
Definition: RawFile.h:63
tuple filename
Definition: lut2db_cfg.py:20
tuple cout
Definition: gather_cfg.py:145
volatile std::atomic< bool > shutdown_flag false
RawFile()
Default constructor.
Definition: RawFile.cc:23
bool isXROOTD()
XROOTD flag.
Definition: RawFile.cc:92