HIPO  4.3.0
High Performance Output data format for experimental physics
datastream.h
Go to the documentation of this file.
1 //******************************************************************************
2 //* ██╗ ██╗██╗██████╗ ██████╗ ██╗ ██╗ ██████╗ *
3 //* ██║ ██║██║██╔══██╗██╔═══██╗ ██║ ██║ ██╔═████╗ *
4 //* ███████║██║██████╔╝██║ ██║ ███████║ ██║██╔██║ *
5 //* ██╔══██║██║██╔═══╝ ██║ ██║ ╚════██║ ████╔╝██║ *
6 //* ██║ ██║██║██║ ╚██████╔╝ ██║██╗╚██████╔╝ *
7 //* ╚═╝ ╚═╝╚═╝╚═╝ ╚═════╝ ╚═╝╚═╝ ╚═════╝ *
8 //************************ Jefferson National Lab (2017) ***********************
9 /*
10  * Copyright (c) 2017. Jefferson Lab (JLab). All rights reserved. Permission
11  * to use, copy, modify, and distribute this software and its documentation
12  * for educational, research, and not-for-profit purposes, without fee and
13  * without a signed licensing agreement.
14  *
15  * IN NO EVENT SHALL JLAB BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL
16  * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
17  * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF JLAB HAS
18  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19  *
20  * JLAB SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE. THE HIPO DATA FORMAT SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
23  * ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". JLAB HAS NO OBLIGATION TO
24  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25  *
26  * This software was developed under the United States Government license.
27  * For more information contact author at gavalian@jlab.org
28  * Department of Experimental Nuclear Physics, Jefferson Lab.
29  */
30 /*
31  * This class is generic class that is suppose to provide a data stream.
32  * it is implemented as a test case for xrootd, functionality to switch
33  * between the regular file and files provided by xrootd server.
34  * File: datastream.h
35  * Author: gavalian
36  *
37  * Created on May 5, 2020, 9:18 PM
38  */
39 
45 
46 #ifndef DATASTREAM_H
47 #define DATASTREAM_H
48 
49 #include <iostream>
50 #include <utility>
51 #include <vector>
52 #include <fstream>
53 #include <cstdio>
54 #include <cstdlib>
55 #include <memory>
56 #include <climits>
57 
58 #ifdef __XROOTD__
59 #include <XrdClient/XrdClient.hh>
60 #include "XrdClient/XrdClientEnv.hh"
61 #include "XrdSys/XrdSysHeaders.hh"
62 #endif
63 
64 namespace hipo {
65 
66 
68 
74 class datastream {
75 
76  private:
77 
78  std::ifstream inputStream;
79  std::string remoteAddress;
80  int streamType = 1;
81 
82 
83  public:
84 
86  virtual ~datastream();
87 
89  virtual long size(){ return 0;}
91  virtual long position(){ return 0;}
95  virtual long position(long pos){ return 0;}
98  virtual void open(const char *filename){}
103  virtual int read(char *s, int size){return 0;}
104  };
105 
109 
110  private:
111  std::ifstream inputStream;
112 
113  public:
114 
119  if(inputStream.is_open()==true){
120  inputStream.close();
121  }
122  }
123 
125  long size(){
126  long pos = inputStream.tellg();
127  inputStream.seekg(0,std::ios_base::end);
128  long lastpos = inputStream.tellg();
129  inputStream.seekg(pos,std::ios_base::beg);
130  return lastpos;
131  }
132 
135  void open(const char *filename){
136  inputStream.open(filename, std::ios::binary);
137  }
139  long position(){ return inputStream.tellg();}
143  long position(long pos){ inputStream.seekg(pos,std::ios_base::beg); return pos; }
148  int read(char *s, int size){inputStream.read(s,size); return size;}
149 };
150 
154 
155 private:
156  #ifdef __XROOTD__
157  kXR_unt16 open_mode = (kXR_ur);
158  kXR_unt16 open_opts = (1);
159  XrdClient *cli = NULL;
160  #endif
161  long streamPosition = 0;
162 
163 public:
168 
170  long size();
172  long position();
176  long position(long pos);
177  //virtual void setAdress(const char *address){}
180  void open(const char *filename);
185  int read(char *s, int size);
186 };
187 }
188 #endif /* DATASTREAM_H */
Data stream implementation for local file I/O using std::ifstream.
Definition: datastream.h:108
datastreamLocalFile()
Default constructor.
void open(const char *filename)
Open a local file for binary reading.
Definition: datastream.h:135
int read(char *s, int size)
Read bytes from the file.
Definition: datastream.h:148
long position(long pos)
Seek to the given byte position.
Definition: datastream.h:143
~datastreamLocalFile()
Destructor; closes the file if open.
Definition: datastream.h:118
Data stream implementation for XRootD remote file access.
Definition: datastream.h:153
~datastreamXrootd()
Destructor; closes the remote connection.
Definition: datastream.cpp:61
void open(const char *filename)
Open a remote file via XRootD.
Definition: datastream.cpp:68
datastreamXrootd()
Default constructor.
Definition: datastream.cpp:51
int read(char *s, int size)
Read bytes from the remote file.
Definition: datastream.cpp:89
Abstract base class for data stream I/O.
Definition: datastream.h:74
virtual int read(char *s, int size)
Read bytes from the stream.
Definition: datastream.h:103
virtual void open(const char *filename)
Open a data source.
Definition: datastream.h:98
virtual long position(long pos)
Seek to a position in the stream.
Definition: datastream.h:95
virtual long size()
Definition: datastream.h:89
virtual ~datastream()
virtual long position()
Definition: datastream.h:91
Definition: bank.cpp:47