;APS00000000000000000000000000000000000000000000000000000000000000000000000000000000
; Simple example of single data items per tag

	incdir	include:
	include	exec/types.i
	include	lvo/dos_lib.i
	include	lvo/exec_lib.i
	include	lvo/preferences_lib.i
	include	libraries/iffparse.i
	include	scalos/preferences.i


start:	movem.l	d1-d7/a0-a6,-(a7)
	move.l	4.w,a6			; Get pointer to execbase

	lea	doslibname(pc),a1	; SPRPTR "dos.library" -> a1
	moveq.l	#36,d0			; 36 (version to open) -> d0
	jsr	_LVOOpenLibrary(a6)	; Open dos.library
	move.l	d0,dosbase		; store/check pointer to dos.library base
	beq	.nodoslib		; if NULL, branch to end of program
	
	lea	prefslibname(pc),a1	; STRPTR "preferences.library" -> a1
	moveq.l	#39,d0			; 39 (version to open) -> d0
	jsr	_LVOOpenLibrary(a6)	; Open preferences.library
	move.l	d0,prefsbase		; store/check pointer to prefsbase
	beq	.noprefslib		; if NULL, branch to end of program

;	First we need to allocate a PrefsHandle by name
	move.l	d0,a6			; pointer to prefsbase
	lea	prefname(pc),a0		; Get name to refer to this PrefsHandle -> a1
	jsr	_LVOAllocPrefsHandle(a6); try to allocate preferences handle
	move.l	d0,prefhandle		; store/check pointer to PrefsHandle
	beq	.noph			; if NULL, wasn't allocated so quit

	move.l	dosbase(pc),a6		; dos.library base -> a6
	move.l	#str_allocok,d1		; pointer to string -> d1
	jsr	_LVOPutStr(a6)		; print string to CLI (just to say we allocated PrefsHandle OK)


;	Try to load the preferences from a file
	move.l	prefsbase(pc),a6	; prefsbase
	move.l	prefhandle(pc),a0	; handle of our preferences allocated previously
	lea	preffile(pc),a1		; address of the string for the filename
	jsr	_LVOReadPrefsHandle(a6)	; call routine
	

;	We need an ID and a Tag value to refer to a specific preference item
;	Remember that the ID must be made from 4 ASCII characters and the Tag
;	value must not be 0
	move.l	#'MAIN',d6		; Set the ID (stored in d6) to 4 char ascii string "MAIN"
	moveq.l	#1,d7			; Set the Tag to 1 (imaginative, huh? ;)


;	Use FindPreferences to check if the item has been loaded
	move.l	d6,d0			; get ID
	move.l	d7,d1			; get Tag
	move.l	prefhandle(pc),a0	; get pointer to PrefsHandle
	move.l	prefsbase(pc),a6	; get pointer to prefsbase
	jsr	_LVOFindPreferences(a6)	; try to get pointer to the preference Tag
	tst.l	d0			; check pointer to Tag
	beq	.nofind			; if NULL, skip past all this stuff, because it didn't exist

	move.l	dosbase(pc),a6		; print a message to say that
	move.l	#str_findok,d1		; the preferences were found OK
	jsr	_LVOPutStr(a6)


;	To show that the data was actually stored, we use GetPreferences
;	to retrieve the data from storage
	move.l	prefhandle(pc),a0	; pointer to PrefsHandle
	move.l	d6,d0			; ID
	move.l	d7,d1			; Tag
	lea	temp(pc),a1		; pointer to storage memory
	move.l	#128,d2			; size of memory to store in (only 128 bytes since we want to use half for the pref string and half for the sprintf strings we will have)
	move.l	prefsbase(pc),a6	; pointer to prefsbase
	jsr	_LVOGetPreferences(a6)	; GetPreferences


	move.l	dosbase(pc),a6		; print message saying "This is contents"
	move.l	#str_contents,d1
	jsr	_LVOPutStr(a6)

	move.l	#temp,d1		; print the contents of what was retrieved
	jsr	_LVOPutStr(a6)

	move.l	#str_newline,d1		; and print a newline
	jsr	_LVOPutStr(a6)

.nofind:
;	Now we set a new value for the data
	move.l	dosbase(pc),a6		; print a prompt for the user
	move.l	#str_prompt,d1
	jsr	_LVOPutStr(a6)

	move.l	#128,d0			; call gets like function to get a string from the user
	lea	temp(pc),a0
	jsr	gets

	lea	temp,a0			; calculate length of string entered by user
	jsr	strlen
	tst.l	d0			
	beq	.nostring		; if length == 0 then user just wants to quit, skip next section


