Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6373 was 6105, checked in by rgrieder, 15 years ago

Merged console branch back to trunk.

  • Property svn:eol-style set to native
File size: 5.0 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
29#include "ConsoleCommandCompilation.h"
[3196]30
31#include <fstream>
32#include <set>
33#include <string>
34
[1747]35#include "util/Debug.h"
[1505]36#include "util/ExprParser.h"
[3196]37#include "ConsoleCommand.h"
[1505]38
39namespace orxonox
40{
[1747]41    SetConsoleCommandShortcutExtern(source).argumentCompleter(0, autocompletion::files());
[1505]42    SetConsoleCommandShortcutExtern(echo);
43    SetConsoleCommandShortcutExtern(puts);
44
[1747]45    SetConsoleCommandShortcutExtern(read).argumentCompleter(0, autocompletion::files());
46    SetConsoleCommandShortcutExtern(append).argumentCompleter(0, autocompletion::files());
47    SetConsoleCommandShortcutExtern(write).argumentCompleter(0, autocompletion::files());
[1505]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        {
[3198]77            std::string line;
78            std::getline(file, line);
[1505]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        return text;
89    }
90
91    void puts(bool newline, const std::string& text)
92    {
93        if (newline)
94        {
[3370]95            COUT(0) << stripEnclosingBraces(text) << std::endl;
[1505]96        }
97        else
98        {
[3370]99            COUT(0) << stripEnclosingBraces(text);
[1505]100        }
101    }
102
103    void write(const std::string& filename, const std::string& text)
104    {
105        std::ofstream file;
106        file.open(filename.c_str(), std::fstream::out);
107
108        if (!file.is_open())
109        {
110            COUT(1) << "Error: Couldn't write to file \"" << filename << "\"." << std::endl;
111            return;
112        }
113
114        file << text << std::endl;
115        file.close();
116    }
117
118    void append(const std::string& filename, const std::string& text)
119    {
120        std::ofstream file;
121        file.open(filename.c_str(), std::fstream::app);
122
123        if (!file.is_open())
124        {
125            COUT(1) << "Error: Couldn't append to file \"" << filename << "\"." << std::endl;
126            return;
127        }
128
129        file << text << std::endl;
130        file.close();
131    }
132
133    std::string read(const std::string& filename)
134    {
135        std::ifstream file;
136        file.open(filename.c_str(), std::fstream::in);
137
138        if (!file.is_open())
139        {
140            COUT(1) << "Error: Couldn't read from file \"" << filename << "\"." << std::endl;
141            return "";
142        }
143
144        std::string output = "";
145        while (file.good() && !file.eof())
146        {
[3198]147            std::string line;
148            std::getline(file, line);
[1505]149            output += line;
150            output += "\n";
151        }
152
153        file.close();
154
155        return output;
156    }
157
158    float calculate(const std::string& calculation)
159    {
160        ExprParser expr(calculation);
161        if (expr.getSuccess())
162        {
163            if (expr.getResult() == 42.0)
164            {
165                COUT(3) << "Greetings from the restaurant at the end of the universe." << std::endl;
166            }
167            if (expr.getRemains() != "")
168            {
169                COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << "'" << std::endl;
170            }
[3196]171            return static_cast<float>(expr.getResult());
[1505]172        }
173        else
174        {
175            COUT(1) << "Error: Cannot calculate expression: Parse error." << std::endl;
176            return 0;
177        }
178    }
179}
Note: See TracBrowser for help on using the repository browser.