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

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 
00025 #ifndef _UCOMMON_BUFFER_H_
00026 #define _UCOMMON_BUFFER_H_
00027 
00028 #ifndef _UCOMMON_CONFIG_H_
00029 #include <ucommon/platform.h>
00030 #endif
00031 
00032 #ifndef _UCOMMON_SOCKET_H_
00033 #include <ucommon/socket.h>
00034 #endif
00035 
00036 #ifndef _UCOMMON_STRING_H_
00037 #include <ucommon/string.h>
00038 #endif
00039 
00040 #ifndef _UCOMMON_FSYS_H_
00041 #include <ucommon/fsys.h>
00042 #endif
00043 
00044 NAMESPACE_UCOMMON
00045 
00054 class __EXPORT IOBuffer
00055 {
00056 public:
00057     typedef enum {BUF_RD, BUF_WR, BUF_RDWR} type_t;
00058 
00059 private:
00060     const char *eol;
00061     char *buffer;
00062     char *input, *output;
00063     size_t bufsize, bufpos, insize, outsize;
00064     bool end;
00065 
00066 protected:
00067     int ioerror;
00068     timeout_t timeout;
00069     const char *format;
00070 
00074     IOBuffer();
00075 
00081     IOBuffer(size_t size, type_t access = BUF_RDWR);
00082 
00086     ~IOBuffer();
00087 
00095     inline void seteol(const char *string)
00096         {eol = string;};
00097 
00104     void allocate(size_t size, type_t access = BUF_RDWR);
00105 
00109     void release(void);
00110 
00118     char *request(size_t size);
00119 
00126     char *gather(size_t size);
00127 
00135     virtual size_t _push(const char *address, size_t size);
00136 
00144     virtual size_t _pull(char *address, size_t size);
00145 
00151     inline size_t _pending(void)
00152         {return bufpos;};
00153 
00158     inline size_t _waiting(void)
00159         {return outsize;};
00160 
00164     inline size_t _buffering(void)
00165         {return bufsize;};
00166 
00167 public:
00175     size_t getstr(char *address, size_t count);
00176 
00185     size_t putstr(const char *address, size_t count = 0);
00186 
00191     int getch(void);
00192 
00197     int putch(int ch);
00198 
00203     inline size_t writes(const char *string)
00204         {return putstr(string);};
00205 
00212     inline size_t write(const char *address, size_t size)
00213         {return putstr(address, size);};
00214 
00221     inline size_t writeln(const char *string)
00222         {return putline(string);};
00223 
00230     inline size_t write(String& string)
00231         {return putline(*string);};
00232 
00240     inline size_t readln(char *address, size_t size)
00241         {return getline(address, size);};
00242 
00249     size_t read(String& string);
00250 
00257     inline size_t read(char *address, size_t size)
00258         {return getstr(address, size);};
00259 
00266     size_t printf(const char *format, ...) __PRINTF(2, 3);
00267 
00272     virtual bool flush(void);
00273 
00277     void purge(void);
00278 
00282     void reset(void);
00283 
00291     size_t getline(char *string, size_t size);
00292 
00299     size_t putline(const char *string);
00300 
00305     bool eof();
00306 
00311     inline int err(void)
00312         {return ioerror;};
00313 
00317     inline void clear(void)
00318         {ioerror = 0;};
00319 
00324     inline operator bool()
00325         {return buffer != NULL;};
00326 
00331     inline bool operator!()
00332         {return buffer == NULL;};
00333 
00338     inline bool isopen(void)
00339         {return buffer != NULL;};
00340 
00345     inline bool isinput(void)
00346         {return input != NULL;};
00347 
00352     inline bool isoutput(void)
00353         {return output != NULL;};
00354 
00358     inline void seteof(void)
00359         {end = true;};
00360 
00365     virtual bool pending(void);
00366 };
00367 
00374 class fbuf : public IOBuffer, private fsys
00375 {
00376 private:
00377     offset_t    inpos, outpos;
00378 
00379 protected:
00380     size_t _push(const char *address, size_t size);
00381     size_t _pull(char *address, size_t size);
00382 
00383     inline fd_t getfile(void)
00384         {return fd;};
00385 
00386 public:
00390     fbuf();
00391 
00395     ~fbuf();
00396 
00404     fbuf(const char *path, fsys::access_t access, unsigned permissions, size_t size);
00405 
00412     fbuf(const char *path, fsys::access_t access, size_t size);
00413 
00422     void create(const char *path, fsys::access_t access = fsys::ACCESS_APPEND, unsigned permissions = 0640, size_t size = 512);
00423 
00430     void open(const char *path, fsys::access_t access = fsys::ACCESS_RDWR, size_t size = 512);
00431 
00435     void close(void);
00436     
00444     bool seek(offset_t offset);
00445 
00452     bool trunc(offset_t offset);
00453 
00460     offset_t tell(void);
00461 };
00462 
00468 class __EXPORT TCPServer : public ListenSocket
00469 {
00470 private:
00471     friend class TCPSocket;
00472 
00473     const char *servicetag;
00474 
00475 public:
00484     TCPServer(const char *service, const char *address = "*", unsigned backlog = 5, int protocol = 0);
00485 };
00486 
00493 class __EXPORT TCPSocket : public IOBuffer, protected Socket
00494 {
00495 private:
00496     void _buffer(size_t size);
00497 
00498     char serviceid[16];
00499     const char *servicetag;
00500 
00501 protected:
00502     virtual size_t _push(const char *address, size_t size);
00503     virtual size_t _pull(char *address, size_t size);
00504 
00509     inline socket_t getsocket(void) const
00510         {return so;};
00511 
00520     inline short getservice(void) const
00521         {return (short)(atol(serviceid));};
00522 
00527     inline const char *tag(void) const
00528         {return servicetag;};
00529 
00538     size_t peek(char *data, size_t size, timeout_t timeout = Timer::inf);
00539 
00540 public:
00545     TCPSocket(const char *service);
00546 
00552     TCPSocket(TCPServer *server, size_t size = 536);
00553 
00560     TCPSocket(const char *service, const char *host, size_t size = 536);
00561 
00565     virtual ~TCPSocket();
00566 
00573     void open(TCPServer *server, size_t size = 536);
00574 
00581     void open(const char *host, size_t size = 536);
00582 
00586     void close(void);
00587 
00592     void blocking(timeout_t timeout = Timer::inf);  
00593 
00598     bool pending(void);
00599 };
00600 
00604 typedef fbuf file_t;
00605 
00609 typedef TCPSocket tcp_t;
00610      
00611 END_NAMESPACE
00612 
00613 #endif

Generated on Sun Sep 4 2011 for UCommon by  doxygen 1.7.1