Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
izstream.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/izstream.hh
11 /// @brief Input file stream wrapper for uncompressed and compressed files
12 /// @author Stuart G. Mentzer (Stuart_Mentzer@objexx.com)
13 /// @author David Kim (dekim@u.washington.edu)
14 
15 
16 #ifndef INCLUDED_utility_io_izstream_hh
17 #define INCLUDED_utility_io_izstream_hh
18 
19 
20 // Unit headers
22 
23 // Package headers
24 #include <utility/io/irstream.hh>
25 #include <utility/io/zipstream.hpp>
26 
27 // Project headers
29 #include <utility/vector1.hh>
30 #if defined( USE_FILE_PROVIDER )
32 #endif
33 
34 // C++ headers
35 #include <fstream>
36 #include <istream>
37 #include <limits>
38 
39 namespace utility {
40 namespace io {
41 
42 
43 /// @brief izstream: Input file stream wrapper for uncompressed and compressed files
44 class izstream :
45  public irstream
46 {
47 
48 
49 private: // Friends
50 
51 
52  friend long utility::file::gzip( std::string const & uncompressedfile, bool overwrite );
53  friend long utility::file::gunzip( std::string const & compressedfile, bool overwrite );
54 
55 
56 private: // Types
57 
58 
59  typedef std::istream & (*manipulator)( izstream & );
60  typedef std::istream & (*std_manipulator)( std::istream & );
61 
63 
64 
65 public: // Creation
66 
67 
68  /// @brief Default constructor
69  inline
71  compression_( NONE ),
72  zip_stream_p_( 0 )
73 #if defined( USE_FILE_PROVIDER )
74  ,file_provider_stream( &bad_stream )
75 #endif
76 
77  {}
78 
79 
80  /// @brief Filename constructor
81  inline
82  explicit
84  std::string const & filename_a,
85  std::ios_base::openmode open_mode = std::ios_base::in // Ignored for gzip files
86  ) :
87  compression_( NONE ),
88  zip_stream_p_( 0 )
89 #if defined( USE_FILE_PROVIDER )
90  ,file_provider_stream( &bad_stream )
91 #endif
92 
93  {
94  open( filename_a, open_mode );
95  }
96 
97 
98  /// @brief Destructor
99  inline
100  virtual
102  {
103  delete zip_stream_p_;
104  if_stream_.close();
105  if_stream_.clear();
106  }
107 
108 
109 public: // Methods: conversion
110 
111 
112  /// @brief bool conversion
113  inline
114  operator bool() const
115  {
116 #if defined( USE_FILE_PROVIDER )
117  // if file is present in inline file provider, return true
118  if ( file_provider_stream->good() ) return true;
119 #endif
120  //return ( zip_stream_p_ ? zip_stream_p_->good() : if_stream_.good() );
121  // proper behavior is actually ( ! fail() )
122  return ( zip_stream_p_ ? !zip_stream_p_->fail() : !!if_stream_ );
123  }
124 
125 
126  /// @brief Stream conversion
127  inline
128  operator std::istream const &() const
129  {
130 #if defined( USE_FILE_PROVIDER )
131  // if file is present in inline file provider, return that - otherwise return an actual istream
132  if ( file_provider_stream->good() ) {
133  return *file_provider_stream;
134  }
135 #endif
136  return ( zip_stream_p_
137  ? static_cast< std::istream const & >( *zip_stream_p_ )
138  : static_cast< std::istream const & >( if_stream_ ) );
139  }
140 
141 
142  /// @brief Stream conversion
143  inline
144  operator std::istream &()
145  {
146 #if defined( USE_FILE_PROVIDER )
147  if ( file_provider_stream->good() ) {
148  return *file_provider_stream;
149  }
150 #endif
151  return ( zip_stream_p_
152  ? static_cast< std::istream & >( *zip_stream_p_ )
153  : static_cast< std::istream & >( if_stream_ ) );
154  }
155 
156 
157 public: // Methods: formatting
158 
159 
160  /// @brief Stream input
161  template< typename T >
162  inline
163  std::istream &
164  operator >>( T & t )
165  {
166  return stream() >> t;
167  }
168 
169 
170  /// @brief Stream manipulator input
171  inline
172  std::istream &
174  {
175  return m( *this );
176  }
177 
178 
179  /// @brief Stream manipulator input
180  inline
181  std::istream &
183  {
184  return m( *this );
185  }
186 
187 
188 public: // Methods: i/o
189 
190 
191  /// @brief Open a file
192  void
193  open(
194  std::string const & filename_a,
195  std::ios_base::openmode open_mode = std::ios_base::in
196  );
197 
198 
199  /// @brief Clear the stream(s)
200  inline
201  void
203  {
204 #if defined( USE_FILE_PROVIDER )
205  // if the file is coming from the file provider then clear that stream. Otherwise go on to clear the actual file streams.
206  if ( file_provider_stream->good() ) {
207  file_provider_stream->clear();
208  return;
209  }
210 #endif
211  if_stream_.clear();
212  if ( zip_stream_p_ ) zip_stream_p_->clear();
213  }
214 
215 
216  /// @brief Close the ifstream and reset the state
217  inline
218  void
220  {
221 #if defined( USE_FILE_PROVIDER )
222  // no need to do anything if file is comign from file provider and not from disk
223  if ( file_provider_stream->good() ) {
224  return;
225  }
226 #endif
227  compression_ = NONE;
228  if_stream_.close();
229  if_stream_.clear();
230  filename_.clear();
231  delete zip_stream_p_; zip_stream_p_ = 0;
232  }
233 
234 
235  /// @brief Seek to the beginning
236  inline
237  void
239  {
240 #if defined( USE_FILE_PROVIDER )
241  if ( file_provider_stream->good() ) {
242  file_provider_stream->clear();
243  file_provider_stream->seekg( std::ios_base::beg );
244  file_provider_stream->clear();
245  return;
246  }
247 #endif
248  if_stream_.clear();
249  if_stream_.seekg( std::ios_base::beg );
250  if_stream_.clear();
251  if ( zip_stream_p_ ) {
253  if ( ( !zip_stream_p_ ) || ( !( *zip_stream_p_ ) ) || ( !zip_stream_p_->is_gzip() ) ) {
254  delete zip_stream_p_; zip_stream_p_ = 0;
255  if_stream_.close();
256  if_stream_.setstate( std::ios_base::failbit ); // set ios state to failbit
257  }
258  }
259  }
260 
261 
262  /// @brief Get the next character
263  inline
264  int
265  get()
266  {
267  return stream().get();
268  }
269 
270 
271  /// @brief Get the next character
272  inline
273  izstream &
274  get( char & c )
275  {
276  stream().get( c );
277  return *this;
278  }
279 
280 
281  /// @brief Get the next specified number of characters
282  inline
283  izstream &
284  get( char * str, std::streamsize const count )
285  {
286  stream().get( str, count );
287  return *this;
288  }
289 
290 
291  /// @brief Get the next specified number of characters
292  inline
293  izstream &
294  get( char * str, std::streamsize const count, char const delim )
295  {
296  stream().get( str, count, delim );
297  return *this;
298  }
299 
300 
301  /// @brief Get the next specified number of characters
302  inline
303  izstream &
304  get( std::string & str, std::streamsize const count )
305  {
306  char * cp = new char[ count ];
307  stream().get( cp, count );
308  str = cp;
309  delete[] cp;
310  return *this;
311  }
312 
313 
314  /// @brief Get the next specified number of characters
315  inline
316  izstream &
317  get( std::string & str, std::streamsize const count, char const delim )
318  {
319  char * cp = new char[ count ];
320  stream().get( cp, count, delim );
321  str = cp;
322  delete[] cp;
323  return *this;
324  }
325 
326 
327  /// @brief Get the rest of the line
328  inline
329  izstream &
330  getline( char * line, std::streamsize const count )
331  {
332  stream().getline( line, count );
333  return *this;
334  }
335 
336 
337  /// @brief Get the rest of the line
338  inline
339  izstream &
340  getline( char * line, std::streamsize const count, char const delim )
341  {
342  stream().getline( line, count, delim );
343  return *this;
344  }
345 
346 
347  /// @brief Get the rest of the line
348  inline
349  izstream &
350  getline( std::string & line )
351  {
352  std::getline( stream(), line );
353  return *this;
354  }
355 
356 
357  /// @brief Get the rest of the line
358  inline
359  izstream &
360  getline( std::string & line, char const delim )
361  {
362  std::getline( stream(), line, delim );
363  return *this;
364  }
365 
366 
367  /// @brief Read the next specified number of characters
368  inline
369  izstream &
370  read( char * str, std::streamsize const count )
371  {
372  stream().read( str, count );
373  return *this;
374  }
375 
376 
377  /// @brief Read the next specified number of characters
378  inline
379  izstream &
380  read( std::string & str, std::streamsize const count )
381  {
382  char * cp = new char[ count ];
383  stream().read( cp, count );
384  str = cp;
385  delete[] cp;
386  return *this;
387  }
388 
389 
390  /// @brief Read the next available specified number of characters
391  inline
392  std::streamsize
393  readsome( char * str, std::streamsize const count )
394  {
395  return stream().readsome( str, count );
396  }
397 
398 
399  /// @brief Read the next available specified number of characters
400  inline
401  std::streamsize
402  readsome( std::string & str, std::streamsize const count )
403  {
404  char * cp = new char[ count ];
405  std::streamsize const n_chars = stream().readsome( cp, count );
406  str = cp;
407  delete[] cp;
408  return n_chars;
409  }
410 
411 
412  /// @brief Skip over the next character
413  inline
414  izstream &
416  {
417  stream().ignore();
418  return *this;
419  }
420 
421 
422  /// @brief Skip over the next specified number of characters
423  inline
424  izstream &
425  ignore( std::streamsize const count )
426  {
427  stream().ignore( count );
428  return *this;
429  }
430 
431 
432  /// @brief Skip over the next specified number of characters
433  inline
434  izstream &
435  ignore( std::streamsize const count, char const delim )
436  {
437  stream().ignore( count, delim );
438  return *this;
439  }
440 
441 
442  /// @brief Returns the next character without extracting it
443  inline
444  int
446  {
447  return stream().peek();
448  }
449 
450 
451  /// @brief Put the last character read back into the stream
452  inline
453  izstream &
455  {
456  stream().unget();
457  return *this;
458  }
459 
460 
461  /// @brief Put the last character read back into the stream and check
462  /// that passed character is correct
463  inline
464  izstream &
465  putback( char c )
466  {
467  stream().putback( c );
468  return *this;
469  }
470 
471 
472 public: // Properties
473 
474 
475  /// @brief Stream access
476  inline
477  std::istream const &
478  operator ()() const
479  {
480 #if defined( USE_FILE_PROVIDER )
481  if ( file_provider_stream->good() ) {
482  return *file_provider_stream;
483  }
484 #endif
485  return ( zip_stream_p_
486  ? static_cast< std::istream const & >( *zip_stream_p_ )
487  : static_cast< std::istream const & >( if_stream_ ) );
488  }
489 
490 
491  /// @brief Stream access
492  inline
493  std::istream &
495  {
496 #if defined( USE_FILE_PROVIDER )
497  if ( file_provider_stream->good() ) {
498  return *file_provider_stream;
499  }
500 #endif
501  return ( zip_stream_p_
502  ? static_cast< std::istream & >( *zip_stream_p_ )
503  : static_cast< std::istream & >( if_stream_ ) );
504  }
505 
506 
507  /// @brief Stream access
508  inline
509  std::istream const &
510  stream() const
511  {
512 #if defined( USE_FILE_PROVIDER )
513  if ( file_provider_stream->good() ) {
514  return *file_provider_stream;
515  }
516 #endif
517  return ( zip_stream_p_
518  ? static_cast< std::istream const & >( *zip_stream_p_ )
519  : static_cast< std::istream const & >( if_stream_ ) );
520  }
521 
522 
523  /// @brief Stream access
524  inline
525  std::istream &
527  {
528 #if defined( USE_FILE_PROVIDER )
529  if ( file_provider_stream->good() ) {
530  return *file_provider_stream;
531  }
532 #endif
533  return ( zip_stream_p_
534  ? static_cast< std::istream & >( *zip_stream_p_ )
535  : static_cast< std::istream & >( if_stream_ ) );
536  }
537 
538 
539  /// @brief Pointer to the stream buffer
540  inline
541  std::streambuf *
542  rdbuf() const
543  {
544  return stream().rdbuf();
545  }
546 
547 
548  /// @brief File name
549  inline
550  std::string const &
551  filename() const
552  {
553  return filename_;
554  }
555 
556 
557  /// @brief Get the number of characters read by the last unformatted read
558  inline
559  std::streamsize
560  gcount() const
561  {
562  return stream().gcount();
563  }
564 
565 
566 public: // Properties: predicate
567 
568 
569  /// @brief Good?
570  inline
571  bool
572  good() const
573  {
574  return stream().good();
575  }
576 
577 
578  /// @brief End of file?
579  inline
580  bool
581  eof() const
582  {
583  return stream().eof();
584  }
585 
586 
587  /// @brief Fail?
588  inline
589  bool
590  fail() const
591  {
592  return stream().fail();
593  }
594 
595 
596  /// @brief Bad?
597  inline
598  bool
599  bad() const
600  {
601  return stream().bad();
602  }
603 
604 
605  /// @brief Compressed?
606  inline
607  bool
608  compressed() const
609  {
610  return ( compression_ == GZIP );
611  }
612 
613 
614  /// @brief Uncompressed?
615  inline
616  bool
617  uncompressed() const
618  {
619  return ( compression_ == UNCOMPRESSED );
620  }
621 
622 
623  /// @brief gzipped?
624  inline
625  bool
626  gzipped() const
627  {
628  return ( compression_ == GZIP );
629  }
630 
631 
632 private: // Properties: internal zip stream predicates
633 
634 
635  /// @brief Is stream attached to a gzip file?
636  inline
637  bool
638  is_gzip() const
639  {
640  return ( zip_stream_p_ ? zip_stream_p_->is_gzip() : false );
641  }
642 
643 
644  /// @brief CRC of gzip file valid?
645  inline
646  bool
647  check_crc() const
648  {
649  return ( zip_stream_p_ ? zip_stream_p_->check_crc() : false );
650  }
651 
652 
653  /// @brief CRC of the uncompressed data (see zipstream documentation)
654  inline
655  long
656  get_crc() const
657  {
658  return ( zip_stream_p_ ? zip_stream_p_->get_crc() : 0 );
659  }
660 
661 
662  /// @brief Compressed data size
663  inline
664  long
665  get_in_size() const
666  {
667  return ( zip_stream_p_ ? zip_stream_p_->get_in_size() : 0 );
668  }
669 
670 
671  /// @brief Uncompressed data size
672  inline
673  long
674  get_out_size() const
675  {
676  return ( zip_stream_p_ ? zip_stream_p_->get_out_size() : 0 );
677  }
678 
679 private:
680 
681  /// @brief Helper function for opening files with alternative search paths
682  void
684  std::string const & name,
685  std::ios_base::openmode open_mode);
686 
687 public:
688 
689  static
690  void
692  vector1< std::string > alternative_search_paths
693  ){
694  alternative_search_paths_ = alternative_search_paths;
695  }
696 
697 
698  static
702  }
703 
704 
705 private: // Fields
706 
707 
708  /// @brief Compression state
710 
711  /// @brief File stream
712  std::ifstream if_stream_;
713 
714  /// @brief File name
715  std::string filename_;
716 
717  /// @brief Zip file stream pointer (owning)
719 
720  /// @brief Alternative search paths
721  /// This initialized by the option system -in:path:path Notice that
722  /// izstream cannot access the option system (because the utility
723  /// library comes before the basic library), so setting the
724  /// alternate search paths is it the responsibility of core::init::init()
726 #if defined( USE_FILE_PROVIDER )
727  std::istream *file_provider_stream;
728  std::stringstream bad_stream;
729 #endif
730 
731 }; // izstream
732 
733 
734 // Non-member izstream functions
735 
736 
737 /// @brief std::getline( std::istream, std::string ) wrapper
738 inline
739 izstream &
740 getline( izstream & stream, std::string & line )
741 {
742  std::getline( stream(), line );
743  return stream;
744 }
745 
746 
747 /// @brief std::getline( std::istream, std::string, char ) wrapper
748 inline
749 izstream &
750 getline( izstream & stream, std::string & line, char const delim )
751 {
752  std::getline( stream(), line, delim );
753  return stream;
754 }
755 
756 
757 /// @brief Skip rest of line and line terminator (manipulator)
758 inline
759 std::istream &
760 skip( izstream & stream )
761 {
762  return stream().ignore( std::numeric_limits< std::streamsize >::max(), '\n' );
763 }
764 
765 
766 /// @brief Skip rest of line and line terminator (manipulator)
767 inline
768 std::istream &
769 skip( std::istream & stream )
770 {
771  return stream.ignore( std::numeric_limits< std::streamsize >::max(), '\n' );
772 }
773 
774 
775 } // namespace io
776 } // namespace utility
777 
778 
779 #endif // INCLUDED_utility_io_izstream_HH
uLong get_crc() const
returns the uncompressed data crc
Definition: zipstream.hpp:346
std::streambuf * rdbuf() const
Pointer to the stream buffer.
Definition: izstream.hh:542
std::istream & stream()
Stream access.
Definition: izstream.hh:526
Altered zipstream library header.
bool good() const
Good?
Definition: izstream.hh:572
izstream & ignore()
Skip over the next character.
Definition: izstream.hh:415
std::istream const & stream() const
Stream access.
Definition: izstream.hh:510
std::istream &(* std_manipulator)(std::istream &)
Definition: izstream.hh:60
static void set_alternative_search_paths(vector1< std::string > alternative_search_paths)
Definition: izstream.hh:691
bool compressed() const
Compressed?
Definition: izstream.hh:608
izstream & getline(std::string &line, char const delim)
Get the rest of the line.
Definition: izstream.hh:360
uLong get_in_size() const
returns the compressed data size
Definition: zipstream.hpp:352
izstream & getline(char *line, std::streamsize const count)
Get the rest of the line.
Definition: izstream.hh:330
izstream & putback(char c)
Put the last character read back into the stream and check that passed character is correct...
Definition: izstream.hh:465
izstream & ignore(std::streamsize const count)
Skip over the next specified number of characters.
Definition: izstream.hh:425
izstream & unget()
Put the last character read back into the stream.
Definition: izstream.hh:454
izstream(std::string const &filename_a, std::ios_base::openmode open_mode=std::ios_base::in)
Filename constructor.
Definition: izstream.hh:83
izstream & getline(char *line, std::streamsize const count, char const delim)
Get the rest of the line.
Definition: izstream.hh:340
Input stream wrapper abstract base class.
long gzip(std::string const &uncompressedfile, bool overwrite)
gzip: file compression
Definition: gzip_util.cc:42
std::istream & skip(izstream &stream)
Skip rest of line and line terminator (manipulator)
Definition: izstream.hh:760
bool is_gzip() const
Is stream attached to a gzip file?
Definition: izstream.hh:638
std::string filename_
File name.
Definition: izstream.hh:715
bool uncompressed() const
Uncompressed?
Definition: izstream.hh:617
static vector1< std::string > get_alternative_search_paths()
Definition: izstream.hh:700
orstream: Input stream wrapper abstract base class
Definition: irstream.hh:33
izstream & ignore(std::streamsize const count, char const delim)
Skip over the next specified number of characters.
Definition: izstream.hh:435
bool check_crc() const
CRC of gzip file valid?
Definition: izstream.hh:647
std::ifstream if_stream_
File stream.
Definition: izstream.hh:712
long get_crc() const
CRC of the uncompressed data (see zipstream documentation)
Definition: izstream.hh:656
int peek()
Returns the next character without extracting it.
Definition: izstream.hh:445
void close()
Close the ifstream and reset the state.
Definition: izstream.hh:219
bool eof() const
End of file?
Definition: izstream.hh:581
bool check_crc() const
return crc check result
Definition: zipstream.hpp:620
std::istream & operator>>(T &t)
Stream input.
Definition: izstream.hh:164
izstream: Input file stream wrapper for uncompressed and compressed files
Definition: izstream.hh:44
bool fail() const
Fail?
Definition: izstream.hh:590
bool gzipped() const
gzipped?
Definition: izstream.hh:626
void clear()
Clear the stream(s)
Definition: izstream.hh:202
izstream & read(char *str, std::streamsize const count)
Read the next specified number of characters.
Definition: izstream.hh:370
void open_ifstream(std::string const &name, std::ios_base::openmode open_mode)
Helper function for opening files with alternative search paths.
Definition: izstream.cc:116
izstream & getline(izstream &stream, std::string &line)
std::getline( std::istream, std::string ) wrapper
Definition: izstream.hh:740
utility::io::izstream forward declarations
long get_in_size() const
Compressed data size.
Definition: izstream.hh:665
static char * line
Definition: Svm.cc:2683
gzip utility functions
izstream & getline(izstream &stream, std::string &line, char const delim)
std::getline( std::istream, std::string, char ) wrapper
Definition: izstream.hh:750
std::streamsize gcount() const
Get the number of characters read by the last unformatted read.
Definition: izstream.hh:560
long gunzip(std::string const &compressedfile, bool overwrite)
gunzip: file decompression
Definition: gzip_util.cc:139
vector1: std::vector with 1-based indexing
bool is_gzip() const
returns true if it is a gzip file
Definition: zipstream.hpp:608
izstream & read(std::string &str, std::streamsize const count)
Read the next specified number of characters.
Definition: izstream.hh:380
long get_out_size() const
Uncompressed data size.
Definition: izstream.hh:674
izstream()
Default constructor.
Definition: izstream.hh:70
std::streamsize readsome(char *str, std::streamsize const count)
Read the next available specified number of characters.
Definition: izstream.hh:393
std::string const & filename() const
File name.
Definition: izstream.hh:551
rule< Scanner, string_closure::context_t > name
Definition: Tag.cc:376
void seek_beg()
Seek to the beginning.
Definition: izstream.hh:238
std::istream &(* manipulator)(izstream &)
Definition: izstream.hh:59
static vector1< std::string > alternative_search_paths_
Alternative search paths This initialized by the option system -in:path:path Notice that izstream can...
Definition: izstream.hh:725
static T max(T x, T y)
Definition: Svm.cc:19
void open(std::string const &filename_a, std::ios_base::openmode open_mode=std::ios_base::in)
Open a file.
Definition: izstream.cc:34
izstream & getline(std::string &line)
Get the rest of the line.
Definition: izstream.hh:350
zlib_stream::zip_istream * zip_stream_p_
Zip file stream pointer (owning)
Definition: izstream.hh:718
bool bad() const
Bad?
Definition: izstream.hh:599
basic_zip_istream< char > zip_istream
Definition: zipstream.hpp:647
Compression compression_
Compression state.
Definition: izstream.hh:709
std::streamsize readsome(std::string &str, std::streamsize const count)
Read the next available specified number of characters.
Definition: izstream.hh:402
uLong get_out_size() const
returns the uncompressed data size
Definition: zipstream.hpp:349
virtual ~izstream()
Destructor.
Definition: izstream.hh:101
std::istream const & operator()() const
Stream access.
Definition: izstream.hh:478