Index: ChangeLog
from Benoît Perrot <benoit(a)lrde.epita.fr>
* src/shell/shell.hh, src/shell.cc: Normalize string stream use.
2004-07-18 Benoît Perrot <benoit(a)lrde.epita.fr>
Index: src/shell/shell.hh
--- src/shell/shell.hh (revision 116)
+++ src/shell/shell.hh (working copy)
@@ -23,6 +23,8 @@
#ifndef SHELL_SHELL_HH
# define SHELL_SHELL_HH
+# include <ctype.h>
+
# include <list>
# include <map>
# include <string>
@@ -163,48 +165,46 @@
return true;
}
+ /// Skip blank characters
+ void skip_blank(std::istringstream &iss)
+ {
+ while (isblank(iss.peek()))
+ iss.get();
+ }
+
/// Get the next word in the command line.
- std::string get_next_word()
+ std::string get_next_word(std::istringstream &iss)
{
- std::stringstream strb;
+ skip_blank(iss);
- while (cmd_buf_[pos_] == ' ')
- ++pos_;
- while (cmd_buf_[pos_] && (cmd_buf_[pos_] != ' '))
- strb << cmd_buf_[pos_++];
- if ((strb.str().length() == 0) && (cmd_buf_[pos_] == 0))
- return std::string(); // FIXME: awkward!
- if (cmd_buf_[pos_] != 0)
- ++pos_;
- return std::string(strb.str());
+ std::string res;
+ while (!isblank(iss.peek()))
+ {
+ char c = static_cast<char>(iss.get());
+ if (iss.eof())
+ break;
+ res += c;
+ }
+ return res;
}
/// Eat a word in the command line and return the corresponding keyword.
- shell_token_type eat_word()
+ shell_token_type eat_word(std::istringstream &iss)
{
- std::stringstream strb;
-
- while (cmd_buf_[pos_] == ' ')
- ++pos_;
- while (cmd_buf_[pos_] && (cmd_buf_[pos_] != ' '))
- strb << cmd_buf_[pos_++];
- if ((strb.str().length() == 0) && (cmd_buf_[pos_] == 0))
+ std::string cmd = get_next_word(iss);
+ if (cmd.empty())
return EOL;
- if (map_token_.find(strb.str()) == map_token_.end())
+
+ map_token_type::const_iterator it = map_token_.find(cmd);
+ if (it != map_token_.end())
+ return it->second;
return TERROR;
- if (cmd_buf_[pos_] != 0)
- ++pos_;
- return map_token_[strb.str()];
}
private:
/// virtual machine link.
vm::VirtualMachine vm_;
- /// command line link.
- std::string cmd_buf_;
- /// position in the command line parsing.
- int pos_;
/// map of token word.
map_token_type map_token_;
/// map of register.
Index: src/shell/shell.cc
--- src/shell/shell.cc (revision 117)
+++ src/shell/shell.cc (working copy)
@@ -30,8 +30,7 @@
// --------------------------------------------------------------------------
Shell::Shell():
- vm_(true, false),
- pos_(0)
+ vm_(true, false)
{
map_token_["run"] = RUN;
@@ -116,28 +115,27 @@
Cmd*
Shell::build_cmd(const std::string &str)
{
- pos_ = 0;
- cmd_buf_ = str;
+ std::istringstream iss(str);
Cmd* command = 0;
- switch (eat_word())
+ switch (eat_word(iss))
{
case QUIT:
return new Cmd(Cmd::cmd_quit, *this);
case RUN:
{
command = new Cmd(Cmd::cmd_run, *this);
- std::string tmp = get_next_word();
+ std::string tmp = get_next_word(iss);
while (0 < tmp.length())
{
command->push_arg(tmp);
- tmp = get_next_word();
+ tmp = get_next_word(iss);
}
return command;
}
case BREAK:
{
command = new Cmd(Cmd::cmd_break, *this);
- std::string tmp = get_next_word();
+ std::string tmp = get_next_word(iss);
if (tmp.empty())
{
delete command;
@@ -161,7 +159,7 @@
case LOAD:
{
command = new Cmd(Cmd::cmd_load, *this);
- std::string tmp = get_next_word();
+ std::string tmp = get_next_word(iss);
if (tmp.empty())
{
delete command;
@@ -173,7 +171,7 @@
case PRINT:
{
command = new Cmd(Cmd::cmd_print, *this);
- std::string tmp = get_next_word();
+ std::string tmp = get_next_word(iss);
if (tmp.empty())
{
delete command;
@@ -191,7 +189,7 @@
case DISPLAY:
{
command = new Cmd(Cmd::cmd_display, *this);
- std::string tmp = get_next_word();
+ std::string tmp = get_next_word(iss);
if (tmp.empty())
{
delete command;
@@ -209,7 +207,7 @@
case UNDISPLAY:
{
command = new Cmd(Cmd::cmd_undisplay, *this);
- std::string tmp = get_next_word();
+ std::string tmp = get_next_word(iss);
if (tmp.empty())
{
delete command;
@@ -227,7 +225,7 @@
case MEM:
{
command = new Cmd(Cmd::cmd_mem, *this);
- std::string tmp = get_next_word();
+ std::string tmp = get_next_word(iss);
if (tmp.empty())
{
delete command;
Show replies by date
>> "Benoît" == Benoît Perrot
<benoit(a)lrde.epita.fr> writes:
Index: ChangeLog
from Benoît Perrot <benoit(a)lrde.epita.fr>
* src/shell/shell.hh, src/shell.cc: Normalize string
stream use.
2004-07-18 Benoît Perrot
<benoit(a)lrde.epita.fr>
Index: src/shell/shell.hh
--- src/shell/shell.hh (revision 116)
+++ src/shell/shell.hh (working copy)
@@ -23,6 +23,8 @@
#ifndef SHELL_SHELL_HH
# define SHELL_SHELL_HH
+# include <ctype.h>
Err.... <cctype>.