Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

CacheBook.cpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file CacheBook.cpp
00003 */
00004 //----------------------------------------------------------------------------
00005 
00006 #include "CacheBook.hpp"
00007 #include "boost/filesystem/path.hpp"
00008 
00009 using namespace benzene;
00010 
00011 //----------------------------------------------------------------------------
00012 
00013 /** Loads cached opening moves from WolveCacheBook.txt in share directory. */
00014 CacheBook::CacheBook()
00015 {
00016 #ifdef ABS_TOP_SRCDIR
00017     boost::filesystem::path normalizedFile =
00018         boost::filesystem::path(ABS_TOP_SRCDIR)
00019         / "share" / "WolveCacheBook.txt";
00020     normalizedFile.normalize();
00021     std::string nativeFile = normalizedFile.native_file_string();
00022     std::ifstream f(nativeFile.c_str());
00023     if (!f.is_open())
00024         LogWarning() << "Could not open cache book file for reading: '"
00025                      << nativeFile << "'" << '\n';
00026     else
00027         ParseFile(f);
00028 #else
00029     LogWarning() << "**** NO CACHE BOOK LOADED ***" << '\n';
00030 #endif
00031 }
00032 
00033 void CacheBook::ParseFile(std::ifstream& inFile)
00034 {
00035     int size;
00036     std::string line;
00037     while (true) {
00038         // Read line, and quit once end of file
00039         getline(inFile, line);
00040         if (inFile.eof())
00041             break;
00042 
00043         // Commented lines are ignored
00044         if (0 < line.size() && '#' == line[0])
00045             continue;
00046 
00047         // Parse line
00048         std::istringstream in(line);
00049         in >> size;
00050         std::vector<HexPoint> variation = ReadPoints(in);
00051         std::vector<HexPoint> moves = ReadPoints(in);
00052         if (0 == variation.size() || 1 != moves.size()) {
00053             LogWarning() << "Error parsing cache book: '" << line << "'\n";
00054             continue;
00055         }
00056         
00057         // Add corresponding entry (if not redundant)
00058         HexState hs(size);
00059         for (size_t i = 0; i < variation.size(); ++i)
00060             hs.PlayMove(variation[i]);
00061         if (!Exists(hs))
00062             operator[](hs) = moves[0];
00063     }
00064     LogInfo() << "CacheBook: contains " << Size() << " entries.\n";
00065 }
00066 
00067 std::vector<HexPoint> CacheBook::ReadPoints(std::istringstream& in) const
00068 {
00069     std::vector<HexPoint> result;
00070     while (true) {
00071         std::string s;
00072         in >> s;
00073         if (!in || s == "|")
00074             break;
00075         HexPoint p = HexPointUtil::FromString(s);
00076         if (INVALID_POINT == p) {
00077             result.clear();
00078             return result;
00079         }
00080         result.push_back(p);
00081     }
00082     return result;
00083 }
00084 
00085 //----------------------------------------------------------------------------


6 Jan 2011 Doxygen 1.6.3