Imported Upstream version 17.25.4
[platform/upstream/libzypp.git] / zypp / base / ExternalDataSource.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/ExternalDataSource.h
10  */
11
12 #ifndef ZYPP_EXTERNALDATASOURCE_H
13 #define ZYPP_EXTERNALDATASOURCE_H
14
15 #include <stdio.h>
16
17 #include <string>
18 #include <optional>
19 #include <zypp/base/IOTools.h>
20
21 namespace zypp {
22   namespace externalprogram {
23
24     /**
25      * @short Bidirectional stream to external data
26      */
27     class ExternalDataSource
28     {
29     protected:
30       FILE *inputfile;
31       FILE *outputfile;
32
33     private:
34       char *linebuffer;
35       size_t linebuffer_size;
36
37     public:
38       /**
39        * Create a new instance.
40        * @param inputfile The stream for reading
41        * @param outputfile The stream for writing
42        * Either can be NULL if no reading/writing is allowed.
43        */
44       ExternalDataSource( FILE *inputfile = 0, FILE *outputfile = 0 );
45
46       /**
47        * Implicitly close the connection.
48        */
49       virtual ~ExternalDataSource ();
50
51       /**
52        * Send some data to the output stream.
53        * @param buffer The data to send
54        * @param length The size of it
55        */
56       bool send( const char *buffer, size_t length );
57
58       /**
59        * Send some data down the stream.
60        * @param string The data to send
61        */
62       bool send( std::string s );
63
64       /**
65        * Read some data from the input stream.
66        * @param buffer Where to put the data
67        * @param length How much to read at most
68        * Returns the amount actually received
69        */
70       size_t receive( char *buffer, size_t length );
71
72       /**
73        * Read one line from the input stream.
74        * Returns the line read, including the terminator.
75        */
76       std::string receiveLine();
77
78       /**
79        * Read one line from the input stream.
80        * Returns the line read, including the terminator.
81        * \note The delimiter is not removed from the string.
82        * \note The \a timeout value is to be specified in milliseconds.
83        * \throws io::TimeoutException if the timeout is reached
84        */
85       std::string receiveLine( io::timeout_type timeout );
86
87       /**
88        * Read characters into a string until delimiter \a c or EOF is
89        * read.
90        * \note The delimiter is not removed from the string.
91        */
92       std::string receiveUpto( char c );
93
94       /**
95        * Read characters into a string until delimiter \a c or EOF is
96        * read or the \a timeout is reached.
97        * \note The delimiter is not removed from the string.
98        * \note The \a timeout value is to be specified in milliseconds.
99        * \throws io::TimeoutException if the timeout is reached
100        */
101       std::string receiveUpto( char c, io::timeout_type timeout );
102
103       /**
104        * Set the blocking mode of the input stream.
105        * @param mode True if the reader should be blocked waiting for input.
106        * This is the initial default.
107        */
108       void setBlocking( bool mode );
109
110       /**
111        * Close the input and output streams.
112        */
113       virtual int close();
114
115       /**
116        * Return the input stream.
117        */
118       FILE *inputFile() const  { return inputfile; }
119
120       /**
121        * Return the output stream.
122        */
123       FILE *outputFile() const { return outputfile; }
124     };
125
126   } // namespace externalprogram
127 } // namespace zypp
128
129 #endif // ZYPP_EXTERNALDATASOURCE_H
130