tricu

A lazy functional language based on Tree Calculus
Log | Files | Refs | README | LICENSE

positioned.tri (1327B)


      1 !import io as IO
      2 !export patch
      3 !export append
      4 !export appendGuards
      5 !export badOffset
      6 
      7 -- Bounded edits and inspection, not atomic publication. Scope owns the file;
      8 -- every operation uses the same logical cursor, including read/write switches.
      9 patch path offset bytes size = IO.io <| IO.scope (owner : do IO.require
     10   file <- IO.openUpdate owner path
     11   original <- IO.fileSize file
     12   IO.seekFile file offset
     13   start <- IO.filePosition file
     14   IO.writeAll file bytes
     15   after <- IO.filePosition file
     16   IO.resizeFile file size
     17   afterResize <- IO.filePosition file
     18   resized <- IO.fileSize file
     19   IO.seekFile file 0
     20   prefix <- IO.readSome file 4096
     21   IO.pure [original start after afterResize resized prefix])
     22 
     23 append path bytes = IO.io <| IO.scope (owner : do IO.require
     24   file <- IO.openAppender owner path
     25   before <- IO.fileSize file
     26   IO.writeAll file bytes
     27   after <- IO.fileSize file
     28   IO.pure [before after])
     29 
     30 appendGuards path = IO.io <| IO.scope (owner : IO.require (IO.openAppender owner path)
     31   (file : do IO.bind
     32     seek <- IO.seekFile file 0
     33     position <- IO.filePosition file
     34     resized <- IO.resizeFile file 0
     35     read <- IO.readSome file 1
     36     IO.pure [seek position resized read]))
     37 
     38 badOffset path offset = IO.io <| IO.scope (owner : IO.require (IO.openUpdate owner path)
     39   (file : IO.seekFile file offset))