1 | #include "configFile.h"
|
---|
2 |
|
---|
3 | static char config_divider[] = ":";
|
---|
4 | static char config_eol[] = "\x0a";
|
---|
5 |
|
---|
6 | char *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 |
|
---|
22 | bool 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 |
|
---|
47 | void configureFile::get(char *label, char **to)
|
---|
48 | { int loop;
|
---|
49 |
|
---|
50 | for (loop = 0; loop < configstr->count; loop ++)
|
---|
51 | { if (strcmp(configstr->item[loop].label, label) == 0)
|
---|
52 | { *to = new char[strlen(configstr->item[loop].data) + 1];
|
---|
53 | if (to)
|
---|
54 | { strcpy(*to, configstr->item[loop].data);
|
---|
55 | return;
|
---|
56 | }
|
---|
57 | else
|
---|
58 | { //Acorn: werr(0, msgs_lookup("MemConfg"));
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | *to = NULL;
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | bool configureFile::put(char *label, char *value)
|
---|
68 | { int loop;
|
---|
69 | void *newstr;
|
---|
70 |
|
---|
71 | for (loop = 0; loop < configstr->count; loop ++)
|
---|
72 | { if (strcmp(configstr->item[loop].label, label) == 0)
|
---|
73 | { strcpy(configstr->item[loop].data, value);
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | newstr = realloc(configstr, sizeof(config_str) +
|
---|
80 | sizeof(config_item) * configstr->count);
|
---|
81 | if (!newstr)
|
---|
82 | { return false;
|
---|
83 | }
|
---|
84 | strcpy(configstr->item[configstr->count].label, label);
|
---|
85 | strcpy(configstr->item[configstr->count].data, value);
|
---|
86 | configstr->count ++;
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | bool configureFile::scan(char *block, unsigned int &pos)
|
---|
91 | { int off = 0;
|
---|
92 | void *newstr;
|
---|
93 |
|
---|
94 | newstr = realloc(configstr, sizeof(config_str) +
|
---|
95 | sizeof(config_item) * configstr->count);
|
---|
96 | if (!newstr)
|
---|
97 | { free(configstr);
|
---|
98 | configstr = NULL;
|
---|
99 | return false;
|
---|
100 | }
|
---|
101 | configstr = (config_str *) newstr;
|
---|
102 |
|
---|
103 | while (block[pos] != ':')
|
---|
104 | { configstr->item[configstr->count].label[off] = block[pos];
|
---|
105 | off ++;
|
---|
106 | pos ++;
|
---|
107 | }
|
---|
108 | configstr->item[configstr->count].label[off] = '\0';
|
---|
109 |
|
---|
110 | off = 0;
|
---|
111 | do
|
---|
112 | { pos ++;
|
---|
113 | } while (block[pos] <= ' ' && block[pos] != '\x0a' && block[pos] != '\x0d');
|
---|
114 |
|
---|
115 | while(block[pos] >= ' ')
|
---|
116 | { configstr->item[configstr->count].data[off] = block[pos];
|
---|
117 | pos ++;
|
---|
118 | off ++;
|
---|
119 | }
|
---|
120 | configstr->item[configstr->count].data[off] = '\0';
|
---|
121 |
|
---|
122 | do
|
---|
123 | { pos ++;
|
---|
124 | }
|
---|
125 | while (block[pos] <= ' ');
|
---|
126 |
|
---|
127 | configstr->count ++;
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | bool configureFile::store(char *name)
|
---|
132 | { char * varval;
|
---|
133 | int loop;
|
---|
134 | FILE * fhandle;
|
---|
135 |
|
---|
136 | varval = this->filename(name);
|
---|
137 |
|
---|
138 | fhandle = fopen(varval, "wb+");
|
---|
139 | if (fhandle)
|
---|
140 | { loop = 0;
|
---|
141 | while(loop < configstr->count)
|
---|
142 | { fprintf(fhandle, "%s%s%s%s", configstr->item[loop].label, config_divider, configstr->item[loop].data, config_eol);
|
---|
143 | loop ++;
|
---|
144 | }
|
---|
145 |
|
---|
146 | fclose(fhandle);
|
---|
147 | }
|
---|
148 |
|
---|
149 | //Acorn: free(varval);
|
---|
150 | return TRUE;
|
---|
151 | }
|
---|
152 |
|
---|
153 | configureFile::configureFile(char *name)
|
---|
154 | { unsigned int length, newlength;
|
---|
155 | unsigned int position;
|
---|
156 | char * varval;
|
---|
157 | char * block;
|
---|
158 | FILE * fhandle;
|
---|
159 |
|
---|
160 | configstr = (config_str *) malloc(sizeof(config_str));
|
---|
161 | if (!configstr)
|
---|
162 | { this->valid = false;
|
---|
163 | return;
|
---|
164 | }
|
---|
165 | configstr->count = 0;
|
---|
166 |
|
---|
167 | varval = this->filename(name);
|
---|
168 |
|
---|
169 | fhandle = fopen(varval, "rb");
|
---|
170 | if (fhandle)
|
---|
171 | { length = 0;
|
---|
172 |
|
---|
173 | //PC Method for fetching file
|
---|
174 | block = (char *) malloc(20);
|
---|
175 | while ((newlength = fread(&block[length], sizeof(char), 20, fhandle)) != 0)
|
---|
176 | { void *newblock;
|
---|
177 |
|
---|
178 | length += newlength;
|
---|
179 | newblock = realloc(block, length + 20);
|
---|
180 | if (!newblock)
|
---|
181 | { free(block);
|
---|
182 | this->valid = false;
|
---|
183 | fclose(fhandle);
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | block = (char *) newblock;
|
---|
187 | }
|
---|
188 | //end of PC method
|
---|
189 |
|
---|
190 | position = 0;
|
---|
191 | while (position < length)
|
---|
192 | { this->scan(block, position);
|
---|
193 | }
|
---|
194 |
|
---|
195 | fclose(fhandle);
|
---|
196 | }
|
---|
197 | else
|
---|
198 | { // No config file MessageBox(0, "oops", "oops", MB_OK);
|
---|
199 | }
|
---|
200 |
|
---|
201 | free(block);
|
---|
202 | //Acorn: free(varval);
|
---|
203 |
|
---|
204 | this->valid = true;
|
---|
205 | }
|
---|
206 |
|
---|
207 | configureFile::~configureFile()
|
---|
208 | { if (configstr)
|
---|
209 | { free(configstr);
|
---|
210 | configstr = NULL;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|