home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / System / ScalosPrefs / examples / ASM / single.s < prev   
Text File  |  1980-08-01  |  7KB  |  208 lines

  1. ;APS00000000000000000000000000000000000000000000000000000000000000000000000000000000
  2. ; Simple example of single data items per tag
  3.  
  4.     incdir    include:
  5.     include    exec/types.i
  6.     include    lvo/dos_lib.i
  7.     include    lvo/exec_lib.i
  8.     include    lvo/preferences_lib.i
  9.     include    libraries/iffparse.i
  10.     include    scalos/preferences.i
  11.  
  12.  
  13. start:    movem.l    d1-d7/a0-a6,-(a7)
  14.     move.l    4.w,a6            ; Get pointer to execbase
  15.  
  16.     lea    doslibname(pc),a1    ; SPRPTR "dos.library" -> a1
  17.     moveq.l    #36,d0            ; 36 (version to open) -> d0
  18.     jsr    _LVOOpenLibrary(a6)    ; Open dos.library
  19.     move.l    d0,dosbase        ; store/check pointer to dos.library base
  20.     beq    .nodoslib        ; if NULL, branch to end of program
  21.     
  22.     lea    prefslibname(pc),a1    ; STRPTR "preferences.library" -> a1
  23.     moveq.l    #39,d0            ; 39 (version to open) -> d0
  24.     jsr    _LVOOpenLibrary(a6)    ; Open preferences.library
  25.     move.l    d0,prefsbase        ; store/check pointer to prefsbase
  26.     beq    .noprefslib        ; if NULL, branch to end of program
  27.  
  28. ;    First we need to allocate a PrefsHandle by name
  29.     move.l    d0,a6            ; pointer to prefsbase
  30.     lea    prefname(pc),a0        ; Get name to refer to this PrefsHandle -> a1
  31.     jsr    _LVOAllocPrefsHandle(a6); try to allocate preferences handle
  32.     move.l    d0,prefhandle        ; store/check pointer to PrefsHandle
  33.     beq    .noph            ; if NULL, wasn't allocated so quit
  34.  
  35.     move.l    dosbase(pc),a6        ; dos.library base -> a6
  36.     move.l    #phallocok,d1        ; pointer to string -> d1
  37.     jsr    _LVOPutStr(a6)        ; print string to CLI (just to say we allocated PrefsHandle OK)
  38.  
  39. ;    We need an ID and a Tag value to refer to a specific preference item
  40. ;    Remember that the ID must be made from 4 ASCII characters and the Tag
  41. ;    value must not be 0
  42.     move.l    #'MAIN',d6        ; Set the ID (stored in d6) to 4 char ascii string "MAIN"
  43.     moveq.l    #1,d7            ; Set the Tag to 1 (imaginative, huh? ;)
  44.  
  45.  
  46. ;    Store the example preference data (string) under the ID and Tag
  47. ;    (as set a couple of lines ago). We pass a length of string length + 1
  48. ;    so that the NULL terminator will get stored in the preference.
  49.     lea    prefdata(pc),a0        ; get pointer to string to store
  50.     move.l    a0,a1            ; store in correct register for lib call
  51.     jsr    strlen            ; calculate length of string
  52.     addq.l    #1,d0            ; increase by 1 for NULL terminator
  53.     move.l    d0,d2            ; set it as the size of data to store
  54.     move.l    prefhandle(pc),a0    ; pointer to previously allocated preferences handle
  55.     move.l    d6,d0            ; get ID
  56.     move.l    d7,d1            ; get Tag
  57.     move.l    prefsbase(pc),a6    ; pointer to prefsbase -> a6
  58.     jsr    _LVOSetPreferences(a6)    ; store data in preferences
  59.  
  60.  
  61. ;    Use FindPreferences to find out if the item was stored OK
  62.     move.l    d6,d0            ; get ID
  63.     move.l    d7,d1            ; get Tag
  64.     move.l    prefhandle(pc),a0    ; get pointer to PrefsHandle
  65.     move.l    prefsbase(pc),a6    ; get pointer to prefsbase
  66.     jsr    _LVOFindPreferences(a6)    ; try to get pointer to the preference Tag
  67.     tst.l    d0            ; check pointer to Tag
  68.     beq    .nofind            ; if NULL, skip past all this stuff, because it didn't exist
  69.     move.l    d0,a2            ; otherwise, keep copy of the pointer in a2
  70.  
  71.     move.l    dosbase(pc),a6        ; print a message to say that
  72.     move.l    #findok,d1        ; the preferences were found OK
  73.     jsr    _LVOPutStr(a6)
  74.  
  75.     lea    prefdata(pc),a0        ; get string that we stored
  76.     jsr    strlen            ; get length of string we stored
  77.     addq.l    #1,d0            ; increase by 1 for NULL character
  78.     move.l    d0,-(a7)        ; store on stack as data for sprintf
  79.     move.l    #str_sizeof,-(a7)    ; store format string on stack
  80.     move.l    #temp,-(a7)        ; store output buffer on stack
  81.     jsr    sprintf            ; call sprintf
  82.     lea    12(a7),a7        ; fix stack
  83.     move.l    #temp,d1        ; get pointer to output buffer
  84.     jsr    _LVOPutStr(a6)        ; print it
  85.  
  86.     move.w    ps_Size(a2),-(a7)    ; get size of data from preference Tag pointer
  87.     move.l    #str_sizepd,-(a7)    ; get format string for message
  88.     move.l    #temp,-(a7)        ; output buffer
  89.     jsr    sprintf            ; sprintf
  90.     lea    10(a7),a7        ; clean up stack
  91.     move.l    #temp,d1        ; pointer to output buffer
  92.     jsr    _LVOPutStr(a6)        ; print string
  93.  
  94.  
  95. ;    To show that the data was actually stored, we use GetPreferences
  96. ;    to retrieve the data from storage
  97.     move.l    prefhandle(pc),a0    ; pointer to PrefsHandle
  98.     move.l    d6,d0            ; ID
  99.     move.l    d7,d1            ; Tag
  100.     lea    temp(pc),a1        ; pointer to storage memory
  101.     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)
  102.     move.l    prefsbase(pc),a6    ; pointer to prefsbase
  103.     jsr    _LVOGetPreferences(a6)    ; GetPreferences
  104.     tst.l    d0            ; test size of data retrieved
  105.     beq    .nodata            ; if =0 then no data was retrieved, no point in showing it :)
  106.  
  107.  
  108.     move.l    d0,-(a7)        ; put amount of bytes retrieved on stack
  109.     move.l    #str_bytesret,-(a7)    ; format string on stack
  110.     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
  111.     jsr    sprintf            ; sprintf
  112.     lea    12(a7),a7        ; clean up stack
  113.     move.l    dosbase(pc),a6        ; dosbase into a6
  114.     move.l    #temp+128,d1        ; get pointer to string created with sprintf
  115.     jsr    _LVOPutStr(a6)        ; print that string
  116.     move.l    #str_contents,d1    ; pointer to string
  117.     jsr    _LVOPutStr(a6)        ; print it
  118.     move.l    #temp,d1        ; pointer to data retrieved by GetPreferences
  119.     jsr    _LVOPutStr(a6)        ; print it
  120.     move.l    #str_newline,d1        ; pointer to a string with a newline
  121.     jsr    _LVOPutStr(a6)        ; print it
  122.  
  123. .nodata:
  124.     
  125.     bra    .endif001
  126. .nofind:
  127.     move.l    dosbase(pc),a6        ; print a message to say that
  128.     move.l    #findnotok,d1        ; the preferences were not
  129.     jsr    _LVOPutStr(a6)        ; found OK
  130.     
  131. .endif001:
  132.  
  133. ;    Finally we free the PrefsHandle when we no longer need it
  134.     move.l    prefsbase(pc),a6    ; prefsbase pointer -> a6
  135.     move.l    prefhandle(pc),a0    ; PrefsHandle pointer -> a0
  136.     jsr    _LVOFreePrefsHandle(a6)    ; Free preferences handle
  137.  
  138. .noph:
  139.     move.l    4.w,a6            ; execbase pointer -> a6
  140.     move.l    prefsbase(pc),a1    ; prefsbase pointer -> a1
  141.     jsr    _LVOCloseLibrary(a6)    ; close preferences.library
  142.  
  143. .noprefslib:
  144.     move.l    dosbase(pc),a1        ; pointer to dos.library base -> a1
  145.     jsr    _LVOCloseLibrary(a6)    ; close dos.library
  146.  
  147. .nodoslib:    
  148.     movem.l    (a7)+,d1-d7/a0-a6
  149.     moveq.l    #0,d0
  150.     rts
  151.     
  152.  
  153. ; Length of string
  154. ; a0 = STRPTR
  155. strlen:
  156.     moveq.l    #0,d0        ; clear count of length of string
  157. .loop:    tst.b    (a0)+        ; check char in string for NULL and move onto next
  158.     beq    .endloop    ; if NULL, skip to end of loop
  159.     addq.l    #1,d0        ; otherwise increase length of string
  160.     bra    .loop        ; repeat loop
  161. .endloop:
  162.     rts
  163.  
  164.  
  165. ; C-like sprintf (from autodocs)
  166. ; parameters on stack from right to left
  167. ; (except the data items, they are on from left to right)
  168. ; output, format [, data]
  169. sprintf:
  170.     movem.l    a2/a3/a6,-(a7)
  171.  
  172.     move.l    16(a7),a3
  173.     move.l    20(a7),a0
  174.     lea    24(a7),a1
  175.     lea    stuffChar(pc),a2
  176.     move.l    4.w,a6
  177.     jsr    _LVORawDoFmt(a6)
  178.  
  179.     movem.l    (a7)+,a2/a3/a6
  180.     rts
  181.  
  182. stuffChar:
  183.     move.b    d0,(a3)+
  184.     rts
  185.     
  186.  
  187. ; Storage space for variables/strings etc
  188. ;execbase:    dc.l    0    ; exec.library base
  189. dosbase:    dc.l    0    ; dos.library base
  190. prefsbase:    dc.l    0    ; Pointer to preferences.library base
  191.  
  192. prefhandle:    dc.l    0    ; Pointer to our PrefsHandle
  193. temp:        ds.b    256    ; Temporary area for storing preferences
  194.  
  195. doslibname:    dc.b    "dos.library",0
  196. prefslibname:    dc.b    "preferences.library",0
  197. prefname:    dc.b    "Example",0        ; Name to refer to our PrefHandle with
  198. prefdata:    dc.b    "This is some example preference data",0
  199. phallocok:    dc.b    "PrefsHandle allocated OK",10,0
  200. findok:        dc.b    "Preference data found",10,0
  201. findnotok:    dc.b    "Preference data not stored",10,0
  202. str_sizeof:    dc.b    "Size of data stored:   %ld",10,0
  203. str_sizepd:    dc.b    "Size of preference data: %d",10,0
  204. str_bytesret:    dc.b    "Bytes retrieved from PrefsHandle: %ld",10,0
  205. str_contents:    dc.b    "Contents of preference data:",10,"    ",0
  206. str_newline:    dc.b    10,0
  207.         cnop    0,4
  208.