Jpp  18.5.2
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  private:
108  };
109 
110 
111  /**
112  * Output file stream buffer.
113  */
115  protected virtual std::vector<char>,
116  public virtual std::streambuf
117  {
118  public:
119 
120 
121  typedef traits_type::int_type int_type;
122  typedef std::streamsize streamsize;
123 
124 
125  /**
126  * Constructor.
127  *
128  * \param file file
129  * \param size size of internal buffer
130  */
132  const std::size_t size = 65536) :
133  out (file)
134  {
135  // reserve one byte for overflow character
136 
137  resize(size);
138 
139  setp(this->data(), this->data() + this->size() - 1);
140  }
141 
142 
143  /**
144  * Destructor.
145  */
147  {
148  sync();
149  }
150 
151 
152  /**
153  * Check overflow.
154  * This method writes one byte if possible.
155  *
156  * \param c character
157  * \return c if OK; else EOF
158  */
159  virtual int_type overflow(int_type c) override
160  {
161  if (c != traits_type::eof()) {
162 
163  *pptr() = (char) c;
164 
165  pbump(1);
166 
167  if (flush() == traits_type::eof()) {
168  return traits_type::eof();
169  }
170  }
171 
172  return c;
173  }
174 
175 
176  /**
177  * Synchronise buffer.
178  */
179  virtual int sync() override
180  {
181  if (flush() == traits_type::eof())
182  return -1;
183  else
184  return 0;
185  }
186 
187 
188  /**
189  * Check availability of output.
190  * This method returns true if at least one byte can be written without blocking.
191  *
192  * \param timeout timeout
193  * \return true if ready to write; else false
194  */
195  bool out_avail(JTimeval timeout = JTimeval::min()) const
196  {
197  return JFileDescriptorMask(out).out_avail(timeout);
198  }
199 
200  protected:
201  /**
202  * Flush internal buffer.
203  */
204  int flush()
205  {
206  const int len = pptr() - pbase();
207 
208  if (len != 0) {
209 
210  if (out.write(this->data(), len) != len) {
211  return traits_type::eof();
212  }
213 
214  pbump(-len);
215  }
216 
217  return len;
218  }
219 
220 
222 
223  private:
228  };
229 
230 
231 
232  /**
233  * Input and output file stream buffer.
234  */
236  public JFileInputStreamBuffer,
238  {
239  public:
240  /**
241  * Constructor.
242  *
243  * \param in input file
244  * \param out output file
245  * \param size size of internal buffer
246  * \param put_back number of put back characters
247  */
249  const JAbstractFile& out,
250  const std::size_t size = 65536,
251  const std::size_t put_back = 8) :
252  JFileInputStreamBuffer (in, size, put_back),
253  JFileOutputStreamBuffer(out, size)
254  {}
255  };
256 }
257 
258 #endif
virtual int_type underflow() override
Check underflow.
bool in_avail(JTimeval timeout=JTimeval::min()) const
Check availability of input.
Input and output file stream buffer.
then usage $script[< detector identifier >< run range >]< QA/QCfile > nExample script to produce data quality plots nWhen a detector identifier and run range are data are downloaded from the database nand subsequently stored in the given QA QC file
Definition: JDataQuality.sh:19
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:80
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
JFileInputStreamBuffer & operator=(const JFileInputStreamBuffer &)
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.
$WORKDIR ev_configure_dqsimulator txt echo process $DQ_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DQ_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
virtual int_type overflow(int_type c) override
Check overflow.
Input file stream buffer.
virtual int write(const char *buffer, const int length)
Write data to file.
Definition: JFile.hh:93
JFileOutputStreamBuffer & operator=(const JFileOutputStreamBuffer &)
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.