例如,这是我使用Haskell和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
...
fileRead
方法返回一个FileInputStream
实例,但我不能在我的生活中找到文档中的任何一个方法来读取它。 GIO中相应的C方法应为g_input_stream_read
,但似乎没有在gtk2hs中实现。
我错过了什么吗?
答案 0 :(得分:1)
经过一些实验,我设法编写了自己的g_input_stream_read
实现。它不是很漂亮,甚至可能不安全,但这将成功打印在main顶部指定的文件的前1,024个字符:
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
为了成为一个简单的inputStreamRead :: InputStream -> IO (String)
,需要做一些工作,但至少它是朝着正确方向迈出的一步。
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