diff options
Diffstat (limited to 'Source/Library/Common')
-rw-r--r-- | Source/Library/Common/FileStream.class.cpp | 67 | ||||
-rw-r--r-- | Source/Library/Common/FileStream.class.h | 41 | ||||
-rw-r--r-- | Source/Library/Common/IStream.proto.cpp | 7 | ||||
-rw-r--r-- | Source/Library/Common/IStream.proto.h | 3 | ||||
-rw-r--r-- | Source/Library/Common/OStream.proto.cpp | 3 | ||||
-rw-r--r-- | Source/Library/Common/OStream.proto.h | 3 | ||||
-rw-r--r-- | Source/Library/Common/StringStream.class.cpp | 28 | ||||
-rw-r--r-- | Source/Library/Common/StringStream.class.h | 35 | ||||
-rw-r--r-- | Source/Library/Common/WChar.class.h | 2 |
9 files changed, 187 insertions, 2 deletions
diff --git a/Source/Library/Common/FileStream.class.cpp b/Source/Library/Common/FileStream.class.cpp new file mode 100644 index 0000000..6154b2f --- /dev/null +++ b/Source/Library/Common/FileStream.class.cpp @@ -0,0 +1,67 @@ +#include "FileStream.class.h" + +//************************************ +// INPUT FILE STREAM +// ******************** + +FileIStream::FileIStream(const String &filename, u8int encoding, FSNode start) { + m_file = new File(filename, FM_READ, start); + 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() { + if (m_file == 0) return ""; + while (m_file->eof() or !m_file->valid()) { + m_file->close(); + delete m_file; + m_file = 0; + if (m_filenames == 0) { + return ""; + } else { +#ifdef THIS_IS_MELON_KERNEL + m_file = new File(m_filenames->v(), FM_READ); +#else + m_file = new File(m_filenames->v(), FM_READ, FS::cwdNode()); +#endif + 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..d0fb971 --- /dev/null +++ b/Source/Library/Common/FileStream.class.h @@ -0,0 +1,41 @@ +#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; + + public: + FileIStream(const String &filename, 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 index 0cc2a79..59c623e 100644 --- a/Source/Library/Common/IStream.proto.cpp +++ b/Source/Library/Common/IStream.proto.cpp @@ -3,6 +3,7 @@ IStream::IStream() { m_buffer = NULL; m_ptr = 0; + m_eof = false; } IStream::IStream(const IStream& other) { @@ -15,8 +16,12 @@ IStream::~IStream() { } bool IStream::populate() { + if (m_eof) return false; String s = read(); - if (s.empty()) return false; + if (s.empty()) { + m_eof = true; + return false; + } if (m_buffer == NULL) { m_buffer = new SimpleList<String>(s); } else { diff --git a/Source/Library/Common/IStream.proto.h b/Source/Library/Common/IStream.proto.h index f689a22..e844726 100644 --- a/Source/Library/Common/IStream.proto.h +++ b/Source/Library/Common/IStream.proto.h @@ -10,6 +10,7 @@ class IStream : private Mutex { SimpleList<String> *m_buffer; int m_ptr; void operator =(IStream& other); + bool m_eof; bool populate(); @@ -21,6 +22,8 @@ class IStream : private Mutex { 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(" "); } diff --git a/Source/Library/Common/OStream.proto.cpp b/Source/Library/Common/OStream.proto.cpp index bbf5dc3..ba876b3 100644 --- a/Source/Library/Common/OStream.proto.cpp +++ b/Source/Library/Common/OStream.proto.cpp @@ -55,6 +55,9 @@ OStream& OStream::operator<< (ostream_modifiers_e m) { flush(); } else if (m == ENDL) { put("\n"); + } else if (m == END) { + put(String(EOF, 1)); + flush(); } return *this; } diff --git a/Source/Library/Common/OStream.proto.h b/Source/Library/Common/OStream.proto.h index 2fc68e1..ff19e7b 100644 --- a/Source/Library/Common/OStream.proto.h +++ b/Source/Library/Common/OStream.proto.h @@ -7,7 +7,8 @@ enum ostream_modifiers_e { FLUSH, - ENDL + ENDL, + END }; class OStream : private Mutex { 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, |