1

clean outstream

This commit is contained in:
2022-07-23 22:46:03 +02:00
parent 226a247043
commit ddad13defa
2 changed files with 49 additions and 72 deletions

View File

@ -22,47 +22,48 @@
#include "lib/OutStream.h" #include "lib/OutStream.h"
// NOTE: I added this, fixed width printing
void OutStream::put(char c) { void OutStream::put(char c) {
if (this->fill_width == 0) { if (fill_width == 0) {
StringBuffer::put(c); StringBuffer::put(c);
} else if (this->fill_used == this->fill_width - 1) { } else if (fill_used == fill_width - 1) {
// This indicates that content has been cut // This indicates that content has been cut
StringBuffer::put('@'); StringBuffer::put('@');
this->fill_use_char(); fill_use_char();
} else if (this->fill_used == this->fill_width) { } else if (fill_used == fill_width) {
// do nothing // do nothing
} else { } else {
StringBuffer::put(c); StringBuffer::put(c);
this->fill_use_char(); fill_use_char();
} }
} }
void OutStream::fill_finalize() { void OutStream::fill_finalize() {
if (this->fill_width == 0 || this->fill_used == this->fill_width) { if (fill_width == 0 || fill_used == fill_width) {
// do nothing // do nothing
} else if (this->fill_used > this->fill_width) { } else if (fill_used > fill_width) {
// should never happen // should never happen
} else { } else {
for (unsigned char i = 0; i < this->fill_width - this->fill_used; ++i) { for (unsigned char i = 0; i < fill_width - fill_used; ++i) {
StringBuffer::put(this->fill_char); StringBuffer::put(fill_char);
} }
} }
this->fill_used = 0; fill_used = 0;
} }
void OutStream::fill_use_char() { void OutStream::fill_use_char() {
if (this->fill_width == 0) { if (fill_width == 0) {
return; return;
} }
this->fill_used++; fill_used++;
} }
void OutStream::flush() { void OutStream::flush() {
// TODO: Should not be reset like this
// Flushing resets fixed width // Flushing resets fixed width
this->base = 10; base = 10;
this->fill_char = ' '; fill_char = ' ';
this->fill_width = 0; fill_width = 0;
this->fill_used = 0; fill_used = 0;
} }
// //

View File

