Mercurial > archived > louis > epitech > zia-api
changeset 23:812d6c5ac125
more StreamBufferImpl methods
author | Zoltan K |
---|---|
date | Sun, 21 Mar 2010 14:19:23 +0100 |
parents | 18ca9800e906 |
children | ce4c3431392f |
files | src/examples/Buffer/Buffer.cpp src/examples/Buffer/Buffer.hpp src/examples/rot13/main.cpp |
diffstat | 3 files changed, 60 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/src/examples/Buffer/Buffer.cpp Sun Mar 21 10:14:14 2010 +0100 +++ b/src/examples/Buffer/Buffer.cpp Sun Mar 21 14:19:23 2010 +0100 @@ -2,25 +2,56 @@ #include "Buffer.hpp" -Buffer::Buffer(void) - : _rawBuffer() +StreamBufferImpl::StreamBufferImpl(void) + : _buffer(NULL), + _size(0) { - char *s; - std::streamsize n = 1024; - - this->_rawBuffer.resize(n, 0); - s = &_rawBuffer[0]; - this->setg(s, s, s + n); - this->setp(s, s + n); } -int Buffer::sync(void) +StreamBufferImpl * StreamBufferImpl::setbuf(char *, std::streamsize) { - std::cout << "Buffer is being sync." << std::endl; + if (s != NULL) + { + /* + * We set the get pointer. + * s is the beginning of the get sequence. + * s is also the current position of the get pointer. + * s + n is a pointer to the end of the get sequence. + */ + this->setg(s, s, s + n); + + /* + * We set the put pointer. + * s is the beginning of the put sequence. + * s + n is a pointer to the end of the put sequence. + */ + this->setp(s, s + n); + } + return (*this); +} + +virtual std::streampos seekoff(std::streamoff off, + std::ios_base::seekdir way, + std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) +{ + if (off == 0) + { + } + if (which == std::ios_base::in | std::ios_base::out) + return (this->gptr() - this->eback()); + else if (which == std::ios_base::in) + return (this->pptr() - this->pbase()); + else + return (this->gptr() - this->eback()); +} + + +int StreamBufferImpl::sync(void) +{ return (0); } -int Buffer::underflow(void) +int StreamBufferImpl::underflow(void) { return (EOF); }
--- a/src/examples/Buffer/Buffer.hpp Sun Mar 21 10:14:14 2010 +0100 +++ b/src/examples/Buffer/Buffer.hpp Sun Mar 21 14:19:23 2010 +0100 @@ -48,6 +48,9 @@ ** This method is called by pubsetbuf. ** @param [in] s A pointer to the array of char where operations will be performed ** @param [in] n An integer giving the size of the array + ** + ** In our example, we will store the pointer to the buffer and its size, + ** and then, set the @b put and @b get pointers using setp and setg. ** @return A pointer to the class itself */ virtual StreamBufferImpl * setbuf(char *s, std::streamsize n); @@ -87,8 +90,11 @@ ** the buffer's content with the ``real'' content it represents ** (a device or anything else). ** This method is called when std::endl is applied on a stream + ** + ** In our example, this method will do nothing. ** @return In case of success, zero is returned. ** Errors are expected to be signaled by any other value, like -1. + ** */ virtual int sync(void); @@ -113,6 +119,9 @@ /** ** @brief This method is called when there is a need to read data from the buffer ** but there no more data available in it. This method doesn't move the @b get pointer. + ** + ** In our example, this method will do nothing since we doesn't want to refill + ** the buffer when it's empty. ** @return The new character available at the get pointer position, if any. Otherwise, @c EOF. */ virtual int underflow(void); @@ -120,6 +129,9 @@ /** ** @brief This method is similar to @ref underflow except that it moves the @b get pointer. + ** + ** In our example, this method will do nothing since we doesn't want to refill + ** the buffer when it's empty. ** @return The new character available at the get pointer position, if any. Otherwise, @c EOF. */ virtual int uflow(void); @@ -163,9 +175,10 @@ private: /** - ** @brief This is the buffer our class will fill or consume + ** @brief This is the buffer our class will fill or consume, with its size associated */ - std::vector<char> _rawBuffer; + char * _buffer; + std::streamsize _size; };
--- a/src/examples/rot13/main.cpp Sun Mar 21 10:14:14 2010 +0100 +++ b/src/examples/rot13/main.cpp Sun Mar 21 14:19:23 2010 +0100 @@ -13,8 +13,8 @@ */ void process_stream(std::istream & is, std::ostream & os) { - ModRot13 module; - std::vector<zia::api::handler::IHandler *> handlers = module.getHandlers(); + ModRot13 module; + const std::vector<zia::api::handler::IHandler *> & handlers = module.getHandlers(); std::cout << "Running: " << module.getName() << " " << module.getVersion() << std::endl;