2021-05-17 16:03:59 +03:00
|
|
|
module TestBuffer
|
|
|
|
|
|
|
|
import Data.Buffer
|
2021-06-10 13:24:05 +03:00
|
|
|
import System.File
|
2021-05-17 16:03:59 +03:00
|
|
|
|
|
|
|
put : Show a => IO a -> IO ()
|
|
|
|
put = (>>= putStrLn . show)
|
|
|
|
|
|
|
|
main : IO ()
|
|
|
|
main = do
|
2021-06-10 18:45:31 +03:00
|
|
|
Just buf <- newBuffer 31
|
2021-05-17 16:03:59 +03:00
|
|
|
| Nothing => pure ()
|
|
|
|
|
|
|
|
setByte buf 0 1
|
|
|
|
setBits8 buf 1 2
|
2021-06-10 18:45:31 +03:00
|
|
|
setInt buf 2 0x1122334455667788
|
|
|
|
setDouble buf 10 (sqrt 2)
|
2021-05-17 16:03:59 +03:00
|
|
|
|
|
|
|
let helloWorld = "Hello, world"
|
|
|
|
|
|
|
|
Just helloWorldBuf <- newBuffer (stringByteLength helloWorld)
|
|
|
|
| Nothing => pure ()
|
|
|
|
|
|
|
|
setString helloWorldBuf 0 "Hello, world"
|
2021-06-10 18:45:31 +03:00
|
|
|
copyData helloWorldBuf 0 12 buf 18
|
2021-05-17 16:03:59 +03:00
|
|
|
|
|
|
|
put $ rawSize buf
|
|
|
|
|
|
|
|
put $ getByte buf 0
|
|
|
|
put $ getBits8 buf 1
|
2021-06-10 18:45:31 +03:00
|
|
|
put $ getInt buf 2
|
|
|
|
put $ getDouble buf 10
|
|
|
|
put $ getString buf 18 12
|
2021-05-17 16:03:59 +03:00
|
|
|
|
|
|
|
put $ bufferData buf
|
|
|
|
|
2021-06-10 13:24:05 +03:00
|
|
|
Just readBuf <- newBuffer 8
|
|
|
|
| Nothing => pure ()
|
|
|
|
Right f <- openFile "testRead.buf" Read
|
|
|
|
| Left err => put $ pure err
|
|
|
|
Right () <- readBufferData f readBuf 0 8
|
|
|
|
| Left err => put $ pure err
|
|
|
|
put $ bufferData readBuf
|
|
|
|
|
|
|
|
Just writeBuf <- newBuffer 8
|
|
|
|
| Nothing => pure ()
|
|
|
|
setInt writeBuf 0 0x7766554433221100
|
|
|
|
Right f <- openFile "testWrite.buf" WriteTruncate
|
|
|
|
| Left err => put $ pure err
|
|
|
|
Right () <- writeBufferData f writeBuf 0 8
|
|
|
|
| Left err => put $ pure err
|
|
|
|
|
2021-05-17 16:03:59 +03:00
|
|
|
freeBuffer helloWorldBuf
|
|
|
|
freeBuffer buf
|