Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/command/ConsoleCommandCompilation.cc @ 7352

Last change on this file since 7352 was 7352, checked in by landauf, 14 years ago

added documentation

also some small naming and readability changes in the code.

  • Property svn:eol-style set to native
File size: 5.6 KB
RevLine 
[1505]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
[7352]29/**
30    @file
31    @brief Implementation of some console commands.
32*/
33
[1505]34#include "ConsoleCommandCompilation.h"
[3196]35
36#include <fstream>
37#include <set>
38#include <string>
39
[1747]40#include "util/Debug.h"
[1505]41#include "util/ExprParser.h"
[6417]42#include "util/StringUtils.h"
[3196]43#include "ConsoleCommand.h"
[7219]44#include "CommandExecutor.h"
[1505]45
46namespace orxonox
47{
[7236]48    SetConsoleCommand("source", source).argumentCompleter(0, autocompletion::files());
49    SetConsoleCommand("echo", echo);
50    SetConsoleCommand("puts", puts);
[1505]51
[7236]52    SetConsoleCommand("read", read).argumentCompleter(0, autocompletion::files());
53    SetConsoleCommand("append", append).argumentCompleter(0, autocompletion::files());
54    SetConsoleCommand("write", write).argumentCompleter(0, autocompletion::files());
[1505]55
[7236]56    SetConsoleCommand("calculate", calculate);
[1505]57
[7352]58    /**
59        @brief Reads the content of a file and executes the commands in it line by line.
60    */
[1505]61    void source(const std::string& filename)
62    {
63        static std::set<std::string> executingFiles;
64
65        std::set<std::string>::const_iterator it = executingFiles.find(filename);
66        if (it != executingFiles.end())
67        {
68            COUT(1) << "Error: Recurring source command in \"" << filename << "\". Stopped execution." << std::endl;
69            return;
70        }
71
72        // Open the file
73        std::ifstream file;
74        file.open(filename.c_str(), std::fstream::in);
75
76        if (!file.is_open())
77        {
78            COUT(1) << "Error: Couldn't open file \"" << filename << "\"." << std::endl;
79            return;
80        }
81
82        executingFiles.insert(filename);
83
84        // Iterate through the file and put the lines into the CommandExecutor
85        while (file.good() && !file.eof())
86        {
[3198]87            std::string line;
88            std::getline(file, line);
[1505]89            CommandExecutor::execute(line);
90        }
91
92        executingFiles.erase(filename);
93        file.close();
94    }
95
[7352]96    /**
97        @brief Simply returns the arguments.
98    */
[1505]99    std::string echo(const std::string& text)
100    {
101        return text;
102    }
103
[7352]104    /**
105        @brief Writes text to the console, depending on the first argument with or without a line-break after it.
106    */
[1505]107    void puts(bool newline, const std::string& text)
108    {
109        if (newline)
110        {
[3370]111            COUT(0) << stripEnclosingBraces(text) << std::endl;
[1505]112        }
113        else
114        {
[3370]115            COUT(0) << stripEnclosingBraces(text);
[1505]116        }
117    }
118
[7352]119    /**
120        @brief Writes text to a file.
121    */
[1505]122    void write(const std::string& filename, const std::string& text)
123    {
124        std::ofstream file;
125        file.open(filename.c_str(), std::fstream::out);
126
127        if (!file.is_open())
128        {
129            COUT(1) << "Error: Couldn't write to file \"" << filename << "\"." << std::endl;
130            return;
131        }
132
133        file << text << std::endl;
134        file.close();
135    }
136
[7352]137    /**
138        @brief Appends text to a file.
139    */
[1505]140    void append(const std::string& filename, const std::string& text)
141    {
142        std::ofstream file;
143        file.open(filename.c_str(), std::fstream::app);
144
145        if (!file.is_open())
146        {
147            COUT(1) << "Error: Couldn't append to file \"" << filename << "\"." << std::endl;
148            return;
149        }
150
151        file << text << std::endl;
152        file.close();
153    }
154
[7352]155    /**
156        @brief Reads text from a file
157    */
[1505]158    std::string read(const std::string& filename)
159    {
160        std::ifstream file;
161        file.open(filename.c_str(), std::fstream::in);
162
163        if (!file.is_open())
164        {
165            COUT(1) << "Error: Couldn't read from file \"" << filename << "\"." << std::endl;
166            return "";
167        }
168
[6417]169        std::string output;
[1505]170        while (file.good() && !file.eof())
171        {
[3198]172            std::string line;
173            std::getline(file, line);
[1505]174            output += line;
175            output += "\n";
176        }
177
178        file.close();
179
180        return output;
181    }
182
[7352]183    /**
184        @brief Parses the mathematical expression and returns the result.
185    */
[1505]186    float calculate(const std::string& calculation)
187    {
[6417]188        ExprParser expr;
189        expr.parse(calculation);
[1505]190        if (expr.getSuccess())
191        {
192            if (expr.getResult() == 42.0)
193            {
194                COUT(3) << "Greetings from the restaurant at the end of the universe." << std::endl;
195            }
[6417]196            if (!expr.getRemains().empty())
[1505]197            {
[6417]198                COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << std::endl;
[1505]199            }
[3196]200            return static_cast<float>(expr.getResult());
[1505]201        }
202        else
203        {
204            COUT(1) << "Error: Cannot calculate expression: Parse error." << std::endl;
205            return 0;
206        }
207    }
208}
Note: See TracBrowser for help on using the repository browser.