ucommon
ucommon/stream.h
Go to the documentation of this file.
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
00002 //
00003 // This file is part of GNU uCommon C++.
00004 //
00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published
00007 // by the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // GNU uCommon C++ is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00017 
00024 #if defined(OLD_STDCPP) || defined(NEW_STDCPP)
00025 #ifndef _UCOMMON_STREAM_H_
00026 #define _UCOMMON_STREAM_H_
00027 
00028 #ifndef _UCOMMON_CONFIG_H
00029 #include <ucommon/platform.h>
00030 #endif
00031 
00032 #ifndef _UCOMMON_PROTOCOLS_H_
00033 #include <ucommon/protocols.h>
00034 #endif
00035 
00036 #ifndef _UCOMMON_THREAD_H_
00037 #include <ucommon/thread.h>
00038 #endif
00039 
00040 #ifndef _UCOMMON_SOCKET_H_
00041 #include <ucommon/socket.h>
00042 #endif
00043 
00044 #ifndef _UCOMMON_FSYS_H_
00045 #include <ucommon/fsys.h>
00046 #endif
00047 
00048 #include <iostream>
00049 
00050 NAMESPACE_UCOMMON
00051 
00058 class __EXPORT StreamProtocol : protected std::streambuf, public std::iostream, public CharacterProtocol
00059 {
00060 protected:
00061     size_t bufsize;
00062     char *gbuf, *pbuf;
00063 
00064     StreamProtocol();
00065 
00066     int underflow();
00067 
00068     int overflow(int code);
00069 
00078     int uflow();
00079 
00080     void release(void);
00081 
00082     void allocate(size_t size);
00083 
00084 public:
00089     int sync(void);
00090 
00091     inline bool is_open(void)
00092         {return bufsize > 0;}
00093 
00094     inline operator bool()
00095         {return bufsize > 0;}
00096 
00097     inline bool operator!()
00098         {return bufsize == 0;}
00099 };
00100 
00109 class __EXPORT tcpstream : public StreamProtocol
00110 {
00111 private:
00112     __LOCAL void allocate(unsigned size);
00113     __LOCAL void reset(void);
00114 
00115 protected:
00116     socket_t so;
00117     timeout_t timeout;
00118 
00119     virtual ssize_t _read(char *buffer, size_t size);
00120 
00121     virtual ssize_t _write(const char *buffer, size_t size);
00122 
00123     virtual bool _wait(void);
00124 
00128     void release(void);
00129 
00136     int _getch(void);
00137 
00144     int _putch(int ch);
00145 
00146     inline socket_t getsocket(void) const
00147         {return so;}
00148 
00149 public:
00154     tcpstream(const tcpstream& copy);
00155 
00162     tcpstream(const TCPServer *server, unsigned segsize = 536, timeout_t timeout = 0);
00163 
00169     tcpstream(int family = PF_INET, timeout_t timeout = 0);
00170 
00179     tcpstream(Socket::address& address, unsigned segsize = 536, timeout_t timeout = 0);
00180 
00184     virtual ~tcpstream();
00185 
00190     inline operator bool() const
00191         {return so != INVALID_SOCKET && bufsize > 0;};
00192 
00197     inline bool operator!() const
00198         {return so == INVALID_SOCKET || bufsize == 0;};
00199 
00205     void open(Socket::address& address, unsigned segment = 536);
00206 
00213     void open(const char *host, const char *service, unsigned segment = 536);
00214 
00219     void close(void);
00220 };
00221 
00230 class __EXPORT pipestream : public StreamProtocol
00231 {
00232 public:
00233     typedef enum {
00234         RDONLY,
00235         WRONLY,
00236         RDWR
00237     } access_t;
00238 
00239 private:
00240     __LOCAL void allocate(size_t size, access_t mode);
00241 
00242 protected:
00243     fsys_t rd, wr;
00244     pid_t pid;
00245 
00249     void release(void);
00250 
00257     int _getch(void);
00258 
00266     int _putch(int ch);
00267 
00268 public:
00272     pipestream();
00273 
00281     pipestream(const char *command, access_t access, const char **env = NULL, size_t size = 512);
00282 
00286     virtual ~pipestream();
00287 
00292     inline operator bool() const
00293         {return (bufsize > 0);};
00294 
00299     inline bool operator!() const
00300         {return bufsize == 0;};
00301 
00309     void open(const char *command, access_t access, const char **env = NULL, size_t buffering = 512);
00310 
00315     void close(void);
00316 
00320     void terminate(void);
00321 };
00322 
00331 class __EXPORT filestream : public StreamProtocol
00332 {
00333 public:
00334     typedef enum {
00335         RDONLY,
00336         WRONLY,
00337         RDWR
00338     } access_t;
00339 
00340 private:
00341     __LOCAL void allocate(size_t size, fsys::access_t mode);
00342 
00343 protected:
00344     fsys_t fd;
00345     fsys::access_t ac;
00346 
00353     int _getch(void);
00354 
00362     int _putch(int ch);
00363 
00364 public:
00368     filestream();
00369 
00373     filestream(const filestream& copy);
00374 
00378     filestream(const char *path, fsys::access_t access, unsigned mode, size_t bufsize);
00379 
00383     filestream(const char *path, fsys::access_t access, size_t bufsize);
00384 
00388     virtual ~filestream();
00389 
00394     inline operator bool() const
00395         {return (bufsize > 0);};
00396 
00401     inline bool operator!() const
00402         {return bufsize == 0;};
00403 
00407     void open(const char *filename, fsys::access_t access, size_t buffering = 512);
00408 
00412     void create(const char *filename, fsys::access_t access, unsigned mode, size_t buffering = 512);
00413 
00417     void close(void);
00418 
00422     void seek(fsys::offset_t offset);
00423 
00428     inline int err(void) const
00429         {return fd.err();};
00430 };
00431 
00432 END_NAMESPACE
00433 
00434 #endif
00435 #endif