libzypp 17.31.23
ExternalDataSource.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#define _GNU_SOURCE 1 // for ::getline
13
14#include <signal.h>
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
25#include <zypp-core/base/Logger.h>
26#include <zypp-core/base/ExternalDataSource.h>
27#include <zypp-core/AutoDispose.h>
28
29using std::endl;
30
31namespace zypp {
32 namespace externalprogram {
33
34 ExternalDataSource::ExternalDataSource( FILE *ifile, FILE *ofile )
35 : inputfile( ifile ),
36 outputfile( ofile ),
37 linebuffer( 0 ),
38 linebuffer_size( 0 )
39 {
40 }
41
42
44 {
45 if (linebuffer)
46 free( linebuffer );
47 close ();
48 }
49
50
51 bool
52 ExternalDataSource::send( const char *buffer, size_t length )
53 {
54 if ( outputfile ) {
55 bool success = fwrite( buffer, length, 1, outputfile ) != 0;
56 fflush( outputfile );
57 return success;
58 }
59 else
60 return false;
61 }
62
63
64 bool
65 ExternalDataSource::send( std::string s )
66 {
67 DBG << "send (" << s << ")";
68 return send( s.data(), s.length() );
69 }
70
71
72 std::string
74 {
75 if ( inputfile && !feof( inputfile ) )
76 {
77 std::ostringstream datas;
78 while ( true )
79 {
80 int readc = fgetc( inputfile );
81 if ( readc == EOF ) break;
82 datas << (char)readc;
83 if ( (char)readc == c ) break;
84 }
85 return datas.str();
86 }
87 return std::string();
88 }
89
91 {
92 const auto &received = io::receiveUpto( inputFile(), c, timeout );
93
94 if ( received.first == io::Timeout )
96
97 return received.second;
98 }
99
100 size_t
101 ExternalDataSource::receive( char *buffer, size_t length )
102 {
103 if ( inputfile )
104 return fread( buffer, 1, length, inputfile );
105 else
106 return 0;
107 }
108
110 {
112 }
113
114 std::string
116 {
117 if ( inputfile )
118 {
119 ssize_t nread = getline( &linebuffer, &linebuffer_size, inputfile );
120 if ( nread == -1 )
121 return "";
122 else
123 return std::string( linebuffer, nread );
124 }
125 else
126 return "";
127 }
128
130 {
131 return receiveUpto( '\n', timeout );
132 }
133
134 int
136 {
137 if ( inputfile && inputfile != outputfile )
138 fclose( inputfile );
139 if ( outputfile )
140 fclose( outputfile );
141 inputfile = 0;
142 outputfile = 0;
143 return 0;
144 }
145
146 } // namespace externalprogram
147} // namespace zypp
148
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:71
size_t timeout_type
Definition: IOTools.h:76
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:428
#define DBG
Definition: Logger.h:95