Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
icstream.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file utility/io/icstream.hh
11 /// @brief Input channel stream wrapper class
12 /// @author Mike Tyka (M.Tyka@bristol.ac.uk) (based on ocstream.hh)
13 
14 
15 #ifndef INCLUDED_utility_io_icstream_hh
16 #define INCLUDED_utility_io_icstream_hh
17 
18 
19 // Unit headers
21 
22 // Package headers
23 #include <utility/io/irstream.hh>
24 
25 // C++ headers
26 #include <istream>
27 
28 
29 namespace utility {
30 namespace io {
31 
32 
33 /// @brief icstream: Input channel stream wrapper class
34 class icstream :
35  public irstream
36 {
37 
38 
39 public: // Creation
40 
41 
42  /// @brief Constructor
43  inline
44  icstream( std::istream & i_stream_a ) :
45  i_stream_( i_stream_a )
46  {}
47 
48 
49  /// @brief Destructor
50  inline
51  virtual
53  {}
54 
55 
56 public: // Methods: conversion
57 
58 
59  /// @brief bool conversion
60  inline
61  operator bool() const
62  {
63  //return i_stream_.good();
64  // proper behavior is actually ( ! fail() )
65  return !!i_stream_;
66  }
67 
68 
69  /// @brief Stream conversion
70  inline
71  operator std::istream const &() const
72  {
73  return i_stream_;
74  }
75 
76 
77  /// @brief Stream conversion
78  inline
79  operator std::istream &()
80  {
81  return i_stream_;
82  }
83 
84 
85 public: // Methods: formatting
86 
87 
88  /// @brief Stream input: Override to preserve type of return value
89  template< typename T >
90  inline
91  icstream &
92  operator >>( T & t )
93  {
94  i_stream_ >> t;
95  return *this;
96  }
97 
98 
99  /// @brief Stream manipulator input
100  inline
101  std::istream &
103  {
104  return m( *this );
105  }
106 
107 
108 public: // Methods: i/o
109 
110 
111  /// @brief Seek to the beginning
112  inline
113  void
115  {
116  stream().clear();
117  stream().seekg( std::ios_base::beg );
118  stream().clear();
119  }
120 
121 
122  /// @brief Get the next character
123  inline
124  int
125  get()
126  {
127  return stream().get();
128  }
129 
130 
131  /// @brief Get the next character
132  inline
133  icstream &
134  get( char & c )
135  {
136  stream().get( c );
137  return *this;
138  }
139 
140 
141  /// @brief Get the next specified number of characters
142  inline
143  icstream &
144  get( char * str, std::streamsize const count )
145  {
146  stream().get( str, count );
147  return *this;
148  }
149 
150 
151  /// @brief Get the next specified number of characters
152  inline
153  icstream &
154  get( char * str, std::streamsize const count, char const delim )
155  {
156  stream().get( str, count, delim );
157  return *this;
158  }
159 
160 
161  /// @brief Get the next specified number of characters
162  inline
163  icstream &
164  get( std::string & str, std::streamsize const count )
165  {
166  char * cp = new char[ count ];
167  stream().get( cp, count );
168  str = cp;
169  delete[] cp;
170  return *this;
171  }
172 
173 
174  /// @brief Get the next specified number of characters
175  inline
176  icstream &
177  get( std::string & str, std::streamsize const count, char const delim )
178  {
179  char * cp = new char[ count ];
180  stream().get( cp, count, delim );
181  str = cp;
182  delete[] cp;
183  return *this;
184  }
185 
186 
187  /// @brief Get the rest of the line
188  inline
189  icstream &
190  getline( char * line, std::streamsize const count )
191  {
192  stream().getline( line, count );
193  return *this;
194  }
195 
196 
197  /// @brief Get the rest of the line
198  inline
199  icstream &
200  getline( char * line, std::streamsize const count, char const delim )
201  {
202  stream().getline( line, count, delim );
203  return *this;
204  }
205 
206 
207  /// @brief Get the rest of the line
208  inline
209  icstream &
210  getline( std::string & line )
211  {
212  std::getline( stream(), line );
213  return *this;
214  }
215 
216 
217  /// @brief Get the rest of the line
218  inline
219  icstream &
220  getline( std::string & line, char const delim )
221  {
222  std::getline( stream(), line, delim );
223  return *this;
224  }
225 
226 
227  /// @brief Read the next specified number of characters
228  inline
229  icstream &
230  read( char * str, std::streamsize const count )
231  {
232  stream().read( str, count );
233  return *this;
234  }
235 
236 
237  /// @brief Read the next specified number of characters
238  inline
239  icstream &
240  read( std::string & str, std::streamsize const count )
241  {
242  char * cp = new char[ count ];
243  stream().read( cp, count );
244  str = cp;
245  delete[] cp;
246  return *this;
247  }
248 
249 
250  /// @brief Read the next available specified number of characters
251  inline
252  std::streamsize
253  readsome( char * str, std::streamsize const count )
254  {
255  return stream().readsome( str, count );
256  }
257 
258 
259  /// @brief Read the next available specified number of characters
260  inline
261  std::streamsize
262  readsome( std::string & str, std::streamsize const count )
263  {
264  char * cp = new char[ count ];
265  std::streamsize const n_chars = stream().readsome( cp, count );
266  str = cp;
267  delete[] cp;
268  return n_chars;
269  }
270 
271 
272  /// @brief Skip over the next character
273  inline
274  icstream &
276  {
277  stream().ignore();
278  return *this;
279  }
280 
281 
282  /// @brief Skip over the next specified number of characters
283  inline
284  icstream &
285  ignore( std::streamsize const count )
286  {
287  stream().ignore( count );
288  return *this;
289  }
290 
291 
292  /// @brief Skip over the next specified number of characters
293  inline
294  icstream &
295  ignore( std::streamsize const count, char const delim )
296  {
297  stream().ignore( count, delim );
298  return *this;
299  }
300 
301 
302  /// @brief Returns the next character without extracting it
303  inline
304  int
306  {
307  return stream().peek();
308  }
309 
310 
311  /// @brief Put the last character read back into the stream
312  inline
313  icstream &
315  {
316  stream().unget();
317  return *this;
318  }
319 
320 
321  /// @brief Put the last character read back into the stream and check that passed character is correct
322  inline
323  icstream &
324  putback( char c )
325  {
326  stream().putback( c );
327  return *this;
328  }
329 
330 
331  /// @brief Clear the stream
332  inline
333  void
335  {
336  i_stream_.clear();
337  }
338 
339 
340 public: // Properties
341 
342 
343  /// @brief Stream access
344  inline
345  std::istream const &
346  operator ()() const
347  {
348  return i_stream_;
349  }
350 
351 
352  /// @brief Stream access
353  inline
354  std::istream &
356  {
357  return i_stream_;
358  }
359 
360 
361  /// @brief Stream access
362  inline
363  std::istream const &
364  stream() const
365  {
366  return i_stream_;
367  }
368 
369 
370  /// @brief Stream access
371  inline
372  std::istream &
374  {
375  return i_stream_;
376  }
377 
378 
379  /// @brief Pointer to the stream buffer
380  inline
381  std::streambuf *
382  rdbuf() const
383  {
384  return i_stream_.rdbuf();
385  }
386 
387 
388 public: // Properties: predicate
389 
390 
391  /// @brief Good?
392  inline
393  bool
394  good() const
395  {
396  return i_stream_.good();
397  }
398 
399 
400  /// @brief End of file?
401  inline
402  bool
403  eof() const
404  {
405  return i_stream_.eof();
406  }
407 
408 
409  /// @brief Fail?
410  inline
411  bool
412  fail() const
413  {
414  return i_stream_.fail();
415  }
416 
417 
418  /// @brief Bad?
419  inline
420  bool
421  bad() const
422  {
423  return i_stream_.bad();
424  }
425 
426 
427  /// @brief Compressed?
428  inline
429  bool
430  compressed() const
431  {
432  return false;
433  }
434 
435 
436  /// @brief Uncompressed?
437  inline
438  bool
439  uncompressed() const
440  {
441  return true;
442  }
443 
444 
445  /// @brief gzipped?
446  inline
447  bool
448  gzipped() const
449  {
450  return false;
451  }
452 
453 
454 private: // Fields
455 
456 
457  /// @brief Input stream reference
458  std::istream & i_stream_;
459 
460 
461 }; // icstream
462 
463 
464 namespace ic { // Predefined icstreams
465 
466 
467 /// @brief Wrapper around std::cin
468 extern icstream cin;
469 
470 
471 } // namespace ic
472 
473 
474 } // namespace io
475 } // namespace utility
476 
477 
478 #endif // INCLUDED_utility_io_icstream_HH
bool gzipped() const
gzipped?
Definition: icstream.hh:448
icstream & getline(char *line, std::streamsize const count)
Get the rest of the line.
Definition: icstream.hh:190
std::istream & i_stream_
Input stream reference.
Definition: icstream.hh:458
icstream & ignore(std::streamsize const count)
Skip over the next specified number of characters.
Definition: icstream.hh:285
icstream & ignore()
Skip over the next character.
Definition: icstream.hh:275
std::istream & getline(std::istream &stream, Fstring &s)
Get Line from Stream.
Definition: Fstring.cc:1610
icstream & ignore(std::streamsize const count, char const delim)
Skip over the next specified number of characters.
Definition: icstream.hh:295
void seek_beg()
Seek to the beginning.
Definition: icstream.hh:114
icstream cin(std::cin)
Wrapper around std::cin.
Definition: icstream.hh:468
Input stream wrapper abstract base class.
icstream(std::istream &i_stream_a)
Constructor.
Definition: icstream.hh:44
std::istream & stream()
Stream access.
Definition: icstream.hh:373
orstream: Input stream wrapper abstract base class
Definition: irstream.hh:33
bool compressed() const
Compressed?
Definition: icstream.hh:430
bool good() const
Good?
Definition: icstream.hh:394
bool eof() const
End of file?
Definition: icstream.hh:403
std::istream &(* std_manipulator)(std::istream &)
Definition: irstream.hh:40
int peek()
Returns the next character without extracting it.
Definition: icstream.hh:305
std::streamsize readsome(std::string &str, std::streamsize const count)
Read the next available specified number of characters.
Definition: icstream.hh:262
virtual ~icstream()
Destructor.
Definition: icstream.hh:52
icstream & operator>>(T &t)
Stream input: Override to preserve type of return value.
Definition: icstream.hh:92
std::istream const & stream() const
Stream access.
Definition: icstream.hh:364
icstream & getline(char *line, std::streamsize const count, char const delim)
Get the rest of the line.
Definition: icstream.hh:200
icstream: Input channel stream wrapper class
Definition: icstream.hh:34
bool bad() const
Bad?
Definition: icstream.hh:421
std::streambuf * rdbuf() const
Pointer to the stream buffer.
Definition: icstream.hh:382
utility::io::icstream forward declarations
static char * line
Definition: Svm.cc:2683
icstream & unget()
Put the last character read back into the stream.
Definition: icstream.hh:314
bool fail() const
Fail?
Definition: icstream.hh:412
icstream & getline(std::string &line)
Get the rest of the line.
Definition: icstream.hh:210
void clear()
Clear the stream.
Definition: icstream.hh:334
std::istream const & operator()() const
Stream access.
Definition: icstream.hh:346
icstream & getline(std::string &line, char const delim)
Get the rest of the line.
Definition: icstream.hh:220
bool uncompressed() const
Uncompressed?
Definition: icstream.hh:439
icstream & putback(char c)
Put the last character read back into the stream and check that passed character is correct...
Definition: icstream.hh:324
icstream & read(std::string &str, std::streamsize const count)
Read the next specified number of characters.
Definition: icstream.hh:240
std::streamsize readsome(char *str, std::streamsize const count)
Read the next available specified number of characters.
Definition: icstream.hh:253
icstream & read(char *str, std::streamsize const count)
Read the next specified number of characters.
Definition: icstream.hh:230