1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/ExternalDataSource.cc
12 #define _GNU_SOURCE 1 // for ::getline
25 #include <zypp/base/Logger.h>
26 #include <zypp/base/ExternalDataSource.h>
27 #include <zypp/AutoDispose.h>
32 namespace externalprogram {
34 ExternalDataSource::ExternalDataSource( FILE *ifile, FILE *ofile )
43 ExternalDataSource::~ExternalDataSource()
52 ExternalDataSource::send( const char *buffer, size_t length )
55 bool success = fwrite( buffer, length, 1, outputfile ) != 0;
65 ExternalDataSource::send( std::string s )
67 DBG << "send (" << s << ")";
68 return send( s.data(), s.length() );
73 ExternalDataSource::receiveUpto( char c )
75 if ( inputfile && !feof( inputfile ) )
77 std::ostringstream datas;
80 int readc = fgetc( inputfile );
81 if ( readc == EOF ) break;
83 if ( (char)readc == c ) break;
90 std::string ExternalDataSource::receiveUpto( char c, io::timeout_type timeout )
92 const auto &received = io::receiveUpto( inputFile(), c, timeout );
94 if ( received.first == io::Timeout )
95 ZYPP_THROW( io::TimeoutException() );
97 return received.second;
101 ExternalDataSource::receive( char *buffer, size_t length )
104 return fread( buffer, 1, length, inputfile );
109 void ExternalDataSource::setBlocking( bool mode )
111 io::setFILEBlocking( inputfile, mode );
115 ExternalDataSource::receiveLine()
119 ssize_t nread = getline( &linebuffer, &linebuffer_size, inputfile );
123 return std::string( linebuffer, nread );
129 std::string ExternalDataSource::receiveLine( io::timeout_type timeout )
131 return receiveUpto( '\n', timeout );
135 ExternalDataSource::close()
137 if ( inputfile && inputfile != outputfile )
140 fclose( outputfile );
146 } // namespace externalprogram