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 #1,d7 ; Set the Tag to 1 (imaginative, huh? ;)
; Store the example preference data (string) under the ID and Tag
; (as set a couple of lines ago). We pass a length of string length + 1
; so that the NULL terminator will get stored in the preference.
lea prefdata(pc),a0 ; get pointer to string to store
move.l a0,a1 ; store in correct register for lib call
jsr strlen ; calculate length of string
addq.l #1,d0 ; increase by 1 for NULL terminator
move.l d0,d2 ; set it as the size of data to store
move.l prefhandle(pc),a0 ; pointer to previously allocated preferences handle
move.l d6,d0 ; get ID
move.l d7,d1 ; get Tag
move.l prefsbase(pc),a6 ; pointer to prefsbase -> a6
jsr _LVOSetPreferences(a6) ; store data in preferences
; Use FindPreferences to find out if the item was stored OK
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 d0,a2 ; otherwise, keep copy of the pointer in a2
move.l dosbase(pc),a6 ; print a message to say that
move.l #findok,d1 ; the preferences were found OK
jsr _LVOPutStr(a6)
lea prefdata(pc),a0 ; get string that we stored
jsr strlen ; get length of string we stored
addq.l #1,d0 ; increase by 1 for NULL character
move.l d0,-(a7) ; store on stack as data for sprintf
move.l #str_sizeof,-(a7) ; store format string on stack
move.l #temp,-(a7) ; store output buffer on stack
jsr sprintf ; call sprintf
lea 12(a7),a7 ; fix stack
move.l #temp,d1 ; get pointer to output buffer
jsr _LVOPutStr(a6) ; print it
move.w ps_Size(a2),-(a7) ; get size of data from preference Tag pointer
move.l #str_sizepd,-(a7) ; get format string for message
move.l #temp,-(a7) ; output buffer
jsr sprintf ; sprintf
lea 10(a7),a7 ; clean up stack
move.l #temp,d1 ; pointer to output buffer
jsr _LVOPutStr(a6) ; print string
; 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
tst.l d0 ; test size of data retrieved
beq .nodata ; if =0 then no data was retrieved, no point in showing it :)
move.l d0,-(a7) ; put amount of bytes retrieved on stack
move.l #str_bytesret,-(a7) ; format string on stack
move.l #temp+128,-(a7) ; use second half of temp memory for this sprintf string, since the first half is being used by the contents of GetPreferences
jsr sprintf ; sprintf
lea 12(a7),a7 ; clean up stack
move.l dosbase(pc),a6 ; dosbase into a6
move.l #temp+128,d1 ; get pointer to string created with sprintf
jsr _LVOPutStr(a6) ; print that string
move.l #str_contents,d1 ; pointer to string
jsr _LVOPutStr(a6) ; print it
move.l #temp,d1 ; pointer to data retrieved by GetPreferences
jsr _LVOPutStr(a6) ; print it
move.l #str_newline,d1 ; pointer to a string with a newline
jsr _LVOPutStr(a6) ; print it
.nodata:
bra .endif001
.nofind:
move.l dosbase(pc),a6 ; print a message to say that
move.l #findnotok,d1 ; the preferences were not
jsr _LVOPutStr(a6) ; found OK
.endif001:
; Finally we free the PrefsHandle when we no longer need it