diff options
Diffstat (limited to 'hscript/script.cc')
-rw-r--r-- | hscript/script.cc | 74 |
1 files changed, 55 insertions, 19 deletions
diff --git a/hscript/script.cc b/hscript/script.cc index 748b09a..a9c39ab 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -18,6 +18,10 @@ #include "network.hh" #include "user.hh" +#include "util/output.hh" + +#define LINE_MAX 512 + namespace Horizon { struct Script::ScriptPrivate { @@ -34,35 +38,67 @@ struct Script::ScriptPrivate { }; Script::Script() { + internal = new ScriptPrivate; } const Script *Script::load(std::string path, ScriptOptions opts) { - std::ifstream file; - std::string maybe_error; - - file.exceptions(std::ios::badbit); - try { - file.open(path); - } catch(const std::ios::failure &error) { - maybe_error = error.what(); - } catch(const std::exception &error) { - maybe_error = error.what(); - } + std::ifstream file(path); if(!file) { - std::cerr << "Cannot open installfile at \"" << path << "\": "; - std::cerr << maybe_error; - std::cerr << std::endl; + output_error(path, "Cannot open installfile", "", + (opts.test(Pretty))); return nullptr; } return Script::load(file, opts); } -const Script *Script::load(std::istream &, ScriptOptions) { - Script *foo = new Script; - delete foo; - std::cout << "loaded" << std::endl; - return nullptr; +const Script *Script::load(std::istream &sstream, ScriptOptions opts) { + Script *the_script = new Script; + + int lineno = 0; + char nextline[LINE_MAX]; + const std::string delim(" \t"); + + while(sstream.getline(nextline, sizeof(nextline))) { + lineno++; + if(nextline[0] == '#') { + /* This is a comment line; ignore it. */ + continue; + } + + const std::string line(nextline); + std::string::size_type start, key_end; + start = line.find_first_not_of(delim); + if(start == std::string::npos) { + /* This is a blank line; ignore it. */ + continue; + } + + key_end = line.find_first_of(delim, start); + if(key_end == std::string::npos) { + /* Key without value */ + output_error("installfile:" + std::to_string(lineno), + "key '" + line.substr(start) + "' has no value", + "", (opts.test(Pretty))); + } + } + + if(sstream.fail() && !sstream.eof()) { + output_error("installfile:" + std::to_string(lineno + 1), + "line exceeds maximum length", + "Maximum length for line is " + std::to_string(LINE_MAX), + (opts.test(Pretty))); + delete the_script; + return nullptr; + } + + if(sstream.bad() && !sstream.eof()) { + output_error("installfile:" + std::to_string(lineno), + "I/O error reading installfile", "", + (opts.test(Pretty))); + } + + return the_script; } bool Script::validate() { |