ucommon
stream.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
24 #if defined(OLD_STDCPP) || defined(NEW_STDCPP)
25 #ifndef _UCOMMON_STREAM_H_
26 #define _UCOMMON_STREAM_H_
27 
28 #ifndef _UCOMMON_CONFIG_H
29 #include <ucommon/platform.h>
30 #endif
31 
32 #ifndef _UCOMMON_PROTOCOLS_H_
33 #include <ucommon/protocols.h>
34 #endif
35 
36 #ifndef _UCOMMON_THREAD_H_
37 #include <ucommon/thread.h>
38 #endif
39 
40 #ifndef _UCOMMON_SOCKET_H_
41 #include <ucommon/socket.h>
42 #endif
43 
44 #ifndef _UCOMMON_FSYS_H_
45 #include <ucommon/fsys.h>
46 #endif
47 
48 #include <iostream>
49 
50 NAMESPACE_UCOMMON
51 
58 class __EXPORT StreamProtocol : protected std::streambuf, public std::iostream, public CharacterProtocol
59 {
60 protected:
61  size_t bufsize;
62  char *gbuf, *pbuf;
63 
65 
66  int underflow();
67 
68  int overflow(int code);
69 
78  int uflow();
79 
80  void release(void);
81 
82  void allocate(size_t size);
83 
84 public:
89  int sync(void);
90 
91  inline bool is_open(void)
92  {return bufsize > 0;}
93 
94  inline operator bool()
95  {return bufsize > 0;}
96 
97  inline bool operator!()
98  {return bufsize == 0;}
99 };
100 
109 class __EXPORT tcpstream : public StreamProtocol
110 {
111 private:
112  __LOCAL void allocate(unsigned size);
113  __LOCAL void reset(void);
114 
115 protected:
116  socket_t so;
117  timeout_t timeout;
118 
119  virtual ssize_t _read(char *buffer, size_t size);
120 
121  virtual ssize_t _write(const char *buffer, size_t size);
122 
123  virtual bool _wait(void);
124 
128  void release(void);
129 
136  int _getch(void);
137 
144  int _putch(int ch);
145 
146  inline socket_t getsocket(void) const
147  {return so;}
148 
149 public:
154  tcpstream(const tcpstream& copy);
155 
162  tcpstream(const TCPServer *server, unsigned segsize = 536, timeout_t timeout = 0);
163 
169  tcpstream(int family = PF_INET, timeout_t timeout = 0);
170 
179  tcpstream(Socket::address& address, unsigned segsize = 536, timeout_t timeout = 0);
180 
184  virtual ~tcpstream();
185 
190  inline operator bool() const
191  {return so != INVALID_SOCKET && bufsize > 0;};
192 
197  inline bool operator!() const
198  {return so == INVALID_SOCKET || bufsize == 0;};
199 
205  void open(Socket::address& address, unsigned segment = 536);
206 
213  void open(const char *host, const char *service, unsigned segment = 536);
214 
219  void close(void);
220 };
221 
230 class __EXPORT pipestream : public StreamProtocol
231 {
232 public:
233  typedef enum {
234  RDONLY,
235  WRONLY,
236  RDWR
237  } access_t;
238 
239 private:
240  __LOCAL void allocate(size_t size, access_t mode);
241 
242 protected:
243  fsys_t rd, wr;
244  pid_t pid;
245 
249  void release(void);
250 
257  int _getch(void);
258 
266  int _putch(int ch);
267 
268 public:
272  pipestream();
273 
281  pipestream(const char *command, access_t access, const char **env = NULL, size_t size = 512);
282 
286  virtual ~pipestream();
287 
292  inline operator bool() const
293  {return (bufsize > 0);};
294 
299  inline bool operator!() const
300  {return bufsize == 0;};
301 
309  void open(const char *command, access_t access, const char **env = NULL, size_t buffering = 512);
310 
315  void close(void);
316 
320  void terminate(void);
321 };
322 
331 class __EXPORT filestream : public StreamProtocol
332 {
333 public:
334  typedef enum {
335  RDONLY,
336  WRONLY,
337  RDWR
338  } access_t;
339 
340 private:
341  __LOCAL void allocate(size_t size, fsys::access_t mode);
342 
343 protected:
344  fsys_t fd;
345  fsys::access_t ac;
346 
353  int _getch(void);
354 
362  int _putch(int ch);
363 
364 public:
368  filestream();
369 
373  filestream(const filestream& copy);
374 
378  filestream(const char *path, fsys::access_t access, unsigned mode, size_t bufsize);
379 
383  filestream(const char *path, fsys::access_t access, size_t bufsize);
384 
388  virtual ~filestream();
389 
394  inline operator bool() const
395  {return (bufsize > 0);};
396 
401  inline bool operator!() const
402  {return bufsize == 0;};
403 
407  void open(const char *filename, fsys::access_t access, size_t buffering = 512);
408 
412  void create(const char *filename, fsys::access_t access, unsigned mode, size_t buffering = 512);
413 
417  void close(void);
418 
422  void seek(fsys::offset_t offset);
423 
428  inline int err(void) const
429  {return fd.err();};
430 };
431 
432 END_NAMESPACE
433 
434 #endif
435 #endif