1 | #include "unInstall.h"
|
---|
2 |
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <stdarg.h>
|
---|
5 | #include "File.h"
|
---|
6 |
|
---|
7 | unInstaller::unInstaller(string &fileName)
|
---|
8 | { if (fileName != "")
|
---|
9 | { this->readFile(fileName);
|
---|
10 | }
|
---|
11 | }
|
---|
12 |
|
---|
13 | unInstaller::unInstaller(const char *fileName)
|
---|
14 | { string filename = fileName;
|
---|
15 |
|
---|
16 | if (fileName != "")
|
---|
17 | { this->readFile(filename);
|
---|
18 | }
|
---|
19 | }
|
---|
20 |
|
---|
21 | bool unInstaller::readFile(string &fileName)
|
---|
22 | { FILE *f;
|
---|
23 | File finfo(fileName.c_str());
|
---|
24 | string currentGroup;
|
---|
25 | char *line, *eol;
|
---|
26 |
|
---|
27 | f = fopen(fileName.c_str(), "rb");
|
---|
28 | if (f == NULL)
|
---|
29 | { return false;
|
---|
30 | }
|
---|
31 |
|
---|
32 | char *buffer = new char[finfo.getFileSize()];
|
---|
33 | if (buffer == NULL)
|
---|
34 | { return false;
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (fread(buffer, 1, finfo.getFileSize(), f) < finfo.getFileSize())
|
---|
38 | { return false;
|
---|
39 | }
|
---|
40 | fclose(f);
|
---|
41 |
|
---|
42 | //parse file into lines and send to the line fn
|
---|
43 | line = buffer;
|
---|
44 | while ((eol = strchr(line, '\x0a')) != NULL) // use 0a for the eol to remove
|
---|
45 | // dependency on dos 0d,0a eol
|
---|
46 | { *eol = '\0';
|
---|
47 |
|
---|
48 | this->getLine(line, currentGroup);
|
---|
49 |
|
---|
50 | line = eol + 1;
|
---|
51 | }
|
---|
52 | this->getLine(line, currentGroup);
|
---|
53 |
|
---|
54 | delete buffer;
|
---|
55 | return true;
|
---|
56 | }
|
---|
57 |
|
---|
58 | bool unInstaller::getLine(char *line, string &group)
|
---|
59 | { char *eol; // we (potentially) need to clean up the end of
|
---|
60 | // line, and we won't use the eol from the main
|
---|
61 | // getManifest fn. as for the last line it'd be
|
---|
62 | // null.
|
---|
63 |
|
---|
64 | // clean up the start of the line
|
---|
65 | while (*line <= ' ' && *line != '\0')
|
---|
66 | { line ++;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // it's a blank line - don't bother!
|
---|
70 | if (line == '\0')
|
---|
71 | { return true;
|
---|
72 | }
|
---|
73 |
|
---|
74 | // clean up the end of line
|
---|
75 | eol = line + strlen(line) - 1;
|
---|
76 | while (*eol <= ' ' && eol >= line)
|
---|
77 | { *eol = '\0';
|
---|
78 | eol --;
|
---|
79 | }
|
---|
80 |
|
---|
81 | // if it's a new group, read in the group name
|
---|
82 | if (*line == '[' && *eol == ']')
|
---|
83 | { string newgroup(line, 1, eol - line - 1);
|
---|
84 | group = newgroup;
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 | // otherwise it should be a command
|
---|
88 | else
|
---|
89 | { char *equals = strchr(line, '=');
|
---|
90 |
|
---|
91 | // if it has no equals it's invalid; ignore it
|
---|
92 | if (equals == NULL)
|
---|
93 | { return true;
|
---|
94 | }
|
---|
95 | else
|
---|
96 | { unInstallCommand command;
|
---|
97 |
|
---|
98 | string cmd(line, 0, equals-line);
|
---|
99 | command.command = cmd;
|
---|
100 |
|
---|
101 | stringArray params = stringArray::words(equals+1);
|
---|
102 | command.parameters = params;
|
---|
103 |
|
---|
104 | this->_map[group].push_back(command);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | return true;
|
---|
108 | }
|
---|
109 |
|
---|
110 | bool unInstaller::saveToFile(string fileName)
|
---|
111 | { unInstallCommandMap::iterator here = this->_map.begin();
|
---|
112 | unInstallCommandMap::iterator end = this->_map.end();
|
---|
113 | string group = "";
|
---|
114 |
|
---|
115 | FILE *f;
|
---|
116 | f = fopen(fileName.c_str(), "wb");
|
---|
117 | if (f == NULL)
|
---|
118 | { return false;
|
---|
119 | }
|
---|
120 |
|
---|
121 | while (here != end)
|
---|
122 | { // if we've changed groups, note the start of the new one
|
---|
123 | fprintf(f, "[%s]\n", (*here).first.c_str());
|
---|
124 |
|
---|
125 | // write the group line-by-line
|
---|
126 | unInstallCommandList::iterator groupHere = (*here).second.begin();
|
---|
127 | unInstallCommandList::iterator groupEnd = (*here).second.end();
|
---|
128 | while (groupHere != groupEnd)
|
---|
129 | { fprintf(f, "%s=", (*groupHere).command.c_str());
|
---|
130 |
|
---|
131 | fprintf(f, "%s", (*groupHere).parameters[0].c_str());
|
---|
132 | for (unsigned int p = 1; p < (*groupHere).parameters.size(); p ++)
|
---|
133 | { fprintf(f, " %s", (*groupHere).parameters[p].c_str());
|
---|
134 | }
|
---|
135 | groupEnd ++;
|
---|
136 | }
|
---|
137 | here ++;
|
---|
138 | }
|
---|
139 | fclose(f);
|
---|
140 | return true;
|
---|
141 | }
|
---|
142 |
|
---|
143 | void unInstaller::unInstall(string &listName)
|
---|
144 | { if (listName == uninstall_ALL)
|
---|
145 | { unInstallCommandMap::reverse_iterator here = this->_map.rbegin();
|
---|
146 | unInstallCommandMap::reverse_iterator end = this->_map.rend();
|
---|
147 |
|
---|
148 | while (here != end)
|
---|
149 | { this->unInstallGroup((*here).second);
|
---|
150 | here ++;
|
---|
151 | }
|
---|
152 | this->_map.empty();
|
---|
153 | }
|
---|
154 | else
|
---|
155 | { this->unInstallGroup(this->_map[listName]);
|
---|
156 | this->_map.erase(listName);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | void unInstaller::unInstallGroup(unInstallCommandList &list)
|
---|
161 | { for (int i = list.size() - 1; i >= 0; i --)
|
---|
162 | { // execute uninstall
|
---|
163 | this->execCommand(list[i].command, list[i].parameters);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | bool unInstaller::execCommand(string &name, stringArray ¶ms)
|
---|
168 | { // undo the command
|
---|
169 | return true;
|
---|
170 | }
|
---|
171 |
|
---|
172 | bool unInstaller::record(string group, string cmd, string parameter)
|
---|
173 | { unInstallCommand command;
|
---|
174 |
|
---|
175 | command.command = cmd;
|
---|
176 | command.parameters.add(parameter);
|
---|
177 |
|
---|
178 | this->_map[group].push_back(command);
|
---|
179 | return true;
|
---|
180 | }
|
---|
181 |
|
---|
182 | bool unInstaller::record(string group, string cmd, int count, ...)
|
---|
183 | { va_list list;
|
---|
184 |
|
---|
185 | va_start(list, count);
|
---|
186 | unInstallCommand command;
|
---|
187 |
|
---|
188 | command.command = cmd;
|
---|
189 | command.parameters.add(*va_arg(list, string *));
|
---|
190 | va_end(list);
|
---|
191 |
|
---|
192 | this->_map[group].push_back(command);
|
---|
193 |
|
---|
194 | return true;
|
---|
195 | }
|
---|