revert between 56095 -> 55830 in arch
[AROS.git] / workbench / prefs / font / prefs.c
blobe4ac878c998a6c7c5d33f016ed1aea2dedf59415
1 #include <prefs/prefhdr.h>
3 #include <proto/exec.h>
4 #include <proto/dos.h>
5 #include <proto/iffparse.h>
7 #include <string.h>
8 #include <stdio.h>
10 #include "prefs.h"
11 #include "misc.h"
12 #include "locale.h"
14 #define PREFS_PATH_ENVARC "ENVARC:SYS/font.prefs"
15 #define PREFS_PATH_ENV "ENV:SYS/font.prefs"
17 /*** Utility Functions ******************************************************/
18 static VOID convertEndian(struct FontPrefs *fp)
20 BYTE i;
22 for (i = 0; i <= 2; i++)
24 fp->fp_Reserved[i] = AROS_BE2LONG(fp->fp_Reserved[i]);
27 fp->fp_Reserved2 = AROS_BE2WORD(fp->fp_Reserved2);
28 fp->fp_Type = AROS_BE2WORD(fp->fp_Type);
29 fp->fp_TextAttr.ta_YSize = AROS_BE2WORD(fp->fp_TextAttr.ta_YSize);
32 static VOID FileFontPrefs2FontPrefs(struct FileFontPrefs *ffp, struct FontPrefs *fp)
34 /* Copy field by field to avoid any alignment problems whatsoever */
35 CopyMem(&ffp->fp_Reserved, &fp->fp_Reserved, sizeof(fp->fp_Reserved));
36 CopyMem(&ffp->fp_Reserved2, &fp->fp_Reserved2, sizeof(fp->fp_Reserved2));
37 CopyMem(&ffp->fp_Type, &fp->fp_Type, sizeof(fp->fp_Type));
38 fp->fp_FrontPen = ffp->fp_FrontPen;
39 fp->fp_BackPen = ffp->fp_BackPen;
40 fp->fp_DrawMode = ffp->fp_Drawmode;
41 fp->fp_TextAttr.ta_Name = fp->fp_Name;
42 CopyMem
44 &ffp->fp_TextAttr_ta_YSize,
45 &fp->fp_TextAttr.ta_YSize,
46 sizeof(fp->fp_TextAttr.ta_YSize)
48 fp->fp_TextAttr.ta_Style = ffp->fp_TextAttr_ta_Style;
49 fp->fp_TextAttr.ta_Flags = ffp->fp_TextAttr_ta_Flags;
50 CopyMem(&ffp->fp_Name, &fp->fp_Name, FONTNAMESIZE);
53 static VOID FontPrefs2FileFontPrefs(struct FontPrefs *fp, struct FileFontPrefs *ffp)
55 /* Copy field by field to avoid any alignment problems whatsoever */
56 CopyMem(&fp->fp_Reserved, &ffp->fp_Reserved, sizeof(fp->fp_Reserved));
57 CopyMem(&fp->fp_Reserved2, &ffp->fp_Reserved2, sizeof(fp->fp_Reserved2));
58 CopyMem(&fp->fp_Type, &ffp->fp_Type, sizeof(fp->fp_Type));
59 ffp->fp_FrontPen = fp->fp_FrontPen;
60 ffp->fp_BackPen = fp->fp_BackPen;
61 ffp->fp_Drawmode = fp->fp_DrawMode;
62 /* fp->fp_TextAttr.ta_Name is not copied, it may have different sizes on
63 different architectures and contains only a pointer, so I guess there's
64 no need to write it on disk. */
65 CopyMem
67 &fp->fp_TextAttr.ta_YSize,
68 &ffp->fp_TextAttr_ta_YSize,
69 sizeof(fp->fp_TextAttr.ta_YSize)
71 ffp->fp_TextAttr_ta_Style = fp->fp_TextAttr.ta_Style;
72 ffp->fp_TextAttr_ta_Flags = fp->fp_TextAttr.ta_Flags;
73 CopyMem(&fp->fp_Name, &ffp->fp_Name, FONTNAMESIZE);
76 static BOOL Prefs_Load(STRPTR from, struct FontPrefs fp[])
78 BOOL retval = FALSE;
80 BPTR fh = Open(from, MODE_OLDFILE);
81 if (fh)
83 retval = Prefs_ImportFH(fh, fp);
84 Close(fh);
86 return retval;
89 BOOL Prefs_HandleArgs(STRPTR from, BOOL use, BOOL save)
91 BPTR fh;
92 struct FontPrefs fp[FP_COUNT];
94 if (from)
96 if (!Prefs_Load(from, fp))
98 ShowMessage("Can't read from input file");
99 return FALSE;
102 else
104 if (!Prefs_Load(PREFS_PATH_ENV, fp))
106 if (!Prefs_Load(PREFS_PATH_ENVARC, fp))
108 ShowMessage
110 "Can't read from file " PREFS_PATH_ENVARC
111 ".\nUsing default values."
113 Prefs_Default(fp);
118 if (use || save)
120 fh = Open(PREFS_PATH_ENV, MODE_NEWFILE);
121 if (fh)
123 Prefs_ExportFH(fh, fp);
124 Close(fh);
126 else
128 ShowMessage("Cant' open " PREFS_PATH_ENV " for writing.");
131 if (save)
133 fh = Open(PREFS_PATH_ENVARC, MODE_NEWFILE);
134 if (fh)
136 Prefs_ExportFH(fh, fp);
137 Close(fh);
139 else
141 ShowMessage("Cant' open " PREFS_PATH_ENVARC " for writing.");
144 return TRUE;
147 BOOL Prefs_Default(struct FontPrefs fp[])
149 UBYTE i;
151 for (i = 0; i < FP_COUNT; i++)
153 fp[i].fp_Type = i;
154 fp[i].fp_FrontPen = 0; /* FIXME: Is this (really) default? Look it up! */
155 fp[i].fp_BackPen = 0; /* FIXME: Is this (really) default? Look it up! */
156 fp[i].fp_DrawMode = 0; /* FIXME: Is this (really) default? Look it up! */
158 fp[i].fp_TextAttr.ta_YSize = 8; /* FIXME: Is this (really) default? Look it up! */
159 fp[i].fp_TextAttr.ta_Style = FS_NORMAL;
160 fp[i].fp_TextAttr.ta_Flags = FPB_DISKFONT; /* FIXME: Is this (really) default? Look it up! */
162 fp[i].fp_Name[0] = '\0';
163 strlcat(fp[i].fp_Name, "topaz.font", FONTNAMESIZE); /* FIXME: Is this (really) default? Check it up! */
164 fp[i].fp_TextAttr.ta_Name = fp[i].fp_Name;
166 return TRUE;
170 BOOL Prefs_ImportFH(BPTR fh, struct FontPrefs fp[])
172 struct IFFHandle *handle;
173 BOOL success = TRUE;
174 LONG error;
176 if (!(handle = AllocIFF()))
178 ShowMessage(_(MSG_CANT_ALLOCATE_IFFPTR));
179 return(FALSE);
182 handle->iff_Stream = (IPTR) fh;
183 InitIFFasDOS(handle);
185 if ((error = OpenIFF(handle, IFFF_READ)) == 0)
187 BYTE i;
189 // FIXME: We want some sanity checking here!
190 for (i = 0; i < FP_COUNT; i++)
192 if ((error = StopChunk(handle, ID_PREF, ID_FONT)) == 0)
194 if ((error = ParseIFF(handle, IFFPARSE_SCAN)) == 0)
196 struct FileFontPrefs ffp;
198 error = ReadChunkBytes
200 handle, &ffp, sizeof(struct FileFontPrefs)
203 if (error < 0)
205 printf("Error: ReadChunkBytes() returned %d!\n", (int)error);
208 FileFontPrefs2FontPrefs(&ffp, &fp[i]);
210 convertEndian(&fp[i]);
212 else
214 printf("ParseIFF() failed, returncode %d!\n", (int)error);
215 success = FALSE;
216 break;
219 else
221 printf("StopChunk() failed, returncode %d!\n", (int)error);
222 success = FALSE;
226 CloseIFF(handle);
228 else
230 ShowMessage(_(MSG_CANT_OPEN_STREAM));
233 FreeIFF(handle);
235 return success;
238 BOOL Prefs_ExportFH(BPTR fh, struct FontPrefs fp[])
240 struct PrefHeader header;
241 struct IFFHandle *handle;
242 BOOL success = TRUE;
243 LONG error = 0;
245 memset(&header, 0, sizeof(struct PrefHeader));
247 if ((handle = AllocIFF()))
249 handle->iff_Stream = (IPTR)fh;
251 InitIFFasDOS(handle);
253 if (!(error = OpenIFF(handle, IFFF_WRITE))) /* NULL = successful! */
255 BYTE i;
257 PushChunk(handle, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN); /* FIXME: IFFSIZE_UNKNOWN? */
259 header.ph_Version = PHV_CURRENT;
260 header.ph_Type = 0;
262 PushChunk(handle, ID_PREF, ID_PRHD, IFFSIZE_UNKNOWN); /* FIXME: IFFSIZE_UNKNOWN? */
264 WriteChunkBytes(handle, &header, sizeof(struct PrefHeader));
266 PopChunk(handle);
268 for (i = 0; i < FP_COUNT; i++)
270 struct FileFontPrefs ffp;
271 error = PushChunk(handle, ID_PREF, ID_FONT, sizeof(struct FileFontPrefs));
273 if (error != 0) // TODO: We need some error checking here!
275 printf("error: PushChunk() = %d\n", (int)error);
278 convertEndian(&fp[i]); // Convert to m68k endian
279 FontPrefs2FileFontPrefs(&fp[i], &ffp);
281 error = WriteChunkBytes(handle, &ffp, sizeof(struct FileFontPrefs));
282 error = PopChunk(handle);
284 convertEndian(&fp[i]); // Revert to initial endian
286 if (error != 0) // TODO: We need some error checking here!
288 printf("error: PopChunk() = %d\n", (int)error);
292 // Terminate the FORM
293 PopChunk(handle);
295 else
297 ShowMessage(_(MSG_CANT_OPEN_STREAM));
298 success = FALSE;
301 CloseIFF(handle);
302 FreeIFF(handle);
304 else // AllocIFF()
306 // Do something more here - if IFF allocation has failed, something isn't right
307 ShowMessage(_(MSG_CANT_ALLOCATE_IFFPTR));
308 success = FALSE;
311 return success;