UCommon

ucommon/buffer.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 
00023 #ifndef _UCOMMON_BUFFER_H_
00024 #define _UCOMMON_BUFFER_H_
00025 
00026 #ifndef _UCOMMON_CONFIG_H_
00027 #include <ucommon/platform.h>
00028 #endif
00029 
00030 #ifndef _UCOMMON_PROTOCOLS_H_
00031 #include <ucommon/protocols.h>
00032 #endif
00033 
00034 #ifndef _UCOMMON_SOCKET_H_
00035 #include <ucommon/socket.h>
00036 #endif
00037 
00038 #ifndef _UCOMMON_STRING_H_
00039 #include <ucommon/string.h>
00040 #endif
00041 
00042 #ifndef _UCOMMON_FSYS_H_
00043 #include <ucommon/fsys.h>
00044 #endif
00045 
00046 NAMESPACE_UCOMMON
00047 
00054 class fbuf : public BufferProtocol, private fsys
00055 {
00056 private:
00057     offset_t    inpos, outpos;
00058 
00059 protected:
00060     size_t _push(const char *address, size_t size);
00061     size_t _pull(char *address, size_t size);
00062     int _err(void) const;
00063     void _clear(void);
00064 
00065     inline fd_t getfile(void)
00066         {return fd;};
00067 
00068 public:
00072     fbuf();
00073 
00077     ~fbuf();
00078 
00086     fbuf(const char *path, fsys::access_t access, unsigned permissions, size_t size);
00087 
00094     fbuf(const char *path, fsys::access_t access, size_t size);
00095 
00104     void create(const char *path, fsys::access_t access = fsys::ACCESS_APPEND, unsigned permissions = 0640, size_t size = 512);
00105 
00112     void open(const char *path, fsys::access_t access = fsys::ACCESS_RDWR, size_t size = 512);
00113 
00117     void close(void);
00118     
00126     bool seek(offset_t offset);
00127 
00134     bool trunc(offset_t offset);
00135 
00142     offset_t tell(void);
00143 };
00144 
00151 class __EXPORT TCPBuffer : public BufferProtocol, protected Socket
00152 {
00153 protected:
00154     void _buffer(size_t size);
00155 
00156     virtual size_t _push(const char *address, size_t size);
00157     virtual size_t _pull(char *address, size_t size);
00158     int _err(void) const;
00159     void _clear(void);
00160     bool _blocking(void);
00161 
00166     inline socket_t getsocket(void) const
00167         {return so;};
00168 
00169 public:
00173     TCPBuffer();
00174 
00180     TCPBuffer(const TCPServer *server, size_t size = 536);
00181 
00188     TCPBuffer(const char *host, const char *service, size_t size = 536);
00189 
00193     virtual ~TCPBuffer();
00194 
00201     void open(const TCPServer *server, size_t size = 536);
00202 
00210     void open(const char *host, const char *service, size_t size = 536);
00211 
00215     void close(void);
00216 
00217 protected:
00222     virtual bool _pending(void);
00223 };
00224 
00228 typedef fbuf file_t;
00229 
00233 typedef TCPBuffer tcp_t;
00234      
00235 END_NAMESPACE
00236 
00237 #endif