CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/IORawData/DTCommissioning/src/RawFile.cc

Go to the documentation of this file.
00001 
00006 #include <IORawData/DTCommissioning/src/RawFile.h>
00007 #include <cstring>
00008 #include <cstdio>
00009 
00010 using namespace std;
00011 
00012 extern "C" {
00013   extern FILE* rfio_fopen(char *path, char *mode);
00014   extern int   rfio_fread(void*, size_t, size_t, void*);
00015   extern int   rfio_fclose(FILE *fd);
00016   extern int   rfio_fseek(FILE *fp, long offset, int whence);
00017   extern int   rfio_feof(FILE *fp);
00018   extern long  rfio_ftell(FILE *fp);
00019 }
00020                                                                                 
00021 RawFile::RawFile() : inputFile(0), rfioFlag(false) {}
00022 
00023 RawFile::RawFile(const char* path) : inputFile(0), rfioFlag(false) {
00024   open(path);
00025 }
00026 
00027 RawFile* RawFile::open(const char* path) {
00028 
00029   //cout << " Full path: " << path << endl;
00030 
00031   char* chaux = new char[strlen(path)+1];
00032   strcpy(chaux,path);
00033   char* prefix = strtok(chaux,":");
00034   //cout << " Prefix: " << prefix << endl;
00035 
00036   char* filename = prefix;
00037   if (strlen(prefix)<strlen(path)) filename = strtok(0,":");
00038   //cout << " Filename: " << filename << endl;
00039 
00040   if (strcmp(prefix,"rfio")==0) rfioFlag = true;
00041   if (strcmp(prefix,"castor")==0) rfioFlag = true;
00042 
00043   if (rfioFlag) {
00044     char chopt[] = "r";
00045     inputFile = rfio_fopen(filename,chopt);
00046   } else {
00047     char chopt[] = "rb";
00048     inputFile = fopen(filename,chopt);
00049   }
00050   if( !inputFile ) {
00051       cout << "RawFile: the input file '" << filename << "' is not present" << endl;
00052   } else {
00053       cout << "RawFile: DAQ file '" << filename << "' was succesfully opened" << endl;
00054   }
00055 
00056   return this;
00057 
00058 }
00059 
00060 int RawFile::close() {
00061   int flag = -1;
00062   if (!inputFile) return flag;
00063   if (rfioFlag) {
00064       flag = rfio_fclose(inputFile);
00065   } else {
00066       flag = fclose(inputFile);
00067   }
00068   inputFile = 0;
00069   return flag;
00070 }
00071 
00072 RawFile::~RawFile(){close();}
00073 
00074 FILE* RawFile::GetPointer(){ return inputFile;}
00075 
00076 bool RawFile::ok(){ return (inputFile!=0);}
00077 
00078 bool RawFile::fail(){ return !ok();}
00079 
00080 bool RawFile::isRFIO() { return rfioFlag;}
00081 
00082 int RawFile::read(void* data, size_t nbytes) {
00083       if (rfioFlag) {
00084             return rfio_fread(data, nbytes, 1, inputFile);
00085       } else {
00086             return fread(data, nbytes, 1, inputFile);
00087       }
00088 }
00089 
00090 int RawFile::seek(long offset, int whence) {
00091       if (rfioFlag) {
00092             return rfio_fseek(inputFile, offset, whence);
00093       } else {
00094             return fseek(inputFile, offset, whence);
00095       }
00096 }
00097 
00098 int RawFile::ignore(long offset) { return seek(offset, SEEK_CUR);}
00099 
00100 int RawFile::eof() {
00101   if (rfioFlag) {
00102       return rfio_feof(inputFile);
00103   } else {
00104       return feof(inputFile);
00105   }
00106 }
00107 
00108 long RawFile::tell() {
00109   if (rfioFlag) {
00110       return rfio_ftell(inputFile);
00111   } else {
00112       return ftell(inputFile);
00113   }
00114 }