26 #ifndef STREAMSET_HPP__
27 #define STREAMSET_HPP__
39 #include <boost/iostreams/stream.hpp>
40 #include <boost/iostreams/device/file_descriptor.hpp>
41 #include <boost/iostreams/device/null.hpp>
42 #include <boost/iostreams/concepts.hpp>
43 #include <boost/iostreams/categories.hpp>
44 #include <boost/thread/recursive_mutex.hpp>
72 static const int BLOCKSIZE_PRESET_VALUES[3];
79 S_MAKE_ENUM_TRAITS(
streams_base::RandomAccessGranularity)
87 typedef boost::recursive_mutex mutex_t;
88 typedef boost::recursive_mutex::scoped_lock lock_t;
89 static int m_filecount;
90 static mutex_t m_filecountmutex;
95 std::string streamname;
96 std::vector<std::istream *> stream_stack;
97 std::vector<int64_t> streamsizes;
101 stream_info(
const std::string &name);
104 std::vector<stream_info> m_streams;
108 static bool filecount_inc();
109 static void filecount_dec();
110 std::istream *rawstream_alloc(
const std::string &fn);
119 static bool has_magic(std::istream *
const in,
const int m1,
const int m2);
122 static bool has_gzip_magic(std::istream *
const in);
125 static bool has_xz_magic(std::istream *
const in);
128 static bool is_gzip(
const std::string &fn);
131 static bool is_xz(
const std::string &fn);
134 static Codec get_codec(
const std::string &fn);
137 static uint64_t uncompressed_file_size(
const std::string &fn);
140 static void read_error_check(std::istream &s,
141 const std::string &stream_name,
142 const std::string &label=std::string());
145 static void read_error_eof_check(std::istream &s,
146 const std::string &stream_name,
147 const std::string &label=std::string());
150 static void read_error_check_rdbuf(std::istream &s,
151 const std::string &stream_name,
152 const std::string &label=std::string());
155 static void save_stream_to_string(std::string &res,std::istream &s,
156 const std::string &streamname);
159 static void save_stream_to_string(std::string &res,
const std::string &s);
165 explicit istreams(
const std::string& fn);
168 explicit istreams(
const std::vector<std::string>& fn);
175 open_files(f_beg,f_end);
183 std::istream &open_file(
const std::string &fnam);
187 std::istream &open_file(
const std::string &fnam,
const Codec c);
194 for(Iter itr=f_beg;itr!=f_end;++itr)
201 std::istream &open_normalfile(
const std::string &fn,
const Codec c=CODEC_PLAIN);
204 std::istream &push_back(std::istream& stream,
const std::string &name=std::string(),
205 const Codec c=CODEC_PLAIN);
208 std::istream& open_pipe(
const std::string& command,
const bool tolerant=
false,
209 const Codec c=CODEC_PLAIN);
212 void add_codec(
const size_t index,
const Codec c);
215 void read_error_check(
const int index,
const std::string &label=std::string());
218 void read_error_check_rdbuf(
const int index,
const std::string &label=std::string());
225 void close_stream(
const std::istream*
const s);
228 void close_stream(
const size_t i);
230 void close_stream(
const int i);
233 void use_mmap(
const bool b);
239 std::istream& operator[](
size_t n);
242 std::istream &front();
245 std::istream &back();
248 uint64_t get_streamsize(
const size_t i)
const;
251 const std::string &get_streamname(
const size_t i)
const;
254 bool is_raw_seekable(
const size_t i)
const;
257 bool is_stream_seekable(
const size_t i)
const;
262 :
public std::ostream,
286 static Codec DEFAULT_ENC;
288 static Codec DEFAULT_FASTENC;
291 static size_t DEFAULT_ENCBS;
297 std::string streamname;
299 std::vector<std::ostream *> stream_stack;
307 std::string rename_name;
310 struct output_flushable
311 :boost::iostreams::output,
312 boost::iostreams::flushable_tag
318 :
public boost::iostreams::device<output_flushable>
322 std::vector<stream_info> *
const m_streams;
326 streamset_sink(std::vector<stream_info> *
const streams);
328 std::streamsize write(
const char* s,std::streamsize n);
334 void imbue(
const std::locale& l);
339 boost::iostreams::stream_buffer<streamset_sink>* m_streambuf;
342 boost::iostreams::stream<boost::iostreams::null_sink> m_null;
345 std::vector<stream_info> m_streams;
388 const std::string &stream_name,
389 const std::string &label=std::string());
397 explicit ostreams(
const std::vector<std::string> &fn,
424 std::ostream &
open_file(
const std::string& fn,
426 const ErrorCheck ec=ERRCHECK_AUTO);
435 const ErrorCheck ec=ERRCHECK_AUTO);
442 for(Iter itr=f_beg;itr!=f_end;++itr)
450 const Codec c=CODEC_PLAIN,
452 const ErrorCheck ec=ERRCHECK_AUTO);
467 const std::string &rename_name,
470 const ErrorCheck ec=ERRCHECK_AUTO);
475 const ErrorCheck ec=ERRCHECK_AUTO);
480 const ErrorCheck ec=ERRCHECK_AUTO);
486 const ErrorCheck ec=ERRCHECK_AUTO);
491 const ErrorCheck ec=ERRCHECK_AUTO);
494 std::ostream &
open_pipe(
const std::string &command,
495 const Codec c=CODEC_PLAIN,
496 const ErrorCheck ec=ERRCHECK_AUTO);
499 std::ostream &
push_back(std::ostream &os,
500 const std::string &name=std::string(),
501 const Codec c=CODEC_PLAIN,
502 const ErrorCheck ec=ERRCHECK_AUTO);
506 const ErrorCheck ec=ERRCHECK_AUTO);
532 std::ostream &
front();
535 std::ostream &
back();
543 :
public boost::iostreams::stream<boost::iostreams::file_descriptor_source>
547 typedef boost::iostreams::file_descriptor_source source_t;
548 typedef boost::iostreams::stream<source_t> base_t;
552 const std::string m_command;
554 const bool m_tolerate_errexit;
558 explicit ipipe(
const std::string& command,
const bool tolerant=
false);
564 :
public boost::iostreams::stream<boost::iostreams::file_descriptor_sink>
568 typedef boost::iostreams::file_descriptor_sink sink_t;
569 typedef boost::iostreams::stream<sink_t> base_t;
573 const std::string m_command;
577 explicit opipe(
const std::string& command);
585 :
public boost::iostreams::device<boost::iostreams::input_seekable>
589 static const std::streamsize m_mmapsize;
601 std::streamsize
read(
char* s,std::streamsize n);
603 std::streampos seek(boost::iostreams::stream_offset off,
604 std::ios_base::seekdir way);
611 :
public boost::iostreams::stream<mmapping_source>
620 #endif // STREAMSET_HPP__