;	Set the new string entered by the user as the preference data
	move.l	prefsbase(pc),a6	; preferences.library base
	addq.l	#1,d0			; increase size of data to store by 1 so that NULL terminator of string gets stored
	move.l	d0,d2			; put size into correct register
	move.l	d6,d0			; preference ID
	move.l	d7,d1			; preference Tag
	move.l	prefhandle(pc),a0	; handle to our preferences
	lea	temp(pc),a1		; buffer to store
	jsr	_LVOSetPreferences(a6)	; call function


;	Finally write the new set of preferences. To test this program
;	run it again and it should show you the string you entered
	move.l	prefhandle(pc),a0	; handle to our preferences
	lea	preffile(pc),a1		; pointer to name of file to save as
	jsr	_LVOWritePrefsHandle(a6); save preferences

.nostring:

;	Free prefs handle
	move.l	prefsbase(pc),a6	; preferences.library base
	move.l	prefhandle(pc),a0	; handle to our preferences
	jsr	_LVOFreePrefsHandle(a6)	; free it

.noph:
	move.l	4.w,a6			; execbase pointer -> a6
	move.l	prefsbase(pc),a1	; prefsbase pointer -> a1
	jsr	_LVOCloseLibrary(a6)	; close preferences.library

.noprefslib:
	move.l	dosbase(pc),a1		; pointer to dos.library base -> a1
	jsr	_LVOCloseLibrary(a6)	; close dos.library

.nodoslib:	
	movem.l	(a7)+,d1-d7/a0-a6
	moveq.l	#0,d0
	rts
	

; Length of string
; a0 = STRPTR
strlen:
	moveq.l	#0,d0		; clear count of length of string
.loop:	tst.b	(a0)+		; check char in string for NULL and move onto next
	beq	.endloop	; if NULL, skip to end of loop
	addq.l	#1,d0		; otherwise increase length of string
	bra	.loop		; repeat loop
.endloop:
	rts


; C-like sprintf (from autodocs)
; parameters on stack from right to left
; (except the data items, they are on from left to right)
; output, format [, data]
sprintf:
	movem.l	a2/a3/a6,-(a7)

	move.l	16(a7),a3
	move.l	20(a7),a0
	lea	24(a7),a1
	lea	stuffChar(pc),a2
	move.l	4.w,a6
	jsr	_LVORawDoFmt(a6)

	movem.l	(a7)+,a2/a3/a6
	rts

stuffChar:
	move.b	d0,(a3)+
	rts


; gets() like function which uses dos.library FGets but strips the newline (if present)
; from the current input stream rather than a file
; a0 = buffer
; d0 = buffer size
gets:
	movem.l	d3/a2,-(a7)
	move.l	d0,d3
	move.l	a0,a2

	move.l	dosbase(pc),a6
	jsr	_LVOOutput(a6)
	move.l	d0,d1
	jsr	_LVOFlush(a6)
	jsr	_LVOInput(a6)
	move.l	d0,d1
	jsr	_LVOFlush(a6)
	jsr	_LVOInput(a6)

	move.l	d0,d1
	move.l	a2,d2
	jsr	_LVOFGets(a6)
	tst.l	d0
	beq	.error

.loop:	move.b	(a2)+,d0
	beq	.error
	cmp.b	#10,d0
	bne	.loop
	move.b	#0,-(a2)
	
.error:	
	movem.l	(a7)+,d3/a2
	rts
	

; Storage space for variables/strings etc
;execbase:	dc.l	0	; exec.library base
dosbase:	dc.l	0	; dos.library base
prefsbase:	dc.l	0	; Pointer to preferences.library base

prefhandle:	dc.l	0	; Pointer to our PrefsHandle
temp:		ds.b	256	; Temporary area for storing preferences

doslibname:	dc.b	"dos.library",0
prefslibname:	dc.b	"preferences.library",0
prefname:	dc.b	"Example",0		; Name to refer to our PrefHandle with
preffile:	dc.b	"example.prefs",0	; Name of preferences file. Does NOT need to be the same as the name of the PrefsHandle
str_allocok:	dc.b	"PrefsHandle allocated OK",10,0
str_findok:	dc.b	"Preference data found",10,0
str_contents:	dc.b	"Contents of preference data:",10,0
str_prompt:	dc.b	"Enter string or leave blank to quit and press return:",10,0
str_newline:	dc.b	10,0
		cnop	0,4
