provide SIZE_MAX
[AROS.git] / workbench / prefs / palette / prefs.c
blob934af518913f7b4410ffa5ad2184b1b8b1ea742b
1 /*
2 Copyright © 2010, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /*********************************************************************************************/
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
15 #include <aros/macros.h>
17 /* #define DEBUG 1 */
18 #include <aros/debug.h>
20 #include <proto/exec.h>
21 #include <proto/iffparse.h>
22 #include <proto/dos.h>
24 #include <prefs/prefhdr.h>
26 #include "prefs.h"
27 #include "misc.h"
29 /*********************************************************************************************/
31 #define PREFS_PATH_ENVARC "ENVARC:SYS/palette.prefs"
32 #define PREFS_PATH_ENV "ENV:SYS/palette.prefs"
34 /*********************************************************************************************/
36 STATIC UWORD defaultpen[32] =
38 0, 1, 1, 2, 1, 3, 1, 0, 2, 1, 2, 1, 65535
41 STATIC struct ColorSpec defaultcolor[32] =
43 {0, 43690, 43690, 43690 },
44 {1, 0, 0, 0 },
45 {2, 65535, 65535, 65535 },
46 {3, 21845, 30583, 43690 },
47 {4, 26214, 26214, 26214 },
48 {5, 61166, 61166, 61166 },
49 {6, 56797, 30583, 17476 },
50 {7, 65535, 61166, 4369 },
51 {65535 }
54 /*********************************************************************************************/
55 struct FilePrefHeader
57 UBYTE ph_Version;
58 UBYTE ph_Type;
59 UBYTE ph_Flags[4];
62 /*********************************************************************************************/
64 struct PalettePrefs paletteprefs;
66 /*********************************************************************************************/
68 static BOOL Prefs_Load(STRPTR from)
70 BOOL retval = FALSE;
72 BPTR fh = Open(from, MODE_OLDFILE);
73 if (fh)
75 retval = Prefs_ImportFH(fh);
76 Close(fh);
79 return retval;
82 /*********************************************************************************************/
84 BOOL Prefs_ImportFH(BPTR fh)
86 struct PalettePrefs loadprefs;
87 struct IFFHandle *iff;
88 BOOL retval = FALSE;
89 LONG i;
91 if ((iff = AllocIFF()))
93 iff->iff_Stream = (IPTR)fh;
95 InitIFFasDOS(iff);
97 if (!OpenIFF(iff, IFFF_READ))
99 D(bug("Prefs_ImportFH: OpenIFF okay.\n"));
101 if (!StopChunk(iff, ID_PREF, ID_PALT))
103 D(bug("Prefs_ImportFH: StopChunk okay.\n"));
105 if (!ParseIFF(iff, IFFPARSE_SCAN))
107 struct ContextNode *cn;
109 D(bug("Prefs_ImportFH: ParseIFF okay.\n"));
111 cn = CurrentChunk(iff);
113 if (cn->cn_Size == sizeof(struct PalettePrefs))
115 D(bug("Prefs_ImportFH: ID_PALT chunk size okay.\n"));
117 if (ReadChunkBytes(iff, &loadprefs, sizeof(struct PalettePrefs)) == sizeof(struct PalettePrefs))
119 D(bug("Prefs_ImportFH: Reading chunk successful.\n"));
121 CopyMem(loadprefs.pap_Reserved, paletteprefs.pap_Reserved, sizeof(paletteprefs.pap_Reserved));
122 for (i = 0; i < 32; i++)
124 paletteprefs.pap_4ColorPens[i] = AROS_BE2WORD(loadprefs.pap_4ColorPens[i]);
125 paletteprefs.pap_8ColorPens[i] = AROS_BE2WORD(loadprefs.pap_8ColorPens[i]);
126 paletteprefs.pap_Colors[i].ColorIndex = AROS_BE2WORD(loadprefs.pap_Colors[i].ColorIndex);
127 paletteprefs.pap_Colors[i].Red = AROS_BE2WORD(loadprefs.pap_Colors[i].Red);
128 paletteprefs.pap_Colors[i].Green = AROS_BE2WORD(loadprefs.pap_Colors[i].Green);
129 paletteprefs.pap_Colors[i].Blue = AROS_BE2WORD(loadprefs.pap_Colors[i].Blue);
132 D(bug("Prefs_ImportFH: Everything okay :-)\n"));
134 retval = TRUE;
137 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
138 } /* if (!StopChunk(iff, ID_PREF, ID_SERL)) */
139 CloseIFF(iff);
140 } /* if (!OpenIFF(iff, IFFF_READ)) */
141 FreeIFF(iff);
142 } /* if ((iff = AllocIFF())) */
144 return retval;
147 /*********************************************************************************************/
149 BOOL Prefs_ExportFH(BPTR fh)
151 struct PalettePrefs saveprefs;
152 struct IFFHandle *iff;
153 BOOL retval = FALSE;
154 BOOL delete_if_error = FALSE;
155 LONG i;
157 CopyMem(paletteprefs.pap_Reserved, saveprefs.pap_Reserved, sizeof(paletteprefs.pap_Reserved));
158 for (i = 0; i < 32; i++)
160 saveprefs.pap_4ColorPens[i] = AROS_WORD2BE(paletteprefs.pap_4ColorPens[i]);
161 saveprefs.pap_8ColorPens[i] = AROS_WORD2BE(paletteprefs.pap_8ColorPens[i]);
162 saveprefs.pap_Colors[i].ColorIndex = AROS_WORD2BE(paletteprefs.pap_Colors[i].ColorIndex);
163 saveprefs.pap_Colors[i].Red = AROS_WORD2BE(paletteprefs.pap_Colors[i].Red);
164 saveprefs.pap_Colors[i].Green = AROS_WORD2BE(paletteprefs.pap_Colors[i].Green);
165 saveprefs.pap_Colors[i].Blue = AROS_WORD2BE(paletteprefs.pap_Colors[i].Blue);
168 D(bug("Prefs_ExportFH: fh: %lx\n", fh));
170 if ((iff = AllocIFF()))
172 iff->iff_Stream = (IPTR) fh;
173 D(bug("Prefs_ExportFH: stream opened.\n"));
175 delete_if_error = TRUE;
177 InitIFFasDOS(iff);
179 if (!OpenIFF(iff, IFFF_WRITE))
181 D(bug("Prefs_ExportFH: OpenIFF okay.\n"));
183 if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN))
185 D(bug("Prefs_ExportFH: PushChunk(FORM) okay.\n"));
187 if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct FilePrefHeader)))
189 struct FilePrefHeader head;
191 D(bug("Prefs_ExportFH: PushChunk(PRHD) okay.\n"));
193 head.ph_Version = PHV_CURRENT;
194 head.ph_Type = 0;
195 head.ph_Flags[0] =
196 head.ph_Flags[1] =
197 head.ph_Flags[2] =
198 head.ph_Flags[3] = 0;
200 if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head))
202 D(bug("Prefs_ExportFH: WriteChunkBytes(PRHD) okay.\n"));
204 PopChunk(iff);
206 if (!PushChunk(iff, ID_PREF, ID_PALT, sizeof(struct PalettePrefs)))
208 D(bug("Prefs_ExportFH: PushChunk(LCLE) okay.\n"));
210 if (WriteChunkBytes(iff, &saveprefs, sizeof(saveprefs)) == sizeof(saveprefs))
212 D(bug("Prefs_ExportFH: WriteChunkBytes(PALT) okay.\n"));
213 D(bug("Prefs_ExportFH: Everything okay :-)\n"));
215 retval = TRUE;
217 PopChunk(iff);
218 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
219 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
220 else
222 PopChunk(iff);
224 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
225 PopChunk(iff);
226 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
227 CloseIFF(iff);
228 } /* if (!OpenIFF(iff, IFFFWRITE)) */
229 FreeIFF(iff);
231 } /* if ((iff = AllocIFF())) */
233 #if 0
234 if (!retval && delete_if_error)
236 DeleteFile(filename);
238 #endif
240 return retval;
243 /*********************************************************************************************/
245 BOOL Prefs_HandleArgs(STRPTR from, BOOL use, BOOL save)
247 BPTR fh;
249 if (from)
251 if (!Prefs_Load(from))
253 ShowMessage("Can't read from input file");
254 return FALSE;
257 else
259 if (!Prefs_Load(PREFS_PATH_ENV))
261 if (!Prefs_Load(PREFS_PATH_ENVARC))
263 ShowMessage
265 "Can't read from file " PREFS_PATH_ENVARC
266 ".\nUsing default values."
268 Prefs_Default();
273 if (use || save)
275 fh = Open(PREFS_PATH_ENV, MODE_NEWFILE);
276 if (fh)
278 Prefs_ExportFH(fh);
279 Close(fh);
281 else
283 ShowMessage("Cant' open " PREFS_PATH_ENV " for writing.");
286 if (save)
288 fh = Open(PREFS_PATH_ENVARC, MODE_NEWFILE);
289 if (fh)
291 Prefs_ExportFH(fh);
292 Close(fh);
294 else
296 ShowMessage("Cant' open " PREFS_PATH_ENVARC " for writing.");
299 return TRUE;
302 /*********************************************************************************************/
304 BOOL Prefs_Default(VOID)
306 paletteprefs.pap_Reserved[0] = 0;
307 paletteprefs.pap_Reserved[1] = 0;
308 paletteprefs.pap_Reserved[2] = 0;
309 paletteprefs.pap_Reserved[3] = 0;
311 CopyMem(defaultpen, paletteprefs.pap_4ColorPens, sizeof paletteprefs.pap_4ColorPens);
312 CopyMem(defaultpen, paletteprefs.pap_8ColorPens, sizeof paletteprefs.pap_8ColorPens);
313 CopyMem(defaultcolor, paletteprefs.pap_Colors, sizeof paletteprefs.pap_Colors);
315 return TRUE;