ucommon
stream.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 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 StreamBuffer : protected std::streambuf, public std::iostream
63 {
64 protected:
65  size_t bufsize;
66  char *gbuf, *pbuf;
67 
68  StreamBuffer();
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 StreamBuffer
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 underflow(void);
137 
144  int overflow(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 StreamBuffer
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  shell::pid_t pid;
245 
249  void release(void);
250 
257  int underflow(void);
258 
266  int overflow(int ch);
267 
268 public:
272  pipestream();
273 
282  pipestream(const char *command, access_t access, char **args, char **env = NULL, size_t size = 512);
283 
287  virtual ~pipestream();
288 
293  inline operator bool() const
294  {return (bufsize > 0);};
295 
300  inline bool operator!() const
301  {return bufsize == 0;};
302 
311  void open(const char *path, access_t access, char **args, char **env = NULL, size_t buffering = 512);
312 
317  int close(void);
318 
322  void terminate(void);
323 
324  inline void cancel(void)
325  {terminate();}
326 };
327 
336 class __EXPORT filestream : public StreamBuffer
337 {
338 public:
339  typedef enum {
340  RDONLY,
341  WRONLY,
342  RDWR
343  } access_t;
344 
345 private:
346  __LOCAL void allocate(size_t size, fsys::access_t mode);
347 
348 protected:
349  fsys_t fd;
350  fsys::access_t ac;
351 
358  int underflow(void);
359 
367  int overflow(int ch);
368 
369 public:
373  filestream();
374 
378  filestream(const filestream& copy);
379 
383  filestream(const char *path, unsigned mode, fsys::access_t access, size_t bufsize = 512);
384 
388  filestream(const char *path, fsys::access_t access, size_t bufsize = 512);
389 
393  virtual ~filestream();
394 
399  inline operator bool() const
400  {return (bufsize > 0);};
401 
406  inline bool operator!() const
407  {return bufsize == 0;};
408 
412  void open(const char *filename, fsys::access_t access, size_t buffering = 512);
413 
417  void open(const char *filename, unsigned mode, fsys::access_t access, size_t buffering = 512);
418 
422  void close(void);
423 
427  void seek(fsys::offset_t offset);
428 
433  inline int err(void) const
434  {return fd.err();};
435 };
436 
441 class __EXPORT _stream_operators
442 {
443 private:
444  inline _stream_operators() {};
445 
446 public:
447  static std::ostream& print(std::ostream& out, const PrintProtocol& format);
448 
449  static std::istream& input(std::istream& inp, InputProtocol& format);
450 
451  static std::ostream& print(std::ostream& out, const string_t& str);
452 
453  static std::istream& input(std::istream& inp, string_t& str);
454 
455  static std::ostream& print(std::ostream& out, const stringlist_t& list);
456 
457  static std::istream& input(std::istream& in, stringlist_t& list);
458 
459 };
460 
461 inline std::ostream& operator<< (std::ostream& out, const PrintProtocol& format)
462  {return _stream_operators::print(out, format);}
463 
464 inline std::istream& operator>> (std::istream& inp, InputProtocol& format)
465  {return _stream_operators::input(inp, format);}
466 
467 inline std::ostream& operator<< (std::ostream& out, const string_t& str)
468  {return _stream_operators::print(out, str);}
469 
470 inline std::istream& operator>> (std::istream& inp, string_t& str)
471  {return _stream_operators::input(inp, str);}
472 
473 inline std::ostream& operator<< (std::ostream& out, const stringlist_t& list)
474  {return _stream_operators::print(out, list);}
475 
476 inline std::istream& operator>> (std::istream& in, stringlist_t& list)
477  {return _stream_operators::input(in, list);}
478 
479 END_NAMESPACE
480 
481 #endif
482 #endif
PersistEngine & operator<<(PersistEngine &ar, PersistObject const &ob)
Definition: persist.h:319
A generic tcp server class.
Definition: socket.h:1645
A generic socket address class.
Definition: socket.h:343
Streamable tcp connection between client and server.
Definition: stream.h:336
Various miscellaneous platform specific headers and defines.
bool operator!() const
See if stream is disconnected.
Definition: stream.h:197
A container for generic and o/s portable threadsafe file system functions.
Definition: fsys.h:124
access_t
Enumerated file access modes.
Definition: fsys.h:158
Generic shell parsing and application services.
Used for forming stream output.
Definition: protocols.h:137
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
Definition: access.h:253
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:260
StringPager stringlist_t
A convenience type for paged string lists.
Definition: memory.h:1388
long offset_t
File offset type.
Definition: fsys.h:174
A copy-on-write string class that operates by reference count.
Definition: string.h:82
String string_t
A convenience type for string.
Definition: string.h:1575
int err(void) const
Get error flag from last i/o operation.
Definition: stream.h:433
bool operator!() const
See if stream is disconnected.
Definition: stream.h:406
Thread classes and sychronization objects.
Streamable tcp connection between client and server.
Definition: stream.h:109
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:479
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
Used for processing input.
Definition: protocols.h:156
bool operator!() const
See if stream is disconnected.
Definition: stream.h:300
Common stream buffer for std C++ i/o classes.
Definition: stream.h:62
int err(void) const
Get last error.
Definition: fsys.h:545
Thread-aware file system manipulation class.
PersistEngine & operator>>(PersistEngine &ar, PersistObject &ob)
Definition: persist.h:315
Abstract interfaces and support.
Streamable tcp connection between client and server.
Definition: stream.h:230
At least with gcc, linking of stream operators was broken.
Definition: stream.h:441
String pager for storing lists of NULL terminated strings.
Definition: memory.h:364
Common socket class and address manipulation.