mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 09:47:46 +00:00
Be sure book file is closed before we leave
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>
This commit is contained in:
+11
-2
@@ -344,10 +344,21 @@ namespace {
|
|||||||
////
|
////
|
||||||
|
|
||||||
|
|
||||||
|
/// Destructor. Be sure file is closed before we leave.
|
||||||
|
|
||||||
|
Book::~Book() {
|
||||||
|
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Book::open() opens a book file with a given file name
|
/// Book::open() opens a book file with a given file name
|
||||||
|
|
||||||
void Book::open(const string& fName) {
|
void Book::open(const string& fName) {
|
||||||
|
|
||||||
|
// Close old file before opening the new
|
||||||
|
close();
|
||||||
|
|
||||||
fileName = fName;
|
fileName = fName;
|
||||||
ifstream::open(fileName.c_str(), ifstream::in | ifstream::binary);
|
ifstream::open(fileName.c_str(), ifstream::in | ifstream::binary);
|
||||||
if (!is_open())
|
if (!is_open())
|
||||||
@@ -361,7 +372,6 @@ void Book::open(const string& fName) {
|
|||||||
if (!good())
|
if (!good())
|
||||||
{
|
{
|
||||||
cerr << "Failed to open book file " << fileName << endl;
|
cerr << "Failed to open book file " << fileName << endl;
|
||||||
close();
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -479,7 +489,6 @@ void Book::read_entry(BookEntry& entry, int idx) {
|
|||||||
if (!good())
|
if (!good())
|
||||||
{
|
{
|
||||||
cerr << "Failed to read book entry at index " << idx << endl;
|
cerr << "Failed to read book entry at index " << idx << endl;
|
||||||
close();
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -53,8 +53,8 @@ struct BookEntry {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class Book : private std::ifstream {
|
class Book : private std::ifstream {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
~Book();
|
||||||
void open(const std::string& fName);
|
void open(const std::string& fName);
|
||||||
void close();
|
void close();
|
||||||
const std::string file_name() const;
|
const std::string file_name() const;
|
||||||
|
|||||||
+1
-5
@@ -371,10 +371,8 @@ void think(const Position &pos, bool infinite, bool ponder, int side_to_move,
|
|||||||
{
|
{
|
||||||
Move bookMove;
|
Move bookMove;
|
||||||
if (get_option_value_string("Book File") != OpeningBook.file_name())
|
if (get_option_value_string("Book File") != OpeningBook.file_name())
|
||||||
{
|
|
||||||
OpeningBook.close();
|
|
||||||
OpeningBook.open("book.bin");
|
OpeningBook.open("book.bin");
|
||||||
}
|
|
||||||
bookMove = OpeningBook.get_move(pos);
|
bookMove = OpeningBook.get_move(pos);
|
||||||
if (bookMove != MOVE_NONE)
|
if (bookMove != MOVE_NONE)
|
||||||
{
|
{
|
||||||
@@ -545,7 +543,6 @@ void think(const Position &pos, bool infinite, bool ponder, int side_to_move,
|
|||||||
|
|
||||||
if (Quit)
|
if (Quit)
|
||||||
{
|
{
|
||||||
OpeningBook.close();
|
|
||||||
stop_threads();
|
stop_threads();
|
||||||
quit_eval();
|
quit_eval();
|
||||||
exit(0);
|
exit(0);
|
||||||
@@ -2561,7 +2558,6 @@ namespace {
|
|||||||
command = "quit";
|
command = "quit";
|
||||||
|
|
||||||
if(command == "quit") {
|
if(command == "quit") {
|
||||||
OpeningBook.close();
|
|
||||||
stop_threads();
|
stop_threads();
|
||||||
quit_eval();
|
quit_eval();
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|||||||
@@ -119,7 +119,6 @@ namespace {
|
|||||||
|
|
||||||
if (token == "quit")
|
if (token == "quit")
|
||||||
{
|
{
|
||||||
OpeningBook.close();
|
|
||||||
stop_threads();
|
stop_threads();
|
||||||
quit_eval();
|
quit_eval();
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|||||||
Reference in New Issue
Block a user