summaryrefslogtreecommitdiff
path: root/Source/Library/Common
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-12-20 13:33:28 +0100
committerAlexis211 <alexis211@gmail.com>2009-12-20 13:33:28 +0100
commit07a73aea7acbf0d9f9182bc3557583aa17af0000 (patch)
tree983e7b70b4d836c4050b8f350d7bd1e7250058b4 /Source/Library/Common
parenta804601188be682ce87e6a9591286e8c8ab2817d (diff)
downloadMelon-07a73aea7acbf0d9f9182bc3557583aa17af0000.tar.gz
Melon-07a73aea7acbf0d9f9182bc3557583aa17af0000.zip
[not really working] Started work on OStream
Diffstat (limited to 'Source/Library/Common')
-rw-r--r--Source/Library/Common/OStream.proto.cpp60
-rw-r--r--Source/Library/Common/OStream.proto.h38
-rw-r--r--Source/Library/Common/SimpleList.class.h13
3 files changed, 111 insertions, 0 deletions
diff --git a/Source/Library/Common/OStream.proto.cpp b/Source/Library/Common/OStream.proto.cpp
new file mode 100644
index 0000000..bbf5dc3
--- /dev/null
+++ b/Source/Library/Common/OStream.proto.cpp
@@ -0,0 +1,60 @@
+#include <OStream.proto.h>
+
+OStream::OStream() {
+ m_buffer = NULL;
+ m_last = NULL;
+}
+
+OStream::OStream(const OStream& other) {
+ m_buffer = NULL;
+ m_last = NULL;
+}
+
+void OStream::put(const String &s) {
+ waitLock();
+ if (m_buffer == NULL or m_last == NULL) {
+ m_buffer = m_last = new SimpleList<String>(s);
+ } else {
+ m_last = m_last->addAtEnd(s);
+ }
+ unlock();
+}
+
+void OStream::flush() {
+ if (m_buffer == NULL or m_last == NULL) return;
+
+ waitLock();
+ //Calculate buffer size
+ int size = 0;
+ SimpleList<String>* iter = m_buffer;
+ for (; iter != 0; iter = iter->next()) {
+ size += iter->v().size();
+ }
+ //Get stuff
+ String buff(WChar(" "), size);
+ iter = m_buffer;
+ for (int i = 0, pos = 0; i < size; i++, pos++) {
+ if (pos >= iter->v().size()) {
+ iter = iter->next();
+ pos = 0;
+ }
+ buff[i] = iter->v()[pos];
+ }
+ //Write it
+ write(buff);
+
+ delete m_buffer;
+ m_buffer = NULL;
+ m_last = NULL;
+
+ unlock();
+}
+
+OStream& OStream::operator<< (ostream_modifiers_e m) {
+ if (m == FLUSH) {
+ flush();
+ } else if (m == ENDL) {
+ put("\n");
+ }
+ return *this;
+}
diff --git a/Source/Library/Common/OStream.proto.h b/Source/Library/Common/OStream.proto.h
new file mode 100644
index 0000000..2fc68e1
--- /dev/null
+++ b/Source/Library/Common/OStream.proto.h
@@ -0,0 +1,38 @@
+#ifndef DEF_ISTREAM_PROTO_H
+#define DEF_ISTREAM_PROTO_H
+
+#include <String.class.h>
+#include <SimpleList.class.h>
+#include <Mutex.class.h>
+
+enum ostream_modifiers_e {
+ FLUSH,
+ ENDL
+};
+
+class OStream : private Mutex {
+ private:
+ SimpleList<String> *m_buffer;
+ SimpleList<String> *m_last;
+ void operator =(OStream& other);
+
+ protected:
+ virtual void write(const String &s) = 0;
+
+ public:
+ OStream();
+ OStream(const OStream& other);
+ virtual ~OStream() { flush(); }
+
+ void put(const String& s);
+ void flush();
+
+ //Formatting functions
+ OStream& operator << (const String& s) { put(s); if (s.contains("\n")) flush(); return *this; }
+ OStream& operator << (s64int i) { put(String::number(i)); return *this; }
+ OStream& operator << (s32int i) { put(String::number(i)); return *this; }
+ OStream& operator << (u32int i) { put(String::hex(i)); return *this; }
+ OStream& operator << (ostream_modifiers_e m);
+};
+
+#endif
diff --git a/Source/Library/Common/SimpleList.class.h b/Source/Library/Common/SimpleList.class.h
index 8fcd834..2bbbace 100644
--- a/Source/Library/Common/SimpleList.class.h
+++ b/Source/Library/Common/SimpleList.class.h
@@ -25,6 +25,19 @@ class SimpleList {
return new SimpleList<T>(value, this);
}
+ SimpleList<T>* addAtEnd(const T& value) {
+ if (this == 0) {
+ return new SimpleList<T>(value);
+ } else {
+ if (m_next == 0) {
+ m_next = new SimpleList<T>(value);
+ return m_next;
+ } else {
+ return m_next->addAtEnd(value);
+ }
+ }
+ }
+
SimpleList<T>* next() {
return m_next;
}