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