summaryrefslogtreecommitdiff
path: root/Source/Library
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-12-20 16:13:44 +0100
committerAlexis211 <alexis211@gmail.com>2009-12-20 16:13:44 +0100
commit2d3c5a9c47d99c8f4f5561f9eae16497c1cde63a (patch)
treea3f800e17a34a16a00b6dc0b980e2f6cfed22dd5 /Source/Library
parentcab353cbb21c142ac1e227d42338fa587e1a7f24 (diff)
downloadMelon-2d3c5a9c47d99c8f4f5561f9eae16497c1cde63a.tar.gz
Melon-2d3c5a9c47d99c8f4f5561f9eae16497c1cde63a.zip
Added file streams and string streams
Diffstat (limited to 'Source/Library')
-rw-r--r--Source/Library/Common/FileStream.class.cpp67
-rw-r--r--Source/Library/Common/FileStream.class.h41
-rw-r--r--Source/Library/Common/IStream.proto.cpp7
-rw-r--r--Source/Library/Common/IStream.proto.h3
-rw-r--r--Source/Library/Common/OStream.proto.cpp3
-rw-r--r--Source/Library/Common/OStream.proto.h3
-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
-rw-r--r--Source/Library/Makefile2
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.cpp4
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.h1
-rw-r--r--Source/Library/Userland/Binding/VirtualTerminal.class.h13
13 files changed, 205 insertions, 4 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,
diff --git a/Source/Library/Makefile b/Source/Library/Makefile
index cee4944..83e3689 100644
--- a/Source/Library/Makefile
+++ b/Source/Library/Makefile
@@ -21,6 +21,8 @@ Objects = Common/WChar.class.uo \
Common/Rand.ns.uo \
Common/OStream.proto.uo \
Common/IStream.proto.uo \
+ Common/FileStream.class.uo \
+ Common/StringStream.class.uo \
Common/cppsupport.wtf.uo \
Userland/App/ShellApp.proto.uo \
Userland/Syscall/Syscall.wtf.uo \
diff --git a/Source/Library/Userland/App/ShellApp.proto.cpp b/Source/Library/Userland/App/ShellApp.proto.cpp
index f2233bc..9528ca2 100644
--- a/Source/Library/Userland/App/ShellApp.proto.cpp
+++ b/Source/Library/Userland/App/ShellApp.proto.cpp
@@ -9,6 +9,10 @@ ShellApp::ShellApp(String name, String desc)
addFlag("h", "help", "Show this help screen");
}
+ShellApp::~ShellApp() {
+ outvt << END;
+}
+
void ShellApp::init() {
//Parse flags
u32int argc = pr.argc();
diff --git a/Source/Library/Userland/App/ShellApp.proto.h b/Source/Library/Userland/App/ShellApp.proto.h
index 5575112..2308fd1 100644
--- a/Source/Library/Userland/App/ShellApp.proto.h
+++ b/Source/Library/Userland/App/ShellApp.proto.h
@@ -24,6 +24,7 @@ class ShellApp : public Application {
Vector<flag_t> flags;
String appName, appDesc;
ShellApp(String name, String desc);
+ ~ShellApp();
virtual void init();
diff --git a/Source/Library/Userland/Binding/VirtualTerminal.class.h b/Source/Library/Userland/Binding/VirtualTerminal.class.h
index 6576c1c..54bdc53 100644
--- a/Source/Library/Userland/Binding/VirtualTerminal.class.h
+++ b/Source/Library/Userland/Binding/VirtualTerminal.class.h
@@ -11,6 +11,9 @@
#include <WChar.class.h>
class VirtualTerminal : public RessourceCaller, public OStream, public IStream {
+ private:
+ bool m_eof;
+
public:
static VirtualTerminal getIn() {
u32int id = RessourceCaller::sCall(VTIF_OBJTYPE, VTIF_SGETPRINVT);
@@ -20,7 +23,7 @@ class VirtualTerminal : public RessourceCaller, public OStream, public IStream {
u32int id = RessourceCaller::sCall(VTIF_OBJTYPE, VTIF_SGETPROUTVT);
return VirtualTerminal(id);
}
- VirtualTerminal(u32int id) : RessourceCaller(id, VTIF_OBJTYPE) {}
+ VirtualTerminal(u32int id) : RessourceCaller(id, VTIF_OBJTYPE) { m_eof = false; }
/*void writeHex(u32int number) {
doCall(VTIF_WRITEHEX, number);
@@ -32,7 +35,13 @@ class VirtualTerminal : public RessourceCaller, public OStream, public IStream {
doCall(VTIF_WRITE, (u32int)&s);
}
String read() {
- return String::unserialize(doCall(VTIF_READLINE, 1)) += "\n";
+ if (m_eof) return "";
+ String ret = String::unserialize(doCall(VTIF_READLINE, 1));
+ if (ret[ret.size() - 1] == WChar(EOF)) {
+ ret = ret.substr(0, ret.size() - 1);
+ m_eof = true;
+ }
+ return ret += "\n";
}
keypress_t getKeypress(bool show = true, bool block = true) {
keypress_t* ptr = (keypress_t*)doCall(VTIF_GETKEYPRESS, (show ? 1 : 0) | (block ? 2 : 0));