• Main Page
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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_THREAD_H_
00029 #include <ucommon/thread.h>
00030 #endif
00031 
00032 #ifndef _UCOMMON_SOCKET_H_
00033 #include <ucommon/socket.h>
00034 #endif
00035 
00036 #ifndef _UCOMMON_FSYS_H_
00037 #include <ucommon/fsys.h>
00038 #endif
00039 
00040 #include <iostream>
00041 
00042 NAMESPACE_UCOMMON
00043 
00052 class __EXPORT tcpstream : protected std::streambuf, public std::iostream
00053 {
00054 private:
00055     __LOCAL void allocate(unsigned size);
00056     __LOCAL void reset(void);
00057 
00058 protected:
00059     socket_t so;
00060     timeout_t timeout;
00061     size_t bufsize;
00062     char *gbuf, *pbuf;
00063 
00067     void release(void);
00068 
00075     int underflow();
00076 
00085     int uflow();
00086 
00094     int overflow(int ch);
00095 
00096 public:
00101     tcpstream(const tcpstream& copy);
00102 
00109     tcpstream(ListenSocket& listener, unsigned segsize = 536, timeout_t timeout = 0);
00110 
00116     tcpstream(int family = PF_INET, timeout_t timeout = 0);
00117 
00126     tcpstream(Socket::address& address, unsigned segsize = 536, timeout_t timeout = 0);
00127 
00131     virtual ~tcpstream();
00132 
00137     inline operator bool() const
00138         {return so != INVALID_SOCKET && bufsize > 0;};
00139 
00144     inline bool operator!() const
00145         {return so == INVALID_SOCKET || bufsize == 0;};
00146 
00152     void open(Socket::address& address, unsigned segment = 536);
00153 
00158     void close(void);
00159 
00164     int sync(void); 
00165 };
00166 
00175 class __EXPORT pipestream : protected std::streambuf, public std::iostream
00176 {
00177 public:
00178     typedef enum {
00179         RDONLY,
00180         WRONLY,
00181         RDWR
00182     } access_t;
00183 
00184 private:
00185     __LOCAL void allocate(size_t size, access_t mode);
00186 
00187 protected:
00188     fsys_t rd, wr;
00189     pid_t pid;
00190     size_t bufsize;
00191     char *gbuf, *pbuf;
00192 
00196     void release(void);
00197 
00204     int underflow();
00205 
00214     int uflow();
00215 
00223     int overflow(int ch);
00224 
00225 public:
00229     pipestream();
00230 
00238     pipestream(const char *command, access_t access, const char **env = NULL, size_t size = 512);
00239 
00243     virtual ~pipestream();
00244 
00249     inline operator bool() const
00250         {return (bufsize > 0);};
00251 
00256     inline bool operator!() const
00257         {return bufsize == 0;};
00258 
00266     void open(const char *command, access_t access, const char **env = NULL, size_t buffering = 512);
00267 
00272     void close(void);
00273 
00277     void terminate(void);
00278 
00283     int sync(void); 
00284 };
00285 
00294 class __EXPORT filestream : protected std::streambuf, public std::iostream
00295 {
00296 public:
00297     typedef enum {
00298         RDONLY,
00299         WRONLY,
00300         RDWR
00301     } access_t;
00302 
00303 private:
00304     __LOCAL void allocate(size_t size, fsys::access_t mode);
00305 
00306 protected:
00307     fsys_t fd;
00308     fsys::access_t ac;
00309     size_t bufsize;
00310     char *gbuf, *pbuf;
00311 
00318     int underflow();
00319 
00328     int uflow();
00329 
00337     int overflow(int ch);
00338 
00339 public:
00343     filestream();
00344 
00348     filestream(const filestream& copy);
00349 
00353     filestream(const char *path, fsys::access_t access, unsigned mode, size_t bufsize);
00354 
00358     filestream(const char *path, fsys::access_t access, size_t bufsize);
00359 
00363     virtual ~filestream();
00364 
00369     inline operator bool() const
00370         {return (bufsize > 0);};
00371 
00376     inline bool operator!() const
00377         {return bufsize == 0;};
00378 
00382     void open(const char *filename, fsys::access_t access, size_t buffering = 512);
00383 
00387     void create(const char *filename, fsys::access_t access, unsigned mode, size_t buffering = 512);
00388 
00392     void close(void);
00393 
00397     void seek(fsys::offset_t offset);
00398 
00403     int sync(void); 
00404 
00409     inline int error(void) const
00410         {return fd.getError();};
00411 };
00412 
00413 END_NAMESPACE
00414 
00415 #endif
00416 #endif

Generated on Sun Sep 4 2011 for UCommon by  doxygen 1.7.1