summaryrefslogtreecommitdiffhomepage
path: root/FoundationEx
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-04-02 18:34:06 +0200
committerAki <please@ignore.pl>2022-04-02 18:34:06 +0200
commitd5fe6f541817105955a0cf48331e564c5e2d5692 (patch)
tree382fcb70fcd70fa6700b974311ee74ac6677b6f5 /FoundationEx
parent814335e5ce06058c0f8ea715a715eb08187d3755 (diff)
downloadstarshatter-d5fe6f541817105955a0cf48331e564c5e2d5692.zip
starshatter-d5fe6f541817105955a0cf48331e564c5e2d5692.tar.gz
starshatter-d5fe6f541817105955a0cf48331e564c5e2d5692.tar.bz2
Moved Reader to FoundationEx
Diffstat (limited to 'FoundationEx')
-rw-r--r--FoundationEx/CMakeLists.txt1
-rw-r--r--FoundationEx/Reader.cpp109
-rw-r--r--FoundationEx/Reader.h67
3 files changed, 177 insertions, 0 deletions
diff --git a/FoundationEx/CMakeLists.txt b/FoundationEx/CMakeLists.txt
index 497c732..71dd25d 100644
--- a/FoundationEx/CMakeLists.txt
+++ b/FoundationEx/CMakeLists.txt
@@ -2,6 +2,7 @@ project(FoundationEx)
add_library(
FoundationEx
STATIC
+ Reader.cpp
Text.cpp
Utils.cpp
)
diff --git a/FoundationEx/Reader.cpp b/FoundationEx/Reader.cpp
new file mode 100644
index 0000000..682627d
--- /dev/null
+++ b/FoundationEx/Reader.cpp
@@ -0,0 +1,109 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+
+ AUTHOR: John DiCamillo
+
+
+ OVERVIEW
+ ========
+ Implementation of the Reader class
+*/
+
+#include "Reader.h"
+#include <stdio.h>
+#include <fstream>
+#include <ctype.h>
+#include "Utils.h"
+
+// +-------------------------------------------------------------------+
+
+Text
+ConsoleReader::more()
+{
+ // loop until the user types something
+ do {
+ printPrimaryPrompt();
+ fillInputBuffer();
+ } while (! *p);
+
+ return Text(p);
+}
+
+void
+ConsoleReader::printPrimaryPrompt()
+{
+ printf("- ");
+}
+
+void
+ConsoleReader::fillInputBuffer()
+{
+ fgets(buffer, 980, stdin);
+ p = buffer;
+ while (isspace(*p)) p++;
+}
+
+// +-------------------------------------------------------------------+
+
+FileReader::FileReader(const char* fname)
+ : filename(fname), done(0)
+{ }
+
+Text
+FileReader::more()
+{
+ if (done) return Text();
+
+ std::fstream fin(filename, std::fstream::in);
+
+ if (!fin) {
+ Print("ERROR(Parse): Could not open file '%s'\n", filename.data());
+ return Text();
+ }
+
+ Text result;
+ char buf[1000], newline;
+
+ while (fin.get(buf, 1000)) {
+ result.append(buf);
+ fin.get(newline);
+ result.append(newline);
+ }
+
+ done = 1;
+ return result;
+}
+
+// +-------------------------------------------------------------------+
+
+BlockReader::BlockReader(const char* block)
+ : data((char*) block), done(0), length(0)
+{ }
+
+BlockReader::BlockReader(const char* block, int len)
+ : data((char*) block), done(0), length(len)
+{ }
+
+Text
+BlockReader::more()
+{
+ if (done) return Text();
+
+ if (length) {
+ Text result(data, length);
+ done = 1;
+ return result;
+ }
+ else if (data) {
+ Text result(data);
+ done = 1;
+ return result;
+ }
+
+ done = 1;
+ return Text();
+}
+
+
diff --git a/FoundationEx/Reader.h b/FoundationEx/Reader.h
new file mode 100644
index 0000000..e1154d1
--- /dev/null
+++ b/FoundationEx/Reader.h
@@ -0,0 +1,67 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+
+ AUTHOR: John DiCamillo
+
+
+ OVERVIEW
+ ========
+ Declaration of the Reader class
+*/
+
+#ifndef READER_H
+#define READER_H
+
+#include "Text.h"
+
+// +-------------------------------------------------------------------+
+
+class Reader
+{
+public:
+ Reader() { }
+ virtual ~Reader() { }
+
+ virtual Text more() = 0;
+};
+
+class ConsoleReader : public Reader
+{
+public:
+ virtual Text more();
+
+ void printPrimaryPrompt();
+ void fillInputBuffer();
+
+private:
+ char buffer[1000];
+ char* p;
+};
+
+class FileReader : public Reader
+{
+public:
+ FileReader(const char* fname);
+ virtual Text more();
+
+private:
+ Text filename;
+ int done;
+};
+
+class BlockReader : public Reader
+{
+public:
+ BlockReader(const char* block);
+ BlockReader(const char* block, int len);
+ virtual Text more();
+
+private:
+ char* data;
+ int done;
+ int length;
+};
+
+#endif