Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
izstream.cc
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.cc
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 // Unit header
17 #include <utility/io/izstream.hh>
18 
19 // Project headers
21 
22 #if defined( USE_FILE_PROVIDER )
24 #endif
25 
26 namespace utility {
27 namespace io {
28 
29 // Initialize private static data
30 vector1< std::string > izstream::alternative_search_paths_;
31 
32 /// @brief Open a file
33 void
35  std::string const & filename_a,
36  std::ios_base::openmode open_mode // Ignored for gzip files
37 )
38 {
39  using std::ios_base;
43 
44  //std::cout << "FILELIST: Trying to open file: '" << filename_a << "' " << std::endl;
45 
46 #if defined( USE_FILE_PROVIDER )
48  if ( (open_mode & std::ios_base::out ) ) {
49  throw( "Cannot open output file in istream inline file provider " );
50  }
51 
52  if ( !provider->get_istream( filename_a , &file_provider_stream ) ) {
53  std::cerr << "Cannot find inline file: " << filename_a << std::endl;
54  file_provider_stream = &bad_stream;
55  file_provider_stream->setstate( ios_base::failbit | ios_base::badbit );
56  }
57 
58 #ifdef __native_client__
59  // in native client mode there is no recourse - no actauly file access is allowed
60  // so ut this point we can only hand back the stream no matter if it is good() or not.
61  return;
62 #endif
63 
64  if ( file_provider_stream->good() ) {
65  return;
66  }
67 #endif
68 
69  // Close the file if open and reset the state
70  close();
71 
72  // Open the ifstream and set the compression state and file name
73  if ( ( open_mode & ios_base::ate ) ||
74  ( open_mode & ios_base::app ) ||
75  ( ( open_mode & ios_base::in ) &&
76  ( open_mode & ios_base::out ) ) ) { // Unsupported for gzip files: Use ifstream
77  open_ifstream(filename_a, open_mode);
79  } else if ( file_extension( filename_a ) == "gz" ) { // gzip file
80  open_ifstream(filename_a, ios_base::in|ios_base::binary );
81  if ( if_stream_ ) { // Open succeeded
83  } else { // Leave stream state so that failure can be detected
85  }
86  } else { // Try with .gz extension added
87  open_ifstream( filename_a + ".gz", ios_base::in|ios_base::binary );
88  if ( if_stream_ ) { // Found/opened with .gz added
90  } else { // Try as an uncompressed file
91  if_stream_.clear();
92  open_ifstream( filename_a, open_mode );
93  if ( if_stream_ ) { // Open succeeded
95  } else { // Leave stream state so that failure can be detected
97  }
98  }
99  }
100 
101  // Attach zip_istream to ifstream if gzip file
102  if ( compression_ == GZIP ) {
103  // zip_stream_p_ deleted by close() above so don't have to here
105  if ( ( !zip_stream_p_ ) || ( !( *zip_stream_p_ ) ) || ( !zip_stream_p_->is_gzip() ) ) {
106  // zip_stream not in good state
107  delete zip_stream_p_; zip_stream_p_ = 0;
108  if_stream_.close();
109  if_stream_.setstate( ios_base::failbit ); // set failbit so failure can be detected
110  }
111  }
112 }
113 
114 
115 void
117  std::string const & name,
118  std::ios_base::openmode open_mode
119 ){
120  using std::ios_base;
122  trytry_ifstream_open( if_stream_, name, open_mode );
123  if ( if_stream_ ) { // Open succeeded
124  filename_ = name;
125  } else { // Try opening file in alternative search paths
128  for ( ; i!=ie; ++i ) {
130  (*i + platform::file::PATH_SEPARATOR) + name,
131  ios_base::in|ios_base::binary );
132  if ( if_stream_ ) {
134  break;
135  }
136  }
137  }
138 }
139 
140 
141 } // namespace io
142 } // namespace utility
ocstream cerr(std::cerr)
Wrapper around std::cerr.
Definition: ocstream.hh:290
bool get_istream(const std::string &filename, std::istream **the_stream)
Platform independent operations on files (except I/O)
std::string filename_
File name.
Definition: izstream.hh:715
std::ifstream if_stream_
File stream.
Definition: izstream.hh:712
static Inline_File_Provider * get_instance()
Safely instantiate a singleton class in a (possibly) multithreaded context.
void close()
Close the ifstream and reset the state.
Definition: izstream.hh:219
Input file stream wrapper for uncompressed and compressed files.
std::string file_extension(std::string const &filename)
Extension of a File Name.
std::vector with 1-based indexing
Definition: vector1.fwd.hh:44
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
char const PATH_SEPARATOR( '/')
File path separator.
bool trytry_ifstream_open(std::ifstream &ifstream_, std::string const &name, std::ios_base::openmode open_mode)
Try to open file a few times just in case it is locked (from BOINC LIB)
bool is_gzip() const
returns true if it is a gzip file
Definition: zipstream.hpp:608
rule< Scanner, string_closure::context_t > name
Definition: Tag.cc:376
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
void open(std::string const &filename_a, std::ios_base::openmode open_mode=std::ios_base::in)
Open a file.
Definition: izstream.cc:34
zlib_stream::zip_istream * zip_stream_p_
Zip file stream pointer (owning)
Definition: izstream.hh:718
basic_zip_istream< char > zip_istream
Definition: zipstream.hpp:647
Compression compression_
Compression state.
Definition: izstream.hh:709