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 using namespace std;
11 
12 extern "C" {
13  extern FILE* rfio_fopen(char *path, char *mode);
14  extern int rfio_fread(void*, size_t, size_t, void*);
15  extern int rfio_fclose(FILE *fd);
16  extern int rfio_fseek(FILE *fp, long offset, int whence);
17  extern int rfio_feof(FILE *fp);
18  extern long rfio_ftell(FILE *fp);
19 }
20 
21 RawFile::RawFile() : inputFile(0), rfioFlag(false) {}
22 
23 RawFile::RawFile(const char* path) : inputFile(0), rfioFlag(false) {
24  open(path);
25 }
26 
27 RawFile* RawFile::open(const char* path) {
28 
29  //cout << " Full path: " << path << endl;
30 
31  char* chaux = new char[strlen(path)+1];
32  strcpy(chaux,path);
33  char* prefix = strtok(chaux,":");
34  //cout << " Prefix: " << prefix << endl;
35 
36  char* filename = prefix;
37  if (strlen(prefix)<strlen(path)) filename = strtok(0,":");
38  //cout << " Filename: " << filename << endl;
39 
40  if (strcmp(prefix,"rfio")==0) rfioFlag = true;
41  if (strcmp(prefix,"castor")==0) rfioFlag = true;
42 
43  if (rfioFlag) {
44  char chopt[] = "r";
45  inputFile = rfio_fopen(filename,chopt);
46  } else {
47  char chopt[] = "rb";
48  inputFile = fopen(filename,chopt);
49  }
50  if( !inputFile ) {
51  cout << "RawFile: the input file '" << filename << "' is not present" << endl;
52  } else {
53  cout << "RawFile: DAQ file '" << filename << "' was succesfully opened" << endl;
54  }
55 
56  return this;
57 
58 }
59 
61  int flag = -1;
62  if (!inputFile) return flag;
63  if (rfioFlag) {
64  flag = rfio_fclose(inputFile);
65  } else {
66  flag = fclose(inputFile);
67  }
68  inputFile = 0;
69  return flag;
70 }
71 
73 
74 FILE* RawFile::GetPointer(){ return inputFile;}
75 
76 bool RawFile::ok(){ return (inputFile!=0);}
77 
78 bool RawFile::fail(){ return !ok();}
79 
80 bool RawFile::isRFIO() { return rfioFlag;}
81 
82 int RawFile::read(void* data, size_t nbytes) {
83  if (rfioFlag) {
84  return rfio_fread(data, nbytes, 1, inputFile);
85  } else {
86  return fread(data, nbytes, 1, inputFile);
87  }
88 }
89 
90 int RawFile::seek(long offset, int whence) {
91  if (rfioFlag) {
92  return rfio_fseek(inputFile, offset, whence);
93  } else {
94  return fseek(inputFile, offset, whence);
95  }
96 }
97 
98 int RawFile::ignore(long offset) { return seek(offset, SEEK_CUR);}
99 
101  if (rfioFlag) {
102  return rfio_feof(inputFile);
103  } else {
104  return feof(inputFile);
105  }
106 }
107 
109  if (rfioFlag) {
110  return rfio_ftell(inputFile);
111  } else {
112  return ftell(inputFile);
113  }
114 }
bool ok()
It is OK (i.e. file was correctly opened)
Definition: RawFile.cc:76
int rfio_fread(void *, size_t, size_t, void *)
long int flag
Definition: mlp_lapack.h:47
long rfio_ftell(FILE *fp)
RawFile * open(const char *path)
Open file.
Definition: RawFile.cc:27
virtual ~RawFile()
Destructor.
Definition: RawFile.cc:72
bool fail()
It is not OK.
Definition: RawFile.cc:78
int read(void *data, size_t nbytes)
Read from file.
Definition: RawFile.cc:82
long tell()
Tell instruction.
Definition: RawFile.cc:108
FILE * GetPointer()
Get file pointer.
Definition: RawFile.cc:74
bool rfioFlag
Definition: RawFile.h:61
int rfio_fclose(FILE *fd)
list path
Definition: scaleCards.py:51
bool isRFIO()
Castor flag.
Definition: RawFile.cc:80
int close()
Close file if necessary.
Definition: RawFile.cc:60
int seek(long offset, int whence)
Go somewhere.
Definition: RawFile.cc:90
int rfio_feof(FILE *fp)
unsigned int offset(bool)
FILE * rfio_fopen(char *path, char *mode)
int eof()
Check end of file.
Definition: RawFile.cc:100
int ignore(long offset)
Ignore some bytes.
Definition: RawFile.cc:98
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
int rfio_fseek(FILE *fp, long offset, int whence)
FILE * inputFile
Definition: RawFile.h:60
tuple filename
Definition: lut2db_cfg.py:20
tuple cout
Definition: gather_cfg.py:121
RawFile()
Default constructor.
Definition: RawFile.cc:21