home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / System / ScalosPrefs / examples / ASM / loadsave.s < prev    next >
Text File  |  1980-08-02  |  7KB  |  231 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    #str_allocok,d1        ; pointer to string -> d1
  37.     jsr    _LVOPutStr(a6)        ; print string to CLI (just to say we allocated PrefsHandle OK)
  38.  
  39.  
  40. ;    Try to load the preferences from a file
  41.     move.l    prefsbase(pc),a6    ; prefsbase
  42.     move.l    prefhandle(pc),a0    ; handle of our preferences allocated previously
  43.     lea    preffile(pc),a1        ; address of the string for the filename
  44.     jsr    _LVOReadPrefsHandle(a6)    ; call routine
  45.     
  46.  
  47. ;    We need an ID and a Tag value to refer to a specific preference item
  48. ;    Remember that the ID must be made from 4 ASCII characters and the Tag
  49. ;    value must not be 0
  50.     move.l    #'MAIN',d6        ; Set the ID (stored in d6) to 4 char ascii string "MAIN"
  51.     moveq.l    #1,d7            ; Set the Tag to 1 (imaginative, huh? ;)
  52.  
  53.  
  54. ;    Use FindPreferences to check if the item has been loaded
  55.     move.l    d6,d0            ; get ID
  56.     move.l    d7,d1            ; get Tag
  57.     move.l    prefhandle(pc),a0    ; get pointer to PrefsHandle
  58.     move.l    prefsbase(pc),a6    ; get pointer to prefsbase
  59.     jsr    _LVOFindPreferences(a6)    ; try to get pointer to the preference Tag
  60.     tst.l    d0            ; check pointer to Tag
  61.     beq    .nofind            ; if NULL, skip past all this stuff, because it didn't exist
  62.  
  63.     move.l    dosbase(pc),a6        ; print a message to say that
  64.     move.l    #str_findok,d1        ; the preferences were found OK
  65.     jsr    _LVOPutStr(a6)
  66.  
  67.  
  68. ;    To show that the data was actually stored, we use GetPreferences
  69. ;    to retrieve the data from storage
  70.     move.l    prefhandle(pc),a0    ; pointer to PrefsHandle
  71.     move.l    d6,d0            ; ID
  72.     move.l    d7,d1            ; Tag
  73.     lea    temp(pc),a1        ; pointer to storage memory
  74.     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)
  75.     move.l    prefsbase(pc),a6    ; pointer to prefsbase
  76.     jsr    _LVOGetPreferences(a6)    ; GetPreferences
  77.  
  78.  
  79.     move.l    dosbase(pc),a6        ; print message saying "This is contents"
  80.     move.l    #str_contents,d1
  81.     jsr    _LVOPutStr(a6)
  82.  
  83.     move.l    #temp,d1        ; print the contents of what was retrieved
  84.     jsr    _LVOPutStr(a6)
  85.  
  86.     move.l    #str_newline,d1        ; and print a newline
  87.     jsr    _LVOPutStr(a6)
  88.  
  89. .nofind:
  90. ;    Now we set a new value for the data
  91.     move.l    dosbase(pc),a6        ; print a prompt for the user
  92.     move.l    #str_prompt,d1
  93.     jsr    _LVOPutStr(a6)
  94.  
  95.     move.l    #128,d0            ; call gets like function to get a string from the user
  96.     lea    temp(pc),a0
  97.     jsr    gets
  98.  
  99.     lea    temp,a0            ; calculate length of string entered by user
  100.     jsr    strlen
  101.     tst.l    d0            
  102.     beq    .nostring        ; if length == 0 then user just wants to quit, skip next section
  103.  
  104.  
  105. ;    Set the new string entered by the user as the preference data
  106.     move.l    prefsbase(pc),a6    ; preferences.library base
  107.     addq.l    #1,d0            ; increase size of data to store by 1 so that NULL terminator of string gets stored
  108.     move.l    d0,d2            ; put size into correct register
  109.     move.l    d6,d0            ; preference ID
  110.     move.l    d7,d1            ; preference Tag
  111.     move.l    prefhandle(pc),a0    ; handle to our preferences
  112.     lea    temp(pc),a1        ; buffer to store
  113.     jsr    _LVOSetPreferences(a6)    ; call function
  114.  
  115.  
  116. ;    Finally write the new set of preferences. To test this program
  117. ;    run it again and it should show you the string you entered
  118.     move.l    prefhandle(pc),a0    ; handle to our preferences
  119.     lea    preffile(pc),a1        ; pointer to name of file to save as
  120.     jsr    _LVOWritePrefsHandle(a6); save preferences
  121.  
  122. .nostring:
  123.  
  124. ;    Free prefs handle
  125.     move.l    prefsbase(pc),a6    ; preferences.library base
  126.     move.l    prefhandle(pc),a0    ; handle to our preferences
  127.     jsr    _LVOFreePrefsHandle(a6)    ; free it
  128.  
  129. .noph:
  130.     move.l    4.w,a6            ; execbase pointer -> a6
  131.     move.l    prefsbase(pc),a1    ; prefsbase pointer -> a1
  132.     jsr    _LVOCloseLibrary(a6)    ; close preferences.library
  133.  
  134. .noprefslib:
  135.     move.l    dosbase(pc),a1        ; pointer to dos.library base -> a1
  136.     jsr    _LVOCloseLibrary(a6)    ; close dos.library
  137.  
  138. .nodoslib:    
  139.     movem.l    (a7)+,d1-d7/a0-a6
  140.     moveq.l    #0,d0
  141.     rts
  142.     
  143.  
  144. ; Length of string
  145. ; a0 = STRPTR
  146. strlen:
  147.     moveq.l    #0,d0        ; clear count of length of string
  148. .loop:    tst.b    (a0)+        ; check char in string for NULL and move onto next
  149.     beq    .endloop    ; if NULL, skip to end of loop
  150.     addq.l    #1,d0        ; otherwise increase length of string
  151.     bra    .loop        ; repeat loop
  152. .endloop:
  153.     rts
  154.  
  155.  
  156. ; C-like sprintf (from autodocs)
  157. ; parameters on stack from right to left
  158. ; (except the data items, they are on from left to right)
  159. ; output, format [, data]
  160. sprintf:
  161.     movem.l    a2/a3/a6,-(a7)
  162.  
  163.     move.l    16(a7),a3
  164.     move.l    20(a7),a0
  165.     lea    24(a7),a1
  166.     lea    stuffChar(pc),a2
  167.     move.l    4.w,a6
  168.     jsr    _LVORawDoFmt(a6)
  169.  
  170.     movem.l    (a7)+,a2/a3/a6
  171.     rts
  172.  
  173. stuffChar:
  174.     move.b    d0,(a3)+
  175.     rts
  176.  
  177.  
  178. ; gets() like function which uses dos.library FGets but strips the newline (if present)
  179. ; from the current input stream rather than a file
  180. ; a0 = buffer
  181. ; d0 = buffer size
  182. gets:
  183.     movem.l    d3/a2,-(a7)
  184.     move.l    d0,d3
  185.     move.l    a0,a2
  186.  
  187.     move.l    dosbase(pc),a6
  188.     jsr    _LVOOutput(a6)
  189.     move.l    d0,d1
  190.     jsr    _LVOFlush(a6)
  191.     jsr    _LVOInput(a6)
  192.     move.l    d0,d1
  193.     jsr    _LVOFlush(a6)
  194.     jsr    _LVOInput(a6)
  195.  
  196.     move.l    d0,d1
  197.     move.l    a2,d2
  198.     jsr    _LVOFGets(a6)
  199.     tst.l    d0
  200.     beq    .error
  201.  
  202. .loop:    move.b    (a2)+,d0
  203.     beq    .error
  204.     cmp.b    #10,d0
  205.     bne    .loop
  206.     move.b    #0,-(a2)
  207.     
  208. .error:    
  209.     movem.l    (a7)+,d3/a2
  210.     rts
  211.     
  212.  
  213. ; Storage space for variables/strings etc
  214. ;execbase:    dc.l    0    ; exec.library base
  215. dosbase:    dc.l    0    ; dos.library base
  216. prefsbase:    dc.l    0    ; Pointer to preferences.library base
  217.  
  218. prefhandle:    dc.l    0    ; Pointer to our PrefsHandle
  219. temp:        ds.b    256    ; Temporary area for storing preferences
  220.  
  221. doslibname:    dc.b    "dos.library",0
  222. prefslibname:    dc.b    "preferences.library",0
  223. prefname:    dc.b    "Example",0        ; Name to refer to our PrefHandle with
  224. preffile:    dc.b    "example.prefs",0    ; Name of preferences file. Does NOT need to be the same as the name of the PrefsHandle
  225. str_allocok:    dc.b    "PrefsHandle allocated OK",10,0
  226. str_findok:    dc.b    "Preference data found",10,0
  227. str_contents:    dc.b    "Contents of preference data:",10,0
  228. str_prompt:    dc.b    "Enter string or leave blank to quit and press return:",10,0
  229. str_newline:    dc.b    10,0
  230.         cnop    0,4
  231.