wip stringbuffer and outputputStream

This commit is contained in:
Paul Bergmann 2023-10-28 13:42:17 +02:00
parent 3e8f31ac80
commit 7c3930bd1a
5 changed files with 107 additions and 6 deletions

View File

@ -1 +1,77 @@
#include "outputstream.h"
OutputStream& OutputStream::operator<<(char c) {
put(c);
return *this;
}
OutputStream& OutputStream::operator<<(unsigned char c) {
put(static_cast<char>(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;
}

View File

@ -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
*/

View File

@ -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();
}
}

View File

@ -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
*/

View File

@ -1,2 +1,9 @@
#include "console_out.h"
ConsoleOut():OutputStream() {}
void flush() {
for(char output : buffer) {
putchar(output);
}
}