From cab353cbb21c142ac1e227d42338fa587e1a7f24 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Sun, 20 Dec 2009 15:21:26 +0100 Subject: Worked on IStream. Seems to work --- Source/Library/Common/IStream.proto.cpp | 73 +++++++++++++++++++++++++++++++++ Source/Library/Common/IStream.proto.h | 29 +++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 Source/Library/Common/IStream.proto.cpp create mode 100644 Source/Library/Common/IStream.proto.h (limited to 'Source/Library/Common') diff --git a/Source/Library/Common/IStream.proto.cpp b/Source/Library/Common/IStream.proto.cpp new file mode 100644 index 0000000..0cc2a79 --- /dev/null +++ b/Source/Library/Common/IStream.proto.cpp @@ -0,0 +1,73 @@ +#include "IStream.proto.h" + +IStream::IStream() { + m_buffer = NULL; + m_ptr = 0; +} + +IStream::IStream(const IStream& other) { + m_buffer = NULL; + m_ptr = 0; +} + +IStream::~IStream() { + delete m_buffer; +} + +bool IStream::populate() { + String s = read(); + if (s.empty()) return false; + if (m_buffer == NULL) { + m_buffer = new SimpleList(s); + } else { + m_buffer->addAtEnd(s); + } + return true; +} + +WChar IStream::getChar() { + waitLock(); + if (m_buffer == 0) { + if (!populate()) { + unlock(); + return WChar("\0"); + } + } + WChar ret = m_buffer->v()[m_ptr]; + m_ptr++; + if (m_ptr >= m_buffer->v().size()) { + m_buffer = m_buffer->delThis(); + m_ptr = 0; + } + unlock(); + return ret; +} + +String IStream::get(WChar delimiter) { + waitLock(); + //calculate length of string to read + if (m_buffer == 0) populate(); + int length = 0, ptr = m_ptr; + for (SimpleList *iter = m_buffer; iter != 0;) { + if (iter->v()[ptr] == delimiter) break; + ptr++; + length++; + if (ptr >= iter->v().size()) { + if (iter->next() == 0) populate(); + iter = iter->next(); + ptr = 0; + } + } + //get it + String ret(WChar(" "), length); + for (int i = 0; i <= length; i++) { + if (i != length) ret[i] = m_buffer->v()[m_ptr]; + m_ptr++; + if (m_ptr >= m_buffer->v().size()) { + m_buffer = m_buffer->delThis(); + m_ptr = 0; + } + } + unlock(); + return ret; +} diff --git a/Source/Library/Common/IStream.proto.h b/Source/Library/Common/IStream.proto.h new file mode 100644 index 0000000..f689a22 --- /dev/null +++ b/Source/Library/Common/IStream.proto.h @@ -0,0 +1,29 @@ +#ifndef DEF_ISTREAM_PROTO_h +#define DEF_ISTREAM_PROTO_h + +#include +#include +#include + +class IStream : private Mutex { + private: + SimpleList *m_buffer; + int m_ptr; + void operator =(IStream& other); + + bool populate(); + + protected: + virtual String read() = 0; + + public: + IStream(); + IStream(const IStream& other); + virtual ~IStream(); + + WChar getChar(); + String get(WChar delimiter = "\n"); + String getWord() { return get(" "); } +}; + +#endif -- cgit v1.2.3