2 Copyright © 1995-2008, The AROS Development Team. All rights reserved.
9 /*********************************************************************************************/
15 #include <aros/macros.h>
18 #include <aros/debug.h>
20 #include <proto/iffparse.h>
21 #include <proto/dos.h>
23 #include <prefs/prefhdr.h>
28 /*********************************************************************************************/
37 /*********************************************************************************************/
39 static struct SerialPrefs restore_prefs
;
40 struct SerialPrefs serialprefs
;
43 /*********************************************************************************************/
45 #define SER_DEFAULT_BAUDRATE 19200
46 #define SER_DEFAULT_IBUFFER 512
47 #define SER_DEFAULT_OBUFFER 512
49 #define SER_DEFAULT_IHANDSHAKE HSHAKE_NONE
50 #define SER_DEFAULT_OHANDSHAKE HSHAKE_NONE
52 #define SER_DEFAULT_PARITY PARITY_NONE
53 #define SER_DEFAULT_BITS_PER_CHAR 8
54 #define SER_DEFAULT_STOP_BITS 1
56 /*********************************************************************************************/
58 VOID
CleanupPrefs(void)
60 D(bug("[serial prefs] CleanupPrefs\n"));
61 if (mempool
) DeletePool(mempool
);
64 /*********************************************************************************************/
66 VOID
CopyPrefs(struct SerialPrefs
*s
, struct SerialPrefs
*d
)
68 CopyMem(s
, d
, sizeof(struct SerialPrefs
));
71 VOID
BackupPrefs(void)
73 CopyPrefs(&serialprefs
, &restore_prefs
);
76 VOID
RestorePrefs(void)
78 CopyPrefs(&restore_prefs
, &serialprefs
);
81 /*********************************************************************************************/
85 ULONG
InitPrefs(STRPTR filename
, BOOL use
, BOOL save
)
87 D(bug("[serial prefs] InitPrefs\n"));
88 mempool
= CreatePool(MEMF_PUBLIC
| MEMF_CLEAR
, 2048, 2048);
91 ShowMsg("Out of memory!");
95 if (!LoadPrefs(filename
))
100 ShowMsg("Panic! Cannot setup default prefs!");
105 restore_prefs
= serialprefs
;
109 SavePrefs((CONST STRPTR
) CONFIGNAME_ENV
);
114 SavePrefs((CONST STRPTR
) CONFIGNAME_ENVARC
);
117 if (use
|| save
) CleanupPrefs();
122 /*********************************************************************************************/
124 BOOL
LoadPrefsFH(BPTR fh
)
126 static struct SerialPrefs loadprefs
;
127 struct IFFHandle
*iff
;
131 if ((iff
= AllocIFF()))
133 iff
->iff_Stream
= (IPTR
)fh
;
137 if (!OpenIFF(iff
, IFFF_READ
))
139 D(bug("LoadPrefs: OpenIFF okay.\n"));
141 if (!StopChunk(iff
, ID_PREF
, ID_SERL
))
143 D(bug("LoadPrefs: StopChunk okay.\n"));
145 if (!ParseIFF(iff
, IFFPARSE_SCAN
))
147 struct ContextNode
*cn
;
149 D(bug("LoadPrefs: ParseIFF okay.\n"));
151 cn
= CurrentChunk(iff
);
153 if (cn
->cn_Size
== sizeof(struct SerialPrefs
))
155 D(bug("LoadPrefs: ID_SERL chunk size okay.\n"));
157 if (ReadChunkBytes(iff
, &loadprefs
, sizeof(struct SerialPrefs
)) == sizeof(struct SerialPrefs
))
159 D(bug("LoadPrefs: Reading chunk successful.\n"));
161 serialprefs
= loadprefs
;
163 D(bug("LoadPrefs: Everything okay :-)\n"));
169 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
171 } /* if (!StopChunk(iff, ID_PREF, ID_SERL)) */
175 } /* if (!OpenIFF(iff, IFFF_READ)) */
180 } /* if ((iff = AllocIFF())) */
185 BOOL
LoadPrefs(STRPTR filename
)
190 D(bug("[serial prefs] LoadPrefsFH: Trying to open \"%s\"\n", filename
));
192 fh
=Open(filename
, MODE_OLDFILE
);
194 if(!fh
) return FALSE
;
201 /*********************************************************************************************/
203 BOOL
SavePrefsFH(BPTR fh
)
205 static struct SerialPrefs saveprefs
;
206 struct IFFHandle
*iff
;
207 BOOL retval
= FALSE
, delete_if_error
= FALSE
;
209 saveprefs
= serialprefs
;
212 D(bug("SavePrefsFH: fh: %lx\n", fh
));
213 //if ((iff->iff_Stream = (IPTR)Open(filename, MODE_NEWFILE)))
215 if ((iff
= AllocIFF()))
217 iff
->iff_Stream
= (IPTR
) fh
;
218 D(bug("SavePrefsFH: stream opened.\n"));
220 delete_if_error
= TRUE
;
224 if (!OpenIFF(iff
, IFFF_WRITE
))
226 D(bug("SavePrefsFH: OpenIFF okay.\n"));
228 if (!PushChunk(iff
, ID_PREF
, ID_FORM
, IFFSIZE_UNKNOWN
))
230 D(bug("SavePrefsFH: PushChunk(FORM) okay.\n"));
232 if (!PushChunk(iff
, ID_PREF
, ID_PRHD
, sizeof(struct FilePrefHeader
)))
234 struct FilePrefHeader head
;
236 D(bug("SavePrefsFH: PushChunk(PRHD) okay.\n"));
238 head
.ph_Version
= PHV_CURRENT
;
243 head
.ph_Flags
[3] = 0;
245 if (WriteChunkBytes(iff
, &head
, sizeof(head
)) == sizeof(head
))
247 D(bug("SavePrefsFH: WriteChunkBytes(PRHD) okay.\n"));
251 if (!PushChunk(iff
, ID_PREF
, ID_SERL
, sizeof(struct SerialPrefs
)))
253 D(bug("SavePrefsFH: PushChunk(LCLE) okay.\n"));
255 if (WriteChunkBytes(iff
, &saveprefs
, sizeof(saveprefs
)) == sizeof(saveprefs
))
257 D(bug("SavePrefsFH: WriteChunkBytes(SERL) okay.\n"));
258 D(bug("SavePrefsFH: Everything okay :-)\n"));
265 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
267 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
273 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
277 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
281 } /* if (!OpenIFF(iff, IFFFWRITE)) */
286 } /* if ((iff = AllocIFF())) */
289 if (!retval
&& delete_if_error
)
291 DeleteFile(filename
);
299 BOOL
SavePrefs(CONST STRPTR filename
)
304 D(bug("[serial prefs] SavePrefs: Trying to open \"%s\"\n", filename
));
306 fh
=Open(filename
, MODE_NEWFILE
);
310 D(bug("[serial prefs] open \"%s\" failed!\n", filename
));
320 /*********************************************************************************************/
326 D(bug("[serial prefs] SaveEnv: Trying to open \"%s\"\n", CONFIGNAME_ENV
));
328 fh
=Open((CONST_STRPTR
) CONFIGNAME_ENV
, MODE_NEWFILE
);
332 D(bug("[serial prefs] open \"%s\" failed!\n", CONFIGNAME_ENV
));
336 result
=SavePrefsFH(fh
);
343 /*********************************************************************************************/
345 BOOL
DefaultPrefs(void)
347 serialprefs
.sp_Reserved
[0] = 0;
348 serialprefs
.sp_Reserved
[1] = 0;
349 serialprefs
.sp_Reserved
[2] = 0;
350 serialprefs
.sp_Unit0Map
= 0;
351 serialprefs
.sp_BaudRate
= SER_DEFAULT_BAUDRATE
;
352 serialprefs
.sp_InputBuffer
= SER_DEFAULT_IBUFFER
;
353 serialprefs
.sp_OutputBuffer
= SER_DEFAULT_OBUFFER
;
354 serialprefs
.sp_InputHandshake
= SER_DEFAULT_IHANDSHAKE
;
355 serialprefs
.sp_OutputHandshake
= SER_DEFAULT_OHANDSHAKE
;
356 serialprefs
.sp_Parity
= SER_DEFAULT_PARITY
;
357 serialprefs
.sp_BitsPerChar
= SER_DEFAULT_BITS_PER_CHAR
;
358 serialprefs
.sp_StopBits
= SER_DEFAULT_STOP_BITS
;