revert between 56095 -> 55830 in arch
[AROS.git] / workbench / devs / printer / prefs.c
blob9962e380230043316deb173d399111207b64d008
1 /*
2 Copyright © 1995-2018, 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 #include <aros/debug.h>
19 #include <proto/exec.h>
20 #include <proto/iffparse.h>
21 #include <proto/dos.h>
23 #include <prefs/prefhdr.h>
25 #include "printer_intern.h"
27 #ifdef BIGENDIAN_PREFS
28 #define GET_WORD AROS_BE2WORD
29 #define GET_LONG AROS_BE2LONG
30 #else
31 #define GET_WORD(x) x
32 #define GET_LONG(x) x
33 #endif
35 /*********************************************************************************************/
37 #define PREFS_PATH_ENVARC "ENVARC:SYS/printer"
38 #define PREFS_PATH_ENV "ENV:SYS/printer"
40 /*********************************************************************************************/
42 struct FilePrefHeader
44 UBYTE ph_Version;
45 UBYTE ph_Type;
46 UBYTE ph_Flags[4];
49 /*********************************************************************************************/
51 #define IMPORT_WORD(x) do { x = AROS_BE2WORD(x); } while (0)
52 #define IMPORT_LONG(x) do { x = AROS_BE2LONG(x); } while (0)
54 #define PATHMAX 63
55 #define EXTMAX 7 /* ".prefs" + 1 for unit num */
57 BOOL Printer_LoadPrefs(struct PrinterBase *PrinterBase, LONG unitnum, struct PrinterPrefs *prefs)
59 struct PrinterTxtPrefs txt = prefs->pp_Txt;
60 struct PrinterUnitPrefs unit = prefs->pp_Unit;
61 struct PrinterDeviceUnitPrefs devunit = prefs->pp_DeviceUnit;
62 struct PrinterGfxPrefs gfx = prefs->pp_Gfx;
63 BPTR fh;
64 TEXT envpath[PATHMAX + 1];
65 TEXT envarcpath[PATHMAX + 1];
67 struct IFFHandle *iff;
68 LONG chunk_map = 0;
69 LONG stop_chunks[] = {
70 ID_PREF, ID_PTXT,
71 ID_PREF, ID_PUNT,
72 ID_PREF, ID_PDEV,
73 ID_PREF, ID_PGFX,
76 AddPart(envpath, PREFS_PATH_ENV, PATHMAX - EXTMAX + 1);
77 AddPart(envarcpath, PREFS_PATH_ENVARC, PATHMAX - EXTMAX + 1);
78 if (unitnum) {
79 TEXT c[2] = { '0' + unitnum, 0 };
80 strcat(envpath, c);
81 strcat(envarcpath, c);
83 strcat(envpath, ".prefs");
84 strcat(envarcpath, ".prefs");
86 D(bug("%s: envpath \"%s\"\n", __func__, envpath));
87 D(bug("%s: envarcpath \"%s\"\n", __func__, envarcpath));
89 if (((fh = Open(envpath, MODE_OLDFILE)) == BNULL) &&
90 ((fh = Open(envarcpath, MODE_OLDFILE)) == BNULL))
91 return FALSE;
93 D(bug("LoadPrefs: Begin\n"));
95 if ((iff = AllocIFF()))
97 iff->iff_Stream = (IPTR)fh;
99 InitIFFasDOS(iff);
101 if (!OpenIFF(iff, IFFF_READ))
103 D(bug("LoadPrefs: OpenIFF okay.\n"));
105 if (!StopChunks(iff, stop_chunks, 4))
107 D(bug("LoadPrefs: StopChunks okay.\n"));
109 while (ParseIFF(iff, IFFPARSE_SCAN) == 0)
111 struct ContextNode *cn;
113 cn = CurrentChunk(iff);
115 D(bug("LoadPrefs: ParseIFF okay: 0x%04x 0x%04x\n", cn->cn_ID, cn->cn_Type));
117 if (cn->cn_ID == ID_PTXT && cn->cn_Size == sizeof(txt))
119 D(bug("LoadPrefs: ID_PTXT chunk size okay.\n"));
120 if (ReadChunkBytes(iff, &txt, sizeof(txt)) == sizeof(txt))
122 D(bug("LoadPrefs: Reading chunk successful.\n"));
124 chunk_map |= (1 << 0);
127 if (cn->cn_ID == ID_PUNT && cn->cn_Size == sizeof(unit))
129 D(bug("LoadPrefs: ID_PUNT chunk size okay.\n"));
130 if (ReadChunkBytes(iff, &unit, sizeof(unit)) == sizeof(unit))
132 D(bug("LoadPrefs: Reading chunk successful.\n"));
134 chunk_map |= (1 << 1);
137 if (cn->cn_ID == ID_PDEV && cn->cn_Size == sizeof(devunit))
139 D(bug("LoadPrefs: ID_PDEV chunk size okay.\n"));
140 if (ReadChunkBytes(iff, &devunit, sizeof(devunit)) == sizeof(devunit))
142 D(bug("LoadPrefs: Reading chunk successful.\n"));
144 chunk_map |= (1 << 2);
147 if (cn->cn_ID == ID_PGFX && cn->cn_Size == sizeof(gfx))
149 D(bug("LoadPrefs: ID_PGFX chunk size okay.\n"));
150 if (ReadChunkBytes(iff, &gfx, sizeof(gfx)) == sizeof(gfx))
152 D(bug("LoadPrefs: Reading chunk successful.\n"));
154 chunk_map |= (1 << 3);
159 CloseIFF(iff);
161 FreeIFF(iff);
163 Close(fh);
165 if (chunk_map & (1 << 0)) {
166 D(bug("LoadPrefs: PTXT\n"));
168 IMPORT_WORD(txt.pt_PaperType);
169 IMPORT_WORD(txt.pt_PaperSize);
170 IMPORT_WORD(txt.pt_PaperLength);
171 IMPORT_WORD(txt.pt_Pitch);
172 IMPORT_WORD(txt.pt_Spacing);
173 IMPORT_WORD(txt.pt_LeftMargin);
174 IMPORT_WORD(txt.pt_RightMargin);
175 IMPORT_WORD(txt.pt_Quality);
176 prefs->pp_Txt = txt;
179 if (chunk_map & (1 << 1)) {
180 D(bug("LoadPrefs: PUNT\n"));
182 IMPORT_LONG(unit.pu_UnitNum);
183 IMPORT_LONG(unit.pu_OpenDeviceFlags);
184 prefs->pp_Unit = unit;
187 if (chunk_map & (1 << 2)) {
188 D(bug("LoadPrefs: PDEV\n"));
190 IMPORT_LONG(devunit.pd_UnitNum);
191 prefs->pp_DeviceUnit = devunit;
194 if (chunk_map & (1 << 3)) {
195 D(bug("LoadPrefs: PGFX\n"));
197 IMPORT_WORD(gfx.pg_Aspect);
198 IMPORT_WORD(gfx.pg_Shade);
199 IMPORT_WORD(gfx.pg_Image);
200 IMPORT_WORD(gfx.pg_Threshold);
201 IMPORT_WORD(gfx.pg_GraphicFlags);
202 IMPORT_WORD(gfx.pg_PrintMaxWidth);
203 IMPORT_WORD(gfx.pg_PrintMaxHeight);
205 prefs->pp_Gfx = gfx;
208 D(bug("LoadPrefs: Done\n"));
209 return (chunk_map & (1 << 0)) ? TRUE : FALSE;