void Book::close() {
- if (is_open())
- ifstream::close();
+ if (bookFile.is_open())
+ bookFile.close();
bookName = "";
}
// Close old file before opening the new
close();
- ifstream::open(fileName.c_str(), ifstream::in | ifstream::binary);
+ bookFile.open(fileName.c_str(), ifstream::in | ifstream::binary);
// Silently return when asked to open a non-exsistent file
- if (!is_open())
+ if (!bookFile.is_open())
return;
// Get the book size in number of entries
- seekg(0, ios::end);
- bookSize = long(tellg()) / sizeof(BookEntry);
+ bookFile.seekg(0, ios::end);
+ bookSize = long(bookFile.tellg()) / sizeof(BookEntry);
- if (!good())
+ if (!bookFile.good())
{
cerr << "Failed to open book file " << fileName << endl;
exit(EXIT_FAILURE);
Move Book::get_move(const Position& pos, bool findBestMove) {
- if (!is_open() || bookSize == 0)
+ if (!bookFile.is_open() || bookSize == 0)
return MOVE_NONE;
BookEntry entry;
move_to(Move(bookMove)), PieceType(p + 1)));
// Verify the book move (if any) is legal
- MoveStack mlist[MOVES_MAX];
+ MoveStack mlist[MAX_MOVES];
MoveStack* last = generate<MV_LEGAL>(pos, mlist);
for (MoveStack* cur = mlist; cur != last; cur++)
if ((int(cur->move) & ~(3 << 14)) == bookMove) // Mask out special flags
}
+/// Book::get_number() reads sizeof(T) chars from the file's binary byte
+/// stream and converts them in a number of type T.
+template<typename T>
+void Book::get_number(T& n) {
+
+ n = 0;
+
+ for (size_t i = 0; i < sizeof(T); i++)
+ n = (n << 8) + (T)bookFile.get();
+}
+
+
/// Book::read_entry() takes an integer index, and returns the BookEntry
/// at the given index in the book file.
BookEntry Book::read_entry(int idx) {
assert(idx >= 0 && idx < bookSize);
- assert(is_open());
+ assert(bookFile.is_open());
BookEntry e;
- seekg(idx * sizeof(BookEntry), ios_base::beg);
+ bookFile.seekg(idx * sizeof(BookEntry), ios_base::beg);
- *this >> e.key >> e.move >> e.count >> e.learn;
+ get_number(e.key);
+ get_number(e.move);
+ get_number(e.count);
+ get_number(e.learn);
- if (!good())
+ if (!bookFile.good())
{
cerr << "Failed to read book entry at index " << idx << endl;
exit(EXIT_FAILURE);