implement fixed width printing
This commit is contained in:
@ -19,15 +19,15 @@
|
||||
#include "lib/OutStream.h"
|
||||
|
||||
class CGA_Stream : public OutStream, public CGA {
|
||||
|
||||
|
||||
private:
|
||||
CGA_Stream(CGA_Stream ©); // Verhindere Kopieren
|
||||
CGA_Stream(CGA_Stream ©); // Verhindere Kopieren
|
||||
|
||||
public:
|
||||
CGA_Stream () : OutStream(), CGA() { flush(); }
|
||||
|
||||
// Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer.
|
||||
virtual void flush ();
|
||||
void flush () override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -22,15 +22,64 @@
|
||||
|
||||
#include "lib/OutStream.h"
|
||||
|
||||
// NOTE: I added this, stream manipulators with arg
|
||||
OutStream& OutStream::operator << (const fillw& w) {
|
||||
this->fill_width = w.w;
|
||||
return *this;
|
||||
}
|
||||
OutStream& OutStream::operator << (const fillc& c) {
|
||||
this->fill_char = c.c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// NOTE: I added this, fixed width printing
|
||||
void OutStream::put(char c) {
|
||||
if (this->fill_width == 0) {
|
||||
StringBuffer::put(c);
|
||||
} else if (this->fill_used == this->fill_width - 1) {
|
||||
// This indicates that content has been cut
|
||||
StringBuffer::put('@');
|
||||
this->fill_use_char();
|
||||
} else if (this->fill_used == this->fill_width) {
|
||||
// do nothing
|
||||
} else {
|
||||
StringBuffer::put(c);
|
||||
this->fill_use_char();
|
||||
}
|
||||
}
|
||||
void OutStream::fill_finalize() {
|
||||
if (this->fill_width == 0 || this->fill_used == this->fill_width) {
|
||||
// do nothing
|
||||
} else if (this->fill_used > this->fill_width) {
|
||||
// should never happen
|
||||
} else {
|
||||
for (unsigned char i = 0; i < this->fill_width - this->fill_used; ++i) {
|
||||
StringBuffer::put(this->fill_char);
|
||||
}
|
||||
}
|
||||
|
||||
this->fill_used = 0;
|
||||
}
|
||||
void OutStream::fill_use_char() {
|
||||
if (this->fill_width == 0) {
|
||||
return;
|
||||
}
|
||||
this->fill_used++;
|
||||
}
|
||||
|
||||
//
|
||||
// Zeichen und Zeichenketten in Stream ausgeben
|
||||
//
|
||||
OutStream& OutStream::operator << (char c) {
|
||||
put(c);
|
||||
if (c != '\n') {
|
||||
// endl() doesn't has access to StringBuffer::put(), so ignore \n here
|
||||
this->fill_finalize(); // NOTE: I added this
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// TODO: shouldn't this be printed as number?
|
||||
OutStream& OutStream::operator << (unsigned char c) {
|
||||
return *this << (char) c;
|
||||
}
|
||||
@ -41,6 +90,7 @@ OutStream& OutStream::operator << (char* string) {
|
||||
put (*pos);
|
||||
pos++;
|
||||
}
|
||||
this->fill_finalize(); // NOTE: I added this
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -79,10 +129,10 @@ OutStream& OutStream::operator << (long ival) {
|
||||
OutStream& OutStream::operator << (unsigned long ival) {
|
||||
unsigned long div;
|
||||
char digit;
|
||||
|
||||
if (base == 8)
|
||||
|
||||
if (base == 8) {
|
||||
put ('0'); // oktale Zahlen erhalten eine fuehrende Null
|
||||
else if (base == 16) {
|
||||
} else if (base == 16) {
|
||||
put ('0'); // hexadezimale Zahlen ein "0x"
|
||||
put ('x');
|
||||
}
|
||||
@ -94,12 +144,14 @@ OutStream& OutStream::operator << (unsigned long ival) {
|
||||
// ziffernweise Ausgabe der Zahl
|
||||
for (; div > 0; div /= (unsigned long) base) {
|
||||
digit = ival / div;
|
||||
if (digit < 10)
|
||||
put ('0' + digit);
|
||||
else
|
||||
put ('a' + digit - 10);
|
||||
if (digit < 10) {
|
||||
put ('0' + digit);
|
||||
} else {
|
||||
put ('a' + digit - 10);
|
||||
}
|
||||
ival %= div;
|
||||
}
|
||||
this->fill_finalize(); // NOTE: I added this
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -130,7 +182,7 @@ OutStream& OutStream::operator << (OutStream& (*f) (OutStream&)) {
|
||||
// Fuege einen Zeilenumbruch in die Ausgabe ein.
|
||||
OutStream& endl (OutStream& os) {
|
||||
os << '\n';
|
||||
os.flush ();
|
||||
os.flush();
|
||||
return os;
|
||||
}
|
||||
|
||||
|
@ -7,13 +7,13 @@
|
||||
* << Operators fuer die wichtigsten der vordefinierten *
|
||||
* Datentypen und realisiert somit die bekannte Ausgabe- *
|
||||
* funktion der C++ iO_Stream Bibliothek. Zur Zeit wird *
|
||||
* die Darstellung von Zeichen, Zeichenketten und ganzen *
|
||||
* die Darstellung von Zeichen, Zeichenketten und ganzen *
|
||||
* Zahlen unterstuetzt. Ein weiterer << Operator erlaubt *
|
||||
* die Verwendung von Manipulatoren. *
|
||||
* *
|
||||
* Neben der Klasse OutStream sind hier auch die *
|
||||
* Manipulatoren hex, dec, oct und bin fuer die Wahl der *
|
||||
* Basis bei der Zahlendarstellung, sowie endl fuer den *
|
||||
* Basis bei der Zahlendarstellung, sowie endl fuer den *
|
||||
* Zeilenumbruch definiert. *
|
||||
* *
|
||||
* Autor: Olaf Spinczyk, TU Dortmund *
|
||||
@ -24,17 +24,51 @@
|
||||
|
||||
#include "lib/StringBuffer.h"
|
||||
|
||||
// NOTE: I added this, stream manipulator with argument
|
||||
class OutStream;
|
||||
|
||||
// TODO: Should this only work for the next << ?
|
||||
class fillw {
|
||||
public:
|
||||
fillw(unsigned char w) : w(w) {};
|
||||
unsigned char w;
|
||||
};
|
||||
|
||||
class fillc {
|
||||
public:
|
||||
fillc(char c) : c(c) {};
|
||||
char c;
|
||||
};
|
||||
|
||||
|
||||
class OutStream : public StringBuffer {
|
||||
|
||||
|
||||
private:
|
||||
OutStream(const OutStream ©); // Verhindere Kopieren
|
||||
|
||||
// NOTE: I added this
|
||||
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
|
||||
void fill_finalize(); // does the filling after text has been written to buffer
|
||||
|
||||
public:
|
||||
int base; // Basis des Zahlensystems: z.B. 2, 8, 10 oder 16
|
||||
|
||||
OutStream () : StringBuffer () { base = 10; } // initial Dezimalsystem
|
||||
// NOTE: I added this
|
||||
unsigned char fill_width;
|
||||
char fill_char; // fill character for fixed width
|
||||
|
||||
virtual void flush () = 0; // weiterhin undefiniert
|
||||
OutStream () : StringBuffer () {
|
||||
base = 10; // initial Dezimalsystem
|
||||
fill_width = 0; // no fixed width
|
||||
fill_used = 0;
|
||||
fill_char = ' '; // fill with spaces
|
||||
}
|
||||
|
||||
void flush() override = 0; // weiterhin undefiniert aber public
|
||||
|
||||
// NOTE: I added this
|
||||
void put(char c) override;
|
||||
|
||||
// OPERATOR << : Umwandlung des angegebenen Datentypes in eine
|
||||
// Zeichenkette.
|
||||
@ -57,9 +91,12 @@ public:
|
||||
// Darstellung eines Zeigers als hexadezimale ganze Zahl
|
||||
OutStream& operator << (void* ptr);
|
||||
|
||||
// NOTE: I added this, set fixed output width
|
||||
OutStream &operator << (const fillw &w);
|
||||
OutStream &operator << (const fillc &c);
|
||||
|
||||
// Aufruf einer Manipulatorfunktion
|
||||
OutStream& operator << (OutStream& (*f) (OutStream&));
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
@ -36,8 +36,9 @@ protected:
|
||||
// StringBuffer: Im Konstruktor wird der Puffer als leer markiert.
|
||||
StringBuffer () : pos(0) {}
|
||||
|
||||
// NOTE: I changed this
|
||||
// Fuegt ein Zeichen in den Puffer ein. Wenn der Puffer
|
||||
void put (char c);
|
||||
virtual void put (char c);
|
||||
|
||||
// Methode zur Ausgabe des Pufferinhalts
|
||||
virtual void flush () = 0;
|
||||
|
Reference in New Issue
Block a user