Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:23

0001 /** \file
0002  *
0003  *  \author J. Alcaraz
0004  */
0005 
0006 #include <IORawData/DTCommissioning/plugins/RawFile.h>
0007 #include <cstring>
0008 #include <cstdio>
0009 #include <memory>
0010 
0011 #include <XrdPosix/XrdPosixExtern.hh>
0012 
0013 using namespace std;
0014 
0015 RawFile::RawFile() : inputFile(nullptr), xrootdFlag(false) {}
0016 
0017 RawFile::RawFile(const char* path) : inputFile(nullptr), xrootdFlag(false) { open(path); }
0018 
0019 RawFile* RawFile::open(const char* path) {
0020   //cout << " Full path: " << path << endl;
0021 
0022   std::unique_ptr<char[]> chaux{new char[strlen(path) + 1]};
0023   strcpy(chaux.get(), path);
0024   char* saveptr;
0025   char* prefix = strtok_r(chaux.get(), ":", &saveptr);
0026   //cout << " Prefix: " << prefix << endl;
0027 
0028   char* filename = prefix;
0029   if (strlen(prefix) < strlen(path))
0030     filename = strtok_r(nullptr, ":", &saveptr);
0031   //cout << " Filename: " << filename << endl;
0032 
0033   if (strcmp(prefix, "root") == 0)
0034     xrootdFlag = true;
0035 
0036   if (xrootdFlag) {
0037     char chopt[] = "rb";
0038     inputFile = XrdPosix_Fopen(path, chopt);
0039   } else {
0040     char chopt[] = "rb";
0041     inputFile = fopen(filename, chopt);
0042   }
0043   if (!inputFile) {
0044     cout << "RawFile: the input file '" << path << "' is not present" << endl;
0045   } else {
0046     cout << "RawFile: DAQ file '" << path << "' was succesfully opened" << endl;
0047   }
0048 
0049   return this;
0050 }
0051 
0052 int RawFile::close() {
0053   int flag = -1;
0054   if (!inputFile)
0055     return flag;
0056 
0057   if (xrootdFlag) {
0058     flag = XrdPosix_Fclose(inputFile);
0059   } else {
0060     flag = fclose(inputFile);
0061   }
0062   inputFile = nullptr;
0063   return flag;
0064 }
0065 
0066 RawFile::~RawFile() { close(); }
0067 
0068 FILE* RawFile::GetPointer() { return inputFile; }
0069 
0070 bool RawFile::ok() { return (inputFile != nullptr); }
0071 
0072 bool RawFile::fail() { return !ok(); }
0073 
0074 bool RawFile::isXROOTD() { return xrootdFlag; }
0075 
0076 int RawFile::read(void* data, size_t nbytes) {
0077   if (xrootdFlag) {
0078     return XrdPosix_Fread(data, nbytes, 1, inputFile);
0079   } else {
0080     return fread(data, nbytes, 1, inputFile);
0081   }
0082 }
0083 
0084 int RawFile::seek(long offset, int whence) {
0085   if (xrootdFlag) {
0086     return XrdPosix_Fseek(inputFile, offset, whence);
0087   } else {
0088     return fseek(inputFile, offset, whence);
0089   }
0090 }
0091 
0092 int RawFile::ignore(long offset) { return seek(offset, SEEK_CUR); }
0093 
0094 int RawFile::eof() {
0095   return feof(inputFile);  // Also for XROOTD
0096 }
0097 
0098 long RawFile::tell() {
0099   if (xrootdFlag) {
0100     return XrdPosix_Ftell(inputFile);
0101   } else {
0102     return ftell(inputFile);
0103   }
0104 }