UCommon
stream.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
25 #ifndef UCOMMON_SYSRUNTIME
26 #ifndef _UCOMMON_STREAM_H_
27 #define _UCOMMON_STREAM_H_
28 
29 #ifndef _UCOMMON_CONFIG_H
30 #include <ucommon/platform.h>
31 #endif
32 
33 #ifndef _UCOMMON_PROTOCOLS_H_
34 #include <ucommon/protocols.h>
35 #endif
36 
37 #ifndef _UCOMMON_THREAD_H_
38 #include <ucommon/thread.h>
39 #endif
40 
41 #ifndef _UCOMMON_SOCKET_H_
42 #include <ucommon/socket.h>
43 #endif
44 
45 #ifndef _UCOMMON_FSYS_H_
46 #include <ucommon/fsys.h>
47 #endif
48 
49 #ifndef _UCOMMON_SHELL_H_
50 #include <ucommon/shell.h>
51 #endif
52 
53 #include <iostream>
54 
55 namespace ucommon {
56 
63 class __EXPORT StreamBuffer : protected std::streambuf, public std::iostream
64 {
65 private:
66  __DELETE_COPY(StreamBuffer);
67 
68 protected:
69  size_t bufsize;
70  char *gbuf, *pbuf;
71 
72  StreamBuffer();
73 
82  int uflow() __OVERRIDE;
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) const
96  {return bufsize > 0;}
97 
98  inline operator bool() const
99  {return bufsize > 0;}
100 
101  inline bool operator!() const
102  {return bufsize == 0;}
103 };
104 
113 class __EXPORT tcpstream : public StreamBuffer
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 underflow(void) __OVERRIDE;
141 
148  int overflow(int ch) __OVERRIDE;
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 StreamBuffer
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  __DELETE_COPY(pipestream);
247 
248 protected:
249  fsys_t rd, wr;
250  shell::pid_t pid;
251 
255  void release(void);
256 
263  int underflow(void) __OVERRIDE;
264 
272  int overflow(int ch) __OVERRIDE;
273 
274 public:
278  pipestream();
279 
288  pipestream(const char *command, access_t access, char **args, char **env = NULL, size_t size = 512);
289 
293  virtual ~pipestream();
294 
299  inline operator bool() const
300  {return (bufsize > 0);}
301 
306  inline bool operator!() const
307  {return bufsize == 0;}
308 
317  void open(const char *path, access_t access, char **args, char **env = NULL, size_t buffering = 512);
318 
323  int close(void);
324 
328  void terminate(void);
329 
330  inline void cancel(void)
331  {terminate();}
332 };
333 
342 class __EXPORT filestream : public StreamBuffer
343 {
344 public:
345  typedef enum {
346  RDONLY,
347  WRONLY,
348  RDWR
349  } access_t;
350 
351 private:
352  __LOCAL void allocate(size_t size, fsys::access_t mode);
353 
354 protected:
355  fsys_t fd;
356  fsys::access_t ac;
357 
364  int underflow(void) __OVERRIDE;
365 
373  int overflow(int ch) __OVERRIDE;
374 
375 public:
379  filestream();
380 
384  filestream(const filestream& copy);
385 
389  filestream(const char *path, unsigned mode, fsys::access_t access, size_t bufsize = 512);
390 
394  filestream(const char *path, fsys::access_t access, size_t bufsize = 512);
395 
399  virtual ~filestream();
400 
405  inline operator bool() const
406  {return (bufsize > 0);}
407 
412  inline bool operator!() const
413  {return bufsize == 0;}
414 
418  void open(const char *filename, fsys::access_t access, size_t buffering = 512);
419 
423  void open(const char *filename, unsigned mode, fsys::access_t access, size_t buffering = 512);
424 
428  void close(void);
429 
433  void seek(fsys::offset_t offset);
434 
439  inline int err(void) const
440  {return fd.err();}
441 };
442 
447 class __EXPORT _stream_operators
448 {
449 private:
450  __DELETE_DEFAULTS(_stream_operators);
451 
452 public:
453  static std::ostream& print(std::ostream& out, const PrintProtocol& format);
454 
455  static std::istream& input(std::istream& inp, InputProtocol& format);
456 
457  static std::ostream& print(std::ostream& out, const string_t& str);
458 
459  static std::istream& input(std::istream& inp, string_t& str);
460 
461  static std::ostream& print(std::ostream& out, const stringlist_t& list);
462 
463  static std::istream& input(std::istream& in, stringlist_t& list);
464 
465  static std::string& append(std::string& target, String& source);
466 };
467 
468 inline std::ostream& operator<< (std::ostream& out, const PrintProtocol& format)
469  {return _stream_operators::print(out, format);}
470 
471 inline std::istream& operator>> (std::istream& inp, InputProtocol& format)
472  {return _stream_operators::input(inp, format);}
473 
474 inline std::ostream& operator<< (std::ostream& out, const string_t& str)
475  {return _stream_operators::print(out, str);}
476 
477 inline std::istream& operator>> (std::istream& inp, string_t& str)
478  {return _stream_operators::input(inp, str);}
479 
480 inline std::ostream& operator<< (std::ostream& out, const stringlist_t& list)
481  {return _stream_operators::print(out, list);}
482 
483 inline std::istream& operator>> (std::istream& in, stringlist_t& list)
484  {return _stream_operators::input(in, list);}
485 
486 inline std::string& operator+(std::string& target, String& source)
487  {return _stream_operators::append(target, source);}
488 
489 inline std::string& operator+=(std::string& target, String& source)
490  {return _stream_operators::append(target, source);}
491 
492 inline std::ostream& operator<<(std::ostream& os, Socket::address& addr) {
493 #ifdef AF_INET6
494  char buf[INET6_ADDRSTRLEN];
495 #else
496  char buf[INET_ADDRSTRLEN];
497 #endif
498  addr.print(buf, sizeof(buf), false, true);
499  os << buf;
500  return os;
501 }
502 
503 } // namespace ucommon
504 
505 #endif
506 #endif
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
Definition: access.h:271
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
StringPager stringlist_t
A convenience type for paged string lists.
Definition: memory.h:1474
Thread classes and sychronization objects.
Various miscellaneous platform specific headers and defines.
Common stream buffer for std C++ i/o classes.
Definition: stream.h:63
Streamable tcp connection between client and server.
Definition: stream.h:342
Thread-aware file system manipulation class.
int pid_t
Standard type of process id for shell class.
Definition: shell.h:151
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
A copy-on-write string class that operates by reference count.
Definition: string.h:83
A container for generic and o/s portable threadsafe file system functions.
Definition: fsys.h:129
access_t
Enumerated file access modes.
Definition: fsys.h:164
Used for forming stream output.
Definition: protocols.h:138
bool operator!() const
See if stream is disconnected.
Definition: stream.h:201
struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:2015
long offset_t
File offset type.
Definition: fsys.h:180
Common socket class and address manipulation.
int err(void) const
Get error flag from last i/o operation.
Definition: stream.h:439
bool operator!() const
See if stream is disconnected.
Definition: stream.h:412
Streamable tcp connection between client and server.
Definition: stream.h:113
Common namespace for all ucommon objects.
Definition: access.h:47
String pager for storing lists of NULL terminated strings.
Definition: memory.h:410
Used for processing input.
Definition: protocols.h:157
bool operator!() const
See if stream is disconnected.
Definition: stream.h:306
String string_t
A convenience type for string.
Definition: string.h:1608
int err(void) const
Get last error.
Definition: fsys.h:562
Abstract interfaces and support.
Streamable tcp connection between client and server.
Definition: stream.h:234
At least with gcc, linking of stream operators was broken.
Definition: stream.h:447
A generic tcp server class.
Definition: socket.h:1861
Generic shell parsing and application services.
A generic socket address class.
Definition: socket.h:359