Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / clustalw / src / general / ClustalWResources.cpp
1 /**
2  * Implements a singleton that maintains program resources.
3  * The single instance is (re)instantiated on demand like:
4  *     Resources *res = Resources::Instance();
5  *
6  * 24-05-07,Nigel Brown(EMBL): created.
7  * 3-7-07, Mark Larkin, modified this class for clustalw 
8  */
9 #ifdef HAVE_CONFIG_H
10     #include "config.h"
11 #endif
12 #include "ClustalWResources.h"
13 #include <iostream>
14 #include "clustalw.h"
15 #include <fstream>
16 using namespace std;
17  
18 namespace clustalw
19 {
20
21 //environment variables
22 static const char *CLUW_INSTALL_DIR = "CLUW_INSTALL_DIR";
23
24 //return the sole instance
25 ClustalWResources *ClustalWResources::Instance() {
26     static ClustalWResources instance;
27     return &instance;
28 }
29
30 ClustalWResources::ClustalWResources()
31 {
32     //defaultPath
33     defaultPath = ".";
34
35     //executablePath
36     executablePath = ".";
37     
38     //installPath
39     installPath = ".";
40     char *env;
41     if ((env = getenv(CLUW_INSTALL_DIR)) != 0) 
42     {
43         installPath = string(env);
44     }
45
46     homePath = "";
47 }
48
49 void ClustalWResources::setPathToExecutable(string path) 
50 {
51     executablePath = dirname(path);
52 }
53
54 string ClustalWResources::dirname(string path) 
55 {
56     string tempString;
57     int size = path.size();
58     tempString = path;
59     for (int i = size - 1; i > 0; i--) 
60     {
61         if (tempString[i] == DIRDELIM) // Mark, no standard function in c++
62         { 
63             tempString.erase(i);
64             break;
65         }
66     }
67     return tempString;
68 }
69
70 void ClustalWResources::dump() 
71 {
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()
76            );
77 }
78
79 string ClustalWResources::findFile(const char *file, const ClustalWResourcePathType where) const 
80 {
81     return findFile(string(file), where);
82 }
83
84 string ClustalWResources::findFile(const string file, const ClustalWResourcePathType where) const 
85 {
86     const string *path;
87     ifstream ifs;
88
89     switch (where) 
90     {
91         case InstallPath:
92             path = &installPath;
93             break;
94         case ExecutablePath:
95             path = &executablePath;
96             break;
97         case HomePath:
98             path = &homePath;
99             break;
100         default:
101             path = &defaultPath;
102             break;
103     }
104     char delim[1];
105     delim[0] = DIRDELIM;
106     delim[1] = 0;
107     
108     string fileName = *path + string(delim) + file;
109
110     ifs.open(fileName.c_str(), ifstream::in);
111     if (ifs.fail()) {
112         return string();
113     }
114     
115     if (ifs.is_open() && ifs.good()) 
116     {
117         ifs.close();
118         return fileName;
119     }
120     return string(); //not found/readable
121 }
122
123 // Search for a (string) file in a succession of likely locations and
124 // return the full path as (string).
125 //
126 string ClustalWResources::searchPathsForFile(const string fileName) const 
127 {
128     string file;
129     while (1) {
130         file = findFile(fileName, InstallPath);
131         if (file != "") break;
132         
133         file = findFile(fileName, ExecutablePath);
134         if (file != "") break;
135         
136         file = findFile(fileName, HomePath);
137         if (file != "") break;
138         
139         file = findFile(fileName);
140         if (file != "") break;
141         
142         file = fileName; // give up
143         break;
144     }
145     return file;
146 }
147
148 }