Move closing of file in Book destructor. This
guarantees us against leaving the file open under
any case and simplifies also Book use.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
////
+/// Destructor. Be sure file is closed before we leave.
+
+Book::~Book() {
+
+ close();
+}
+
+
/// Book::open() opens a book file with a given file name
void Book::open(const string& fName) {
+ // Close old file before opening the new
+ close();
+
fileName = fName;
ifstream::open(fileName.c_str(), ifstream::in | ifstream::binary);
if (!is_open())
if (!good())
{
cerr << "Failed to open book file " << fileName << endl;
- close();
exit(EXIT_FAILURE);
}
}
if (!good())
{
cerr << "Failed to read book entry at index " << idx << endl;
- close();
exit(EXIT_FAILURE);
}
}
};
class Book : private std::ifstream {
-
public:
+ ~Book();
void open(const std::string& fName);
void close();
const std::string file_name() const;
{
Move bookMove;
if (get_option_value_string("Book File") != OpeningBook.file_name())
- {
- OpeningBook.close();
OpeningBook.open("book.bin");
- }
+
bookMove = OpeningBook.get_move(pos);
if (bookMove != MOVE_NONE)
{
if (Quit)
{
- OpeningBook.close();
stop_threads();
quit_eval();
exit(0);
command = "quit";
if(command == "quit") {
- OpeningBook.close();
stop_threads();
quit_eval();
exit(0);
if (token == "quit")
{
- OpeningBook.close();
stop_threads();
quit_eval();
exit(0);