lib/rdconfig.c

changeset 7
d74b26b2f217
child 8
e584bc0177df
equal deleted inserted replaced
6:9db76e20e21e 7:d74b26b2f217
1 /*****************************************************************************
2 * Copyright (C) 2008-2014
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of the mbsePi-apps
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * mbsePi-apps is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with EC-65K; see the file COPYING. If not, write to the Free
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/
22
23 #include "../config.h"
24 #include "mbselib.h"
25
26 // #define DEBUG_CONFIG 1
27
28
29 extern int DebugEmu;
30 extern char *Private_Path;
31
32 static char *mypath;
33 static char *k, *v;
34 static int linecnt = 0;
35
36 extern int MustSave;
37
38
39 sys_config Config; /* System configuration */
40
41
42
43 static int getstr(char **);
44 static int getint(char **);
45 //static int getbyt(char **);
46 //static int gethex(char **);
47
48
49
50 /*
51 * System configuration table
52 */
53 key_list keytab[] = {
54 {(char *)"name", getstr, &Config.name},
55 {(char *)"mosq_server", getstr, &Config.mosq_server},
56 {(char *)"mosq_port", getint, (char **)&Config.mosq_port},
57 {NULL, NULL, NULL}
58 };
59
60
61
62 void killconfig(void)
63 {
64 if (Config.name)
65 free(Config.name);
66 Config.name = NULL;
67
68 }
69
70
71
72 int wrconfig(char *machine)
73 {
74 FILE *fp;
75
76 /*
77 * Get config from the system path
78 */
79 mypath = xstrcpy(Private_Path);
80 mypath = xstrcat(mypath, (char *)"/data/machines/");
81 mypath = xstrcat(mypath, machine);
82 mypath = xstrcat(mypath, (char *)".conf");
83
84 if ((fp = fopen(mypath, "w")) == NULL) {
85 // Log_Msg("[rdconfig] could not rewrite %s", mypath);
86 return 1;
87 }
88
89 fprintf(fp, "# Configuration file for EC65K %s\n", VERSION);
90 fprintf(fp, "# Machine: `%s'\n", machine);
91 fprintf(fp, "#\n");
92 // fprintf(fp, "description %s\n", Config.description);
93 // fprintf(fp, "\n");
94 // fprintf(fp, "# 65(C)02 CPU\n");
95 // fprintf(fp, "#\n");
96
97 fprintf(fp, "# End of generated configuration\n");
98 fclose(fp);
99 free(mypath);
100 mypath = NULL;
101 MustSave = FALSE;
102
103 return 0;
104 }
105
106
107
108 int rdconfig(char *machine)
109 {
110 char buf[256], *p;
111 FILE *fp;
112 int i, rc = 0;
113
114 killconfig();
115
116 /*
117 * Get config from the system path
118 */
119 mypath = xstrcpy(Private_Path);
120 mypath = xstrcat(mypath, (char *)"/data/machines/");
121 mypath = xstrcat(mypath, machine);
122 mypath = xstrcat(mypath, (char *)".conf");
123
124 if ((fp = fopen(mypath, "r")) == NULL) {
125 // Log_Msg("[rdconfig] could not open %s", mypath);
126 return 1;
127 }
128
129 linecnt = 0;
130 while (fgets(buf, sizeof(buf) -1, fp)) {
131 linecnt++;
132 if (*(p = buf + strlen(buf) -1) != '\n') {
133 // Log_Msg("[rdconfig] %s(%d): \"%s\" - line too long", mypath, linecnt, buf);
134 rc = 1;
135 break;
136 }
137 *p-- = '\0';
138 while ((p >= buf) && isspace(*p))
139 *p-- = '\0';
140 k = buf;
141 while (*k && isspace(*k))
142 k++;
143 p = k;
144 while (*p && !isspace(*p))
145 p++;
146 *p++='\0';
147 v = p;
148 while (*v && isspace(*v))
149 v++;
150
151 if ((*k == '\0') || (*k == '#')) {
152 continue;
153 }
154
155 for (i = 0; keytab[i].key; i++)
156 if (strcasecmp(k,keytab[i].key) == 0)
157 break;
158
159 if (keytab[i].key == NULL) {
160 // Log_Msg("[rdconfig] %s(%d): %s %s - unknown keyword", mypath, linecnt, S(k), S(v));
161 rc = 1;
162 break;
163 } else if ((keytab[i].prc(keytab[i].dest))) {
164 rc = 1;
165 break;
166 }
167
168 }
169 fclose(fp);
170
171 free(mypath);
172 mypath = NULL;
173 MustSave = FALSE;
174
175 return rc;
176 }
177
178
179
180 static int getstr(char **dest)
181 {
182 // Log_Msg("[rdconfig] getstr: %s(%d): %s %s", mypath, linecnt, S(k), S(v));
183 *dest = xstrcpy(v);
184 return 0;
185 }
186
187
188
189 static int getint(char **dest)
190 {
191 // Log_Msg("[rdconfig] getint: %s(%d): %s %s", mypath, linecnt, k, v);
192 // if (strspn(v,"0123456789") != strlen(v))
193 // Log_Msg("[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v));
194 // else
195 *((int*)dest)=atoi(v);
196 return 0;
197 }
198
199
200 /*
201 static int getbyt(char **dest)
202 {
203 Log_Msg("[rdconfig] getbyt: %s(%d): %s %s", mypath, linecnt, k, v);
204 if (strspn(v,"0123456789") != strlen(v))
205 Log_Msg("[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v));
206 else
207 *((Uint8*)dest)=atoi(v);
208 return 0;
209 }
210 */
211
212
213 /*
214 static int gethex(char **dest)
215 {
216 unsigned int val = 0;
217 int rc;
218
219 Log_Msg("[rdconfig] gethex: %s(%d): %s %s", mypath, linecnt, k, v);
220 rc = sscanf(v, "%08x", &val);
221 if (rc != 1) {
222 Log_Msg("[rdconfig] %s(%d): %s %s - bad hex value", mypath, linecnt, S(k), S(v));
223 return 1;
224 }
225 *((int*)dest) = val;
226
227 return 0;
228 }
229 */
230
231

mercurial