;APS00000000000000000000000000000000000000000000000000000000000000000000000000000000
; Simple example of multiple 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	#phallocok,d1		; pointer to string -> d1
	jsr	_LVOPutStr(a6)		; print string to CLI (just to say we allocated PrefsHandle OK)

;	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	#2,d7			; Set the Tag to 2 (imaginative, huh? ;)


	moveq.l	#0,d5			; Set the current entry number to 0
.loop:					; loop to allow user to enter strings of data as the preference data
	move.l	dosbase(pc),a6		; Print a prompt for the user
	move.l	#str_prompt,d1
	jsr	_LVOPutStr(a6)

	move.l	#255,d0			; Get a string from the user
	lea	temp(pc),a0
	jsr	gets


	lea	temp(pc),a0		; check length of string. if 0 we
	jsr	strlen			; want to quit this loop
	tst.l	d0
	beq	.nostr

	addq.l	#1,d0			; increase length of string by 1 so we store the NULL terminator
	move.l	d0,d2			; size of buffer to store into d2
	move.l	d6,d0			; ID
	move.l	d7,d1			; Tag
	lea	temp(pc),a1		; buffer to store
	move.l	prefhandle(pc),a0	; PrefsHandle
	move.l	prefsbase(pc),a6	; prefsbase
	move.l	d5,d3			; entry number
	jsr	_LVOSetEntry(a6)	; SetEntry

	addq.l	#1,d5			; increase entry number
	bra	.loop
.nostr:	


;	Now we will show each data item in turn as well as removing it,
;	this means we do not need to play around with entry numbers here
.loop2:	move.l	prefsbase(pc),a6
	move.l	d6,d0			; ID
	move.l	d7,d1			; Tag
	move.l	#255,d2
	moveq.l	#0,d3	
	move.l	prefhandle(pc),a0
	lea	temp(pc),a1
	jsr	_LVOGetEntry(a6)
	tst.l	d0
	beq	.endloop2


;	Print some info and the contents of what was retrieved
	move.l	dosbase(pc),a6
	move.l	#str_retreive,d1
	jsr	_LVOPutStr(a6)
	move.l	#temp,d1
	jsr	_LVOPutStr(a6)
	move.l	#str_newline,d1
	jsr	_LVOPutStr(a6)

	
;	Remove the entry from the list and print a message to indicate success or failure
	move.l	prefsbase(pc),a6
	move.l	d6,d0
	move.l	d7,d1
	moveq.l	#0,d2
	move.l	prefhandle(pc),a0
	jsr	_LVORemEntry(a6)
	tst.l	d0
	beq	.remfailed

	move.l	dosbase(pc),a6
	move.l	#str_removed,d1
	jsr	_LVOPutStr(a6)
	bra	.loop2
.remfailed:
	move.l	dosbase(pc),a6
	move.l	#str_notremoved,d1
	jsr	_LVOPutStr(a6)
	bra	.loop2

.endloop2:

;	Finally we free the PrefsHandle when we no longer need it
	move.l	prefsbase(pc),a6	; prefsbase pointer -> a6
	move.l	prefhandle(pc),a0	; PrefsHandle pointer -> a0
	jsr	_LVOFreePrefsHandle(a6)	; Free preferences handle

.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 and working with strings

doslibname:	dc.b	"dos.library",0
prefslibname:	dc.b	"preferences.library",0
prefname:	dc.b	"Example",0		
phallocok:	dc.b	"PrefsHandle allocated OK",10,0
str_prompt:	dc.b	"Enter a string or leave blank to quit and press return:",10,0
str_retreive:	dc.b	"Retreived: ",0
str_removed:	dc.b	"Entry removed",10,0
str_notremoved:	dc.b	"Entry NOT removed",10,0
str_newline:	dc.b	10,0
		cnop	0,4
