summaryrefslogtreecommitdiff
path: root/Source/Library
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-12-20 20:03:22 +0100
committerAlexis211 <alexis211@gmail.com>2009-12-20 20:03:22 +0100
commit09353da6e91a0968ae24d4b4d97ed434520e6217 (patch)
tree016694d12e9dfebc773cef629eb56556d0024b22 /Source/Library
parent247070cc7e5ae117fd0d1b551fafdf5c13f0dd6b (diff)
parent18454dc8be12827a84c2ebc58aa5d31bb44e1e6a (diff)
downloadMelon-09353da6e91a0968ae24d4b4d97ed434520e6217.tar.gz
Melon-09353da6e91a0968ae24d4b4d97ed434520e6217.zip
Merge branch 'framework'
Conflicts: .gitignore
Diffstat (limited to 'Source/Library')
-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
-rw-r--r--Source/Library/Makefile5
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.cpp34
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.h3
-rw-r--r--Source/Library/Userland/App/StreamApp.proto.cpp40
-rw-r--r--Source/Library/Userland/App/StreamApp.proto.h27
-rw-r--r--Source/Library/Userland/Binding/Process.class.h4
-rw-r--r--Source/Library/Userland/Binding/VirtualTerminal.class.h32
-rw-r--r--Source/Library/Userland/Syscall/RessourceCaller.class.h4
18 files changed, 532 insertions, 26 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,
diff --git a/Source/Library/Makefile b/Source/Library/Makefile
index b3040d4..c67daf7 100644
--- a/Source/Library/Makefile
+++ b/Source/Library/Makefile
@@ -19,8 +19,13 @@ Objects = Common/WChar.class.uo \
Common/TextFile.class.uo \
Common/ByteArray.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/App/StreamApp.proto.uo \
Userland/Syscall/Syscall.wtf.uo \
Userland/Syscall/RessourceCaller.class.uo \
Userland/Start.uo
diff --git a/Source/Library/Userland/App/ShellApp.proto.cpp b/Source/Library/Userland/App/ShellApp.proto.cpp
index a3d9737..123eb73 100644
--- a/Source/Library/Userland/App/ShellApp.proto.cpp
+++ b/Source/Library/Userland/App/ShellApp.proto.cpp
@@ -1,6 +1,6 @@
#include "ShellApp.proto.h"
-ShellApp::ShellApp(String name, String desc)
+ShellApp::ShellApp(const String &name, const String &desc)
: Application(), invt(VirtualTerminal::getIn()), outvt(VirtualTerminal::getOut()) {
appName = name, appDesc = desc;
if (!invt.valid()) exit(1);
@@ -9,11 +9,17 @@ 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();
- for (u32int i = 0; i < argc; i++) {
+ args.clear();
+ for (u32int i = 1; i < argc; i++) {
String arg = pr.argv(i);
+ if (arg.empty()) continue;
if (arg == "-") {
i++;
if (i == argc) {
@@ -26,13 +32,13 @@ void ShellApp::init() {
bool found = false;
for (u32int i = 0; i < flags.size(); i++) {
if (flags[i].type == FT_BOOL) {
- if (arg == String("--no-") + flags[i].lName) {
+ if (arg == (String("--no-") += flags[i].lName)) {
flags[i].boolVal = false;
found = true;
}
}
}
- if (!found) outvt << "Unknown option : " << arg << "\n";
+ if (!found) outvt << "Unknown option : " << arg << ENDL;
} else if (arg.substr(0, 2) == "--") {
bool found = false;
for (u32int i = 0; i < flags.size(); i++) {
@@ -49,21 +55,29 @@ void ShellApp::init() {
}
}
}
- if (!found) outvt << "Unknown option : " << arg << "\n";
+ if (!found) outvt << "Unknown option : " << arg << ENDL;
} else {
for (u32int j = 1; j < arg.size(); j++) {
bool found = false;
for (u32int k = 0; k < flags.size(); k++) {
if (flags[k].sName == arg[j]) {
found = true;
- if (flags[k].type == FT_BOOL) flags[k].boolVal = true;
- if (flags[k].type == FT_INT) flags[k].intVal = pr.argv(++i).toInt();
- if (flags[k].type == FT_STR) flags[k].strVal = pr.argv(++i);
+ if (flags[k].type == FT_BOOL) {
+ flags[k].boolVal = true;
+ } else {
+ i++;
+ if (i >= argc) {
+ outvt << "Missing argument for flag : -" << String(arg[j], 1) << ENDL;
+ } else {
+ flags[k].strVal = pr.argv(i);
+ if (flags[k].type == FT_INT) flags[k].intVal = flags[k].strVal.toInt();
+ }
+ }
break;
}
}
if (!found) {
- outvt << "Unknown option : -" << String(arg[j]) << "\n";
+ outvt << "Unknown option : -" << String(arg[j], 1) << ENDL;
exit(-1);
}
}
@@ -76,7 +90,7 @@ void ShellApp::init() {
//Eventually show help screen
if (bFlag("help")) {
outvt << appName << ": " << appDesc << "\n";
- outvt << "Usage: \t" << appName << " <flags> [-] <arguments>\n\n";
+ outvt << "Usage: \t" << pr.argv(0) << " <flags> [-] <arguments>\n\n";
outvt << "Possible flags :\n";
for (u32int i = 0; i < flags.size(); i++) {
outvt << " --" << flags[i].lName << "\t" << (flags[i].sName != 0 ? "-" : "") << String(flags[i].sName) << "\t";
diff --git a/Source/Library/Userland/App/ShellApp.proto.h b/Source/Library/Userland/App/ShellApp.proto.h
index 5575112..bc57cd0 100644
--- a/Source/Library/Userland/App/ShellApp.proto.h
+++ b/Source/Library/Userland/App/ShellApp.proto.h
@@ -23,7 +23,8 @@ class ShellApp : public Application {
Vector<String> args;
Vector<flag_t> flags;
String appName, appDesc;
- ShellApp(String name, String desc);
+ ShellApp(const String &name, const String &desc);
+ ~ShellApp();
virtual void init();
diff --git a/Source/Library/Userland/App/StreamApp.proto.cpp b/Source/Library/Userland/App/StreamApp.proto.cpp
new file mode 100644
index 0000000..b1dc7dd
--- /dev/null
+++ b/Source/Library/Userland/App/StreamApp.proto.cpp
@@ -0,0 +1,40 @@
+#include "StreamApp.proto.h"
+
+#include <FileStream.class.h>
+
+StreamApp::StreamApp(const String& name, const String& desc)
+ : ShellApp(name, desc) {
+ addFlag("o", "output", "Set the output to a file instead of the text output", FT_STR, "");
+ addFlag("e", "encoding", "Set the encoding for files (input and output)", FT_STR, "utf8");
+ addFlag("a", "append", "When writing to a file, append instead of truncating", FT_BOOL, "");
+}
+
+StreamApp::~StreamApp() {
+}
+
+void StreamApp::init() {
+ ShellApp::init();
+
+ u8int encoding = UE_UTF8;
+ if (sFlag("encoding") == "utf8") encoding = UE_UTF8;
+ if (sFlag("encoding") == "utf16be") encoding = UE_UTF16_BE;
+ if (sFlag("encoding") == "utf16le") encoding = UE_UTF16_LE;
+ if (sFlag("encoding") == "utf32be") encoding = UE_UTF32_BE;
+ if (sFlag("encoding") == "utf32le") encoding = UE_UTF32_LE;
+
+ if (sFlag("output") == "") {
+ out = &outvt;
+ } else {
+ out = new FileOStream(sFlag("output"), (bFlag("append") ? FM_APPEND : FM_TRUNCATE), encoding, FS::cwdNode());
+ }
+
+ if (args.size() == 0) {
+ in = &invt;
+ } else {
+ FileIStream *f = new FileIStream(encoding, FS::cwdNode());
+ for (u32int i = 0; i < args.size(); i++) {
+ f->appendFile(args[i]);
+ }
+ in = f;
+ }
+}
diff --git a/Source/Library/Userland/App/StreamApp.proto.h b/Source/Library/Userland/App/StreamApp.proto.h
new file mode 100644
index 0000000..462b1f3
--- /dev/null
+++ b/Source/Library/Userland/App/StreamApp.proto.h
@@ -0,0 +1,27 @@
+#ifndef DEF_STREAMAPP_PROTO_H
+#define DEF_STREAMAPP_PROTO_H
+
+#include "ShellApp.proto.h"
+
+#include <IStream.proto.h>
+#include <OStream.proto.h>
+
+/*
+ * This class implements basic utilities for apps that simply take some input, process it and output something.
+ * Examples : cat, grep, ...
+ */
+
+class StreamApp : public ShellApp {
+ protected:
+
+ IStream *in;
+ OStream *out;
+
+ public:
+ StreamApp(const String& name, const String& desc);
+ ~StreamApp();
+
+ virtual void init();
+};
+
+#endif
diff --git a/Source/Library/Userland/Binding/Process.class.h b/Source/Library/Userland/Binding/Process.class.h
index 2200f59..e94084b 100644
--- a/Source/Library/Userland/Binding/Process.class.h
+++ b/Source/Library/Userland/Binding/Process.class.h
@@ -54,10 +54,10 @@ class Process : public RessourceCaller {
void pushArg(const String& arg) {
doCall(PRIF_PUSHARG, (u32int)&arg);
}
- void setInVT(VirtualTerminal vt) {
+ void setInVT(const VirtualTerminal& vt) {
doCall(PRIF_SETINVT, vt.resId());
}
- void setOutVT(VirtualTerminal vt) {
+ void setOutVT(const VirtualTerminal& vt) {
doCall(PRIF_SETOUTVT, vt.resId());
}
bool authenticatePW(String user, String pw) {
diff --git a/Source/Library/Userland/Binding/VirtualTerminal.class.h b/Source/Library/Userland/Binding/VirtualTerminal.class.h
index 70c6b23..2a38abd 100644
--- a/Source/Library/Userland/Binding/VirtualTerminal.class.h
+++ b/Source/Library/Userland/Binding/VirtualTerminal.class.h
@@ -6,10 +6,14 @@
#include <VirtualTerminal.iface.h>
#include <Kbd.iface.h>
-#include <String.class.h>
+#include <OStream.proto.h>
+#include <IStream.proto.h>
#include <WChar.class.h>
-class VirtualTerminal : public RessourceCaller {
+class VirtualTerminal : public RessourceCaller, public OStream, public IStream {
+ private:
+ bool m_eof;
+
public:
static VirtualTerminal getIn() {
u32int id = RessourceCaller::sCall(VTIF_OBJTYPE, VTIF_SGETPRINVT);
@@ -19,22 +23,33 @@ class VirtualTerminal : public RessourceCaller {
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) {
+ /*void writeHex(u32int number) {
doCall(VTIF_WRITEHEX, number);
}
void writeDec(s64int number) {
doCall(VTIF_WRITEDEC, (number >> 32), number);
- }
- void write(String s) {
+ }*/
+ void write(const String &s) {
doCall(VTIF_WRITE, (u32int)&s);
}
+ String read() {
+ 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);
+ if (ret.empty()) return "";
+ 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));
return *ptr;
}
String readLine(bool show = true) {
+ flush();
return String::unserialize(doCall(VTIF_READLINE, (show ? 1 : 0)));
}
void setColor(u8int fg, u8int bg = 0xFF) {
@@ -64,11 +79,6 @@ class VirtualTerminal : public RessourceCaller {
void put(u8int line, u8int col, WChar c) {
moveCursor(line, col); put(c);
}
-
- inline VirtualTerminal& operator<<(const String& s) { write(s); return *this; }
- inline VirtualTerminal& operator<<(s32int i) { writeDec(i); return *this; }
- inline VirtualTerminal& operator<<(s64int i) { writeDec(i); return *this; }
- inline VirtualTerminal& operator<<(u32int i) { writeHex(i); return *this; }
};
#endif
diff --git a/Source/Library/Userland/Syscall/RessourceCaller.class.h b/Source/Library/Userland/Syscall/RessourceCaller.class.h
index f26216d..39091ed 100644
--- a/Source/Library/Userland/Syscall/RessourceCaller.class.h
+++ b/Source/Library/Userland/Syscall/RessourceCaller.class.h
@@ -28,8 +28,8 @@ class RessourceCaller {
public:
static u32int sCall(u32int type, u8int wat, u32int a = 0, u32int b = 0, u32int c = 0, u32int d = 0);
- u32int resId() { return m_id; }
- u32int resType() { return m_type; }
+ u32int resId() const { return m_id; }
+ u32int resType() const { return m_type; }
bool valid() { return m_type != 0; }
};