📄 buffer.h
字号:
/*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************//** * Copyright (c) 2000 * * This file is a part of the C++ Threads library. * * An implementation of buffered I/O for the C++ threads library. * * @author Orn E. Hansen <oe.hansen@gamma.telenordia.se> */#ifndef THREADS_BUFFERS#define THREADS_BUFFERS#include <thread.h>#include <threads/socket.h>#include <threads/linked_list.h>#include <string>#if( GCC_VERSION>=2096 )#include <sstream>#else#include <strstream>#endifnamespace cpp_threads { class HeadNode : public Mutex, public Node { public: HeadNode(); ~HeadNode(); void add(Node *); }; class BufElement : public Node { public: size_t _size; size_t _used; size_t _pos; char* _buf; BufElement(); BufElement(char*,int); ~BufElement(); void pack_in(); void make_room(size_t); const char *pos(); int len(); }; /** * Dynamic IO Buffer * * The streamed IO classes, provide the ability for a partially * dynamically allocated space. When data is written onto a stream * space is dynamically allocated for it, but when you have read the * stream data it is not freed dynamically. * * This class provides dynamic buffers, that are allocated and freed * as needed. This is done, by providing a linked list of memory * space allocated for each write into the buffer. Each subsequent * read, will get one slice, or multiple slices depending on the * amount of data wanted. * * By maintaining the data as a linked list of slices, there is no * need to relocate large blocks of data, every time the buffer is * filled or data freed from it. Each block is minimal, and the * only relocations done, are when data needed is actually smaller * than the data held. And only in cases, where small amount of * data is in the buffer. * * @short Dynamic IO Buffers * @author Orn E. Hansen <oe.hansen@gamma.telenordia.se> */ class IOBuffer { private: HeadNode _head; Mutex _stream_safe; void create_new_element(char *,int); bool lock_if_not_empty(); char getch(); char peekch(); public: /** * Allocate buffer space, that will always hold a minimum of * free space available. This comes in handy when you are using * this buffer with read(..) where you need to have some space to * read the data. */ IOBuffer(); ~IOBuffer(); /** * Tell if the buffer has any items, this will clear any empty * pages, and then return true if a non-empty page exists. * * @return False if buffer contains data. */ bool empty(); /** * Read data from the buffer, and write it directly onto a * specific socket. * * @param s Socket to write to. */ int get(Socket&); /** * Write data into the buffer, by reading it from the given * socket. * * @param s socket to read from. */ int put(Socket&); /** * Write data into the buffer, by putting it at the end of * currently held data, and adjusting the buffer size accordingly. * * @param s String containing data to put into the buffer. */ void put(const std::string&); /** * Write a single character into the buffer. This will look at * the buffer, and see if a page has space for a character. If * it does, it will be added to that page, otherwise a new page * will be created, one which can contain characters of this * kind. * * If the character being put into the page, is a newline or * a string terminator. Then that page is closed for further * character adds. * * @param c Character to add to page. */ void put(char); /** * Read a single byte of data from the buffer, adjusting the * pointer to the next read position one position, but never * beyond the end pointer. * * @return Character read. */ char getchar(); /** * Read a whole segment from the buffer and put it into the stream * passed to the method. * * @return Number of characters actuall gotten. */#if( GCC_VERSION >=2096 ) int get(std::stringstream&);#else int get(std::strstream&);#endif /** * Read one string from the buffer, ending at the next space * or at the end of the buffer, whichever comes first. * * @return String read. */ std::string gets(); /** * Read one line of data from the buffer, until the delimiter * is reached. * * @return A complete line of data. */ std::string getline(char delim='\n'); /** * The amount of data, that is allocated at the end of the buffer * and can be used. * * @return free data at end of buffer. */ uint free(); /** * The amount of data contained in the buffer, and which has * not been read. * * @return Available data */ uint count(); /** * Operator >> * * This operator will dump the available data in the buffer, onto a * socket, and adjust the read pointer beyond the data written. */ IOBuffer& operator >>(cpp_threads::Socket&); /** * Operator << * * This operator will fill the free space in the buffer, with data * from the socket, and adjust the available data pointer to allocate * the data read. */ IOBuffer& operator <<(cpp_threads::Socket&); /** * Operator >> on string * * This operator will operate in the same manner, as the one above, * except it will will get the next string word from the buffer. */ IOBuffer& operator >>(std::string&); /** * Operator >> on stringstream. * * This operator is similar to the one above, except the parameter * in this case is a string stream. The next available buffer * string/message is put into the string stream. */#if( GCC_VERSION >= 2096 ) IOBuffer& operator >>(std::stringstream&);#else IOBuffer& operator >>(std::strstream&);#endif /** * Operator << on string * * As above, except the contents of the string will be put onto * the buffer, and space made for it as needed. */ IOBuffer& operator <<(std::string&); /** * Operator << on stringstream. * * As above, except the next element in the string stream given * is put into the buffer. */#if( GCC_VERSION >= 2096 ) IOBuffer& operator <<(std::stringstream&);#else IOBuffer& operator <<(std::strstream&);#endif /** * Operator >> on integer * * Read an integer value from the buffer. */ IOBuffer& operator >>(int&); /** * Operator << on integer * * Write an integer value, into the buffer. */ IOBuffer& operator <<(int&); /** * Operator >> on double * * Read a real value from the buffer. */ IOBuffer& operator >>(double&); /** * Operator << on double * * Write a real value onto the buffer. */ IOBuffer& operator <<(double&); };}; // namespace#endif // THREADS_BUFFERS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -