source: trunk/gsinstaller/configFile.cpp@ 3177

Last change on this file since 3177 was 1475, checked in by cs025, 24 years ago

Updated sources with most of uninstall added

  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1#include "configFile.h"
2
3static char config_divider[] = ":";
4static char config_eol[] = "\x0a";
5
6char *configureFile::filename(char *configFile)
7{ static char filename[512];
8 char *leaf;
9
10 GetModuleFileName(0, filename, 512);
11 leaf = strrchr(filename, '\\');
12 if (!leaf)
13 { return 0;
14 }
15
16 leaf ++;
17 strcpy(leaf, configFile);
18
19 return filename;
20}
21
22bool configureFile::concat(char *label, char *buffer, unsigned int &space)
23{ int loop;
24
25 // iterate through the list to find it
26 for (loop = 0; loop < configstr->count; loop ++)
27 { if (strcmp(configstr->item[loop].label, label) == 0)
28 { break;
29 }
30 }
31
32 // abort if not found
33 if (loop == configstr->count)
34 { return false;
35 }
36
37 // don't concatenate if there's no space
38 if (strlen(configstr->item[loop].data) > space)
39 { return false;
40 }
41
42 strcat(buffer, configstr->item[loop].data);
43 space -= strlen(configstr->item[loop].data);
44 return true;
45}
46
47string configureFile::getString(string label)
48{ for (int loop = 0; loop < configstr->count; loop ++)
49 { if (strcmp(configstr->item[loop].label, label.c_str()) == 0)
50 { return configstr->item[loop].data;
51 }
52 }
53 return "";
54}
55
56void configureFile::get(char *label, char **to)
57{ int loop;
58
59 for (loop = 0; loop < configstr->count; loop ++)
60 { if (strcmp(configstr->item[loop].label, label) == 0)
61 { *to = new char[strlen(configstr->item[loop].data) + 1];
62 if (to)
63 { strcpy(*to, configstr->item[loop].data);
64 return;
65 }
66 else
67 { //Acorn: werr(0, msgs_lookup("MemConfg"));
68 }
69 }
70 }
71
72 *to = NULL;
73 return;
74}
75
76bool configureFile::put(char *label, char *value)
77{ int loop;
78 void *newstr;
79
80 for (loop = 0; loop < configstr->count; loop ++)
81 { if (strcmp(configstr->item[loop].label, label) == 0)
82 { strcpy(configstr->item[loop].data, value);
83 return true;
84 }
85 }
86
87
88 newstr = realloc(configstr, sizeof(config_str) +
89 sizeof(config_item) * configstr->count);
90 if (!newstr)
91 { return false;
92 }
93 strcpy(configstr->item[configstr->count].label, label);
94 strcpy(configstr->item[configstr->count].data, value);
95 configstr->count ++;
96 return true;
97}
98
99/**
100 * Scan one line of the configuration file
101 */
102bool configureFile::scan(char *block, unsigned int &pos, unsigned int length)
103{ int off = 0;
104 void *newstr;
105
106 if (pos == length)
107 { return true;
108 }
109
110 // if it's a comment line, skip it straightaway
111 if (block[pos] == '#')
112 { while (block[pos] >= ' ')
113 { block ++;
114 }
115 while (block[pos] < ' ')
116 { block ++;
117 if (block[pos-1] == '\0')
118 { break;
119 }
120 }
121 return true;
122 }
123
124 // otherwise create a new struct to cope with the list of configuration items
125 newstr = realloc(configstr, sizeof(config_str) +
126 sizeof(config_item) * configstr->count);
127 if (!newstr)
128 { free(configstr);
129 configstr = NULL;
130 return false;
131 }
132 configstr = (config_str *) newstr;
133
134 // scan to the colon to get the label
135 while (pos < length && block[pos] != ':')
136 { configstr->item[configstr->count].label[off] = block[pos];
137 off ++;
138 pos ++;
139 }
140 configstr->item[configstr->count].label[off] = '\0';
141
142 // read until we get past any leading whitespace or tabs
143 off = 0;
144 if (pos < length)
145 { do
146 { pos ++;
147 }
148 while (block[pos] <= ' ' && block[pos] != '\x0a' && block[pos] != '\x0d');
149 }
150
151 // read until we reach a "magic" character - this is where we read the value
152 while(pos < length && block[pos] >= ' ')
153 { configstr->item[configstr->count].data[off] = block[pos];
154 pos ++;
155 off ++;
156 }
157 configstr->item[configstr->count].data[off] = '\0';
158
159 // skip any magic characters
160 if (pos < length)
161 { do
162 { pos ++;
163 }
164 while (block[pos] <= ' ' && pos < length);
165 }
166
167 // remember that we've added to the list
168 configstr->count ++;
169 return true;
170}
171
172bool configureFile::store(char *name)
173{ char * varval;
174 int loop;
175 FILE * fhandle;
176
177 varval = this->filename(name);
178
179 fhandle = fopen(varval, "wb+");
180 if (fhandle)
181 { loop = 0;
182 while(loop < configstr->count)
183 { fprintf(fhandle, "%s%s%s%s", configstr->item[loop].label, config_divider, configstr->item[loop].data, config_eol);
184 loop ++;
185 }
186
187 fclose(fhandle);
188 }
189
190//Acorn: free(varval);
191 return TRUE;
192}
193
194configureFile::configureFile(char *name)
195{ unsigned int length, newlength;
196 unsigned int position;
197 char * varval;
198 char * block;
199 FILE * fhandle;
200
201 configstr = (config_str *) malloc(sizeof(config_str));
202 if (!configstr)
203 { this->valid = false;
204 return;
205 }
206 configstr->count = 0;
207
208 varval = this->filename(name);
209
210 fhandle = fopen(varval, "rb");
211 if (fhandle)
212 { length = 0;
213
214 //PC Method for fetching file
215 block = (char *) malloc(20);
216 while ((newlength = fread(&block[length], sizeof(char), 20, fhandle)) != 0)
217 { void *newblock;
218
219 length += newlength;
220 newblock = realloc(block, length + 20);
221 if (!newblock)
222 { free(block);
223 this->valid = false;
224 fclose(fhandle);
225 return;
226 }
227 block = (char *) newblock;
228 }
229 //end of PC method
230
231 position = 0;
232 while (position < length)
233 { this->scan(block, position, length);
234 }
235
236 fclose(fhandle);
237
238 free(block);
239
240 this->valid = true;
241 }
242 else
243 { // No config file MessageBox(0, "oops", "oops", MB_OK);
244 this->valid = false;
245 }
246//Acorn: free(varval);
247
248}
249
250configureFile::~configureFile()
251{ if (configstr)
252 { free(configstr);
253 configstr = NULL;
254 }
255}
256
Note: See TracBrowser for help on using the repository browser.