Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/ConsoleCommandCompilation.cc @ 3222

Last change on this file since 3222 was 3198, checked in by scheusso, 15 years ago

merged netp4 back to trunk

  • 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 "ConsoleCommand.h"
38
39namespace orxonox
40{
41    SetConsoleCommandShortcutExtern(source).argumentCompleter(0, autocompletion::files());
42    SetConsoleCommandShortcutExtern(echo);
43    SetConsoleCommandShortcutExtern(puts);
44
45    SetConsoleCommandShortcutExtern(read).argumentCompleter(0, autocompletion::files());
46    SetConsoleCommandShortcutExtern(append).argumentCompleter(0, autocompletion::files());
47    SetConsoleCommandShortcutExtern(write).argumentCompleter(0, autocompletion::files());
48
49    SetConsoleCommandShortcutExtern(calculate);
50
51    void source(const std::string& filename)
52    {
53        static std::set<std::string> executingFiles;
54
55        std::set<std::string>::const_iterator it = executingFiles.find(filename);
56        if (it != executingFiles.end())
57        {
58            COUT(1) << "Error: Recurring source command in \"" << filename << "\". Stopped execution." << std::endl;
59            return;
60        }
61
62        // Open the file
63        std::ifstream file;
64        file.open(filename.c_str(), std::fstream::in);
65
66        if (!file.is_open())
67        {
68            COUT(1) << "Error: Couldn't open file \"" << filename << "\"." << std::endl;
69            return;
70        }
71
72        executingFiles.insert(filename);
73
74        // Iterate through the file and put the lines into the CommandExecutor
75        while (file.good() && !file.eof())
76        {
77            std::string line;
78            std::getline(file, line);
79            CommandExecutor::execute(line);
80        }
81
82        executingFiles.erase(filename);
83        file.close();
84    }
85
86    std::string echo(const std::string& text)
87    {
88        std::cout << text << std::endl;
89        return text;
90    }
91
92    void puts(bool newline, const std::string& text)
93    {
94        if (newline)
95        {
96            COUT(0) << text << std::endl;
97        }
98        else
99        {
100            COUT(0) << 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(calculation);
162        if (expr.getSuccess())
163        {
164            if (expr.getResult() == 42.0)
165            {
166                COUT(3) << "Greetings from the restaurant at the end of the universe." << std::endl;
167            }
168            if (expr.getRemains() != "")
169            {
170                COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << "'" << std::endl;
171            }
172            return static_cast<float>(expr.getResult());
173        }
174        else
175        {
176            COUT(1) << "Error: Cannot calculate expression: Parse error." << std::endl;
177            return 0;
178        }
179    }
180}
Note: See TracBrowser for help on using the repository browser.