1 #include <prefs/prefhdr.h>
3 #include <proto/exec.h>
5 #include <proto/iffparse.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
)
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
;
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. */
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
[])
80 BPTR fh
= Open(from
, MODE_OLDFILE
);
83 retval
= Prefs_ImportFH(fh
, fp
);
89 BOOL
Prefs_HandleArgs(STRPTR from
, BOOL use
, BOOL save
)
92 struct FontPrefs fp
[FP_COUNT
];
96 if (!Prefs_Load(from
, fp
))
98 ShowMessage("Can't read from input file");
104 if (!Prefs_Load(PREFS_PATH_ENV
, fp
))
106 if (!Prefs_Load(PREFS_PATH_ENVARC
, fp
))
110 "Can't read from file " PREFS_PATH_ENVARC
111 ".\nUsing default values."
120 fh
= Open(PREFS_PATH_ENV
, MODE_NEWFILE
);
123 Prefs_ExportFH(fh
, fp
);
128 ShowMessage("Cant' open " PREFS_PATH_ENV
" for writing.");
133 fh
= Open(PREFS_PATH_ENVARC
, MODE_NEWFILE
);
136 Prefs_ExportFH(fh
, fp
);
141 ShowMessage("Cant' open " PREFS_PATH_ENVARC
" for writing.");
147 BOOL
Prefs_Default(struct FontPrefs fp
[])
151 for (i
= 0; i
< FP_COUNT
; 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
;
170 BOOL
Prefs_ImportFH(BPTR fh
, struct FontPrefs fp
[])
172 struct IFFHandle
*handle
;
176 if (!(handle
= AllocIFF()))
178 ShowMessage(_(MSG_CANT_ALLOCATE_IFFPTR
));
182 handle
->iff_Stream
= (IPTR
) fh
;
183 InitIFFasDOS(handle
);
185 if ((error
= OpenIFF(handle
, IFFF_READ
)) == 0)
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
)
205 printf("Error: ReadChunkBytes() returned %d!\n", (int)error
);
208 FileFontPrefs2FontPrefs(&ffp
, &fp
[i
]);
210 convertEndian(&fp
[i
]);
214 printf("ParseIFF() failed, returncode %d!\n", (int)error
);
221 printf("StopChunk() failed, returncode %d!\n", (int)error
);
230 ShowMessage(_(MSG_CANT_OPEN_STREAM
));
238 BOOL
Prefs_ExportFH(BPTR fh
, struct FontPrefs fp
[])
240 struct PrefHeader header
;
241 struct IFFHandle
*handle
;
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! */
257 PushChunk(handle
, ID_PREF
, ID_FORM
, IFFSIZE_UNKNOWN
); /* FIXME: IFFSIZE_UNKNOWN? */
259 header
.ph_Version
= PHV_CURRENT
;
262 PushChunk(handle
, ID_PREF
, ID_PRHD
, IFFSIZE_UNKNOWN
); /* FIXME: IFFSIZE_UNKNOWN? */
264 WriteChunkBytes(handle
, &header
, sizeof(struct PrefHeader
));
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
297 ShowMessage(_(MSG_CANT_OPEN_STREAM
));
306 // Do something more here - if IFF allocation has failed, something isn't right
307 ShowMessage(_(MSG_CANT_ALLOCATE_IFFPTR
));