Jpp  16.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFileStreamBuffer.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JFILESTREAMBUFFER__
2 #define __JLANG__JFILESTREAMBUFFER__
3 
4 #include <streambuf>
5 #include <stdio.h>
6 #include <string.h>
7 #include <vector>
8 
9 #include "JLang/JFile.hh"
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace JLANG {}
17 namespace JPP { using namespace JLANG; }
18 
19 namespace JLANG {
20 
21 
22  /**
23  * Input file stream buffer.
24  */
26  protected virtual std::vector<char>,
27  public virtual std::streambuf
28  {
29  public:
30 
31 
32  typedef traits_type::int_type int_type;
33 
34 
35  /**
36  * Constructor.
37  *
38  * \param file file
39  * \param size size of internal buffer
40  * \param put_back number of put back characters
41  */
43  const std::size_t size = 65536,
44  const std::size_t put_back = 8) :
45  in (file),
46  memory(put_back)
47  {
48  resize(size + put_back),
49 
50  setg(this->data(), this->data(), this->data());
51  }
52 
53 
54  /**
55  * Check underflow.
56  * This method reads as many bytes as possible.
57  *
58  * \return first character if OK; else EOF
59  */
60  virtual int_type underflow() override
61  {
62  if (gptr() >= egptr()) {
63 
64  char* __begin = this->data();
65 
66  if (eback() == __begin) {
67 
68  // push put back characters
69 
70  memmove(__begin, egptr() - memory, memory);
71 
72  __begin += memory;
73  }
74 
75  const size_t len = in.read(__begin, this->size() - (__begin - this->data()));
76 
77  if (len != 0)
78  setg(this->data(), __begin, __begin + len);
79  else
80  return traits_type::eof();
81  }
82 
83  return *gptr();
84  }
85 
86 
87  /**
88  * Check availability of input.
89  * This method returns true if at least one byte can be read without blocking.
90  *
91  * \param timeout timeout
92  * \return true if ready to read; else false
93  */
94  bool in_avail(JTimeval timeout = JTimeval::min()) const
95  {
96  return JFileDescriptorMask(in).in_avail(timeout);
97  }
98 
99  protected:
101  std::size_t memory;
102  };
103 
104 
105  /**
106  * Output file stream buffer.
107  */
109  protected virtual std::vector<char>,
110  public virtual std::streambuf
111  {
112  public:
113 
114 
115  typedef traits_type::int_type int_type;
116  typedef std::streamsize streamsize;
117 
118 
119  /**
120  * Constructor.
121  *
122  * \param file file
123  * \param size size of internal buffer
124  */
126  const std::size_t size = 65536) :
127  out (file)
128  {
129  // reserve one byte for overflow character
130 
131  resize(size);
132 
133  setp(this->data(), this->data() + this->size() - 1);
134  }
135 
136 
137  /**
138  * Destructor.
139  */
141  {
142  sync();
143  }
144 
145 
146  /**
147  * Check overflow.
148  * This method writes one byte if possible.
149  *
150  * \param c character
151  * \return c if OK; else EOF
152  */
153  virtual int_type overflow(int_type c) override
154  {
155  if (c != traits_type::eof()) {
156 
157  *pptr() = (char) c;
158 
159  pbump(1);
160 
161  if (flush() == traits_type::eof()) {
162  return traits_type::eof();
163  }
164  }
165 
166  return c;
167  }
168 
169 
170  /**
171  * Synchronise buffer.
172  */
173  virtual int sync() override
174  {
175  if (flush() == traits_type::eof())
176  return -1;
177  else
178  return 0;
179  }
180 
181 
182  /**
183  * Check availability of output.
184  * This method returns true if at least one byte can be written without blocking.
185  *
186  * \param timeout timeout
187  * \return true if ready to write; else false
188  */
189  bool out_avail(JTimeval timeout = JTimeval::min()) const
190  {
191  return JFileDescriptorMask(out).out_avail(timeout);
192  }
193 
194  protected:
195  /**
196  * Flush internal buffer.
197  */
198  int flush()
199  {
200  const int len = pptr() - pbase();
201 
202  if (len != 0) {
203 
204  if (out.write(this->data(), len) != len) {
205  return traits_type::eof();
206  }
207 
208  pbump(-len);
209  }
210 
211  return len;
212  }
213 
214 
216  };
217 
218 
219 
220  /**
221  * Input and output file stream buffer.
222  */
224  public JFileInputStreamBuffer,
226  {
227  public:
228  /**
229  * Constructor.
230  *
231  * \param in input file
232  * \param out output file
233  * \param size size of internal buffer
234  * \param put_back number of put back characters
235  */
237  const JAbstractFile& out,
238  const std::size_t size = 65536,
239  const std::size_t put_back = 8) :
240  JFileInputStreamBuffer (in, size, put_back),
241  JFileOutputStreamBuffer(out, size)
242  {}
243  };
244 }
245 
246 #endif
virtual int_type underflow() override
Check underflow.
bool in_avail(JTimeval timeout=JTimeval::min()) const
Check availability of input.
then set_variable PMT_FILE set_variable DAQ_FILE set_variable OUTPUT_FILE set_variable DETECTOR else fatal Wrong number of arguments fi set_variable RUNBYRUN file
Input and output file stream buffer.
virtual int sync() override
Synchronise buffer.
static JTimeval min()
Get minimal time value.
Definition: JTimeval.hh:119
then usage $script< detector file >< detectorfile > nIf the range of floors is the first detector file is aligned to the second before the comparison nIn this
virtual int read(char *buffer, const int length)
Read data from file.
Definition: JFile.hh:74
JFileStreamBuffer(const JAbstractFile &in, const JAbstractFile &out, const std::size_t size=65536, const std::size_t put_back=8)
Constructor.
JFileOutputStreamBuffer(const JAbstractFile &file, const std::size_t size=65536)
Constructor.
int flush()
Flush internal buffer.
Auxiliary class for time values.
Definition: JTimeval.hh:26
The JFile class extends the JAbstractFile class.
Definition: JFile.hh:26
bool out_avail(JTimeval timeout=JTimeval::min())
Check availability of output.
traits_type::int_type int_type
JFileInputStreamBuffer(const JAbstractFile &file, const std::size_t size=65536, const std::size_t put_back=8)
Constructor.
Auxiliary class for method select.
The JAbstractFile class encapsulates the c-style file descriptor.
virtual ~JFileOutputStreamBuffer()
Destructor.
bool out_avail(JTimeval timeout=JTimeval::min()) const
Check availability of output.
Output file stream buffer.
virtual int_type overflow(int_type c) override
Check overflow.
Input file stream buffer.
$WORKDIR ev_configure_domsimulator txt echo process $DOM_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DOM_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
virtual int write(const char *buffer, const int length)
Write data to file.
Definition: JFile.hh:87
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.