Belofte version 2.2.0
A promising chess program using the UCI or Winboard interface
commandreader.cpp
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: commandreader.cpp
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#include "belofte.h"
9
10//-----------------------------------------------------------------------
11
13{
14 while (true) {
15 if (!isFileAttached()) AppEI()->sendPrompt();
16 try {
18 AppEI()->execute(cmd.m_command, cmd.m_args);
19 } catch (const quitCommandException&) {
20 // quit requested
21 break;
22 } catch (const noCommandException&) {
23 // skip comments and empty lines
24 } catch (...) { throw; // push other exceptions down
25 }
26 }
27}
28
29std::string commandReader::readLine()
30{
31 std::string line;
32
33 while (line.empty()) {
34 if (!isFileAttached()) {
35 if (!std::getline(std::cin, line)) throw noCommandException();
36 } else {
37 if (!std::getline(m_inputfile, line)) {
38 detach();
39 break;
40 }
41 }
42 if (!line.empty()) {
43 /// @todo output to attached file only
44 if ((line[0] == '#')
45 || (line[0] == '=')
46 || (line[0] == ';')) line = "";
47 }
48 }
49
50 return line;
51}
52
54{
55 std::string line = readLine();
56 cmdParam cmd;
57
58 if (line == "__END__") {
59 detach();
60 } else if (!line.empty()) {
61 std::stringstream ss(line);
62 std::getline(ss, cmd.m_command, ' ');
63 std::getline(ss, cmd.m_args);
64
65 /// @attention Bughouse moves use @ character as well
66 if ((cmd.m_command.substr(0,1) == "@") && (cmd.m_command.length() > 1)) {
67 // @file passed, move file to arg and truncate command
68 cmd.m_args = cmd.m_command.substr(1);
69 cmd.m_command = "@";
70 }
71 }
72
73 return cmd;
74}
75
76bool commandReader::attach(std::string const& ifile)
77{
78 m_filename = ifile;
79 m_inputfile.open(m_filename.c_str(), std::ios_base::in);
80 return isFileAttached();
81}
82
84{
85 m_inputfile.close();
86 m_filename = "";
87}
88
89//-----------------------------------------------------------------------
90
91// eof
appInstance & App()
Definition belofte.cpp:242
engineInterface * AppEI()
Definition belofte.cpp:248
This is the main include file, needs to be included before any other include.
commandReader m_reader
searching output
Definition belofte.h:180
command and parameters
Definition usercmd.h:41
std::string m_command
Definition usercmd.h:58
std::string m_args
Definition usercmd.h:59
bool isFileAttached()
cmdParam getCommand()
bool attach(std::string const &ifile)
void execute(std::string const &command, std::string const &params)
virtual void sendPrompt()