forked from forefireAPI/forefire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandShell.cpp
More file actions
161 lines (131 loc) · 4.24 KB
/
CommandShell.cpp
File metadata and controls
161 lines (131 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Copyright (C) 2012 ForeFire Team, SPE, Universit� de Corse.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 US
*/
#include "CommandShell.h"
using namespace std;
namespace libforefire {
CommandShell::CommandShell() : executor() {
}
CommandShell::~CommandShell() {
}
void CommandShell::usage(const char* name){
cout << "usage: " << name << " [-i file] [-o file]" << endl;
cout << " -i: input command file" << endl;
cout << " -o: output command file" << endl;
}
int CommandShell::startShell(int argc, char* argv[]){
// temporary streams
ifstream* inputStream = 0;
// Reading the options
int fileopt;
int EOO = -1;
while((fileopt = getopt(argc, argv, "w:i:o:")) != EOO) {
if (fileopt == 'w')
{
// Web Mode
FFWebShell(optarg);
return 1;
}
else if (fileopt == 'i')
{
// Standard Mode
inputStream = new ifstream(optarg);
if ( !inputStream ){
cout << "wrong input file, check your settings..." << optarg << endl;
}
}
}
FFShell(inputStream);
delete inputStream;
return 1;
}
void CommandShell::FFWebShell(char* path)
{
executeCommandFile(path);
SimulationParameters *simParam = SimulationParameters::GetInstance();
string logFile(simParam->getParameter("caseDirectory") + "/"
+ simParam->getParameter("ForeFireDataDirectory") + "/log.ff");
cout << endl << "Log file : " << logFile << endl;
copyFileInEndOfFile(path, logFile.c_str());
emptyFile(path);
while (true)
{
executeCommandFile(path);
copyFileInEndOfFile(path, logFile.c_str());
emptyFile(path);
sleep(1);
}
}
void CommandShell::FFShell(ifstream* inputStream){
if ( inputStream ){
// reading all the commands (one command per line) of the input file
string line;
size_t numLine = 0;
string standAlone = "setParameter[runmode=standalone]";
executor.ExecuteCommand(standAlone);
while ( getline( *inputStream, line ) ) {
numLine++;
// checking for comments or newline
if((line[0] == '#')||(line[0] == '*')||(line[0] == '\n'))
continue;
// treating the command of the current line
executor.ExecuteCommand(line);
}
} else {
cout << "Welcome to the ForeFireShell" << endl;
cout << "type 'help[]' for information" << endl;
// reading all the commands (one command per line) from the terminal
string line;
size_t numLine = 0;
while ( getline( cin, line ) ) {
numLine++;
// checking for comments or newline
if((line[0] == '#')||(line[0] == '*')||(line[0] == '\n'))
continue;
// treating the command of the current line
executor.ExecuteCommand(line);
}
}
}
void CommandShell::executeCommandFile(char* path)
{
string command("include["+string(path)+"]");
fstream *src = new fstream(path, ofstream::in);
executor.ExecuteCommand(command);
src->close();
delete src;
}
void CommandShell::emptyFile(char* path)
{
fstream *src = new fstream(path, ofstream::out | ofstream::trunc);
src->close();
delete src;
}
void CommandShell::copyFileInEndOfFile(char* srcPath, const char* dstPath)
{
ofstream *dst = new ofstream(dstPath, fstream::out | fstream::app);
fstream *src = new fstream(srcPath, ofstream::in);
(*dst) << src->rdbuf();
dst->close();
src->close();
delete dst;
delete src;
}
}
int main( int argc, char* argv[] ){
using namespace libforefire;
CommandShell FFShell;
FFShell.startShell(argc, argv);
return 0;
}