Belofte version 2.1.9
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
17
19{
20 while (true) {
21 if (!isFileAttached()) AppEI()->sendPrompt();
22 try {
24 AppEI()->execute(cmd.m_command, cmd.m_args);
25 } catch (const quitCommandException&) {
26 // quit requested
27 break;
28 } catch (const noCommandException&) {
29 // skip comments and empty lines
30 } catch (...) { throw; // push other exceptions down
31 }
32 }
33}
34
35std::string commandReader::readLine()
36{
37 std::string line;
38
39 while (line.empty()) {
40 if (!isFileAttached()) {
41 if (!std::getline(std::cin, line)) throw noCommandException();
42 } else {
43 if (!std::getline(m_inputfile, line)) {
44 detach();
45 break;
46 }
47 }
48 if (!line.empty()) {
49 AppEI()->sendDebug(2, line);
50 if ((line[0] == '#')
51 || (line[0] == '=')
52 || (line[0] == ';')) line = "";
53 }
54 }
55
56 return line;
57}
58
60{
61 std::string line = readLine();
62 cmdParam cmd;
63
64 if (line == "__END__") {
65 detach();
66 } else if (!line.empty()) {
67 std::stringstream ss(line);
68 std::getline(ss, cmd.m_command, ' ');
69 std::getline(ss, cmd.m_args);
70
71 /// @attention Bughouse moves use @ character as well
72 if ((cmd.m_command.substr(0,1) == "@") && (cmd.m_command.length() > 1)) {
73 // @file passed, move file to arg and truncate command
74 cmd.m_args = cmd.m_command.substr(1);
75 cmd.m_command = "@";
76 }
77 }
78
79 return cmd;
80}
81
82bool commandReader::attach(std::string const& ifile)
83{
84 m_filename = ifile;
85 m_inputfile.open(m_filename.c_str(), std::ios_base::in);
86 return isFileAttached();
87}
88
90{
91 if (m_filename == "") return false;
92 if (!m_inputfile) return false;
93 if (m_inputfile.eof()) detach();
94 return m_inputfile.is_open();
95}
96
98{
99 m_inputfile.close();
100 m_filename = "";
101}
102
103/** no interactive input, mainly during start and '@' execute
104 */
106{
107 if (isFileAttached()) return true;
108 return m_isBatch;
109}
110
112{
113 m_isBatch = true;
114}
115
117{
118 m_isBatch = false;
119}
120
121//-----------------------------------------------------------------------
122
123// eof
appInstance & App()
Definition belofte.cpp:225
engineInterface * AppEI()
Definition belofte.cpp:231
This is the main include file, needs to be included before any other include.
commandReader m_reader
searching output
Definition belofte.h:175
command and parameters
Definition usercmd.h:41
std::string m_command
Definition usercmd.h:58
std::string m_args
Definition usercmd.h:59
bool isBatchMode()
no interactive input, mainly during start and '@' execute
void runner(void)
cmdParam getCommand()
bool attach(std::string const &ifile)
virtual void sendDebug(int const l, std::string const &info)
void execute(std::string const &command, std::string const &params)
virtual void sendPrompt()