2 * Implements a singleton that maintains program resources.
3 * The single instance is (re)instantiated on demand like:
4 * Resources *res = Resources::Instance();
6 * 24-05-07,Nigel Brown(EMBL): created.
7 * 3-7-07, Mark Larkin, modified this class for clustalw
12 #include "ClustalWResources.h"
21 //environment variables
22 static const char *CLUW_INSTALL_DIR = "CLUW_INSTALL_DIR";
24 //return the sole instance
25 ClustalWResources *ClustalWResources::Instance() {
26 static ClustalWResources instance;
30 ClustalWResources::ClustalWResources()
41 if ((env = getenv(CLUW_INSTALL_DIR)) != 0)
43 installPath = string(env);
49 void ClustalWResources::setPathToExecutable(string path)
51 executablePath = dirname(path);
54 string ClustalWResources::dirname(string path)
57 int size = path.size();
59 for (int i = size - 1; i > 0; i--)
61 if (tempString[i] == DIRDELIM) // Mark, no standard function in c++
70 void ClustalWResources::dump()
72 printf("%s => %s [%s]\n%s => %s\n%s => %s\n",
73 "installPath", installPath.c_str(), CLUW_INSTALL_DIR,
74 "executablePath", executablePath.c_str(),
75 "homePath", homePath.c_str()
79 string ClustalWResources::findFile(const char *file, const ClustalWResourcePathType where) const
81 return findFile(string(file), where);
84 string ClustalWResources::findFile(const string file, const ClustalWResourcePathType where) const
95 path = &executablePath;
108 string fileName = *path + string(delim) + file;
110 ifs.open(fileName.c_str(), ifstream::in);
115 if (ifs.is_open() && ifs.good())
120 return string(); //not found/readable
123 // Search for a (string) file in a succession of likely locations and
124 // return the full path as (string).
126 string ClustalWResources::searchPathsForFile(const string fileName) const
130 file = findFile(fileName, InstallPath);
131 if (file != "") break;
133 file = findFile(fileName, ExecutablePath);
134 if (file != "") break;
136 file = findFile(fileName, HomePath);
137 if (file != "") break;
139 file = findFile(fileName);
140 if (file != "") break;
142 file = fileName; // give up