24 #ifdef USE_FDSTREAM_HPP 25 #include "fdstream.hpp" 41 #ifndef USE_FDSTREAM_HPP 42 boost::scoped_ptr<std::ifstream> file;
43 boost::scoped_ptr<boost::iostreams::filtering_streambuf<boost::iostreams::input> >
46 std::istream *filestream_;
48 std::string auxfilename;
55 filestream_ ( NULL ) {
60 filestream_ ( NULL ) {
66 filestream_ ( NULL ) {
75 inline void open (
const std::stringstream& ss ) {
77 filestream_ =
new std::stringstream ( ss.str() );
86 inline void open (
const std::string& filename ) {
88 #ifdef USE_FDSTREAM_HPP 89 if ( filename !=
"-" ) {
90 LDEBUG (
"Test file=[" << filename <<
"]" );
91 sfile_ = fopen ( filename.c_str(),
"r" );
92 USER_CHECK ( sfile_ != NULL,
"Error while opening file" );
97 std::string command =
"zcat -f ";
99 LINFO (
"Opening (fd)" << command );
100 sfile_ = popen ( command.c_str(),
"r" );
101 USER_CHECK ( sfile_ != NULL,
"Error while opening pipe" );
102 filestream_ =
new boost::fdistream ( fileno ( sfile_ ) );
103 USER_CHECK (filestream_,
"File Stream allocation failed!");
105 LINFO (
"Opening " << filename );
106 std::string auxfilename = filename;
107 if (auxfilename ==
"-" ) auxfilename =
"/dev/stdin";
108 file.reset (
new std::ifstream (auxfilename.c_str(),
109 std::ios_base::in | std::ios_base::binary) );
110 if (!USER_CHECK (file->is_open(),
111 "Error while opening file:") ) exit (EXIT_FAILURE);
112 in.reset (
new boost::iostreams::filtering_streambuf<boost::iostreams::input>);
113 if (auxfilename.substr (0, 5) !=
"/dev/" ) {
115 in->push (boost::iostreams::gzip_decompressor() );
118 filestream_ =
new std::istream (&*in);
119 if (!USER_CHECK (filestream_,
120 "File Stream allocation failed!") ) exit (EXIT_FAILURE);
124 if (!USER_CHECK (filestream_->good(),
125 "File not open/doesn't exist... Or possibly not compressed but ends with .gz? " ) )
133 return ( filestream_ != NULL );
142 virtual inline int eof() {
143 return filestream_->eof();
149 #ifndef USE_FDSTREAM_HPP 164 virtual operator void *() {
165 return eof() ? NULL :
this;
173 template <
typename T>
189 template <
typename T>
191 *iszf.filestream_ >> stff;
207 #ifndef USE_FDSTREAM_HPP 208 boost::scoped_ptr<std::ofstream> file;
209 boost::scoped_ptr<boost::iostreams::filtering_streambuf<boost::iostreams::output> >
212 std::ostream *filestream_;
214 std::string myfilename;
223 oszfstream (
const std::string& filename ,
bool append =
false) :
225 filestream_ ( NULL ),
236 sfile_ ( NULL ), filestream_ ( NULL ), append_ (false) {
245 void open (
const std::stringstream& ss ) {
247 filestream_ =
new std::stringstream ( ss.str() );
255 void open (
const std::string& filename ) {
257 if (filename ==
"") {
258 LWARN (
"Empty file name?");
263 if ( cmd !=
"" && cmd !=
"./" && cmd !=
"/" ) {
264 cmd =
"mkdir -p " + cmd;
265 int a = system ( cmd.c_str() );
267 #ifdef USE_FDSTREAM_HPP 268 std::string command =
"cat - >";
269 if ( filename.size() > 3 )
270 if ( filename.substr ( filename.size() - 3 ) ==
".gz" ) command =
"gzip > ";
271 if (append_) command +=
">";
272 if ( filename ==
"-" ) command +=
"/dev/stdout";
273 else command += filename;
274 if ( ( sfile_ = popen ( command.c_str(),
"w" ) ) == NULL ) {
275 cerr <<
"Error while opening file via: " << command << endl;
276 exit ( EXIT_FAILURE );
278 LINFO (
"Opening (fd)" << command );
279 filestream_ =
new boost::fdostream ( fileno ( sfile_ ) );
281 if ( filename ==
"-" ) myfilename =
"/dev/stdout";
282 else myfilename = filename;
284 file.reset (
new std::ofstream (myfilename.c_str(),
285 std::ios_base::out | std::ios_base::binary ) );
287 file.reset (
new std::ofstream (myfilename.c_str(),
288 std::ios_base::out | std::ios_base::binary | std::ios_base::app ) );
289 if (!file->is_open() ) {
290 std::cerr <<
"Error while opening " << filename << std::endl;
293 out.reset (
new boost::iostreams::filtering_streambuf<boost::iostreams::output>);
294 if (filename.substr (0, 5) !=
"/dev/" ) {
296 out->push (boost::iostreams::gzip_compressor() );
300 LINFO (
"Opening " << filename );
301 filestream_ =
new std::ostream (&*out);
302 if (filestream_ == NULL) {
303 std::cerr <<
"Error while opening " << filename << std::endl;
319 return ( filestream_ != NULL );
325 #ifndef USE_FDSTREAM_HPP 339 typedef std::basic_ostream<char, std::char_traits<char> >
CoutType;
340 typedef CoutType& ( *StandardEndLine ) ( CoutType&);
349 template <
typename T>
351 *filestream_ << stff;
358 template <
typename FM>
362 while (
getline ( iszf, line ) ) {
369 template <
typename FM>
373 while ( fm.toLine ( line ) ) {
374 oszf << line << endl;
380 template <
class StreamT = std::istream >
383 boost::scoped_ptr<StreamT> af_;
394 inline bool operator() ( uint
id, std::string *line ) {
395 bool finished =
false;
398 USER_CHECK ( idx_ <=
id,
"Will not read backwards!" );
399 while ( ++idx_ <
id ) {
402 if ( !
getline ( *af_, *line ) ) {
Wrapper stream class that writes to pipes, text files or gzipped files.
Convenience class that reads "quickly" until a queried line.
std::basic_ostream< char, std::char_traits< char > > CoutType
std::ostream * getStream()
Returns internal stream.
friend iszfstream & operator>>(iszfstream &, T &)
Templated operator >> for streaming out of iszfstream.
virtual iszfstream & getline(std::string &line)
Read a line.
iszfstream()
Empty constructor.
oszfstream(const std::stringstream &ss)
Constructor.
void open(const std::stringstream &ss)
void open(const std::string &filename)
Opens a file. using boost: using fdstream: All three cases are handled with zcat -f,, which is piped (i.e. handled by another processor).
iszfstream(const std::stringstream &ss)
iszfstream(const std::string &filename)
Constructor with a file name. Opens the file.
bool is_open()
Checks if the file/pipe is open.
std::ostream & operator<<(std::ostream &o, Container< T > const &container)
~iszfstream()
Destructor. Closes the file.
iszfstream & getline(iszfstream &izs, std::string &line)
void open(const std::string &filename)
Opens a [file].
oszfstream(const std::string &filename, bool append=false)
Constructor.
bool is_open()
Checks whether the file is open.
void writetextfile(const std::string &filename, FM &fm)
Function that writes to file. Templated on any external class with a toLine method.
bool DirName(std::string &dirname, const std::string &filename)
std::istream * getStream()
Returns internal stream.
void readtextfile(const std::string &filename, FM &fm)
Function that reads from a file. Templated on any external class with a parse method.
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
void open(const std::stringstream &ss)
~oszfstream()
Destructor. Closes the file.
virtual int eof()
Checks for end-of-file.
Wrapper stream class that reads pipes, text files or gzipped files.
bool ends_with(std::string const &haystack, std::string const &needle)
FastForwardRead(StreamT *af)
Constructor.
void close()
Closes the file.