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
56namespace ucommon {
57
64class __EXPORT StreamBuffer : protected std::streambuf, public std::iostream
65{
66private:
67 __DELETE_COPY(StreamBuffer);
68
69protected:
70 size_t bufsize;
71 char *gbuf, *pbuf;
72
74
83 int uflow() __OVERRIDE;
84
85 void release(void);
86
87 void allocate(size_t size);
88
89public:
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
114class __EXPORT tcpstream : public StreamBuffer
115{
116private:
117 __LOCAL void allocate(unsigned size);
118 __LOCAL void reset(void);
119
120protected:
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
155public:
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
237class __EXPORT pipestream : public StreamBuffer
238{
239public:
240 typedef enum {
241 RDONLY,
242 WRONLY,
243 RDWR
244 } access_t;
245
246private:
247 __LOCAL void allocate(size_t size, access_t mode);
248
249 __DELETE_COPY(pipestream);
250
251protected:
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
277public:
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
347class __EXPORT filestream : public StreamBuffer
348{
349public:
350 typedef enum {
351 RDONLY,
352 WRONLY,
353 RDWR
354 } access_t;
355
356private:
357 __LOCAL void allocate(size_t size, fsys::access_t mode);
358
359protected:
360 fsys_t fd;
362
369 int underflow(void) __OVERRIDE;
370
378 int overflow(int ch) __OVERRIDE;
379
380public:
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
456class __EXPORT imemstream : protected std::streambuf, public std::istream
457{
458private:
459 __DELETE_DEFAULTS(imemstream);
460
461 size_t count;
462 const uint8_t *pos, *bp;
463
464public:
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
492class __EXPORT omemstream : protected std::streambuf, public std::ostream
493{
494private:
495 __DELETE_DEFAULTS(omemstream);
496
497 size_t count;
498 uint8_t *pos, *bp;
499 bool zb;
500
501public:
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
524bool __EXPORT getline(std::istream& in, char *buffer, size_t size);
525
526bool __EXPORT putline(std::ostream& out, const char *buffer);
527
532class __EXPORT _stream_operators
533{
534private:
535 __DELETE_DEFAULTS(_stream_operators);
536
537public:
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
553inline std::ostream& operator<< (std::ostream& out, const PrintProtocol& format) {
554 return _stream_operators::print(out, format);
555}
556
557inline std::istream& operator>> (std::istream& inp, InputProtocol& format) {
558 return _stream_operators::input(inp, format);
559}
560
561inline std::ostream& operator<< (std::ostream& out, const string_t& str) {
562 return _stream_operators::print(out, str);
563}
564
565inline std::istream& operator>> (std::istream& inp, string_t& str) {
566 return _stream_operators::input(inp, str);
567}
568
569inline std::ostream& operator<< (std::ostream& out, const stringlist_t& list) {
570 return _stream_operators::print(out, list);
571}
572
573inline std::istream& operator>> (std::istream& in, stringlist_t& list) {
574 return _stream_operators::input(in, list);
575}
576
577inline std::string& operator+(std::string& target, String& source) {
578 return _stream_operators::append(target, source);
579}
580
581inline std::string& operator+=(std::string& target, String& source) {
582 return _stream_operators::append(target, source);
583}
584
585inline 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
598namespace std {
599 extern __EXPORT iostream& null;
600}
601
602#endif
603#endif
Thread-aware file system manipulation class.
Various miscellaneous platform specific headers and defines.
Abstract interfaces and support.
Generic shell parsing and application services.
Common namespace for all ucommon objects.
Definition access.h:47
String string_t
A convenience type for string.
Definition string.h:1579
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition socket.h:2089
StringPager stringlist_t
A convenience type for paged string lists.
Definition memory.h:885
A container for generic and o/s portable threadsafe file system functions.
Definition fsys.h:126
int err(void) const
Get last error.
Definition fsys.h:557
access_t
Enumerated file access modes.
Definition fsys.h:160
long offset_t
File offset type.
Definition fsys.h:176
String pager for storing lists of NULL terminated strings.
Definition memory.h:402
Used for forming stream output.
Definition protocols.h:135
Used for processing input.
Definition protocols.h:154
int pid_t
Standard type of process id for shell class.
Definition shell.h:147
A generic socket address class.
Definition socket.h:365
A generic tcp server class.
Definition socket.h:1901
Common stream buffer for std C++ i/o classes.
Definition stream.h:65
int uflow()
This streambuf method is used for doing unbuffered reads through the establish tcp socket connection ...
Streamable tcp connection between client and server.
Definition stream.h:115
tcpstream(int family=2, timeout_t timeout=0)
Create an unconnected tcp stream object that is idle until opened.
int underflow(void)
This streambuf method is used to load the input buffer through the established tcp socket connection.
int overflow(int ch)
This streambuf method is used to write the output buffer through the established tcp connection.
void open(Socket::address &address, unsigned segment=536)
Open a stream connection to a tcp service.
virtual ~tcpstream()
Destroy a tcp stream.
void close(void)
Close an active stream connection.
void open(const char *host, const char *service, unsigned segment=536)
Open a stream connectoion to a host and service.
tcpstream(const tcpstream &copy)
Copy constructor...
void release(void)
Release the tcp stream and destroy the underlying socket.
tcpstream(const TCPServer *server, unsigned segsize=536, timeout_t timeout=0)
Create a stream from an existing tcp listener.
bool operator!() const
See if stream is disconnected.
Definition stream.h:204
tcpstream(Socket::address &address, unsigned segsize=536, timeout_t timeout=0)
A convenience constructor that creates a connected tcp stream directly from an address.
Streamable pipe socket connection.
Definition stream.h:238
void release(void)
Release the stream, detach/do not wait for the process.
int underflow(void)
This streambuf method is used to load the input buffer through the established pipe connection.
int overflow(int ch)
This streambuf method is used to write the output buffer through the established pipe connection.
virtual ~pipestream()
Destroy a pipe stream.
bool operator!() const
See if stream is disconnected.
Definition stream.h:310
pipestream(const char *command, access_t access, char **args, char **env=NULL, size_t size=512)
Create child process and start pipe.
pipestream()
Create an unopened pipe stream.
void terminate(void)
Force terminate child and close.
void open(const char *path, access_t access, char **args, char **env=NULL, size_t buffering=512)
Open a stream connection to a pipe service.
int close(void)
Close an active stream connection.
Streamable file class based on low level fsys io.
Definition stream.h:348
filestream(const filestream &copy)
Create duplicate stream.
void open(const char *filename, fsys::access_t access, size_t buffering=512)
Open a stream connection to a tcp service.
int overflow(int ch)
This streambuf method is used to write the output buffer through the established pipe connection.
virtual ~filestream()
Destroy a file stream.
void seek(fsys::offset_t offset)
Seek position.
bool operator!() const
See if stream is disconnected.
Definition stream.h:418
filestream(const char *path, fsys::access_t access, size_t bufsize=512)
Open file stream.
void close(void)
Close an active stream connection.
filestream()
Create an unopened pipe stream.
void open(const char *filename, unsigned mode, fsys::access_t access, size_t buffering=512)
Create a stream connection to a tcp service.
int err(void) const
Get error flag from last i/o operation.
Definition stream.h:448
int underflow(void)
This streambuf method is used to load the input buffer through the established pipe connection.
filestream(const char *path, unsigned mode, fsys::access_t access, size_t bufsize=512)
Create and open a file stream.
Stream class to read from a memory buffer.
Definition stream.h:457
Stream class to write to memory buffer.
Definition stream.h:493
At least with gcc, linking of stream operators was broken.
Definition stream.h:533
A copy-on-write string class that operates by reference count.
Definition string.h:79
Common socket class and address manipulation.
Thread classes and sychronization objects.