From 3c487c5cd69c53d6fea948643c0a76df03516605 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 1 Apr 2022 21:23:39 +0200 Subject: Moved Stars45 to StarsEx --- StarsEx/Reader.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 StarsEx/Reader.cpp (limited to 'StarsEx/Reader.cpp') diff --git a/StarsEx/Reader.cpp b/StarsEx/Reader.cpp new file mode 100644 index 0000000..682627d --- /dev/null +++ b/StarsEx/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 +#include +#include +#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(); +} + + -- cgit v1.1