00001 //---------------------------------------------------------------------------- 00002 /** @file FuegoMainUtil.cpp 00003 See FuegoMainUtil.h */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include "SgSystem.h" 00007 #include "FuegoMainUtil.h" 00008 00009 #include <fstream> 00010 #include <sstream> 00011 #include "GoBook.h" 00012 #include "SgDebug.h" 00013 00014 using namespace std; 00015 using namespace boost::filesystem; 00016 00017 //---------------------------------------------------------------------------- 00018 00019 namespace { 00020 00021 bool LoadBookFile(GoBook& book, const path& file) 00022 { 00023 path normalizedFile = file; 00024 normalizedFile.normalize(); 00025 string nativeFile = normalizedFile.native_file_string(); 00026 SgDebug() << "Loading opening book from '" << nativeFile << "'... "; 00027 ifstream in(nativeFile.c_str()); 00028 if (! in) 00029 { 00030 SgDebug() << "not found\n"; 00031 return false; 00032 } 00033 try 00034 { 00035 book.Read(in); 00036 } 00037 catch (const SgException& e) 00038 { 00039 SgDebug() << "error: " << e.what() << '\n'; 00040 return false; 00041 } 00042 SgDebug() << "ok\n"; 00043 return true; 00044 } 00045 00046 } // namespace 00047 00048 //---------------------------------------------------------------------------- 00049 00050 void FuegoMainUtil::LoadBook(GoBook& book, 00051 const boost::filesystem::path& programDir) 00052 { 00053 const string fileName = "book.dat"; 00054 if (LoadBookFile(book, programDir / fileName)) 00055 return; 00056 #ifdef ABS_TOP_SRCDIR 00057 if (LoadBookFile(book, path(ABS_TOP_SRCDIR) / "book" / fileName)) 00058 return; 00059 #endif 00060 #if defined(DATADIR) && defined(PACKAGE) 00061 if (LoadBookFile(book, path(DATADIR) / PACKAGE / fileName)) 00062 return; 00063 #endif 00064 throw SgException("Could not find opening book."); 00065 } 00066 00067 std::string FuegoMainUtil::Version() 00068 { 00069 ostringstream s; 00070 #ifdef VERSION 00071 s << VERSION; 00072 #else 00073 s << "(" __DATE__ ")"; 00074 #endif 00075 #ifdef SVNREV 00076 s << "(" SVNREV ")"; 00077 #endif 00078 #ifndef NDEBUG 00079 s << " (dbg)"; 00080 #endif 00081 return s.str(); 00082 } 00083 00084 //----------------------------------------------------------------------------