@ -23,14 +23,14 @@
#define __OutStream_include__ #define __OutStream_include__
#include "lib/StringBuffer.h" #include "lib/StringBuffer.h"
// #include <concepts> // NOTE: Only available in c++20
// NOTE: I added this // Some basic width formatting
class fillw { class fillw {
public: public:
constexpr fillw(const unsigned char w) : w(w) {} constexpr fillw(const unsigned int w) : w(w) {}
const unsigned char w; const unsigned int w;
}; };
class fillc { class fillc {
public: public:
constexpr fillc(const char c) : c(c) {} constexpr fillc(const char c) : c(c) {}
@ -42,18 +42,16 @@ private:
OutStream(const OutStream& copy) = delete; // Verhindere Kopieren OutStream(const OutStream& copy) = delete; // Verhindere Kopieren
// Some stream formatting // Some stream formatting
unsigned char fill_used; // indicates how many characters are already used by the text internally unsigned char fill_used; // indicates how many characters are already used by the text internally
void fill_use_char(); // recognizes that one char from the print width has been used up unsigned char fill_width; // If input is shorter than fill_width fill remaining space up with fill_char
void fill_finalize(); // does the filling after text has been written to buffer char fill_char; // fill character for fixed width
int base; // Basis des Zahlensystems: z.B. 2, 8, 10 oder 16
void fill_use_char(); // recognizes that one char from the print width has been used up
void fill_finalize(); // does the filling after text has been written to buffer
public: public:
int base; // Basis des Zahlensystems: z.B. 2, 8, 10 oder 16 OutStream() : fill_used(0), fill_width(0), fill_char(' '), base(10) {}
// // Some stream formatting
unsigned char fill_width;
char fill_char; // fill character for fixed width
OutStream() : fill_used(0), base(10), fill_width(0), fill_char(' ') {}
// Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer. // Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer.
void flush() override; void flush() override;
@ -68,31 +66,22 @@ public:
// This allows chaining of operator<< of different streams. // This allows chaining of operator<< of different streams.
// Needed because I added operator<< overloads to the CGA_Stream class to change color with manipulators. // Needed because I added operator<< overloads to the CGA_Stream class to change color with manipulators.
// NOTE: The templace concepts are only available in c++20.
// Leaving them out makes this unsafe, but it doesn't matter as much in this case since we only
// use a CGA_Stream in Globals.h anyway.
// Darstellung eines Zeichens (trivial) // Darstellung eines Zeichens (trivial)
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, char c) { friend T& operator<<(T& os, char c) {
os.put(c); os.put(c);
if (c != '\n') { if (c != '\n') {
// endl() doesn't has access to StringBuffer::put(), so ignore \n here // endl() doesn't has access to StringBuffer::put(), so ignore \n here
os.fill_finalize(); // NOTE: I added this os.fill_finalize();
} }
return os; return os;
} }
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream> friend T& operator<<(T& os, unsigned char c) { return os << (char)c; }
friend T& operator<<(T& os, unsigned char c) {
return os << (char)c;
}
// Darstellung einer nullterminierten Zeichenkette // Darstellung einer nullterminierten Zeichenkette
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, char* string) { friend T& operator<<(T& os, char* string) {
char* pos = string; char* pos = string;
@ -106,31 +95,18 @@ public:
// Darstellung ganzer Zahlen im Zahlensystem zur Basis base // Darstellung ganzer Zahlen im Zahlensystem zur Basis base
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream> friend T& operator<<(T& os, short ival) { return os << (long)ival; }
friend T& operator<<(T& os, short ival) {
return os << (long)ival;
}
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream> friend T& operator<<(T& os, unsigned short ival) { return os << (unsigned long)ival; }
friend T& operator<<(T& os, unsigned short ival) {
return os << (unsigned long)ival;
}
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream> friend T& operator<<(T& os, int ival) { return os << (long)ival; }
friend T& operator<<(T& os, int ival) {
return os << (long)ival;
}
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream> friend T& operator<<(T& os, unsigned int ival) { return os << (unsigned long)ival; }
friend T& operator<<(T& os, unsigned int ival) {
return os << (unsigned long)ival;
}
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, long ival) { friend T& operator<<(T& os, long ival) {
// Bei negativen Werten wird ein Minuszeichen ausgegeben. // Bei negativen Werten wird ein Minuszeichen ausgegeben.
if (ival < 0) { if (ival < 0) {
@ -142,10 +118,9 @@ public:
} }
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, unsigned long ival) { friend T& operator<<(T& os, unsigned long ival) {
unsigned long div; unsigned long div = 0;
char digit; char digit = 0;
if (os.base == 8) { if (os.base == 8) {
os.put('0'); // oktale Zahlen erhalten eine fuehrende Null os.put('0'); // oktale Zahlen erhalten eine fuehrende Null
@ -168,13 +143,12 @@ public:
} }
ival %= div; ival %= div;
} }
os.fill_finalize(); // NOTE: I added this os.fill_finalize();
return os; return os;
} }
// Darstellung eines Zeigers als hexadezimale ganze Zahl // Darstellung eines Zeigers als hexadezimale ganze Zahl
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, void* ptr) { friend T& operator<<(T& os, void* ptr) {
int oldbase = os.base; int oldbase = os.base;
os.base = 16; os.base = 16;
@ -184,28 +158,30 @@ public:
} }
// Aufruf einer Manipulatorfunktion // Aufruf einer Manipulatorfunktion
// NOTE: Changed the function pointer type including the manipulator functions
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream> friend T& operator<<(T& os, T& (*f)(T&)) { return f(os); }
friend T& operator<<(T& os, T& (*f)(T&)) {
return f(os);
}
// For stream formatting // For stream formatting
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, const fillw& w) { friend T& operator<<(T& os, const fillw& w) {
os.flush(); // Flush the buffer to not modify previous output os.flush(); // Flush the buffer to not modify previous output
os.fill_width = w.w; os.fill_width = w.w;
return os; return os;
} }
template<typename T> template<typename T>
// requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, const fillc& c) { friend T& operator<<(T& os, const fillc& c) {
os.flush(); os.flush();
os.fill_char = c.c; os.fill_char = c.c;
return os; return os;
} }
// Allow access to base member
template<typename T> friend T& endl(T& os);
template<typename T> friend T& bin(T& os);
template<typename T> friend T& oct(T& os);
template<typename T> friend T& dec(T& os);
template<typename T> friend T& hex(T& os);
}; };
// //