;
; choices.prefs.s
;
; Management of application's preferences file
;
;  1995 Straylight
;

;----- Standard header ------------------------------------------------------

		GET	libs:header
		GET	libs:swis

		GET	libs:stream

;----- External dependencies ------------------------------------------------

		GET	sapphire:alloc
		GET	sapphire:chunk
		GET	sapphire:flex
		GET	sapphire:heap
		GET	sapphire:res
		GET	sapphire:sapphire

		GET	sapphire:xfer.load

		GET	sapphire:choices.choices

;----- Main code ------------------------------------------------------------

		AREA	|Sapphire$$Code|,CODE,READONLY

; --- prefs_find ---
;
; On entry:	--
;
; On exit:	R0 == chunk handle of loaded preferences file
;
; Use:		Returns the chunk handle of the preferences file.  You can
;		then claim your chunk of options from the preferences file
;		using options_read.

		EXPORT	prefs_find
prefs_find	ROUT

		STMFD	R13!,{R14}		;Save a register
		WSPACE	prefs__wSpace,R0	;Find my workspace
		LDR	R0,[R0,#:INDEX:prefs__handle]
		LDMFD	R13!,{PC}^		;And return to caller

		LTORG

; --- prefs_init ---
;
; On entry:	R0 == pointer to application name
;
; On exit:	--
;
; Use:		Loads the application's preferences file.

		EXPORT	prefs_init
prefs_init	ROUT

		STMFD	R13!,{R0-R2,R12,R14}	;Save some registers
		WSPACE	prefs__wSpace		;Find my workspace
		LDR	R14,prefs__handle	;Get the prefs chunk file
		CMP	R14,#0			;Is it already created?
		LDMNEFD	R13!,{R0-R2,R12,PC}^	;Yes -- nothing doing

		; --- Initialise bits of the library ---

		BL	res_init		;So I can find resources
		BL	flex_init		;For memory management
		BL	alloc_init		;For memory management too
		BL	heap_init		;So that it doesn't get upset

		; --- Create a chunk file ---

		BL	chunk_create		;Create the file
		SWIVS	OS_GenerateError	;If it failed, take it badly
		STR	R0,prefs__handle	;Save that away for later

		; --- Now load the preferences file ---

		ADR	R0,prefs__file		;Point to leafname
		MOV	R1,R11			;Find a spare buffer
		BL	choices_find		;Get a complete filename
		LDMCCFD	R13!,{R0-R2,R12,PC}^	;If file not there, stop
		MOV	R1,R0			;Point to this name
		SUB	R13,R13,#4		;Make a flex anchor
		MOV	R2,R13			;Point to this anchor
		BL	load_file		;And load the file into it
		BVS	%99prefs_init		;If it failed, tidy up
		MOV	R1,R13			;Point to the anchor
		LDR	R0,prefs__handle	;Load the chunk file anchor
		BL	chunk_read		;Read the preferences file

		; --- Tidy up and return ---

		MOV	R0,R13			;Point to the flex anchor
		BL	flex_free		;Free the file block

99		ADD	R13,R13,#4		;Clear up the stack space
		LDMFD	R13!,{R0-R2,R12,PC}^	;And return to caller

prefs__file	DCB	"Choices",0

		LTORG

prefs__wSpace	DCD	0

;----- Workspace ------------------------------------------------------------

		^	0,R12
prefs__wStart	#	0

prefs__handle	#	4			;Handle of prefs chunk file

prefs__wSize	EQU	{VAR}-prefs__wStart

		AREA	|Sapphire$$LibData|,CODE,READONLY

		DCD	prefs__wSize
		DCD	prefs__wSpace
		DCD	0
		DCD	prefs_init

;----- That's all, folks ----------------------------------------------------

		END
