1 | #include "unInstall.h"
|
---|
2 |
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <stdarg.h>
|
---|
5 | #include "File.h"
|
---|
6 |
|
---|
7 | bool installManager::openLog(string filename, bool write)
|
---|
8 | { string command;
|
---|
9 | stringArray params;
|
---|
10 |
|
---|
11 | if (this->logfile.rdbuf()->is_open())
|
---|
12 | { this->closeLog();
|
---|
13 | }
|
---|
14 | this->logfileName = filename;
|
---|
15 | this->logfile.open(filename.c_str(), ios::out | ios::in);
|
---|
16 | if (write == false)
|
---|
17 | { while ((command = this->readCommand(params)) != "")
|
---|
18 | { if (command[0] == '[' && command[command.length()-1] == ']')
|
---|
19 | { this->setModule(command.substr(1, command.length() - 2));
|
---|
20 | }
|
---|
21 | else
|
---|
22 | { unInstallCommand action(command, params);
|
---|
23 | this->storeCommand(action);
|
---|
24 | }
|
---|
25 | }
|
---|
26 | }
|
---|
27 | this->setModule("default");
|
---|
28 | return true;
|
---|
29 | }
|
---|
30 |
|
---|
31 | bool installManager::reopenLog()
|
---|
32 | { if (this->logfile.rdbuf()->is_open())
|
---|
33 | { this->logfile.close();
|
---|
34 | }
|
---|
35 | else
|
---|
36 | { this->logfile.close();
|
---|
37 | }
|
---|
38 |
|
---|
39 | this->logfile.open(this->logfileName.c_str(), ios::out | ios::in);
|
---|
40 | return true;
|
---|
41 | }
|
---|
42 |
|
---|
43 | void installManager::setModule(string moduleName)
|
---|
44 | { this->currentModule = moduleName;
|
---|
45 | }
|
---|
46 |
|
---|
47 | bool installManager::storeCommand(unInstallCommand &command)
|
---|
48 | { this->modules[this->currentModule].push_back(command);
|
---|
49 | return true;
|
---|
50 | }
|
---|
51 |
|
---|
52 | bool installManager::writeCommand(unInstallCommand &command)
|
---|
53 | { if (!this->writeString(command.command))
|
---|
54 | { return false;
|
---|
55 | }
|
---|
56 | for (unsigned int p = 0; p < command.parameters.size(); p ++)
|
---|
57 | { if (!this->writeSeparator())
|
---|
58 | { return false;
|
---|
59 | }
|
---|
60 | if (!this->writeString(command.parameters[p]))
|
---|
61 | { return false;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | if (!this->writeString("\n"))
|
---|
65 | { return false;
|
---|
66 | }
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 |
|
---|
70 | bool installManager::writeString(char *buffer)
|
---|
71 | { string s(buffer);
|
---|
72 | return this->writeString(s);
|
---|
73 | }
|
---|
74 |
|
---|
75 | bool installManager::writeString(string str)
|
---|
76 | { bool quote;
|
---|
77 |
|
---|
78 | // TODO: check for space characters in str and quote if necessary
|
---|
79 | quote = str.find_first_of(' ') < str.length();
|
---|
80 |
|
---|
81 | if (quote)
|
---|
82 | { this->logfile << "\"";
|
---|
83 | }
|
---|
84 | this->logfile << str;
|
---|
85 | if (quote)
|
---|
86 | { this->logfile << "\"";
|
---|
87 | }
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 |
|
---|
91 | bool installManager::writeSeparator()
|
---|
92 | { this->logfile << " ";
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|
96 | string installManager::readString()
|
---|
97 | { string reply = "";
|
---|
98 | char c;
|
---|
99 |
|
---|
100 | if (this->logfile.eof())
|
---|
101 | { return reply;
|
---|
102 | }
|
---|
103 |
|
---|
104 | this->logfile >> c;
|
---|
105 | while (c <= ' ' && !this->logfile.eof())
|
---|
106 | { this->logfile.get();
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (this->logfile.eof())
|
---|
110 | { return reply;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (c == '\"')
|
---|
114 | { do
|
---|
115 | { c = this->logfile.get();
|
---|
116 | if (c != '\"')
|
---|
117 | { reply += c;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | while (c != '\"' && !this->logfile.eof());
|
---|
121 | }
|
---|
122 | else
|
---|
123 | { while (c > ' ')
|
---|
124 | { reply += c;
|
---|
125 | if (this->logfile.eof())
|
---|
126 | { break;
|
---|
127 | }
|
---|
128 | c = this->logfile.get();
|
---|
129 | }
|
---|
130 | if (!this->logfile.eof())
|
---|
131 | { this->logfile.putback(c);
|
---|
132 | }
|
---|
133 | //this->logfile >> reply;
|
---|
134 | }
|
---|
135 | return reply;
|
---|
136 | }
|
---|
137 |
|
---|
138 | string installManager::readCommand(stringArray &array)
|
---|
139 | { string reply = "";
|
---|
140 | char c;
|
---|
141 |
|
---|
142 | array.clear();
|
---|
143 |
|
---|
144 | if (this->logfile.eof())
|
---|
145 | { return reply;
|
---|
146 | }
|
---|
147 |
|
---|
148 | reply = this->readString();
|
---|
149 | if (reply == "")
|
---|
150 | { return reply;
|
---|
151 | }
|
---|
152 |
|
---|
153 | while(!this->logfile.eof() &&
|
---|
154 | (c = this->logfile.get()) != '\n')
|
---|
155 | { this->logfile.putback(c);
|
---|
156 | array.add(this->readString());
|
---|
157 | }
|
---|
158 | if (!this->logfile.eof())
|
---|
159 | { this->logfile.putback(c);
|
---|
160 | }
|
---|
161 |
|
---|
162 | return reply;
|
---|
163 | }
|
---|
164 |
|
---|
165 | string installManager::popCommand(stringArray &array)
|
---|
166 | { string command;
|
---|
167 |
|
---|
168 | if (this->modules[this->currentModule].size() == 0)
|
---|
169 | { command = "";
|
---|
170 | }
|
---|
171 | else
|
---|
172 | { unsigned int last;
|
---|
173 |
|
---|
174 | last = this->modules[this->currentModule].size() - 1;
|
---|
175 | command = this->modules[this->currentModule][last].commandName();
|
---|
176 | array = this->modules[this->currentModule][last].parameterList();
|
---|
177 | this->modules[this->currentModule].erase(this->modules[this->currentModule].begin() + last);
|
---|
178 | }
|
---|
179 | return command;
|
---|
180 | }
|
---|
181 |
|
---|
182 | bool installManager::closeLog()
|
---|
183 | { if (this->logfile.rdbuf()->is_open() == false)
|
---|
184 | { return false;
|
---|
185 | }
|
---|
186 |
|
---|
187 | unInstallCommandMap::iterator here = this->modules.begin();
|
---|
188 | unInstallCommandMap::iterator end = this->modules.end();
|
---|
189 |
|
---|
190 | while (here != end)
|
---|
191 | { this->writeString("[" + (*here).first + "]\n");
|
---|
192 |
|
---|
193 | unInstallCommandList::iterator ahere = (*here).second.begin();
|
---|
194 | unInstallCommandList::iterator aend = (*here).second.end();
|
---|
195 | while (ahere != aend)
|
---|
196 | { this->writeCommand(*ahere);
|
---|
197 | ahere ++;
|
---|
198 | }
|
---|
199 | here ++;
|
---|
200 | }
|
---|
201 | this->logfile.close();
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 |
|
---|
205 | bool installManager::isEmpty()
|
---|
206 | { unInstallCommandMap::iterator here = this->modules.begin();
|
---|
207 | unInstallCommandMap::iterator end = this->modules.end();
|
---|
208 |
|
---|
209 | while (here != end)
|
---|
210 | { this->writeString("[" + (*here).first + "]\n");
|
---|
211 |
|
---|
212 | unInstallCommandList::iterator ahere = (*here).second.begin();
|
---|
213 | unInstallCommandList::iterator aend = (*here).second.end();
|
---|
214 | if (ahere != aend)
|
---|
215 | { return false;
|
---|
216 | }
|
---|
217 | here ++;
|
---|
218 | }
|
---|
219 | return true;
|
---|
220 | }
|
---|
221 |
|
---|
222 | installManager::~installManager()
|
---|
223 | { if (this->logfile.rdbuf()->is_open())
|
---|
224 | { this->closeLog();
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|