libzypp 17.37.17
ExternalDataSource.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
11
12
13#define _GNU_SOURCE 1 // for ::getline
14
15#include <errno.h>
16#include <unistd.h>
17#include <sys/wait.h>
18#include <fcntl.h>
19#include <iostream>
20#include <stdlib.h>
21#include <string.h>
22#include <sstream>
23#include <string>
24
29
30using std::endl;
31
32namespace zypp {
33 namespace externalprogram {
34
35 ExternalDataSource::ExternalDataSource( FILE *ifile, FILE *ofile )
36 : inputfile( ifile ),
37 outputfile( ofile ),
38 linebuffer( 0 ),
40 {
41 }
42
43
50
51
52 bool
53 ExternalDataSource::send( const char *buffer, size_t length )
54 {
55 if ( outputfile ) {
56 bool success = fwrite( buffer, length, 1, outputfile ) != 0;
57 fflush( outputfile );
58 return success;
59 }
60 else
61 return false;
62 }
63
64
65 bool
66 ExternalDataSource::send( std::string s )
67 {
68 DBG << "send (" << s << ")";
69 return send( s.data(), s.length() );
70 }
71
72
73 std::string
75 {
76 if ( inputfile && !feof( inputfile ) )
77 {
78 std::ostringstream datas;
79 while ( true )
80 {
81 int readc = fgetc( inputfile );
82 if ( readc == EOF ) break;
83 datas << (char)readc;
84 if ( (char)readc == c ) break;
85 }
86 return datas.str();
87 }
88 return std::string();
89 }
90
92 {
93 const auto &received = io::receiveUpto( inputFile(), c, timeout );
94
95 if ( received.first == io::Timeout )
97
98 return received.second;
99 }
100
101 size_t
102 ExternalDataSource::receive( char *buffer, size_t length )
103 {
104 if ( inputfile )
105 return fread( buffer, 1, length, inputfile );
106 else
107 return 0;
108 }
109
111 {
113 }
114
116 {
117 if ( inputfile ) {
118 ssize_t nread = zyppng::eintrSafeCallEx( getline, [&](){clearerr ( inputfile );}, &linebuffer, &linebuffer_size, inputfile );
119 if ( nread == -1 )
120 return "";
121 else
122 return std::string( linebuffer, nread );
123 }
124 else
125 return "";
126 }
127
129 {
130 return receiveUpto( '\n', timeout );
131 }
132
133 int
135 {
136 if ( inputfile && inputfile != outputfile )
137 fclose( inputfile );
138 if ( outputfile )
139 fclose( outputfile );
140 inputfile = 0;
141 outputfile = 0;
142 return 0;
143 }
144
145 } // namespace externalprogram
146} // namespace zypp
147
size_t receive(char *buffer, size_t length)
Read some data from the input stream.
bool send(const char *buffer, size_t length)
Send some data to the output stream.
ExternalDataSource(FILE *inputfile=0, FILE *outputfile=0)
Create a new instance.
virtual int close()
Close the input and output streams.
void setBlocking(bool mode)
Set the blocking mode of the input stream.
FILE * inputFile() const
Return the input stream.
std::string receiveUpto(char c)
Read characters into a string until delimiter c or EOF is read.
std::string receiveLine()
Read one line from the input stream.
virtual ~ExternalDataSource()
Implicitly close the connection.
BlockingMode setFILEBlocking(FILE *file, bool mode)
Enables or disabled non blocking mode on a file descriptor.
Definition IOTools.cc:25
std::pair< ReceiveUpToResult, std::string > receiveUpto(FILE *file, char c, timeout_type timeout, bool failOnUnblockError)
Definition IOTools.cc:85
@ Timeout
Definition IOTools.h:72
size_t timeout_type
Definition IOTools.h:77
Easy-to use interface to the ZYPP dependency resolver.
auto eintrSafeCallEx(const Fun &function, const RestartCb &restartCb, Args &&... args)
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
#define DBG
Definition Logger.h:99