1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/ExternalDataSource.cc
12 #define _GNU_SOURCE 1 // for ::getline
26 #include "zypp/base/Logger.h"
27 #include "zypp/base/ExternalDataSource.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;
92 ExternalDataSource::receive (char *buffer, size_t length)
95 return fread (buffer, 1, length, inputfile);
100 void ExternalDataSource::setBlocking(bool mode)
102 if(!inputfile) return;
104 int fd = ::fileno(inputfile);
107 { ERR << strerror(errno) << endl; return; }
109 int flags = ::fcntl(fd,F_GETFL);
112 { ERR << strerror(errno) << endl; return; }
115 flags = flags | O_NONBLOCK;
116 else if(flags & O_NONBLOCK)
117 flags = flags ^ O_NONBLOCK;
119 flags = ::fcntl(fd,F_SETFL,flags);
122 { ERR << strerror(errno) << endl; return; }
126 ExternalDataSource::receiveLine()
130 ssize_t nread = getline (&linebuffer, &linebuffer_size, inputfile);
134 return string (linebuffer, nread);
142 ExternalDataSource::close ()
144 if (inputfile && inputfile != outputfile)
154 } // namespace externalprogram