Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lastmanstanding2/src/libraries/core/command/ConsoleCommandCompilation.cc @ 8767

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

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 5.6 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/Debug.h"
41#include "util/ExprParser.h"
42#include "util/StringUtils.h"
43#include "ConsoleCommand.h"
44#include "CommandExecutor.h"
45
46namespace orxonox
47{
48    SetConsoleCommand("source", source).argumentCompleter(0, autocompletion::files());
49    SetConsoleCommand("echo", echo);
50    SetConsoleCommand("puts", puts);
51
52    SetConsoleCommand("read", read).argumentCompleter(0, autocompletion::files());
53    SetConsoleCommand("append", append).argumentCompleter(0, autocompletion::files());
54    SetConsoleCommand("write", write).argumentCompleter(0, autocompletion::files());
55
56    SetConsoleCommand("calculate", calculate);
57
58    /**
59        @brief Reads the content of a file and executes the commands in it line by line.
60    */
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        {
87            std::string line;
88            std::getline(file, line);
89            CommandExecutor::execute(line);
90        }
91
92        executingFiles.erase(filename);
93        file.close();
94    }
95
96    /**
97        @brief Simply returns the arguments.
98    */
99    std::string echo(const std::string& text)
100    {
101        return text;
102    }
103
104    /**
105        @brief Writes text to the console, depending on the first argument with or without a line-break after it.
106    */
107    void puts(bool newline, const std::string& text)
108    {
109        if (newline)
110        {
111            COUT(0) << stripEnclosingBraces(text) << std::endl;
112        }
113        else
114        {
115            COUT(0) << stripEnclosingBraces(text);
116        }
117    }
118
119    /**
120        @brief Writes text to a file.
121    */
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
137    /**
138        @brief Appends text to a file.
139    */
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
155    /**
156        @brief Reads text from a file
157    */
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
169        std::string output;
170        while (file.good() && !file.eof())
171        {
172            std::string line;
173            std::getline(file, line);
174            output += line;
175            output += "\n";
176        }
177
178        file.close();
179
180        return output;
181    }
182
183    /**
184        @brief Parses the mathematical expression and returns the result.
185    */
186    float calculate(const std::string& calculation)
187    {
188        ExprParser expr;
189        expr.parse(calculation);
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            }
196            if (!expr.getRemains().empty())
197            {
198                COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << std::endl;
199            }
200            return static_cast<float>(expr.getResult());
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.