Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an in-memory handle in Haskell?

I want something that looks like a file handle but is really backed by an in-memory buffer to use for I/O redirects. How can I do this?

like image 982
airportyh Avatar asked Sep 13 '08 14:09

airportyh


3 Answers

I just wrote a library which provides this, called "knob" [hackage]. You can use it to create Handles which reference/modify a ByteString:

import Data.ByteString (pack)
import Data.Knob
import System.IO

main = do
    knob <- newKnob (pack [])
    h <- newFileHandle knob "test.txt" WriteMode
    hPutStrLn h "Hello world!"
    hClose h
    bytes <- Data.Knob.getContents knob
    putStrLn ("Wrote bytes: " ++ show bytes)
like image 89
John Millikin Avatar answered Oct 01 '22 02:10

John Millikin


If you can express what you want to do in terms of C or system calls you could use Haskell's Foreign Function Interface (FFI). I started to suggest using mmap, but on second thought I think mmap might be a mapping the wrong way even if you used it with the anonymous option.

You can find more information about the Haskell FFI at the haskell.org wiki.

like image 3
Jason Dagit Avatar answered Oct 01 '22 01:10

Jason Dagit


This is actually a bug in the library design, and one that's annoyed me, too. I see two approaches to doing what you want, neither of which is terribly attractive.

  1. Create a new typeclass, make the current handle an instance of it, write another instance to do the in-memory-data thing, and change all of your programs that need to use this facility. Possibly this is as simple as importing System.SIO (or whatever you want to call it) instead of System.IO. But if you use the custom I/O routines in libraries such as Data.ByteString, there's more work to be done there.

  2. Rewrite the I/O libraries to extend them to support this. Not trivial, and a lot of work, but it wouldn't be particularly difficult work to do. However, then you've got a compatibility issue with systems that don't have this library.

like image 2
cjs Avatar answered Oct 01 '22 02:10

cjs