damien
damien

Reputation: 922

How can I use Haskell and GIO to read a file?

For example, here's a small piece of a program I'm writing using Haskell and gtk2hs:

import System.GIO

foreign import ccall safe "g_type_init"
    g_type_init :: IO ()

main :: IO ()
main = do
    g_type_init
    let file = fileFromParseName "my-file.txt"
    inputStream <- fileRead file Nothing
    ...

The fileRead method returns a FileInputStream instance, but I can't for the life of me find anywhere in the documentation a method to read from it. The corresponding C method in GIO should be g_input_stream_read, but it doesn't seem to be implemented in gtk2hs.

Am I missing something?

Upvotes: 0

Views: 236

Answers (1)

damien
damien

Reputation: 922

After some experimentation I managed to write my own implementation of g_input_stream_read. It's not very pretty and might not even be safe, but this will successfully print the first 1,024 characters of the file specified at the top of main:

import Control.Monad (liftM)
import Data.Char (chr)
import Data.Word (Word8)
import Foreign
import Foreign.C (CInt)
import System.GIO
import System.GIO.Types (unInputStream, toInputStream)
import System.Glib (glibTypeInit, GError)

foreign import ccall unsafe "g_input_stream_read"
    -- inputStreamRead <stream> <buffer> <count> <cancellable> <error>, returns the number of bytes read
    inputStreamRead :: Ptr InputStream -> Ptr a -> CInt -> Ptr (Maybe Cancellable) -> Ptr GError -> IO (CInt)

addrToChar :: Ptr a -> Int -> IO (Char)
addrToChar p i = do
    let addr = p `plusPtr` i
    val <- peek addr :: IO Word8
    return $ chr (fromIntegral val)

main :: IO ()
main = do
    glibTypeInit
    let file = fileFromParseName "file.txt"
    fileInputStream <- fileRead file Nothing
    let stream = unInputStream $ toInputStream fileInputStream
    allocaBytes 1024 $ \buffer -> do
        alloca $ \err -> do
            bytesRead <- liftM fromEnum $ inputStreamRead (unsafeForeignPtrToPtr stream) buffer 1024 nullPtr err :: IO Int
            result <- mapM (addrToChar buffer) [0..bytesRead]
            putStrLn result

It needs some work in order to become a simple inputStreamRead :: InputStream -> IO (String), but at least it's a step in the right direction.

EDIT: found a better solution. This one should keep reading until the number of bytes read equals 0, and has a friendlier entry point:

import Control.Monad (liftM)
import Data.Char (chr)
import Data.Word (Word8)
import Foreign
import Foreign.C (CInt)
import System.GIO
import System.GIO.Types
import System.Glib (glibTypeInit, GError)

foreign import ccall unsafe "g_input_stream_read"
    -- inputStreamRead <stream> <buffer> <count> <cancellable> <error>, returns the number of bytes read
    inputStreamRead :: Ptr InputStream -> Ptr a -> CInt -> Ptr Cancellable -> Ptr GError -> IO (CInt)

chunk :: Int
chunk = 4096

bytesToText :: [Word8] -> [Char]
bytesToText [] = []
bytesToText (x:xs) = (chr $ fromEnum x):(bytesToText xs)

readGIOStream :: InputStream -> Maybe Cancellable -> IO ([Word8])
readGIOStream stream cancel = do
    allocaBytes chunk $ \buffer -> do
        alloca $ \err -> do
            case cancel of
                Just c  -> withForeignPtr (unCancellable c) $ \c' -> readChunk buffer c' err streamPtr
                Nothing -> readChunk buffer nullPtr err streamPtr
            where streamPtr = unInputStream stream
                  readChunk b c e s = withForeignPtr s $ \s' -> do
                          bytesRead <- liftM fromEnum $ inputStreamRead s' b (toEnum chunk) c e
                          result <- mapM (\i -> peek $ b `plusPtr` i) [0..(bytesRead-1)]
                          if bytesRead == 0
                              then return result
                              else do rest <- readChunk b c e s
                                      return $ result ++ rest

main :: IO ()
main = do
    glibTypeInit
    let file = fileFromParseName "live-forever.txt"
    fileInputStream <- fileRead file Nothing
    text <- liftM bytesToText $ readGIOStream (toInputStream fileInputStream) Nothing
    putStrLn text

Upvotes: 1

Related Questions