1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2004-2021 Free Software Foundation, Inc.
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 3 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General
15    Public License along with this library.  If not, see
16    <http://www.gnu.org/licenses/>. */
17 
18 #ifndef _MUCPP_STREAM_H
19 #define _MUCPP_STREAM_H
20 
21 #include <string>
22 #include <errno.h>
23 #include <mailutils/stream.h>
24 #include <mailutils/prog.h>
25 #include <mailutils/wordsplit.h>
26 #include <mailutils/cpp/error.h>
27 
28 namespace mailutils
29 {
30 
31 class Stream
32 {
33  protected:
34   mu_stream_t stm;
35   size_t read_count;
36   size_t write_count;
37   int wflags;
38   bool opened;
39   size_t reference_count;
40 
41   // Inlines
reference()42   void reference () {
43     reference_count++;
44   }
dereference()45   bool dereference () {
46     return --reference_count == 0;
47   }
48 
49   // Friends
50   friend class FilterStream;
51   friend class FilterProgStream;
52   friend class FilterIconvStream;
53   friend class Folder;
54   friend class Mailcap;
55   friend class Message;
56   friend class Pop3;
57 
58  public:
59   Stream ();
60   Stream (Stream& s);
61   Stream (const mu_stream_t);
62   ~Stream ();
63 
64   void open ();
65   void close ();
66   void set_waitflags (int flags);
67   void wait (); // timeval is missing
68   void wait (int flags); // timeval is missing
69   int  read (void* rbuf, size_t size);
70   int  write (void* wbuf, size_t size);
71   int  write (const std::string& wbuf, size_t size);
72   int  readline (char* rbuf, size_t size);
73   void flush ();
74 
75   // Inlines
get_read_count()76   size_t get_read_count () const {
77     return read_count;
78   };
get_write_count()79   size_t get_write_count () const {
80     return write_count;
81   };
82 
83   friend Stream& operator << (Stream&, const std::string&);
84   friend Stream& operator >> (Stream&, std::string&);
85 
86   // Stream Exceptions
87   class EAgain : public Exception {
88   public:
EAgain(const char * m,int s)89     EAgain (const char* m, int s) : Exception (m, s) {}
90   };
91 };
92 
93 class TcpStream : public Stream
94 {
95  public:
96   TcpStream (const std::string&, int, int);
97 };
98 
99 class FileStream : public Stream
100 {
101  public:
102   FileStream (const std::string&, int);
103 };
104 
105 class StdioStream : public Stream
106 {
107  public:
108   StdioStream (int fd, int flags);
109 };
110 
111 class ProgStream : public Stream
112 {
113  public:
114   ProgStream (const std::string&, int);
115 };
116 
117 class FilterProgStream : public Stream
118 {
119  private:
120   Stream *input;
121  public:
122   FilterProgStream (const std::string&, Stream&);
123   FilterProgStream (const std::string&, Stream*);
124 };
125 
126 }
127 
128 #endif // not _MUCPP_STREAM_H
129 
130