Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/command/ConsoleCommandCompilation.cc @ 11183

Last change on this file since 11183 was 11099, checked in by muemart, 9 years ago

Fix loads of doxygen warnings and other documentation issues

  • Property svn:eol-style set to native
File size: 9.1 KB
Line 
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/**
30    @file
31    @brief Implementation of some console commands.
32*/
33
34#include "ConsoleCommandCompilation.h"
35
36#include <fstream>
37#include <set>
38#include <string>
39
40#include "util/Output.h"
41#include "util/ExprParser.h"
42#include "util/StringUtils.h"
43#include "ConsoleCommandIncludes.h"
44#include "CommandExecutor.h"
45
46namespace orxonox
47{
48    SetConsoleCommand("echo", echo);
49
50    SetConsoleCommand("orxout", orxout_level);
51    SetConsoleCommand("orxout_context", orxout_level_context);
52
53    SetConsoleCommand("log"    , log    );
54    SetConsoleCommand("error"  , error  ).hide();
55    SetConsoleCommand("warning", warning).hide();
56    SetConsoleCommand("status" , status ).hide();
57    SetConsoleCommand("info"   , info   ).hide();
58    SetConsoleCommand("debug"  , debug  ).hide();
59
60//    SetConsoleCommand("source", source).argumentCompleter(0, autocompletion::files());  // disabled because we use the implementation in Tcl
61//    SetConsoleCommand("read", read).argumentCompleter(0, autocompletion::files());      // disabled because we use the implementation in Tcl
62//    SetConsoleCommand("append", append).argumentCompleter(0, autocompletion::files());  // disabled because we use the implementation in Tcl
63//    SetConsoleCommand("write", write).argumentCompleter(0, autocompletion::files());    // disabled because we use the implementation in Tcl
64
65    SetConsoleCommand("calculate", calculate);
66
67    /**
68        @brief Simply returns the arguments.
69    */
70    std::string echo(const std::string& text)
71    {
72        return text;
73    }
74
75    /**
76        @brief Builds a map that maps the levels of all output levels to their ID.
77    */
78    std::map<std::string, OutputLevel> getOutputLevelsMap()
79    {
80        std::map<std::string, OutputLevel> levels;
81
82        levels["message"]          = level::message;
83        levels["debug_output"]     = level::debug_output;
84        levels["user_error"]       = level::user_error;
85        levels["user_warning"]     = level::user_warning;
86        levels["user_status"]      = level::user_status;
87        levels["user_info"]        = level::user_info;
88        levels["internal_error"]   = level::internal_error;
89        levels["internal_warning"] = level::internal_warning;
90        levels["internal_status"]  = level::internal_status;
91        levels["internal_info"]    = level::internal_info;
92        levels["verbose"]          = level::verbose;
93        levels["verbose_more"]     = level::verbose_more;
94        levels["verbose_ultra"]    = level::verbose_ultra;
95
96        return levels;
97    }
98
99    /**
100        @brief Prints text to the console.
101        @param level_name The name of the output level
102        @param text The text to print
103    */
104    void orxout_level(const std::string& level_name, const std::string& text)
105    {
106        static std::map<std::string, OutputLevel> levels = getOutputLevelsMap();
107
108        OutputLevel level = level::debug_output;
109        std::map<std::string, OutputLevel>::iterator it = levels.find(level_name);
110        if (it != levels.end())
111            level = it->second;
112        else
113            orxout(internal_warning) << "'" << level_name << "' is not a valid output level" << endl;
114
115        orxout(level) << text << endl;
116    }
117
118    /**
119        @brief Prints text to the console.
120        @param level_name The name of the output level
121        @param context_name The name of the output context
122        @param text The text to print
123    */
124    void orxout_level_context(const std::string& level_name, const std::string& context_name, const std::string& text)
125    {
126        static std::map<std::string, OutputLevel> levels = getOutputLevelsMap();
127
128        OutputLevel level = level::debug_output;
129        std::map<std::string, OutputLevel>::iterator it = levels.find(level_name);
130        if (it != levels.end())
131            level = it->second;
132        else
133            orxout(internal_warning) << "'" << level_name << "' is not a valid output level" << endl;
134
135        OutputContextContainer context = registerContext(context_name);
136
137        orxout(level, context) << text << endl;
138    }
139
140    /// @brief Prints text to the console and the logfile.
141    void log(const std::string& text)
142    { orxout() << text << endl; }
143
144    /// @brief Prints output with error level.
145    void error(const std::string& text)
146    { orxout(user_error) << text << endl; }
147
148    /// @brief Prints output with warning level.
149    void warning(const std::string& text)
150    { orxout(user_warning) << text << endl; }
151
152    /// @brief Prints output with status level.
153    void status(const std::string& text)
154    { orxout(user_status) << text << endl; }
155
156    /// @brief Prints output with info level.
157    void info(const std::string& text)
158    { orxout(user_info) << text << endl; }
159
160    /// @brief Prints debug output with verbose level.
161    void debug(const std::string& text)
162    { orxout(verbose, context::tcl) << text << endl; }
163
164    /**
165        @brief Reads the content of a file and executes the commands in it line by line.
166    */
167    void source(const std::string& filename)
168    {
169        static std::set<std::string> executingFiles;
170
171        std::set<std::string>::const_iterator it = executingFiles.find(filename);
172        if (it != executingFiles.end())
173        {
174            orxout(user_error) << "Recurring source command in \"" << filename << "\". Stopped execution." << endl;
175            return;
176        }
177
178        // Open the file
179        std::ifstream file;
180        file.open(filename.c_str(), std::fstream::in);
181
182        if (!file.is_open())
183        {
184            orxout(user_error) << "Couldn't open file \"" << filename << "\"." << endl;
185            return;
186        }
187
188        executingFiles.insert(filename);
189
190        // Iterate through the file and put the lines into the CommandExecutor
191        while (file.good() && !file.eof())
192        {
193            std::string line;
194            std::getline(file, line);
195            CommandExecutor::execute(line);
196        }
197
198        executingFiles.erase(filename);
199        file.close();
200    }
201
202    /**
203        @brief Writes text to a file.
204    */
205    void write(const std::string& filename, const std::string& text)
206    {
207        std::ofstream file;
208        file.open(filename.c_str(), std::fstream::out);
209
210        if (!file.is_open())
211        {
212            orxout(user_error) << "Couldn't write to file \"" << filename << "\"." << endl;
213            return;
214        }
215
216        file << text << endl;
217        file.close();
218    }
219
220    /**
221        @brief Appends text to a file.
222    */
223    void append(const std::string& filename, const std::string& text)
224    {
225        std::ofstream file;
226        file.open(filename.c_str(), std::fstream::app);
227
228        if (!file.is_open())
229        {
230            orxout(user_error) << "Couldn't append to file \"" << filename << "\"." << endl;
231            return;
232        }
233
234        file << text << endl;
235        file.close();
236    }
237
238    /**
239        @brief Reads text from a file
240    */
241    std::string read(const std::string& filename)
242    {
243        std::ifstream file;
244        file.open(filename.c_str(), std::fstream::in);
245
246        if (!file.is_open())
247        {
248            orxout(user_error) << "Couldn't read from file \"" << filename << "\"." << endl;
249            return "";
250        }
251
252        std::string output;
253        while (file.good() && !file.eof())
254        {
255            std::string line;
256            std::getline(file, line);
257            output += line;
258            output += "\n";
259        }
260
261        file.close();
262
263        return output;
264    }
265
266    /**
267        @brief Parses the mathematical expression and returns the result.
268    */
269    float calculate(const std::string& calculation)
270    {
271        ExprParser expr;
272        expr.parse(calculation);
273        if (expr.getSuccess())
274        {
275            if (expr.getResult() == 42.0)
276            {
277                orxout(user_info) << "Greetings from the restaurant at the end of the universe." << endl;
278            }
279            if (!expr.getRemains().empty())
280            {
281                orxout(user_warning) << "Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << endl;
282            }
283            return static_cast<float>(expr.getResult());
284        }
285        else
286        {
287            orxout(user_error) << "Cannot calculate expression: Parse error." << endl;
288            return 0;
289        }
290    }
291}
Note: See TracBrowser for help on using the repository browser.