00001
00002
00003 #include "osl/oslConfig.h"
00004 #include "osl/misc/ncores.h"
00005 #include <map>
00006 #include <boost/filesystem/operations.hpp>
00007 #include <iostream>
00008 #include <cstdlib>
00009
00010 static bool verbose = false;
00011 #ifndef OSL_NCPUS
00012 static const int default_ncpus = osl::misc::ncores();
00013 #else
00014 static const int default_ncpus = OSL_NCPUS;
00015 #endif
00016 static int num_cpu = default_ncpus;
00017
00018 void osl::OslConfig::setNumCPUs(int ncpu)
00019 {
00020 num_cpu = ncpu;
00021 }
00022
00023 int osl::OslConfig::numCPUs()
00024 {
00025 return num_cpu;
00026 }
00027
00028 void osl::OslConfig::setVerbose(bool v)
00029 {
00030 ::verbose = v;
00031 }
00032
00033 bool osl::OslConfig::verbose()
00034 {
00035 return ::verbose;
00036 }
00037
00038 void osl::OslConfig::showOslHome(const std::string& home)
00039 {
00040 std::cerr << "using " << home << " as OSL_HOME" << std::endl;
00041 }
00042
00043 void osl::OslConfig::showOslHome()
00044 {
00045 showOslHome(home());
00046 }
00047
00048 bool osl::OslConfig::isGoodDir(const std::string& dir)
00049 {
00050 return boost::filesystem::exists(dir)
00051 && boost::filesystem::is_directory(dir);
00052 }
00053
00054 void osl::OslConfig::trySetDir(std::string& dir, const std::string& candidate)
00055 {
00056 if (isGoodDir(candidate))
00057 {
00058 dir = candidate;
00059 return;
00060 }
00061 if (verbose())
00062 std::cerr << "skipping " << candidate << std::endl;
00063 }
00064
00065 const std::string osl::OslConfig::makeHome()
00066 {
00067 std::string result;
00068 if (const char *env = getenv("GPSSHOGI_HOME"))
00069 trySetDir(result, env);
00070
00071 #if defined GPSSHOGI_HOME
00072 if (result.empty())
00073 trySetDir(result, GPSSHOGI_HOME);
00074 #endif
00075
00076 if (result.empty())
00077 if (const char *env = getenv("OSL_HOME"))
00078 trySetDir(result, env);
00079
00080 if (result.empty())
00081 result = OSL_HOME;
00082
00083 if (verbose())
00084 showOslHome(result);
00085 return result;
00086 }
00087
00088 const std::string& osl::OslConfig::home()
00089 {
00090 static const std::string home_directory = makeHome();
00091 return home_directory;
00092 }
00093
00094 const char * osl::OslConfig::home_c_str()
00095 {
00096 return home().c_str();
00097 }
00098
00099 const std::string osl::OslConfig::makeTest()
00100 {
00101 std::string result;
00102 if (const char *env = getenv("OSL_TEST"))
00103 trySetDir(result, env);
00104
00105 if (result.empty())
00106 result = home() + "/data";
00107
00108 std::cerr << "using " << result << " as OSL_TEST" << std::endl;
00109 return result;
00110 }
00111
00112 const std::string osl::OslConfig::test()
00113 {
00114 static const std::string test_directory = makeTest();
00115 return test_directory;
00116 }
00117
00118 namespace
00119 {
00120 struct NameHolder : std::map<std::string,std::string>
00121 {
00122 std::string directory;
00123
00124 NameHolder(const std::string& d) : directory(d)
00125 {
00126 directory += "/";
00127 }
00128
00129 iterator add(const std::string& key, const std::string& value)
00130 {
00131 return insert(std::make_pair(key, value)).first;
00132 }
00133 iterator addRelative(const std::string& key, const std::string& filename)
00134 {
00135 std::string value = directory + filename;
00136 return add(key, value);
00137 }
00138 iterator addRelative(const std::string& filename)
00139 {
00140 return addRelative(filename, filename);
00141 }
00142 };
00143 }
00144
00145 const char * osl::OslConfig::testFile(const std::string& filename)
00146 {
00147 static NameHolder table(test());
00148 NameHolder::iterator p=table.find(filename);
00149 if (p == table.end()) {
00150 p = table.addRelative(filename);
00151 }
00152 return p->second.c_str();
00153 }
00154
00155 const char * osl::OslConfig::testCsaFile(const std::string& filename)
00156 {
00157 static NameHolder table(test()+"/kifdat");
00158 NameHolder::iterator p=table.find(filename);
00159 if (p == table.end()) {
00160 p = table.addRelative(filename);
00161 }
00162 return p->second.c_str();
00163 }
00164
00165 const char *osl::OslConfig::openingBook(const std::string& filename)
00166 {
00167 static NameHolder table(home()+"/data");
00168 NameHolder::iterator p=table.find(filename);
00169 if (p == table.end()) {
00170 if (! filename.empty() && filename[0] == '/') {
00171
00172 p = table.add(filename, filename);
00173 }
00174 else {
00175
00176 p = table.addRelative(filename,
00177 (filename == "" ? "joseki.dat" : filename));
00178 }
00179 }
00180 return p->second.c_str();
00181 }
00182
00183
00184
00185
00186
00187