From 13d720389a01161a327a30918ad7ac9eec4a3d6c Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Sun, 20 Dec 2009 16:30:30 +0100 Subject: [not tested] Introduced StreamApp.class --- Source/Library/Userland/App/ShellApp.proto.cpp | 2 +- Source/Library/Userland/App/ShellApp.proto.h | 2 +- Source/Library/Userland/App/StreamApp.proto.cpp | 39 +++++++++++++++++++++++++ Source/Library/Userland/App/StreamApp.proto.h | 27 +++++++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 Source/Library/Userland/App/StreamApp.proto.cpp create mode 100644 Source/Library/Userland/App/StreamApp.proto.h (limited to 'Source/Library/Userland') 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 args; Vector 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 + +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 +#include + +/* + * 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 -- cgit v1.2.3