summaryrefslogtreecommitdiff
path: root/Source/Library/Common
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Library/Common')
-rw-r--r--Source/Library/Common/FileStream.class.cpp71
-rw-r--r--Source/Library/Common/FileStream.class.h43
-rw-r--r--Source/Library/Common/IStream.proto.cpp81
-rw-r--r--Source/Library/Common/IStream.proto.h32
-rw-r--r--Source/Library/Common/OStream.proto.cpp65
-rw-r--r--Source/Library/Common/OStream.proto.h39
-rw-r--r--Source/Library/Common/SimpleList.class.h13
-rw-r--r--Source/Library/Common/StringStream.class.cpp28
-rw-r--r--Source/Library/Common/StringStream.class.h35
-rw-r--r--Source/Library/Common/WChar.class.h2
10 files changed, 409 insertions, 0 deletions
diff --git a/Source/Library/Common/FileStream.class.cpp b/Source/Library/Common/FileStream.class.cpp
new file mode 100644
index 0000000..317a023
--- /dev/null
+++ b/Source/Library/Common/FileStream.class.cpp
@@ -0,0 +1,71 @@
+#include "FileStream.class.h"
+
+//************************************
+// INPUT FILE STREAM
+// ********************
+
+FileIStream::FileIStream(const String &filename, u8int encoding, FSNode start) : m_start(start) {
+ m_file = new File(filename, FM_READ, start);
+ m_filenames = 0;
+ m_encoding = encoding;
+}
+
+FileIStream::FileIStream(u8int encoding, FSNode start) : m_start(start) {
+ m_file = 0;
+ m_filenames = 0;
+ m_encoding = encoding;
+}
+
+FileIStream::~FileIStream() {
+ if (m_file != 0) {
+ m_file->close();
+ delete m_file;
+ }
+}
+
+void FileIStream::appendFile(const String &filename) {
+ if (m_filenames == 0) {
+ m_filenames = new SimpleList<String>(filename);
+ } else {
+ m_filenames->addAtEnd(filename);
+ }
+}
+
+String FileIStream::read() {
+ while (m_file == 0 or m_file->eof() or !m_file->valid()) {
+ if (m_file != 0) {
+ m_file->close();
+ delete m_file;
+ }
+ m_file = 0;
+ if (m_filenames == 0) {
+ return "";
+ } else {
+ m_file = new File(m_filenames->v(), FM_READ, m_start);
+ m_filenames = m_filenames->delThis();
+ }
+ }
+ ByteArray temp;
+ temp.resize(512);
+ u32int r = m_file->read(temp);
+ temp.resize(r);
+ return temp.toString(m_encoding);
+}
+
+//************************************
+// OUTPUT FILE STREAM
+// ********************
+
+FileOStream::FileOStream(const String& filename, u8int mode, u8int encoding, FSNode start) {
+ m_file = new File(filename, mode, start);
+ m_encoding = encoding;
+}
+
+FileOStream::~FileOStream() {
+ delete m_file;
+}
+
+void FileOStream::write(const String &s) {
+ ByteArray a(s, m_encoding);
+ m_file->write(a);
+}
diff --git a/Source/Library/Common/FileStream.class.h b/Source/Library/Common/FileStream.class.h
new file mode 100644
index 0000000..99bf0e8
--- /dev/null
+++ b/Source/Library/Common/FileStream.class.h
@@ -0,0 +1,43 @@
+#ifndef DEF_FILESTREAM_CLASS_H
+#define DEF_FILESTREAM_CLASS_H
+
+#include <IStream.proto.h>
+#include <OStream.proto.h>
+
+#ifdef THIS_IS_MELON_KERNEL
+#include <VFS/File.class.h>
+#else
+#include <Binding/File.class.h>
+#endif
+
+class FileIStream : public IStream {
+ private:
+ String read();
+
+ SimpleList<String> *m_filenames;
+
+ File *m_file;
+ u8int m_encoding;
+ FSNode m_start;
+
+ public:
+ FileIStream(const String &filename, u8int encoding = UE_UTF8, FSNode start = FSNode(0));
+ FileIStream(u8int encoding = UE_UTF8, FSNode start = FSNode(0));
+ ~FileIStream();
+
+ void appendFile(const String &filename);
+};
+
+class FileOStream : public OStream {
+ private:
+ File *m_file;
+ u8int m_encoding;
+
+ public:
+ FileOStream(const String &filename, u8int mode, u8int encoding = UE_UTF8, FSNode start = FSNode(0));
+ ~FileOStream();
+
+ void write(const String &s);
+};
+
+#endif
diff --git a/Source/Library/Common/IStream.proto.cpp b/Source/Library/Common/IStream.proto.cpp
new file mode 100644
index 0000000..6176eac
--- /dev/null
+++ b/Source/Library/Common/IStream.proto.cpp
@@ -0,0 +1,81 @@
+#include "IStream.proto.h"
+
+IStream::IStream() {
+ m_buffer = NULL;
+ m_ptr = 0;
+ m_eof = false;
+}
+
+IStream::IStream(const IStream& other) {
+ m_buffer = NULL;
+ m_ptr = 0;
+}
+
+IStream::~IStream() {
+ delete m_buffer;
+}
+
+bool IStream::populate() {
+ if (m_eof) return false;
+ String s = read();
+ if (s.empty()) {
+ m_eof = true;
+ return false;
+ }
+ if (m_buffer == NULL) {
+ m_buffer = new SimpleList<String>(s);
+ } else {
+ m_buffer->addAtEnd(s);
+ }
+ return true;
+}
+
+WChar IStream::getChar() {
+ waitLock();
+ if (m_buffer == 0) {
+ if (!populate()) {
+ unlock();
+ return WChar("\0");
+ }
+ }
+ WChar ret = m_buffer->v()[m_ptr];
+ m_ptr++;
+ if (m_ptr >= m_buffer->v().size()) {
+ m_buffer = m_buffer->delThis();
+ m_ptr = 0;
+ }
+ unlock();
+ return ret;
+}
+
+String IStream::get(WChar delimiter) {
+ waitLock();
+ //calculate length of string to read
+ if (m_buffer == 0) {
+ unlock();
+ if (!populate()) return "";
+ }
+ int length = 0, ptr = m_ptr;
+ for (SimpleList<String> *iter = m_buffer; iter != 0;) {
+ if (iter->v()[ptr] == delimiter) break;
+ ptr++;
+ length++;
+ if (ptr >= iter->v().size()) {
+ if (iter->next() == 0) populate();
+ iter = iter->next();
+ ptr = 0;
+ }
+ }
+ //get it
+ String ret(WChar(" "), length);
+ for (int i = 0; i <= length; i++) {
+ if (i != length) ret[i] = m_buffer->v()[m_ptr];
+ m_ptr++;
+ if (m_ptr >= m_buffer->v().size()) {
+ m_buffer = m_buffer->delThis();
+ m_ptr = 0;
+ }
+ }
+ unlock();
+ return ret;
+}
diff --git a/Source/Library/Common/IStream.proto.h b/Source/Library/Common/IStream.proto.h
new file mode 100644
index 0000000..e844726
--- /dev/null
+++ b/Source/Library/Common/IStream.proto.h
@@ -0,0 +1,32 @@
+#ifndef DEF_ISTREAM_PROTO_h
+#define DEF_ISTREAM_PROTO_h
+
+#include <String.class.h>
+#include <SimpleList.class.h>
+#include <Mutex.class.h>
+
+class IStream : private Mutex {
+ private:
+ SimpleList<String> *m_buffer;
+ int m_ptr;
+ void operator =(IStream& other);
+ bool m_eof;
+
+ bool populate();
+
+ protected:
+ virtual String read() = 0;
+
+ public:
+ IStream();
+ IStream(const IStream& other);
+ virtual ~IStream();
+
+ bool eof() const { return m_eof && (m_buffer == NULL); }
+
+ WChar getChar();
+ String get(WChar delimiter = "\n");
+ String getWord() { return get(" "); }
+};
+
+#endif
diff --git a/Source/Library/Common/OStream.proto.cpp b/Source/Library/Common/OStream.proto.cpp
new file mode 100644
index 0000000..f0667e7
--- /dev/null
+++ b/Source/Library/Common/OStream.proto.cpp
@@ -0,0 +1,65 @@
+#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) {
+ if (s.empty()) return;
+ 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");
+ flush();
+ } else if (m == END) {
+ put(EOF);
+ flush();
+ }
+ return *this;
+}
diff --git a/Source/Library/Common/OStream.proto.h b/Source/Library/Common/OStream.proto.h
new file mode 100644
index 0000000..ff19e7b
--- /dev/null
+++ b/Source/Library/Common/OStream.proto.h
@@ -0,0 +1,39 @@
+#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,
+ END
+};
+
+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;
}
diff --git a/Source/Library/Common/StringStream.class.cpp b/Source/Library/Common/StringStream.class.cpp
new file mode 100644
index 0000000..c4377ce
--- /dev/null
+++ b/Source/Library/Common/StringStream.class.cpp
@@ -0,0 +1,28 @@
+#include "StringStream.class.h"
+
+StringIStream::StringIStream(const String &e) {
+ m_elements = new SimpleList<String>(e);
+}
+
+StringIStream::StringIStream() {
+ m_elements = 0;
+}
+
+StringIStream::~StringIStream() {
+ if (m_elements != 0) delete m_elements;
+}
+
+String StringIStream::read() {
+ if (m_elements == 0) return "";
+ String ret = m_elements->v();
+ m_elements = m_elements->delThis();
+ return ret;
+}
+
+void StringIStream::append(const String &e) {
+ if (m_elements == 0) {
+ m_elements = new SimpleList<String>(e);
+ } else {
+ m_elements->addAtEnd(e);
+ }
+}
diff --git a/Source/Library/Common/StringStream.class.h b/Source/Library/Common/StringStream.class.h
new file mode 100644
index 0000000..814b539
--- /dev/null
+++ b/Source/Library/Common/StringStream.class.h
@@ -0,0 +1,35 @@
+#ifndef DEF_STRINGSTREAM_CLASS_H
+#define DEF_STRINGSTREAM_CLASS_H
+
+#include <IStream.proto.h>
+#include <OStream.proto.h>
+
+class StringIStream : public IStream {
+ private:
+ SimpleList<String> *m_elements;
+
+ String read();
+
+ public:
+ StringIStream(const String &e);
+ StringIStream();
+ ~StringIStream();
+
+ void append(const String &e);
+};
+
+class StringOStream : public OStream {
+ private:
+ String m_str;
+
+ void write(const String& s) { m_str += s; }
+
+ public:
+
+ const String &str() const { return m_str; }
+ void clear() { m_str = ""; }
+};
+
+
+#endif
+
diff --git a/Source/Library/Common/WChar.class.h b/Source/Library/Common/WChar.class.h
index afaeb44..6db4b1b 100644
--- a/Source/Library/Common/WChar.class.h
+++ b/Source/Library/Common/WChar.class.h
@@ -7,6 +7,8 @@
#include <common.h>
#endif
+#define EOF "\3"
+
enum {
UE_UTF8,
UE_UTF16_LE,