source: trunk/gsinstaller/unInstall.cpp@ 2490

Last change on this file since 2490 was 2288, checked in by cs025, 23 years ago

Improvements to destroy the gsdlsite.cfg file as required at uninstall.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1#include "unInstall.h"
2
3#include <stdio.h>
4#include <stdarg.h>
5#include "File.h"
6
7bool installManager::logExists()
8{
9 HANDLE fHandle;
10
11 fHandle = CreateFile(this->logfileName.c_str(), GENERIC_READ | GENERIC_WRITE,
12 FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
13 FILE_ATTRIBUTE_NORMAL, NULL);
14 if (fHandle == INVALID_HANDLE_VALUE)
15 { return false;
16 }
17 CloseHandle(fHandle);
18 return true;
19}
20
21bool installManager::ensureLog()
22{
23 if (this->logExists() == false)
24 { HANDLE fHandle;
25
26 unInstallCommand command("InstallRoot");
27 command.addParameter(this->logfileName);
28
29 if (this->modules["default"].begin()->command != "InstallRoot")
30 { this->modules["default"].insert(this->modules["default"].begin(), command);
31 }
32 fHandle = CreateFile(this->logfileName.c_str(), GENERIC_READ | GENERIC_WRITE,
33 FILE_SHARE_WRITE, NULL, CREATE_NEW,
34 FILE_ATTRIBUTE_NORMAL, NULL);
35 if (fHandle != INVALID_HANDLE_VALUE)
36 {
37 CloseHandle(fHandle);
38 return true;
39 }
40 return false;
41 }
42 else
43 { if (this->modules["default"].begin()->command == "InstallRoot")
44 { this->logfileName = this->modules["default"].begin()->parameters[0];
45 }
46 }
47 return true;
48}
49
50bool installManager::setLogFile(string filename)
51{ this->logfileName = filename;
52 this->setModule("default");
53 return true;
54}
55
56bool installManager::readLog()
57{ string command;
58 stringArray params;
59
60 // don't try to open an non-existing log; this has serious
61 // repercussions with bad old VC++
62 if (!this->logExists())
63 { this->setModule("default");
64 this->changed = false;
65 return true;
66 }
67
68 // open the log file
69 this->logfile.open(this->logfileName.c_str(), ios::in);
70#ifndef __BORLANDC__
71 if (this->logfile.is_open())
72#else
73 if (this->logfile.rdbuf()->is_open())
74#endif
75 {
76 // Get the commands into this object from the existing log file
77 while ((command = this->readCommand(params)) != "")
78 { if (command[0] == '[' && command[command.length()-1] == ']')
79 { this->setModule(command.substr(1, command.length() - 2));
80 }
81 else
82 { unInstallCommand action(command, params);
83 this->storeCommand(action);
84 }
85 }
86
87 // close the logfile
88 this->logfile.close();
89
90 // and clear the status bits, 'cos VC++ doesn't clear them when
91 // we reopen the file later.
92 this->logfile.clear();
93 }
94 this->changed = false;
95
96 // set to the default module
97 this->setModule("default");
98 return true;
99}
100
101void installManager::setModule(string moduleName)
102{ this->currentModule = moduleName;
103}
104
105bool installManager::storeCommand(unInstallCommand &command)
106{ this->modules[this->currentModule].push_back(command);
107 this->changed = true; // set the "changed" flag
108 return true;
109}
110
111bool installManager::writeCommand(unInstallCommand &command)
112{ if (!this->writeString(command.command))
113 { return false;
114 }
115 for (unsigned int p = 0; p < command.parameters.size(); p ++)
116 { if (!this->writeSeparator())
117 { return false;
118 }
119 if (!this->writeString(command.parameters[p]))
120 { return false;
121 }
122 }
123 if (!this->writeString("\n"))
124 { return false;
125 }
126 return true;
127}
128
129bool installManager::writeString(char *buffer)
130{ string s(buffer);
131 return this->writeString(s);
132}
133
134bool installManager::writeString(string str)
135{ bool quote;
136
137 // TODO: check for space characters in str and quote if necessary
138 quote = str.find_first_of(' ') < str.length();
139
140 if (quote)
141 { this->logfile << "\"";
142 }
143 this->logfile << str;
144 if (quote)
145 { this->logfile << "\"";
146 }
147 return true;
148}
149
150bool installManager::writeSeparator()
151{ this->logfile << " ";
152 return true;
153}
154
155string installManager::readString()
156{ string reply = "";
157 char c;
158
159 if (this->logfile.eof())
160 { return reply;
161 }
162
163 this->logfile >> c;
164 while (c <= ' ' && !this->logfile.eof())
165 { this->logfile.get();
166 }
167
168 if (this->logfile.eof())
169 { return reply;
170 }
171
172 if (c == '\"')
173 { do
174 { c = this->logfile.get();
175 if (c != '\"')
176 { reply += c;
177 }
178 }
179 while (c != '\"' && !this->logfile.eof());
180 }
181 else
182 { while (c > ' ')
183 { reply += c;
184 if (this->logfile.eof())
185 { break;
186 }
187 c = this->logfile.get();
188 }
189 if (!this->logfile.eof())
190 { this->logfile.putback(c);
191 }
192 //this->logfile >> reply;
193 }
194 return reply;
195}
196
197string installManager::readCommand(stringArray &array)
198{ string reply = "";
199 char c;
200
201 array.clear();
202
203 if (this->logfile.eof())
204 { return reply;
205 }
206
207 reply = this->readString();
208 if (reply == "")
209 { return reply;
210 }
211
212 while(!this->logfile.eof() &&
213 (c = this->logfile.get()) != '\n')
214 { this->logfile.putback(c);
215 array.add(this->readString());
216 }
217 if (!this->logfile.eof())
218 { this->logfile.putback(c);
219 }
220
221 return reply;
222}
223
224string installManager::popCommand(stringArray &array)
225{ string command;
226
227 if (this->modules[this->currentModule].size() == 0)
228 { command = "";
229 }
230 else
231 { unsigned int last;
232
233 last = this->modules[this->currentModule].size() - 1;
234 command = this->modules[this->currentModule][last].commandName();
235 array = this->modules[this->currentModule][last].parameterList();
236 this->modules[this->currentModule].erase(this->modules[this->currentModule].begin() + last);
237 }
238 return command;
239}
240
241bool installManager::recordLog()
242{ // just return if there are no changes to record
243 if (this->changed == false)
244 { return true;
245 }
246
247 // if the file didn't open, then this is almost certainly being compiled
248 // with Microsoft Visual C++ which has a library which fails to open the
249 // file if it doesn't already exist. Do a createfile and hope for success.
250 // TODO: add proper error handling here
251 this->ensureLog();
252 this->logfile.open(this->logfileName.c_str(), ios::out);
253#ifndef __BORLANDC__
254 if (!this->logfile.is_open())
255#else
256 if (!this->logfile.rdbuf()->is_open())
257#endif
258 { MessageBox(0, "Unable to open log file", this->logfileName.c_str(), MB_OK);
259 }
260
261 unInstallCommandMap::iterator here = this->modules.begin();
262 unInstallCommandMap::iterator end = this->modules.end();
263
264 while (here != end)
265 {
266 this->writeString("[" + (*here).first + "]\n");
267
268 unInstallCommandList::iterator ahere = (*here).second.begin();
269 unInstallCommandList::iterator aend = (*here).second.end();
270
271 while (ahere != aend)
272 { this->writeCommand(*ahere);
273 ahere ++;
274 }
275 here ++;
276 }
277 this->logfile.close();
278
279 this->changed = false;
280
281 return true;
282}
283
284bool installManager::isEmpty()
285{ unInstallCommandMap::iterator here = this->modules.begin();
286 unInstallCommandMap::iterator end = this->modules.end();
287
288 while (here != end)
289 { this->writeString("[" + (*here).first + "]\n");
290
291 unInstallCommandList::iterator ahere = (*here).second.begin();
292 unInstallCommandList::iterator aend = (*here).second.end();
293 if (ahere != aend)
294 { return false;
295 }
296 here ++;
297 }
298 return true;
299}
300
301string installManager::installRoot()
302{ if (this->modules["default"].begin()->command != "InstallRoot")
303 return "";
304 return this->modules["default"].begin()->parameters[0];
305}
306
307installManager::~installManager()
308{ this->recordLog();
309}
Note: See TracBrowser for help on using the repository browser.