summaryrefslogtreecommitdiff
path: root/Source/Library
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-12-20 15:21:26 +0100
committerAlexis211 <alexis211@gmail.com>2009-12-20 15:21:26 +0100
commitcab353cbb21c142ac1e227d42338fa587e1a7f24 (patch)
tree618c50ba58cc3186b3b82a3df2561063d4aaee85 /Source/Library
parent07a73aea7acbf0d9f9182bc3557583aa17af0000 (diff)
downloadMelon-cab353cbb21c142ac1e227d42338fa587e1a7f24.tar.gz
Melon-cab353cbb21c142ac1e227d42338fa587e1a7f24.zip
Worked on IStream. Seems to work
Diffstat (limited to 'Source/Library')
-rw-r--r--Source/Library/Common/IStream.proto.cpp73
-rw-r--r--Source/Library/Common/IStream.proto.h29
-rw-r--r--Source/Library/Makefile1
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.cpp23
-rw-r--r--Source/Library/Userland/Binding/Process.class.h4
-rw-r--r--Source/Library/Userland/Binding/VirtualTerminal.class.h6
-rw-r--r--Source/Library/Userland/Syscall/RessourceCaller.class.h4
7 files changed, 128 insertions, 12 deletions
diff --git a/Source/Library/Common/IStream.proto.cpp b/Source/Library/Common/IStream.proto.cpp
new file mode 100644
index 0000000..0cc2a79
--- /dev/null
+++ b/Source/Library/Common/IStream.proto.cpp
@@ -0,0 +1,73 @@
+#include "IStream.proto.h"
+
+IStream::IStream() {
+ m_buffer = NULL;
+ m_ptr = 0;
+}
+
+IStream::IStream(const IStream& other) {
+ m_buffer = NULL;
+ m_ptr = 0;
+}
+
+IStream::~IStream() {
+ delete m_buffer;
+}
+
+bool IStream::populate() {
+ String s = read();
+ if (s.empty()) 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) populate();
+ 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..f689a22
--- /dev/null
+++ b/Source/Library/Common/IStream.proto.h
@@ -0,0 +1,29 @@
+#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 populate();
+
+ protected:
+ virtual String read() = 0;
+
+ public:
+ IStream();
+ IStream(const IStream& other);
+ virtual ~IStream();
+
+ WChar getChar();
+ String get(WChar delimiter = "\n");
+ String getWord() { return get(" "); }
+};
+
+#endif
diff --git a/Source/Library/Makefile b/Source/Library/Makefile
index ed65ac4..cee4944 100644
--- a/Source/Library/Makefile
+++ b/Source/Library/Makefile
@@ -20,6 +20,7 @@ Objects = Common/WChar.class.uo \
Common/ByteArray.class.uo \
Common/Rand.ns.uo \
Common/OStream.proto.uo \
+ Common/IStream.proto.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 a3d9737..f2233bc 100644
--- a/Source/Library/Userland/App/ShellApp.proto.cpp
+++ b/Source/Library/Userland/App/ShellApp.proto.cpp
@@ -14,6 +14,7 @@ void ShellApp::init() {
u32int argc = pr.argc();
for (u32int i = 0; i < argc; i++) {
String arg = pr.argv(i);
+ if (arg.empty()) continue;
if (arg == "-") {
i++;
if (i == argc) {
@@ -26,13 +27,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 +50,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);
}
}
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 df44124..6576c1c 100644
--- a/Source/Library/Userland/Binding/VirtualTerminal.class.h
+++ b/Source/Library/Userland/Binding/VirtualTerminal.class.h
@@ -7,9 +7,10 @@
#include <Kbd.iface.h>
#include <OStream.proto.h>
+#include <IStream.proto.h>
#include <WChar.class.h>
-class VirtualTerminal : public RessourceCaller, public OStream {
+class VirtualTerminal : public RessourceCaller, public OStream, public IStream {
public:
static VirtualTerminal getIn() {
u32int id = RessourceCaller::sCall(VTIF_OBJTYPE, VTIF_SGETPRINVT);
@@ -30,6 +31,9 @@ class VirtualTerminal : public RessourceCaller, public OStream {
void write(const String &s) {
doCall(VTIF_WRITE, (u32int)&s);
}
+ String read() {
+ return String::unserialize(doCall(VTIF_READLINE, 1)) += "\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;
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; }
};