ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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 #include <fstream>
55 
56 namespace ucommon {
57 
64 class __EXPORT StreamBuffer : protected std::streambuf, public std::iostream
65 {
66 private:
68 
69 protected:
70  size_t bufsize;
71  char *gbuf, *pbuf;
72 
73  StreamBuffer();
74 
83  int uflow() __OVERRIDE;
84 
85  void release(void);
86 
87  void allocate(size_t size);
88 
89 public:
94  int sync(void);
95 
96  inline bool is_open(void) const
97  {return bufsize > 0;}
98 
99  inline operator bool() const
100  {return bufsize > 0;}
101 
102  inline bool operator!() const
103  {return bufsize == 0;}
104 };
105 
115 {
116 private:
117  __LOCAL void allocate(unsigned size);
118  __LOCAL void reset(void);
119 
120 protected:
123 
124  virtual ssize_t _read(char *buffer, size_t size);
125 
126  virtual ssize_t _write(const char *buffer, size_t size);
127 
128  virtual bool _wait(void);
129 
133  void release(void);
134 
141  int underflow(void) __OVERRIDE;
142 
149  int overflow(int ch) __OVERRIDE;
150 
151  inline socket_t getsocket(void) const {
152  return so;
153  }
154 
155 public:
160  tcpstream(const tcpstream& copy);
161 
168  tcpstream(const TCPServer *server, unsigned segsize = 536, timeout_t timeout = 0);
169 
175  tcpstream(int family = PF_INET, timeout_t timeout = 0);
176 
185  tcpstream(Socket::address& address, unsigned segsize = 536, timeout_t timeout = 0);
186 
190  virtual ~tcpstream();
191 
196  inline operator bool() const {
197  return so != INVALID_SOCKET && bufsize > 0;
198  }
199 
204  inline bool operator!() const {
205  return so == INVALID_SOCKET || bufsize == 0;
206  }
207 
213  void open(Socket::address& address, unsigned segment = 536);
214 
221  void open(const char *host, const char *service, unsigned segment = 536);
222 
227  void close(void);
228 };
229 
238 {
239 public:
240  typedef enum {
243  RDWR
244  } access_t;
245 
246 private:
247  __LOCAL void allocate(size_t size, access_t mode);
248 
250 
251 protected:
252  fsys_t rd, wr;
254 
258  void release(void);
259 
266  int underflow(void) __OVERRIDE;
267 
275  int overflow(int ch) __OVERRIDE;
276 
277 public:
281  pipestream();
282 
291  pipestream(const char *command, access_t access, char **args, char **env = NULL, size_t size = 512);
292 
296  virtual ~pipestream();
297 
302  inline operator bool() const {
303  return (bufsize > 0);
304  }
305 
310  inline bool operator!() const {
311  return bufsize == 0;
312  }
313 
322  void open(const char *path, access_t access, char **args, char **env = NULL, size_t buffering = 512);
323 
328  int close(void);
329 
333  void terminate(void);
334 
335  inline void cancel(void) {
336  terminate();
337  }
338 };
339 
348 {
349 public:
350  typedef enum {
353  RDWR
354  } access_t;
355 
356 private:
357  __LOCAL void allocate(size_t size, fsys::access_t mode);
358 
359 protected:
362 
369  int underflow(void) __OVERRIDE;
370 
378  int overflow(int ch) __OVERRIDE;
379 
380 public:
384  filestream();
385 
389  filestream(const filestream& copy);
390 
394  filestream(const char *path, unsigned mode, fsys::access_t access, size_t bufsize = 512);
395 
399  filestream(const char *path, fsys::access_t access, size_t bufsize = 512);
400 
404  virtual ~filestream();
405 
410  inline operator bool() const {
411  return (bufsize > 0);
412  }
413 
418  inline bool operator!() const {
419  return bufsize == 0;
420  }
421 
425  void open(const char *filename, fsys::access_t access, size_t buffering = 512);
426 
430  void open(const char *filename, unsigned mode, fsys::access_t access, size_t buffering = 512);
431 
435  void close(void);
436 
440  void seek(fsys::offset_t offset);
441 
442  void rewind(void);
443 
448  inline int err(void) const
449  {return fd.err();}
450 };
451 
456 class __EXPORT imemstream : protected std::streambuf, public std::istream
457 {
458 private:
460 
461  size_t count;
462  const uint8_t *pos, *bp;
463 
464 public:
465  imemstream(const uint8_t *data, size_t size);
466  imemstream(const char *data);
467 
468  int underflow() __OVERRIDE;
469 
470  int uflow() __OVERRIDE;
471 
472  inline size_t remains() const {
473  return count;
474  }
475 
476  inline const uint8_t *mem() const {
477  return bp;
478  }
479 
480  inline const char *chr() const {
481  return (const char *)bp;
482  }
483 
484  inline size_t len() const {
485  return (size_t)(pos - bp) + count;
486  }
487 };
488 
492 class __EXPORT omemstream : protected std::streambuf, public std::ostream
493 {
494 private:
496 
497  size_t count;
498  uint8_t *pos, *bp;
499  bool zb;
500 
501 public:
502  explicit omemstream(uint8_t *data, size_t size);
503  omemstream(char *data, size_t size);
504 
505  int overflow(int ch) __OVERRIDE;
506 
507  inline size_t remains() const {
508  return count;
509  }
510 
511  inline uint8_t *mem() const {
512  return bp;
513  }
514 
515  inline char *chr() const {
516  return (char *)bp;
517  }
518 
519  inline size_t len() const {
520  return (size_t)(pos - bp);
521  }
522 };
523 
524 bool __EXPORT getline(std::istream& in, char *buffer, size_t size);
525 
526 bool __EXPORT putline(std::ostream& out, const char *buffer);
527 
533 {
534 private:
536 
537 public:
538  static std::ostream& print(std::ostream& out, const PrintProtocol& format);
539 
540  static std::istream& input(std::istream& inp, InputProtocol& format);
541 
542  static std::ostream& print(std::ostream& out, const string_t& str);
543 
544  static std::istream& input(std::istream& inp, string_t& str);
545 
546  static std::ostream& print(std::ostream& out, const stringlist_t& list);
547 
548  static std::istream& input(std::istream& in, stringlist_t& list);
549 
550  static std::string& append(std::string& target, String& source);
551 };
552 
553 inline std::ostream& operator<< (std::ostream& out, const PrintProtocol& format) {
554  return _stream_operators::print(out, format);
555 }
556 
557 inline std::istream& operator>> (std::istream& inp, InputProtocol& format) {
558  return _stream_operators::input(inp, format);
559 }
560 
561 inline std::ostream& operator<< (std::ostream& out, const string_t& str) {
563 }
564 
565 inline std::istream& operator>> (std::istream& inp, string_t& str) {
566  return _stream_operators::input(inp, str);
567 }
568 
569 inline std::ostream& operator<< (std::ostream& out, const stringlist_t& list) {
570  return _stream_operators::print(out, list);
571 }
572 
573 inline std::istream& operator>> (std::istream& in, stringlist_t& list) {
574  return _stream_operators::input(in, list);
575 }
576 
577 inline std::string& operator+(std::string& target, String& source) {
578  return _stream_operators::append(target, source);
579 }
580 
581 inline std::string& operator+=(std::string& target, String& source) {
582  return _stream_operators::append(target, source);
583 }
584 
585 inline std::ostream& operator<<(std::ostream& os, Socket::address& addr) {
586 #ifdef AF_INET6
587  char buf[INET6_ADDRSTRLEN];
588 #else
589  char buf[INET_ADDRSTRLEN];
590 #endif
591  addr.print(buf, sizeof(buf), false, true);
592  os << buf;
593  return os;
594 }
595 
596 } // namespace ucommon
597 
598 namespace std {
599  extern __EXPORT iostream& null;
600 }
601 
602 #endif
603 #endif
ucommon::operator<<
std::ostream & operator<<(std::ostream &out, const PrintProtocol &format)
Definition: stream.h:553
ucommon::imemstream::len
size_t len() const
Definition: stream.h:484
ucommon::imemstream::pos
const uint8_t * pos
Definition: stream.h:462
ucommon::filestream::ac
fsys::access_t ac
Definition: stream.h:361
ucommon::StreamBuffer::pbuf
char * pbuf
Definition: stream.h:71
ucommon::PrintProtocol
Definition: protocols.h:134
ucommon::operator+
std::string & operator+(std::string &target, String &source)
Definition: stream.h:577
ucommon::fsys::access_t
access_t
Definition: fsys.h:160
ucommon::InputProtocol
Definition: protocols.h:153
out
static shell::stringopt out('o', "--output", _TEXT("output file"), "filename", "-")
ucommon::operator>>
std::istream & operator>>(std::istream &inp, InputProtocol &format)
Definition: stream.h:557
ucommon
Definition: access.cpp:23
ucommon::pipestream::WRONLY
Definition: stream.h:242
ucommon::omemstream::zb
bool zb
Definition: stream.h:499
ucommon::omemstream::count
size_t count
Definition: stream.h:497
__LOCAL
#define __LOCAL
Definition: platform.h:298
protocols.h
__OVERRIDE
#define __OVERRIDE
Definition: platform.h:158
ucommon::fsys::err
int err(void) const
Definition: fsys.h:557
ucommon::String
Definition: string.h:78
ucommon::filestream::WRONLY
Definition: stream.h:352
timeout_t
unsigned long timeout_t
Definition: platform.h:453
ucommon::imemstream::mem
const uint8_t * mem() const
Definition: stream.h:476
ucommon::_stream_operators::print
static std::ostream & print(std::ostream &out, const PrintProtocol &format)
Definition: stream.cpp:922
ucommon::omemstream::remains
size_t remains() const
Definition: stream.h:507
ucommon::filestream
Definition: stream.h:347
ucommon::StringPager
Definition: memory.h:401
ucommon::pipestream
Definition: stream.h:237
ucommon::copy
T copy(const T &src)
Definition: generics.h:395
ucommon::filestream::RDONLY
Definition: stream.h:351
ucommon::fsys::offset_t
long offset_t
Definition: fsys.h:176
socket_t
int socket_t
Definition: platform.h:414
ucommon::getline
bool getline(std::istream &in, char *buffer, size_t size)
Definition: stream.cpp:882
ucommon::omemstream
Definition: stream.h:492
ucommon::pipestream::cancel
void cancel(void)
Definition: stream.h:335
ucommon::omemstream::len
size_t len() const
Definition: stream.h:519
ucommon::pipestream::RDONLY
Definition: stream.h:241
ucommon::omemstream::pos
uint8_t * pos
Definition: stream.h:498
ucommon::imemstream::chr
const char * chr() const
Definition: stream.h:480
ucommon::StreamBuffer
Definition: stream.h:64
ucommon::filestream::fd
fsys_t fd
Definition: stream.h:360
__EXPORT
#define __EXPORT
Definition: config.h:49
thread.h
input
static shell::stringopt input('i', "--input", _TEXT("stdin path to use"), "filename")
ucommon::TCPServer
Definition: socket.h:1900
ucommon::tcpstream::getsocket
socket_t getsocket(void) const
Definition: stream.h:151
ucommon::StreamBuffer::operator!
bool operator!() const
Definition: stream.h:102
ucommon::StreamBuffer::bufsize
size_t bufsize
Definition: stream.h:70
ucommon::pipestream::wr
fsys_t wr
Definition: stream.h:252
ch
#define ch(x, y, z)
Definition: sha2.cpp:120
INVALID_SOCKET
#define INVALID_SOCKET
Definition: platform.h:416
ucommon::pipestream::pid
shell::pid_t pid
Definition: stream.h:253
__DELETE_COPY
#define __DELETE_COPY(x)
Definition: platform.h:160
platform.h
fsys.h
ucommon::omemstream::mem
uint8_t * mem() const
Definition: stream.h:511
ucommon::str
String str(Socket &so, size_t size)
Definition: socket.cpp:3507
ucommon::filestream::operator!
bool operator!() const
Definition: stream.h:418
ucommon::filestream::err
int err(void) const
Definition: stream.h:448
buffer
static uint8_t buffer[65536]
Definition: zerofill.cpp:27
__DELETE_DEFAULTS
#define __DELETE_DEFAULTS(x)
Definition: platform.h:162
ucommon::_stream_operators::append
static std::string & append(std::string &target, String &source)
Definition: stream.cpp:975
ucommon::Socket::address
Definition: socket.h:364
ucommon::tcpstream::so
socket_t so
Definition: stream.h:121
ucommon::shell::pid_t
int pid_t
Definition: shell.h:147
timeout
static shell::numericopt timeout('t', "--timeout", _TEXT("optional keyboard input timeout"), "seconds", 0)
ucommon::fsys
Definition: fsys.h:125
ucommon::imemstream
Definition: stream.h:456
ucommon::putline
bool putline(std::ostream &out, char *buffer)
Definition: stream.cpp:895
socket.h
ucommon::imemstream::count
size_t count
Definition: stream.h:461
ucommon::operator+=
std::string & operator+=(std::string &target, String &source)
Definition: stream.h:581
ucommon::_stream_operators
Definition: stream.h:532
ucommon::pipestream::access_t
access_t
Definition: stream.h:240
shell.h
ucommon::_stream_operators::input
static std::istream & input(std::istream &inp, InputProtocol &format)
Definition: stream.cpp:904
ucommon::addr
const struct sockaddr * addr(Socket::address &address)
Definition: socket.h:2089
ucommon::omemstream::chr
char * chr() const
Definition: stream.h:515
ucommon::tcpstream
Definition: stream.h:114
ucommon::tcpstream::timeout
timeout_t timeout
Definition: stream.h:122
ucommon::pipestream::operator!
bool operator!() const
Definition: stream.h:310
ucommon::tcpstream::operator!
bool operator!() const
Definition: stream.h:204