home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MCD.DMS / in.adf / libsrc / files.asm next >
Encoding:
Assembly Source File  |  1995-04-18  |  4.4 KB  |  254 lines

  1.  
  2. * Maxon C++ Library:
  3. * Modul "files"
  4. * Jens Gelhar 19.03.93, 17.04.94, 18.04.95
  5.  
  6.     xdef    _fopen,fopen__PCcPCc
  7.     xdef    _freopen,freopen__PCcPCcP06stream
  8.     xdef    _fclose,fclose__P06stream
  9.     xdef    _allocstream,allocstream__Ui
  10.     xdef    ___Filelist
  11.     xdef    _EXIT_1_close_sTiLl_open_files
  12.  
  13.     xref    _DOSBase,_std__in,_std__out,_std__err,_errno
  14.  
  15. Execbase    = 4
  16. AllocMem    = -198
  17. FreeMem    = -210
  18.  
  19. Open    = -30
  20. Close    = -36
  21. Seek    = -66
  22. IoErr    = -132
  23.  
  24. str_mode    = 6
  25. str_error    = 7
  26. str_bufptr    = 8
  27. str_flags    = 12
  28. str_sizeof    = 13
  29.  
  30. buf_close    = 24
  31.  
  32.  
  33. _EXIT_1_close_sTiLl_open_files:
  34.     move.l    ___Filelist,d0
  35.     beq.b    _ex_std
  36.     addq.l    #4,d0
  37.     move.l    d0,-(a7)
  38.     bsr    _fclose
  39.     addq.l    #4,a7
  40.     bra.b    _EXIT_1_close_sTiLl_open_files
  41. _ex_std    pea    _std__out
  42.     bsr    _fclose
  43.     move.l    #_std__in,(a7)
  44.     bsr    _fclose
  45.     move.l    #_std__err,(a7)
  46.     bsr    _fclose
  47.     addq.l    #4,a7
  48.     rts
  49.  
  50. _freopen:
  51. freopen__PCcPCcP06stream:
  52.     movem.l d2-d3/a6,-(a7)
  53.     move.l    _DOSBase,a6
  54.     move.l    3*4+12(a7),a0        ; Stream-Struktur
  55.     ; testen: ist die Datei noch offen?
  56.     move.l    (a0),d1
  57.     beq.b    .ok1
  58.     btst    #6,str_flags(a0)
  59.     beq.b    .ok1            ; darf geschlossen werden?
  60.     jsr    Close(a6)        ; ja, also zuerst schließen
  61. .ok1    move.l    3*4+8(a7),a0        ; Zugriffsart
  62.     move.l    3*4+4(a7),d1        ; Dateiname
  63.     bsr    openfile
  64.     tst.l    d0
  65.     beq.b    err
  66.     move.l    3*4+12(a7),a0
  67.     move.l    d0,(a0)
  68.     move.b    d1,str_mode(a0)
  69.     bset    #6,str_flags(a0)
  70.     clr.b    str_error(a0)
  71.     move.l    a0,d0
  72.     movem.l    (a7)+,d2-d3/a6
  73.     rts
  74.  
  75. _fopen:
  76. fopen__PCcPCc:    ; Stack-Parameter: Filename, Accessmode
  77.     movem.l d2-d3/a6,-(a7)
  78.     move.l    _DOSBase,a6
  79.     move.l    3*4+8(a7),a0        ; Zugriffsart
  80.     move.l    3*4+4(a7),d1        ; Dateiname
  81.     bsr.b    openfile
  82.     tst.l    d0
  83.     beq.b    err
  84.     move.l    d1,-(a7)
  85.     move.l    d0,-(a7)
  86.     bsr.b    allocstream__Ui
  87.     tst.l    d0
  88.     beq.b    memerr
  89.     move.l    d0,a0
  90.     addq.l    #4,a7
  91.     move.l    (a7)+,d1
  92.     move.b    d1,str_mode(a0)        ; Zugriffsart
  93. fopRet    movem.l    (a7)+,d2-d3/a6
  94.     rts
  95.  
  96. memerr:    ; allocstream schlug fehl:
  97.     move.l    _DOSBase,a6
  98.     move.l    (a1)+,d1        ; File wieder schließen
  99.     jsr    Close(a6)
  100.     addq.l    #4,a7
  101. err:    ; Datei nicht geöffnet:
  102.     move.l    _DOSBase,a6
  103.     jsr    IoErr(a6)
  104.     move.l    d0,_errno
  105.     moveq    #0,d0
  106.     bra.b    fopRet
  107.  
  108. _allocstream:
  109. allocstream__Ui:
  110.     ; Streamstruct für Filehandle 4(a7) allozieren
  111.     movem.l    d2/a2/a6,-(a7)
  112.     move.l    Execbase.w,a6
  113.     moveq    #str_sizeof+4,d0
  114.     move.l    #$10000,d1
  115.     jsr    AllocMem(a6)
  116.     tst.l    d0
  117.     beq.b    ast1
  118.     move.l    d0,a0
  119.     addq.l    #4,d0
  120.     move.l    3*4+4(a7),4(a0)          ; Filehandle eintragen
  121.     move.b    #$C0,str_flags+4(a0)
  122.     move.l    ___Filelist,(a0)
  123.     move.l    a0,___Filelist
  124. ast1    movem.l    (a7)+,d2/a2/a6
  125.     rts
  126.  
  127. openfile: ; Zugriffsart a0, Name d1: Filehandle nach d0, Modus (1=read, 2=write, 3 = rw) nach d1
  128.     move.b    (a0)+,d0
  129.     cmp.b    #'b',(a0)
  130.     bne.b    noB
  131.     addq.l    #1,a0
  132. noB:    cmp.b    #'r',d0
  133.     beq.b    fread
  134.     cmp.b    #'w',d0
  135.     beq.b    fwrite
  136.     cmp.b    #'a',d0
  137.     beq.b    fappend
  138.     moveq    #0,d0
  139.     rts
  140. fread:    cmp.b    #'+',(a0)
  141.     beq.b    freadwrite
  142.     move.l    #1005,d2
  143.     jsr    Open(a6)
  144.     moveq    #1,d1
  145.     rts
  146.  
  147. freadwrite:
  148.     move.l #1005,d2
  149.     jsr Open(a6)
  150.     moveq #3,d1
  151.     rts
  152.  
  153. fwrite:    cmp.b #'+',(a0)
  154.     beq.b frwnew
  155. fwnew:    move.l #1006,d2
  156.     jsr Open(a6)
  157.     moveq #2,d1
  158.     rts
  159.  
  160. frwnew:    ; neue Datei erzeugen für Lesen+schreiben:
  161.     move.l #1006,d2
  162.     move.l d1,-(a7)
  163.     jsr Open(a6)
  164.     move.l (a7)+,d1
  165.     tst.l d0
  166.     beq.b offen
  167.     move.l d1,-(a7)
  168.     move.l d0,d1
  169.     jsr Close(a6)
  170.     move.l (a7)+,d1
  171.     move.l #1005,d2
  172.     jsr Open(a6)
  173.     moveq #3,d1
  174. offen:    rts
  175.  
  176. fappend:
  177.     cmp.b #'+',(a0)
  178.     bne.b fapprw
  179.     bsr.b fapprw
  180.     moveq #3,d1
  181.     rts
  182. fapprw:    move.l d1,-(a7)
  183.     move.l #1005,d2
  184.     jsr Open(a6)
  185.     move.l (a7)+,d1
  186.     tst.l d0
  187.     beq.b fwnew
  188.     move.l d0,-(a7)
  189.     move.l d0,d1
  190.     moveq #0,d2
  191.     moveq #1,d3
  192.     jsr Seek(a6)
  193.     move.l (a7)+,d0
  194.     moveq #2,d1
  195.     rts
  196.  
  197. _fclose:
  198. fclose__P06stream:
  199.     movem.l d1-d3/a0/a1/a6,-(a7)
  200.     move.l 6*4+4(a7),d1
  201.     subq.l #4,d1
  202.     lea ___Filelist,a0
  203. loop:    cmp.l (a0),d1
  204.     beq.b found
  205.     move.l (a0),d0
  206.     beq.b notfound
  207.     move.l d0,a0
  208.     bra.b loop
  209. notfound:    ; Datei gibt's nicht offiziell:
  210.     cmp.l #_std__in-4,d1
  211.     beq.b found
  212.     cmp.l #_std__out-4,d1
  213.     beq.b found
  214.     cmp.l #_std__err-4,d1
  215.     beq.b found
  216.     moveq #-1,d0
  217.     bra.b fcret
  218. found:    move.l d1,a1
  219.     clr.l -(a7)
  220.     btst #6,str_flags+4(a1)
  221.     beq.b fc0
  222.     move.l 4(a1),(a7)       ; Filehandle
  223. fc0:    tst.b str_flags+4(a1)
  224.     bpl.b fc1
  225.     move.l (a1),(a0)        ; aus Liste aushängen
  226. fc1:    move.l str_bufptr+4(a1),d0
  227.     beq.b fc2
  228.     ; Puffer war eingerichtet:
  229.     move.l a1,-(a7)
  230.     move.l d0,a0
  231.     move.l buf_close(a0),a6
  232.     jsr (a6)
  233.     move.l (a7)+,a1
  234. fc2:    ; Struct freigeben:
  235.     tst.b str_flags+4(a1)
  236.     bpl.b fc3
  237.     moveq #str_sizeof+4,d0
  238.     move.l Execbase.w,a6
  239.     jsr FreeMem(a6)
  240. fc3:    move.l (a7)+,d1
  241.     beq.b fc4
  242.     move.l _DOSBase,a6
  243.     jsr Close(a6)           ; Datei endlich schließen
  244. fc4:    moveq #0,d0
  245. fcret:    movem.l (a7)+,d1-d3/a0/a1/a6
  246.     rts
  247.  
  248.     SECTION D,DATA
  249.  
  250. ___Filelist:    dc.l 0
  251.  
  252.     end
  253.  
  254.