summaryrefslogtreecommitdiff
path: root/Source/Library
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-12-20 16:30:30 +0100
committerAlexis211 <alexis211@gmail.com>2009-12-20 16:30:30 +0100
commit13d720389a01161a327a30918ad7ac9eec4a3d6c (patch)
treee1b6ebb12f00e476284684c8deb4a513c4960e2a /Source/Library
parent2d3c5a9c47d99c8f4f5561f9eae16497c1cde63a (diff)
downloadMelon-13d720389a01161a327a30918ad7ac9eec4a3d6c.tar.gz
Melon-13d720389a01161a327a30918ad7ac9eec4a3d6c.zip
[not tested] Introduced StreamApp.class
Diffstat (limited to 'Source/Library')
-rw-r--r--Source/Library/Common/FileStream.class.cpp13
-rw-r--r--Source/Library/Common/FileStream.class.h2
-rw-r--r--Source/Library/Makefile1
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.cpp2
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.h2
-rw-r--r--Source/Library/Userland/App/StreamApp.proto.cpp39
-rw-r--r--Source/Library/Userland/App/StreamApp.proto.h27
7 files changed, 78 insertions, 8 deletions
diff --git a/Source/Library/Common/FileStream.class.cpp b/Source/Library/Common/FileStream.class.cpp
index 6154b2f..322e9aa 100644
--- a/Source/Library/Common/FileStream.class.cpp
+++ b/Source/Library/Common/FileStream.class.cpp
@@ -4,11 +4,16 @@
// INPUT FILE STREAM
// ********************
-FileIStream::FileIStream(const String &filename, u8int encoding, FSNode start) {
+FileIStream::FileIStream(const String &filename, u8int encoding, FSNode start) : m_start(start) {
m_file = new File(filename, FM_READ, start);
m_encoding = encoding;
}
+FileIStream::FileIStream(u8int encoding, FSNode start) : m_start(start) {
+ m_file = 0;
+ m_encoding = encoding;
+}
+
FileIStream::~FileIStream() {
if (m_file != 0) {
m_file->close();
@@ -33,11 +38,7 @@ String FileIStream::read() {
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_file = new File(m_filenames->v(), FM_READ, m_start);
m_filenames = m_filenames->delThis();
}
}
diff --git a/Source/Library/Common/FileStream.class.h b/Source/Library/Common/FileStream.class.h
index d0fb971..99bf0e8 100644
--- a/Source/Library/Common/FileStream.class.h
+++ b/Source/Library/Common/FileStream.class.h
@@ -18,9 +18,11 @@ class FileIStream : public IStream {
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);
diff --git a/Source/Library/Makefile b/Source/Library/Makefile
index 83e3689..c67daf7 100644
--- a/Source/Library/Makefile
+++ b/Source/Library/Makefile
@@ -25,6 +25,7 @@ Objects = Common/WChar.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 9528ca2..840e985 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);
diff --git a/Source/Library/Userland/App/ShellApp.proto.h b/Source/Library/Userland/App/ShellApp.proto.h
index 2308fd1..bc57cd0 100644
--- a/Source/Library/Userland/App/ShellApp.proto.h
+++ b/Source/Library/Userland/App/ShellApp.proto.h
@@ -23,7 +23,7 @@ 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..97f473e
--- /dev/null
+++ b/Source/Library/Userland/App/StreamApp.proto.cpp
@@ -0,0 +1,39 @@
+#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");
+}
+
+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"), 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