]> git.sesse.net Git - stockfish/commitdiff
Be sure book file is closed before we leave
authorMarco Costalba <mcostalba@gmail.com>
Thu, 7 May 2009 07:27:38 +0000 (09:27 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Thu, 7 May 2009 07:27:38 +0000 (09:27 +0200)
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>
src/book.cpp
src/book.h
src/search.cpp
src/uci.cpp

index 8e4778eed711813b5ad6c3a46ff77b783aa859c9..c69eab739b7dfcae8284db5c417225ad466a831a 100644 (file)
@@ -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
 
 void Book::open(const string& fName) {
 
 /// 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())
   fileName = fName;
   ifstream::open(fileName.c_str(), ifstream::in | ifstream::binary);
   if (!is_open())
@@ -361,7 +372,6 @@ void Book::open(const string& fName) {
   if (!good())
   {
       cerr << "Failed to open book file " << fileName << endl;
   if (!good())
   {
       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())
   {
       cerr << "Failed to read book entry at index " << idx << endl;
   if (!good())
   {
       cerr << "Failed to read book entry at index " << idx << endl;
-      close();
       exit(EXIT_FAILURE);
   }
 }
       exit(EXIT_FAILURE);
   }
 }
index e70bd0f1dabd2a31efe88783c76c377f4d7d88e8..740ef74992562f3d7b6399096d3357124604903c 100644 (file)
@@ -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 close();
   const std::string file_name() const;
   void open(const std::string& fName);
   void close();
   const std::string file_name() const;
index 22bfe8d6daa72bd80d63eae109e8f817da6ce7a6..64863be166b07f84f53571c10ee79e392f3d210d 100644 (file)
@@ -371,10 +371,8 @@ void think(const Position &pos, bool infinite, bool ponder, int side_to_move,
   {
       Move bookMove;
       if (get_option_value_string("Book File") != OpeningBook.file_name())
   {
       Move bookMove;
       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);
       if (bookMove != MOVE_NONE)
       {
       bookMove = OpeningBook.get_move(pos);
       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();
       quit_eval();
       exit(0);
       stop_threads();
       quit_eval();
       exit(0);
@@ -2561,7 +2558,6 @@ namespace {
         command = "quit";
 
       if(command == "quit") {
         command = "quit";
 
       if(command == "quit") {
-        OpeningBook.close();
         stop_threads();
         quit_eval();
         exit(0);
         stop_threads();
         quit_eval();
         exit(0);
index c9120ddbd7728c1b7dfc72cf6b6930118dc2f594..9c13eff7abfdf2ced889d5273c3e1e3651160284 100644 (file)
@@ -119,7 +119,6 @@ namespace {
 
     if (token == "quit")
     {
 
     if (token == "quit")
     {
-        OpeningBook.close();
         stop_threads();
         quit_eval();
         exit(0);
         stop_threads();
         quit_eval();
         exit(0);