[1341] | 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 "Debug.h" |
---|
| 32 | #include "util/ExprParser.h" |
---|
| 33 | |
---|
| 34 | namespace orxonox |
---|
| 35 | { |
---|
| 36 | SetConsoleCommandShortcutExtern(source); |
---|
| 37 | SetConsoleCommandShortcutExtern(echo); |
---|
| 38 | SetConsoleCommandShortcutExtern(puts); |
---|
| 39 | |
---|
| 40 | SetConsoleCommandShortcutExtern(read); |
---|
| 41 | SetConsoleCommandShortcutExtern(append); |
---|
| 42 | SetConsoleCommandShortcutExtern(write); |
---|
| 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 | COUT(0) << text << std::endl; |
---|
| 91 | else |
---|
| 92 | COUT(0) << text; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void write(const std::string& filename, const std::string& text) |
---|
| 96 | { |
---|
| 97 | std::ofstream file; |
---|
| 98 | file.open(filename.c_str(), std::fstream::out); |
---|
| 99 | |
---|
| 100 | if (!file.is_open()) |
---|
| 101 | { |
---|
| 102 | COUT(1) << "Error: Couldn't write to file \"" << filename << "\"." << std::endl; |
---|
| 103 | return; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | file << text << std::endl; |
---|
| 107 | file.close(); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void append(const std::string& filename, const std::string& text) |
---|
| 111 | { |
---|
| 112 | std::ofstream file; |
---|
| 113 | file.open(filename.c_str(), std::fstream::app); |
---|
| 114 | |
---|
| 115 | if (!file.is_open()) |
---|
| 116 | { |
---|
| 117 | COUT(1) << "Error: Couldn't append to file \"" << filename << "\"." << std::endl; |
---|
| 118 | return; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | file << text << std::endl; |
---|
| 122 | file.close(); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | std::string read(const std::string& filename) |
---|
| 126 | { |
---|
| 127 | std::ifstream file; |
---|
| 128 | file.open(filename.c_str(), std::fstream::in); |
---|
| 129 | |
---|
| 130 | if (!file.is_open()) |
---|
| 131 | { |
---|
| 132 | COUT(1) << "Error: Couldn't read from file \"" << filename << "\"." << std::endl; |
---|
| 133 | return ""; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | std::string output = ""; |
---|
| 137 | char line[1024]; |
---|
| 138 | while (file.good() && !file.eof()) |
---|
| 139 | { |
---|
| 140 | file.getline(line, 1024); |
---|
| 141 | output += line; |
---|
| 142 | output += "\n"; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | file.close(); |
---|
| 146 | |
---|
| 147 | return output; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | float calculate(const std::string& calculation) |
---|
| 151 | { |
---|
| 152 | ExprParser expr(calculation); |
---|
| 153 | if (expr.getSuccess()) |
---|
| 154 | { |
---|
| 155 | if (expr.getResult() == 42.0) |
---|
| 156 | { |
---|
| 157 | COUT(3) << "Greetings from the restaurant at the end of the universe." << std::endl; |
---|
| 158 | } |
---|
| 159 | if (expr.getRemains() != "") |
---|
| 160 | { |
---|
| 161 | COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << "'" << std::endl; |
---|
| 162 | } |
---|
| 163 | return expr.getResult(); |
---|
| 164 | } |
---|
| 165 | else |
---|
| 166 | { |
---|
| 167 | COUT(1) << "Error: Cannot calculate expression: Parse error." << std::endl; |
---|
| 168 | return 0; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | } |
---|