Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/rocket2/src/libraries/core/ConsoleCommandCompilation.cc @ 7129

Last change on this file since 7129 was 6417, checked in by rgrieder, 15 years ago

Merged presentation2 branch back to trunk.
Major new features:

  • Actual GUI with settings, etc.
  • Improved space ship steering (human interaction)
  • Rocket fire and more particle effects
  • Advanced sound framework
  • Property svn:eol-style set to native
File size: 5.0 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#include "ConsoleCommandCompilation.h"
30
31#include <fstream>
32#include <set>
33#include <string>
34
35#include "util/Debug.h"
36#include "util/ExprParser.h"
37#include "util/StringUtils.h"
38#include "ConsoleCommand.h"
39
40namespace orxonox
41{
42    SetConsoleCommandShortcutExtern(source).argumentCompleter(0, autocompletion::files());
43    SetConsoleCommandShortcutExtern(echo);
44    SetConsoleCommandShortcutExtern(puts);
45
46    SetConsoleCommandShortcutExtern(read).argumentCompleter(0, autocompletion::files());
47    SetConsoleCommandShortcutExtern(append).argumentCompleter(0, autocompletion::files());
48    SetConsoleCommandShortcutExtern(write).argumentCompleter(0, autocompletion::files());
49
50    SetConsoleCommandShortcutExtern(calculate);
51
52    void source(const std::string& filename)
53    {
54        static std::set<std::string> executingFiles;
55
56        std::set<std::string>::const_iterator it = executingFiles.find(filename);
57        if (it != executingFiles.end())
58        {
59            COUT(1) << "Error: Recurring source command in \"" << filename << "\". Stopped execution." << std::endl;
60            return;
61        }
62
63        // Open the file
64        std::ifstream file;
65        file.open(filename.c_str(), std::fstream::in);
66
67        if (!file.is_open())
68        {
69            COUT(1) << "Error: Couldn't open file \"" << filename << "\"." << std::endl;
70            return;
71        }
72
73        executingFiles.insert(filename);
74
75        // Iterate through the file and put the lines into the CommandExecutor
76        while (file.good() && !file.eof())
77        {
78            std::string line;
79            std::getline(file, line);
80            CommandExecutor::execute(line);
81        }
82
83        executingFiles.erase(filename);
84        file.close();
85    }
86
87    std::string echo(const std::string& text)
88    {
89        return text;
90    }
91
92    void puts(bool newline, const std::string& text)
93    {
94        if (newline)
95        {
96            COUT(0) << stripEnclosingBraces(text) << std::endl;
97        }
98        else
99        {
100            COUT(0) << stripEnclosingBraces(text);
101        }
102    }
103
104    void write(const std::string& filename, const std::string& text)
105    {
106        std::ofstream file;
107        file.open(filename.c_str(), std::fstream::out);
108
109        if (!file.is_open())
110        {
111            COUT(1) << "Error: Couldn't write to file \"" << filename << "\"." << std::endl;
112            return;
113        }
114
115        file << text << std::endl;
116        file.close();
117    }
118
119    void append(const std::string& filename, const std::string& text)
120    {
121        std::ofstream file;
122        file.open(filename.c_str(), std::fstream::app);
123
124        if (!file.is_open())
125        {
126            COUT(1) << "Error: Couldn't append to file \"" << filename << "\"." << std::endl;
127            return;
128        }
129
130        file << text << std::endl;
131        file.close();
132    }
133
134    std::string read(const std::string& filename)
135    {
136        std::ifstream file;
137        file.open(filename.c_str(), std::fstream::in);
138
139        if (!file.is_open())
140        {
141            COUT(1) << "Error: Couldn't read from file \"" << filename << "\"." << std::endl;
142            return "";
143        }
144
145        std::string output;
146        while (file.good() && !file.eof())
147        {
148            std::string line;
149            std::getline(file, line);
150            output += line;
151            output += "\n";
152        }
153
154        file.close();
155
156        return output;
157    }
158
159    float calculate(const std::string& calculation)
160    {
161        ExprParser expr;
162        expr.parse(calculation);
163        if (expr.getSuccess())
164        {
165            if (expr.getResult() == 42.0)
166            {
167                COUT(3) << "Greetings from the restaurant at the end of the universe." << std::endl;
168            }
169            if (!expr.getRemains().empty())
170            {
171                COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << std::endl;
172            }
173            return static_cast<float>(expr.getResult());
174        }
175        else
176        {
177            COUT(1) << "Error: Cannot calculate expression: Parse error." << std::endl;
178            return 0;
179        }
180    }
181}
Note: See TracBrowser for help on using the repository browser.