UCommon
buffer.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
23 #ifndef _UCOMMON_BUFFER_H_
24 #define _UCOMMON_BUFFER_H_
25 
26 #ifndef _UCOMMON_CONFIG_H_
27 #include <ucommon/platform.h>
28 #endif
29 
30 #ifndef _UCOMMON_PROTOCOLS_H_
31 #include <ucommon/protocols.h>
32 #endif
33 
34 #ifndef _UCOMMON_SOCKET_H_
35 #include <ucommon/socket.h>
36 #endif
37 
38 #ifndef _UCOMMON_STRING_H_
39 #include <ucommon/string.h>
40 #endif
41 
42 #ifndef _UCOMMON_FSYS_H_
43 #include <ucommon/fsys.h>
44 #endif
45 
46 NAMESPACE_UCOMMON
47 
54 class fbuf : public BufferProtocol, private fsys
55 {
56 private:
57  offset_t inpos, outpos;
58 
59 protected:
60  size_t _push(const char *address, size_t size);
61  size_t _pull(char *address, size_t size);
62  int _err(void) const;
63  void _clear(void);
64 
65  inline fd_t getfile(void)
66  {return fd;};
67 
68 public:
72  fbuf();
73 
77  ~fbuf();
78 
86  fbuf(const char *path, fsys::access_t access, unsigned permissions, size_t size);
87 
94  fbuf(const char *path, fsys::access_t access, size_t size);
95 
104  void create(const char *path, fsys::access_t access = fsys::ACCESS_APPEND, unsigned permissions = 0640, size_t size = 512);
105 
112  void open(const char *path, fsys::access_t access = fsys::ACCESS_RDWR, size_t size = 512);
113 
117  void close(void);
118 
126  bool seek(offset_t offset);
127 
134  bool trunc(offset_t offset);
135 
142  offset_t tell(void);
143 };
144 
151 class __EXPORT TCPBuffer : public BufferProtocol, protected Socket
152 {
153 protected:
154  void _buffer(size_t size);
155 
156  virtual size_t _push(const char *address, size_t size);
157  virtual size_t _pull(char *address, size_t size);
158  int _err(void) const;
159  void _clear(void);
160  bool _blocking(void);
161 
166  inline socket_t getsocket(void) const
167  {return so;};
168 
169 public:
173  TCPBuffer();
174 
180  TCPBuffer(const TCPServer *server, size_t size = 536);
181 
188  TCPBuffer(const char *host, const char *service, size_t size = 536);
189 
193  virtual ~TCPBuffer();
194 
201  void open(const TCPServer *server, size_t size = 536);
202 
210  void open(const char *host, const char *service, size_t size = 536);
211 
215  void close(void);
216 
217 protected:
222  virtual bool _pending(void);
223 };
224 
228 typedef fbuf file_t;
229 
233 typedef TCPBuffer tcp_t;
234 
235 END_NAMESPACE
236 
237 #endif