2 Copyright © 2010, The AROS Development Team. All rights reserved.
6 /*********************************************************************************************/
12 #include <aros/macros.h>
15 #include <aros/debug.h>
17 #include <proto/exec.h>
18 #include <proto/iffparse.h>
19 #include <proto/dos.h>
21 #include <prefs/prefhdr.h>
26 /*********************************************************************************************/
28 #define PREFS_PATH_ENVARC "ENVARC:SYS/pointer.prefs"
29 #define PREFS_PATH_ENV "ENV:SYS/pointer.prefs"
31 /*********************************************************************************************/
40 /*********************************************************************************************/
42 struct ExtPointerPrefs pointerprefs
[MAXPOINTER
];
44 /*********************************************************************************************/
46 static BOOL
Prefs_Load(STRPTR from
)
50 BPTR fh
= Open(from
, MODE_OLDFILE
);
53 retval
= Prefs_ImportFH(fh
);
60 /*********************************************************************************************/
62 BOOL
Prefs_ImportFH(BPTR fh
)
64 struct ExtPointerPrefs loadprefs
;
65 struct IFFHandle
*iff
;
68 if ((iff
= AllocIFF()))
70 iff
->iff_Stream
= (IPTR
)fh
;
74 if (!OpenIFF(iff
, IFFF_READ
))
76 D(bug("Prefs_ImportFH: OpenIFF okay.\n"));
78 if (!StopChunk(iff
, ID_PREF
, ID_NPTR
))
80 D(bug("Prefs_ImportFH: StopChunk okay.\n"));
82 while (!ParseIFF(iff
, IFFPARSE_SCAN
))
84 struct ContextNode
*cn
;
86 D(bug("Prefs_ImportFH: ParseIFF okay.\n"));
88 memset(&loadprefs
, 0, sizeof(loadprefs
));
89 cn
= CurrentChunk(iff
);
91 if (cn
->cn_Size
>= sizeof(struct NewPointerPrefs
))
93 D(bug("Prefs_ImportFH: ID_NPTR chunk size okay.\n"));
95 if (ReadChunkBytes(iff
, &loadprefs
, sizeof(struct ExtPointerPrefs
)) >= sizeof(struct NewPointerPrefs
))
97 D(bug("Prefs_ImportFH: Reading chunk successful.\n"));
99 UWORD which
= AROS_BE2WORD(loadprefs
.npp
.npp_Which
);
100 if (which
< MAXPOINTER
)
102 pointerprefs
[which
].npp
.npp_Which
= which
;
103 pointerprefs
[which
].npp
.npp_AlphaValue
= AROS_BE2WORD(loadprefs
.npp
.npp_AlphaValue
);
104 pointerprefs
[which
].npp
.npp_WhichInFile
= AROS_BE2LONG(loadprefs
.npp
.npp_WhichInFile
);
105 pointerprefs
[which
].npp
.npp_X
= AROS_BE2WORD(loadprefs
.npp
.npp_X
);
106 pointerprefs
[which
].npp
.npp_Y
= AROS_BE2WORD(loadprefs
.npp
.npp_Y
);
107 strlcpy(pointerprefs
[which
].filename
, loadprefs
.filename
, NAMEBUFLEN
);
109 D(bug("Prefs_ImportFH: which %d name %s\n", which
, pointerprefs
[which
].filename
));
112 D(bug("Prefs_ImportFH: Everything okay :-)\n"));
117 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
118 } /* if (!StopChunk(iff, ID_PREF, ID_SERL)) */
120 } /* if (!OpenIFF(iff, IFFF_READ)) */
122 } /* if ((iff = AllocIFF())) */
127 /*********************************************************************************************/
129 BOOL
Prefs_ExportFH(BPTR fh
)
131 struct ExtPointerPrefs saveprefs
;
132 struct IFFHandle
*iff
;
135 BOOL delete_if_error
= FALSE
;
139 D(bug("Prefs_ExportFH: fh: %lx\n", fh
));
141 if ((iff
= AllocIFF()))
143 iff
->iff_Stream
= (IPTR
) fh
;
144 D(bug("Prefs_ExportFH: stream opened.\n"));
147 delete_if_error
= TRUE
;
152 if (!OpenIFF(iff
, IFFF_WRITE
))
154 D(bug("Prefs_ExportFH: OpenIFF okay.\n"));
156 if (!PushChunk(iff
, ID_PREF
, ID_FORM
, IFFSIZE_UNKNOWN
))
158 D(bug("Prefs_ExportFH: PushChunk(FORM) okay.\n"));
160 if (!PushChunk(iff
, ID_PREF
, ID_PRHD
, sizeof(struct FilePrefHeader
)))
162 struct FilePrefHeader head
;
164 D(bug("Prefs_ExportFH: PushChunk(PRHD) okay.\n"));
166 head
.ph_Version
= PHV_CURRENT
;
171 head
.ph_Flags
[3] = 0;
173 if (WriteChunkBytes(iff
, &head
, sizeof(head
)) == sizeof(head
))
175 D(bug("Prefs_ExportFH: WriteChunkBytes(PRHD) okay.\n"));
179 for (i
= 0; i
< MAXPOINTER
; i
++)
181 saveprefs
.npp
.npp_Which
= AROS_WORD2BE(i
);
182 saveprefs
.npp
.npp_AlphaValue
= AROS_WORD2BE(pointerprefs
[i
].npp
.npp_AlphaValue
);
183 saveprefs
.npp
.npp_WhichInFile
= AROS_LONG2BE(pointerprefs
[i
].npp
.npp_WhichInFile
);
184 saveprefs
.npp
.npp_X
= AROS_WORD2BE(pointerprefs
[i
].npp
.npp_X
);
185 saveprefs
.npp
.npp_Y
= AROS_WORD2BE(pointerprefs
[i
].npp
.npp_Y
);
186 strlcpy(saveprefs
.filename
, pointerprefs
[i
].filename
, NAMEBUFLEN
);
188 ULONG chunksize
= sizeof(struct NewPointerPrefs
) + strlen(saveprefs
.filename
) + 1;
189 D(bug("Prefs_ExportFH: size %d name %s\n", chunksize
, saveprefs
.filename
));
191 if (!PushChunk(iff
, ID_PREF
, ID_NPTR
, chunksize
))
193 D(bug("Prefs_ExportFH: PushChunk(NPTR) okay.\n"));
195 if (WriteChunkBytes(iff
, &saveprefs
, chunksize
) == chunksize
)
197 D(bug("Prefs_ExportFH: WriteChunkBytes(NPTR) okay.\n"));
198 D(bug("Prefs_ExportFH: Everything okay :-)\n"));
203 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
205 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
210 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
212 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
214 } /* if (!OpenIFF(iff, IFFFWRITE)) */
217 } /* if ((iff = AllocIFF())) */
220 if (!retval
&& delete_if_error
)
222 DeleteFile(filename
);
229 /*********************************************************************************************/
231 BOOL
Prefs_HandleArgs(STRPTR from
, BOOL use
, BOOL save
)
237 if (!Prefs_Load(from
))
239 ShowMessage("Can't read from input file");
245 if (!Prefs_Load(PREFS_PATH_ENV
))
247 if (!Prefs_Load(PREFS_PATH_ENVARC
))
251 "Can't read from file " PREFS_PATH_ENVARC
252 ".\nUsing default values."
261 fh
= Open(PREFS_PATH_ENV
, MODE_NEWFILE
);
269 ShowMessage("Can't open " PREFS_PATH_ENV
" for writing.");
274 fh
= Open(PREFS_PATH_ENVARC
, MODE_NEWFILE
);
282 ShowMessage("Can't open " PREFS_PATH_ENVARC
" for writing.");
288 /*********************************************************************************************/
290 BOOL
Prefs_Default(VOID
)
294 memset(pointerprefs
, 0, sizeof(pointerprefs
));
295 for (i
= 0; i
< MAXPOINTER
; i
++)
297 pointerprefs
[i
].npp
.npp_Which
= i
;
298 pointerprefs
[i
].npp
.npp_AlphaValue
= 0xffff;
299 strcpy(pointerprefs
[i
].filename
, "Images:Pointers/");