more rendering corrections
[AROS.git] / workbench / tools / HDToolBox / prefs.c
blob8ed54c5f93745f995208652a19e89637dd46c518
1 /*
2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/dos.h>
7 #include <proto/exec.h>
8 #include <proto/icon.h>
10 #include <dos/dos.h>
11 #include <dos/rdargs.h>
12 #include <exec/lists.h>
13 #include <exec/memory.h>
14 #include <workbench/icon.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <strings.h>
20 #define DEBUG 0
21 #include "debug.h"
23 #include "prefs.h"
24 #include "devices.h"
25 #include "hdtoolbox_support.h"
26 #include "platform.h"
28 struct List tabletypelist;
30 struct TableTypeNode *findTableTypeNodeName(STRPTR name)
32 struct TableTypeNode *ttn;
34 D(bug("[HDToolBox] findTableTypeNodeName('%s')\n", name));
36 ttn = (struct TableTypeNode *)tabletypelist.lh_Head;
37 while (ttn->ln.ln_Succ)
39 if (strcmp(ttn->pti->pti_Name, name) == 0)
40 return ttn;
41 ttn = (struct TableTypeNode *)ttn->ln.ln_Succ;
43 return NULL;
46 struct TableTypeNode *findTableTypeNode(ULONG tabletype)
48 struct TableTypeNode *ttn;
50 D(bug("[HDToolBox] findTableTypeNode()\n"));
52 ttn = (struct TableTypeNode *)tabletypelist.lh_Head;
53 while (ttn->ln.ln_Succ)
55 if (ttn->pti->pti_Type == tabletype)
56 return ttn;
57 ttn = (struct TableTypeNode *)ttn->ln.ln_Succ;
59 return NULL;
62 struct TypeNode *findPartitionType(struct PartitionType *type, ULONG tabletype)
64 struct TableTypeNode *ttn;
65 struct TypeNode *tn;
67 D(bug("[HDToolBox] findPartitionType()\n"));
69 ttn = findTableTypeNode(tabletype);
70 if (ttn)
72 tn = (struct TypeNode *)ttn->typelist->lh_Head;
73 while (tn->ln.ln_Succ)
75 if (tn->type.id_len == type->id_len)
77 if (memcmp(tn->type.id, type->id, type->id_len) == 0)
78 return tn;
80 tn = (struct TypeNode *)tn->ln.ln_Succ;
83 return NULL;
86 void getTableTypeList(struct List *list)
88 struct TableTypeNode *ttn;
89 int i;
91 D(bug("[HDToolBox] getTableTypeList()\n"));
93 NEWLIST(&tabletypelist);
94 for (i=0;PartitionBase->tables[i];i++)
96 ttn = AllocMem(sizeof(struct TableTypeNode), MEMF_PUBLIC | MEMF_CLEAR);
97 if (ttn)
99 ttn->typelist =
100 AllocMem(sizeof(struct List), MEMF_PUBLIC | MEMF_CLEAR);
101 if (ttn->typelist != NULL)
103 ttn->pti = PartitionBase->tables[i];
104 NEWLIST(ttn->typelist);
105 AddTail(list, &ttn->ln);
107 else
109 FreeMem(ttn, sizeof(struct TableTypeNode));
110 ttn = NULL;
116 LONG parsePrefs(char *buffer, LONG size)
118 struct TableTypeNode *ttn=NULL;
119 struct TypeNode *tn;
120 struct CSource csrc = {buffer, size, 0};
121 struct DiskObject *hdtbicon=NULL;
122 char ident[256];
123 LONG res;
124 ULONG id_len = 0;
125 WORD current = 0;
126 WORD line = 1;
128 D(bug("[HDToolBox] parsePrefs()\n"));
130 while (csrc.CS_CurChr < csrc.CS_Length)
132 res = ReadItem(ident, 256, &csrc);
133 switch (res)
135 case ITEM_ERROR:
136 return IoErr();
137 case ITEM_UNQUOTED:
138 if (strcasecmp(ident, "[Devices]") == 0)
139 current = 1;
140 else if (strcasecmp(ident, "[TableIDs]") == 0)
142 current = 2;
143 ttn = NULL;
144 id_len = 0;
146 else
148 if (current == 1)
149 addDeviceName(ident);
150 else if (current == 2)
152 if (strcasecmp(ident, "TableType") == 0)
154 res = ReadItem(ident, 256, &csrc);
155 if (res == ITEM_ERROR)
156 return IoErr();
157 else if (res == ITEM_EQUAL)
159 res = ReadItem(ident, 256, &csrc);
160 if (res == ITEM_ERROR)
161 return IoErr();
162 else if (res == ITEM_QUOTED)
163 ttn = findTableTypeNodeName(ident);
164 else if (res == ITEM_UNQUOTED)
165 ttn = findTableTypeNode(strtoul(ident, NULL, 0));
166 else
168 printf("LINE %d: Unexpected item in TableType\n", line);
169 return 0;
171 if (ttn == 0)
173 printf("LINE %d: Unknown Table %s\n", line, ident);
174 return 0;
177 else
179 printf("LINE %d: Unexpected item after TableType\n", line);
180 return 0;
183 else if (strcasecmp(ident, "IDLen") == 0)
185 res = ReadItem(ident, 256, &csrc);
186 if (res == ITEM_ERROR)
187 return IoErr();
188 else if (res == ITEM_EQUAL)
190 res = ReadItem(ident, 256, &csrc);
191 if (res == ITEM_ERROR)
192 return IoErr();
193 else if (res == ITEM_UNQUOTED)
195 id_len = strtoul(ident, NULL, 0);
196 if (id_len == 0)
198 printf("LINE %d: Illegal value of IDLen\n", line);
199 return 0;
202 else
204 printf("LINE %d: Value in IDLen expected\n", line);
205 return 0;
208 else
210 printf("LINE %d: Unexpected item after IDLen\n", line);
211 return 0;
214 else if (strcasecmp(ident, "Default") == 0)
216 if (ttn && id_len)
218 res = ReadItem(ident, 256, &csrc);
219 if (res == ITEM_ERROR)
220 return IoErr();
221 else if (res == ITEM_EQUAL)
223 res = ReadItem(ident, 256, &csrc);
224 if (res == ITEM_ERROR)
225 return IoErr();
226 else if (res == ITEM_QUOTED)
228 ttn->defaulttype.id_len = id_len;
229 strcpyESC(ttn->defaulttype.id, ident);
231 else
233 printf("LINE %d: Unexpected expression after Default\n", line);
234 return 0;
237 else
239 printf("LINE %d: Unexpected item after IDLen\n", line);
240 return 0;
243 else
245 printf("LINE %d: Unknown option '%s'\n", line, ident);
246 return 0;
249 else
251 printf("LINE %d: Missing partition table type or IDLen\n", line);
252 return 0;
255 else
257 printf("LINE %d: Unexpected item '%s' in prefs\n", line, ident);
258 return 0;
261 break;
262 case ITEM_QUOTED:
263 if (ttn && id_len)
265 tn = AllocMem(sizeof(struct TypeNode), MEMF_PUBLIC | MEMF_CLEAR);
266 if (tn == NULL)
267 return ERROR_NO_FREE_STORE;
268 tn->type.id_len = id_len;
269 strcpyESC(tn->type.id, ident);
270 res = ReadItem(ident, 256, &csrc);
271 if (res == ITEM_ERROR)
272 return IoErr();
273 if (res == ITEM_EQUAL)
275 res = ReadItem(ident, 256, &csrc);
276 if (res == ITEM_ERROR)
277 return IoErr();
278 else if (res == ITEM_QUOTED)
280 tn->ln.ln_Name = AllocVec(strlen(ident)+1, MEMF_PUBLIC | MEMF_CLEAR);
281 if (tn->ln.ln_Name == NULL)
282 return ERROR_NO_FREE_STORE;
283 strcpy(tn->ln.ln_Name, ident);
285 else
287 printf("LINE %d: Quoted expression expected\n", line);
288 return 0;
291 else
293 printf("LINE %d: Unexpected item after table id\n", line);
294 return 0;
296 AddTail(ttn->typelist, &tn->ln);
298 else
300 printf("LINE %d: Missing partition table type or IDLen\n", line);
301 return 0;
303 break;
304 case ITEM_NOTHING:
305 line++;
306 break;
310 hdtbicon = GetIconTags("HDToolBox",
311 ICONGETA_FailIfUnavailable, TRUE,
312 ICONGETA_IsDefaultIcon, FALSE,
313 TAG_DONE);
315 if (hdtbicon != NULL)
317 D(bug("[HDToolBox] Got our Icon..\n"));
318 if (hdtbicon->do_ToolTypes)
320 char *tt = NULL, *devicename;
321 int i = 0;
322 D(bug("[HDToolBox] Icon has tooltypes..\n"));
324 while ((tt = hdtbicon->do_ToolTypes[i]) != NULL)
326 if (strncmp(hdtbicon->do_ToolTypes[i], "DEVICE=", 7) == 0)
328 devicename = hdtbicon->do_ToolTypes[i] + 7;
329 D(bug("[HDToolBox] Adding Device '%s' from ToolType\n", devicename));
330 addDeviceName(devicename);
332 i++;
337 // EBR uses same types as MBR
338 findTableTypeNode(PHPTT_EBR)->typelist =
339 findTableTypeNode(PHPTT_MBR)->typelist;
341 return 0;
344 void LoadPrefs(STRPTR filename)
346 struct FileInfoBlock fib;
347 char *buffer;
348 LONG retval;
349 LONG size;
350 BPTR fh;
352 D(bug("[HDToolBox] LoadPrefs('%s')\n", filename));
354 getTableTypeList(&tabletypelist);
355 fh = Open(filename, MODE_OLDFILE);
356 if (fh)
358 if (ExamineFH(fh, &fib))
360 if (fib.fib_Size>0)
362 buffer = AllocMem(fib.fib_Size, MEMF_PUBLIC | MEMF_CLEAR);
363 if (buffer)
365 size = Read(fh, buffer, fib.fib_Size);
366 if (size == fib.fib_Size)
368 retval = parsePrefs(buffer, size);
369 if (retval)
370 PrintFault(retval, filename);
372 FreeMem(buffer, fib.fib_Size);
374 else
375 PrintFault(ERROR_NO_FREE_STORE, filename);
378 Close(fh);
380 else
381 PrintFault(IoErr(), filename);