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 #ifndef _UCOMMON_SHELL_H_
49 #include <ucommon/shell.h>
50 #endif
51 
52 #include <iostream>
53 
54 NAMESPACE_UCOMMON
55 
62 class __EXPORT StreamProtocol : protected std::streambuf, public std::iostream, public CharacterProtocol
63 {
64 protected:
65  size_t bufsize;
66  char *gbuf, *pbuf;
67 
69 
70  int underflow();
71 
72  int overflow(int code);
73 
82  int uflow();
83 
84  void release(void);
85 
86  void allocate(size_t size);
87 
88 public:
93  int sync(void);
94 
95  inline bool is_open(void)
96  {return bufsize > 0;}
97 
98  inline operator bool()
99  {return bufsize > 0;}
100 
101  inline bool operator!()
102  {return bufsize == 0;}
103 };
104 
113 class __EXPORT tcpstream : public StreamProtocol
114 {
115 private:
116  __LOCAL void allocate(unsigned size);
117  __LOCAL void reset(void);
118 
119 protected:
120  socket_t so;
121  timeout_t timeout;
122 
123  virtual ssize_t _read(char *buffer, size_t size);
124 
125  virtual ssize_t _write(const char *buffer, size_t size);
126 
127  virtual bool _wait(void);
128 
132  void release(void);
133 
140  int _getch(void);
141 
148  int _putch(int ch);
149 
150  inline socket_t getsocket(void) const
151  {return so;}
152 
153 public:
158  tcpstream(const tcpstream& copy);
159 
166  tcpstream(const TCPServer *server, unsigned segsize = 536, timeout_t timeout = 0);
167 
173  tcpstream(int family = PF_INET, timeout_t timeout = 0);
174 
183  tcpstream(Socket::address& address, unsigned segsize = 536, timeout_t timeout = 0);
184 
188  virtual ~tcpstream();
189 
194  inline operator bool() const
195  {return so != INVALID_SOCKET && bufsize > 0;};
196 
201  inline bool operator!() const
202  {return so == INVALID_SOCKET || bufsize == 0;};
203 
209  void open(Socket::address& address, unsigned segment = 536);
210 
217  void open(const char *host, const char *service, unsigned segment = 536);
218 
223  void close(void);
224 };
225 
234 class __EXPORT pipestream : public StreamProtocol
235 {
236 public:
237  typedef enum {
238  RDONLY,
239  WRONLY,
240  RDWR
241  } access_t;
242 
243 private:
244  __LOCAL void allocate(size_t size, access_t mode);
245 
246 protected:
247  fsys_t rd, wr;
248  shell::pid_t pid;
249 
253  void release(void);
254 
261  int _getch(void);
262 
270  int _putch(int ch);
271 
272 public:
276  pipestream();
277 
286  pipestream(const char *command, access_t access, char **args, char **env = NULL, size_t size = 512);
287 
291  virtual ~pipestream();
292 
297  inline operator bool() const
298  {return (bufsize > 0);};
299 
304  inline bool operator!() const
305  {return bufsize == 0;};
306 
315  void open(const char *path, access_t access, char **args, char **env = NULL, size_t buffering = 512);
316 
321  int close(void);
322 
326  void terminate(void);
327 
328  inline void cancel(void)
329  {terminate();}
330 };
331 
340 class __EXPORT filestream : public StreamProtocol
341 {
342 public:
343  typedef enum {
344  RDONLY,
345  WRONLY,
346  RDWR
347  } access_t;
348 
349 private:
350  __LOCAL void allocate(size_t size, fsys::access_t mode);
351 
352 protected:
353  fsys_t fd;
354  fsys::access_t ac;
355 
362  int _getch(void);
363 
371  int _putch(int ch);
372 
373 public:
377  filestream();
378 
382  filestream(const filestream& copy);
383 
387  filestream(const char *path, fsys::access_t access, unsigned mode, size_t bufsize);
388 
392  filestream(const char *path, fsys::access_t access, size_t bufsize);
393 
397  virtual ~filestream();
398 
403  inline operator bool() const
404  {return (bufsize > 0);};
405 
410  inline bool operator!() const
411  {return bufsize == 0;};
412 
416  void open(const char *filename, fsys::access_t access, size_t buffering = 512);
417 
421  void create(const char *filename, fsys::access_t access, unsigned mode, size_t buffering = 512);
422 
426  void close(void);
427 
431  void seek(fsys::offset_t offset);
432 
437  inline int err(void) const
438  {return fd.err();};
439 };
440 
441 END_NAMESPACE
442 
443 #endif
444 #endif