| 1 | /* | 
|---|
| 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 | *                    > www.orxonox.net < | 
|---|
| 4 | * | 
|---|
| 5 | * | 
|---|
| 6 | *   License notice: | 
|---|
| 7 | * | 
|---|
| 8 | *   This program is free software; you can redistribute it and/or | 
|---|
| 9 | *   modify it under the terms of the GNU General Public License | 
|---|
| 10 | *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 | *   of the License, or (at your option) any later version. | 
|---|
| 12 | * | 
|---|
| 13 | *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | *   GNU General Public License for more details. | 
|---|
| 17 | * | 
|---|
| 18 | *   You should have received a copy of the GNU General Public License | 
|---|
| 19 | *   along with this program; if not, write to the Free Software | 
|---|
| 20 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 | * | 
|---|
| 22 | *   Author: | 
|---|
| 23 | *      Fabian 'x3n' Landau | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "ConsoleCommandCompilation.h" | 
|---|
| 30 | #include "ConsoleCommand.h" | 
|---|
| 31 | #include "util/Debug.h" | 
|---|
| 32 | #include "util/ExprParser.h" | 
|---|
| 33 |  | 
|---|
| 34 | namespace orxonox | 
|---|
| 35 | { | 
|---|
| 36 | SetConsoleCommandShortcutExtern(source).argumentCompleter(0, autocompletion::files()); | 
|---|
| 37 | SetConsoleCommandShortcutExtern(echo); | 
|---|
| 38 | SetConsoleCommandShortcutExtern(puts); | 
|---|
| 39 |  | 
|---|
| 40 | SetConsoleCommandShortcutExtern(read).argumentCompleter(0, autocompletion::files()); | 
|---|
| 41 | SetConsoleCommandShortcutExtern(append).argumentCompleter(0, autocompletion::files()); | 
|---|
| 42 | SetConsoleCommandShortcutExtern(write).argumentCompleter(0, autocompletion::files()); | 
|---|
| 43 |  | 
|---|
| 44 | SetConsoleCommandShortcutExtern(calculate); | 
|---|
| 45 |  | 
|---|
| 46 | void source(const std::string& filename) | 
|---|
| 47 | { | 
|---|
| 48 | static std::set<std::string> executingFiles; | 
|---|
| 49 |  | 
|---|
| 50 | std::set<std::string>::const_iterator it = executingFiles.find(filename); | 
|---|
| 51 | if (it != executingFiles.end()) | 
|---|
| 52 | { | 
|---|
| 53 | COUT(1) << "Error: Recurring source command in \"" << filename << "\". Stopped execution." << std::endl; | 
|---|
| 54 | return; | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | // Open the file | 
|---|
| 58 | std::ifstream file; | 
|---|
| 59 | file.open(filename.c_str(), std::fstream::in); | 
|---|
| 60 |  | 
|---|
| 61 | if (!file.is_open()) | 
|---|
| 62 | { | 
|---|
| 63 | COUT(1) << "Error: Couldn't open file \"" << filename << "\"." << std::endl; | 
|---|
| 64 | return; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | executingFiles.insert(filename); | 
|---|
| 68 |  | 
|---|
| 69 | // Iterate through the file and put the lines into the CommandExecutor | 
|---|
| 70 | char line[1024]; | 
|---|
| 71 | while (file.good() && !file.eof()) | 
|---|
| 72 | { | 
|---|
| 73 | file.getline(line, 1024); | 
|---|
| 74 | CommandExecutor::execute(line); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | executingFiles.erase(filename); | 
|---|
| 78 | file.close(); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | std::string echo(const std::string& text) | 
|---|
| 82 | { | 
|---|
| 83 | std::cout << text << std::endl; | 
|---|
| 84 | return text; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | void puts(bool newline, const std::string& text) | 
|---|
| 88 | { | 
|---|
| 89 | if (newline) | 
|---|
| 90 | { | 
|---|
| 91 | COUT(0) << text << std::endl; | 
|---|
| 92 | } | 
|---|
| 93 | else | 
|---|
| 94 | { | 
|---|
| 95 | COUT(0) << text; | 
|---|
| 96 | } | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void write(const std::string& filename, const std::string& text) | 
|---|
| 100 | { | 
|---|
| 101 | std::ofstream file; | 
|---|
| 102 | file.open(filename.c_str(), std::fstream::out); | 
|---|
| 103 |  | 
|---|
| 104 | if (!file.is_open()) | 
|---|
| 105 | { | 
|---|
| 106 | COUT(1) << "Error: Couldn't write to file \"" << filename << "\"." << std::endl; | 
|---|
| 107 | return; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | file << text << std::endl; | 
|---|
| 111 | file.close(); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | void append(const std::string& filename, const std::string& text) | 
|---|
| 115 | { | 
|---|
| 116 | std::ofstream file; | 
|---|
| 117 | file.open(filename.c_str(), std::fstream::app); | 
|---|
| 118 |  | 
|---|
| 119 | if (!file.is_open()) | 
|---|
| 120 | { | 
|---|
| 121 | COUT(1) << "Error: Couldn't append to file \"" << filename << "\"." << std::endl; | 
|---|
| 122 | return; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | file << text << std::endl; | 
|---|
| 126 | file.close(); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | std::string read(const std::string& filename) | 
|---|
| 130 | { | 
|---|
| 131 | std::ifstream file; | 
|---|
| 132 | file.open(filename.c_str(), std::fstream::in); | 
|---|
| 133 |  | 
|---|
| 134 | if (!file.is_open()) | 
|---|
| 135 | { | 
|---|
| 136 | COUT(1) << "Error: Couldn't read from file \"" << filename << "\"." << std::endl; | 
|---|
| 137 | return ""; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | std::string output = ""; | 
|---|
| 141 | char line[1024]; | 
|---|
| 142 | while (file.good() && !file.eof()) | 
|---|
| 143 | { | 
|---|
| 144 | file.getline(line, 1024); | 
|---|
| 145 | output += line; | 
|---|
| 146 | output += "\n"; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | file.close(); | 
|---|
| 150 |  | 
|---|
| 151 | return output; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | float calculate(const std::string& calculation) | 
|---|
| 155 | { | 
|---|
| 156 | ExprParser expr(calculation); | 
|---|
| 157 | if (expr.getSuccess()) | 
|---|
| 158 | { | 
|---|
| 159 | if (expr.getResult() == 42.0) | 
|---|
| 160 | { | 
|---|
| 161 | COUT(3) << "Greetings from the restaurant at the end of the universe." << std::endl; | 
|---|
| 162 | } | 
|---|
| 163 | if (expr.getRemains() != "") | 
|---|
| 164 | { | 
|---|
| 165 | COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << "'" << std::endl; | 
|---|
| 166 | } | 
|---|
| 167 | return expr.getResult(); | 
|---|
| 168 | } | 
|---|
| 169 | else | 
|---|
| 170 | { | 
|---|
| 171 | COUT(1) << "Error: Cannot calculate expression: Parse error." << std::endl; | 
|---|
| 172 | return 0; | 
|---|
| 173 | } | 
|---|
| 174 | } | 
|---|
| 175 | } | 
|---|