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 #include <fstream>
55 
56 namespace ucommon {
57 
64 class __EXPORT StreamBuffer : protected std::streambuf, public std::iostream
65 {
66 private:
67  __DELETE_COPY(StreamBuffer);
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 
114 class __EXPORT tcpstream : public StreamBuffer
115 {
116 private:
117  __LOCAL void allocate(unsigned size);
118  __LOCAL void reset(void);
119 
120 protected:
121  socket_t so;
122  timeout_t timeout;
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 
237 class __EXPORT pipestream : public StreamBuffer
238 {
239 public:
240  typedef enum {
241  RDONLY,
242  WRONLY,
243  RDWR
244  } access_t;
245 
246 private:
247  __LOCAL void allocate(size_t size, access_t mode);
248 
249  __DELETE_COPY(pipestream);
250 
251 protected:
252  fsys_t rd, wr;
253  shell::pid_t pid;
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 
347 class __EXPORT filestream : public StreamBuffer
348 {
349 public:
350  typedef enum {
351  RDONLY,
352  WRONLY,
353  RDWR
354  } access_t;
355 
356 private:
357  __LOCAL void allocate(size_t size, fsys::access_t mode);
358 
359 protected:
360  fsys_t fd;
361  fsys::access_t ac;
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:
459  __DELETE_DEFAULTS(imemstream);
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:
495  __DELETE_DEFAULTS(omemstream);
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 
532 class __EXPORT _stream_operators
533 {
534 private:
535  __DELETE_DEFAULTS(_stream_operators);
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) {
562  return _stream_operators::print(out, 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
Thread classes and sychronization objects.
Common socket class and address manipulation.
Various miscellaneous platform specific headers and defines.
Generic shell parsing and application services.
Abstract interfaces and support.
Common namespace for all ucommon objects.
Definition: access.h:47
T copy(const T &src)
Convenience function to copy objects.
Definition: generics.h:395
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:2089
Thread-aware file system manipulation class.
StringPager stringlist_t
A convenience type for paged string lists.
Definition: memory.h:885
class __attribute__((visibility("default"))) dir typedef fsys fsys_t
Convenience class for directories.
Definition: fsys.h:743
Definition: stream.h:598
String string_t
A convenience type for string.
Definition: string.h:1579