@@ -12,6 +12,7 @@ module Data.Object.Yaml.Lib
1212 -- * Higher level functions
1313 , encode
1414 , decode
15+ , decodeFile
1516 ) where
1617
1718import qualified Data.ByteString.Internal as B
@@ -53,6 +54,37 @@ foreign import ccall unsafe "yaml_parser_set_input_string"
5354 -> CULong
5455 -> IO ()
5556
57+foreign import ccall unsafe "yaml_parser_set_input_file"
58+ c_yaml_parser_set_input_file :: Parser
59+ -> File
60+ -> IO ()
61+
62+data FileStruct
63+type File = Ptr FileStruct
64+
65+foreign import ccall unsafe "fopen"
66+ c_fopen :: Ptr CChar
67+ -> Ptr CChar
68+ -> IO File
69+
70+foreign import ccall unsafe "fclose"
71+ c_fclose :: File
72+ -> IO ()
73+
74+withFileParser :: FilePath -> (Parser -> IO a) -> IO a
75+withFileParser fp f = allocaBytes parserSize $ \p ->
76+ do
77+ res <- c_yaml_parser_initialize p
78+ when (res == 0) $ throwIO YamlOutOfMemory
79+ file <- withCString fp $ \fp' -> withCString "r" $ \r' ->
80+ c_fopen fp' r'
81+ when (file == nullPtr) $ throwIO $ YamlFileNotFound fp
82+ c_yaml_parser_set_input_file p file
83+ ret <- f p
84+ c_fclose file
85+ c_yaml_parser_delete p
86+ return ret
87+
5688withParser :: B.ByteString -> (Parser -> IO a) -> IO a
5789withParser bs f = allocaBytes parserSize $ \p ->
5890 do
@@ -399,3 +431,8 @@ decode :: MonadFailure YamlException m
399431 => B.ByteString
400432 -> IO (m [Event])
401433decode bs = withParser bs parserParse
434+
435+decodeFile :: MonadFailure YamlException m
436+ => FilePath
437+ -> IO (m [Event])
438+decodeFile fp = withFileParser fp parserParse