From 7c3930bd1a1d2efd62ee8a33d6854f4626c359df Mon Sep 17 00:00:00 2001 From: Paul Bergmann Date: Sat, 28 Oct 2023 13:42:17 +0200 Subject: [PATCH] wip stringbuffer and outputputStream --- object/outputstream.cc | 76 ++++++++++++++++++++++++++++++++++++++ object/outputstream.h | 6 ++- object/stringbuffer.cc | 13 +++++++ object/stringbuffer.h | 11 ++++-- test-stream/console_out.cc | 7 ++++ 5 files changed, 107 insertions(+), 6 deletions(-) diff --git a/object/outputstream.cc b/object/outputstream.cc index 5a5a450..56c98c6 100644 --- a/object/outputstream.cc +++ b/object/outputstream.cc @@ -1 +1,77 @@ #include "outputstream.h" + +OutputStream& OutputStream::operator<<(char c) { + put(c); + return *this; +} + +OutputStream& OutputStream::operator<<(unsigned char c) { + put(static_cast(c)); + return *this; +} + +OutputStream& OutputStream::operator<< (const char* string) { + + // check for sufficient space + // string length = sizeof(string)/sizeof(char) + // max size of buffer = sizeof(buffer)/sizeof(char) + // next position = pos + + + if (sizeof(string)/sizeof(char) <= (sizeof(buffer)/sizeof(char)) - pos) { + for (int i = 0; i < sizeof(string)/sizeof(char); i++) { + put(string[i]); + } + } +} + +OutputStream& OutputStream::operator<< (bool b) { + if (b) { + OutputStream::operator<<("True"); + } + else { + OutputStream::operator<<("False"); + } + return *this; +} + +OutputStream& OutputStream::operator<< (short ival) { + if (base = 2) { + + } + return *this; +} + +OutputStream& OutputStream::operator<< (const void* ptr) { + return *this; +} + +OutputStream& OutputStream::operator<< (OutputStream& (*f) (OutputStream&)) { + return f(*this); +} + +OutputStream& bin(OutputStream& os) { + os -> base = 2; + return os; +} + +OutputStream& hex(OutputStream& os) { + os -> base = 16; + return os; +} + +OutputStream& oct(OutputStream& os) { + os -> base = 8; + return os; +} + +OutputStream& dec(OutputStream& os) { + os -> base = 10; + return os; +} + +OutputStream& endl(OutputStream& os) { + os << "\n"; + os.flush(); + return os; +} \ No newline at end of file diff --git a/object/outputstream.h b/object/outputstream.h index e9d3e7b..622c05e 100644 --- a/object/outputstream.h +++ b/object/outputstream.h @@ -58,7 +58,7 @@ * whose detailed description is given below. */ -class OutputStream { +class OutputStream : public Stringbuffer{ OutputStream(const OutputStream&) = delete; OutputStream& operator=(const OutputStream&) = delete; @@ -73,7 +73,9 @@ class OutputStream { * \todo Implement Constructor * */ - OutputStream() {} + explicit OutputStream(int base = 10)::Stringbuffer() { + this -> base = base; + } /*! \brief Destructor */ diff --git a/object/stringbuffer.cc b/object/stringbuffer.cc index bb96473..cb9a776 100644 --- a/object/stringbuffer.cc +++ b/object/stringbuffer.cc @@ -1,2 +1,15 @@ #include "stringbuffer.h" + +void Stringbuffer::put(char c) { + + if (pos < sizeof(buffer)/sizeof(char)) { + buffer[pos] = c; + pos++; + } + + if (pos >= sizeof(buffer)/sizeof(char)) { + flush(); + } +} + diff --git a/object/stringbuffer.h b/object/stringbuffer.h index 152cc92..bdf9914 100644 --- a/object/stringbuffer.h +++ b/object/stringbuffer.h @@ -40,22 +40,25 @@ class Stringbuffer { protected: /// buffer containing characters that will be printed upon flush() - char buffer[80]; + char buffer[80] = {0}; /// current position in the buffer long unsigned pos; /*! \brief Constructor; Marks the buffer as empty * - * \todo Complete Constructor + * */ - Stringbuffer() { } + Stringbuffer() { + pos = 0; + // buffer = {0}; + } /*! \brief Inserts a character into the buffer. * * Once the buffer is full, a call to flush() will be issued and * thereby clearing the buffer. * - * \todo Implement Method + * * * \param c Char to be added */ diff --git a/test-stream/console_out.cc b/test-stream/console_out.cc index adbe263..1db16d8 100644 --- a/test-stream/console_out.cc +++ b/test-stream/console_out.cc @@ -1,2 +1,9 @@ #include "console_out.h" +ConsoleOut():OutputStream() {} + +void flush() { + for(char output : buffer) { + putchar(output); + } +}