@@ -12,6 +12,7 @@ module Data.Object.Yaml.Lib
12 -- * Higher level functions
12 -- * Higher level functions
17import qualified Data.ByteString.Internal as B
18import qualified Data.ByteString.Internal as B
@@ -53,6 +54,37 @@ foreign import ccall unsafe "yaml_parser_set_input_string"
57+foreign import ccall unsafe "yaml_parser_set_input_file"
58+ c_yaml_parser_set_input_file :: Parser
63+type File = Ptr FileStruct
65+foreign import ccall unsafe "fopen"
70+foreign import ccall unsafe "fclose"
74+withFileParser :: FilePath -> (Parser -> IO a) -> IO a
75+withFileParser fp f = allocaBytes parserSize $ \p ->
77+ res <- c_yaml_parser_initialize p
78+ when (res == 0) $ throwIO YamlOutOfMemory
79+ file <- withCString fp $ \fp' -> withCString "r" $ \r' ->
81+ when (file == nullPtr) $ throwIO $ YamlFileNotFound fp
82+ c_yaml_parser_set_input_file p file
85+ c_yaml_parser_delete p
56withParser :: B.ByteString -> (Parser -> IO a) -> IO a
88withParser :: B.ByteString -> (Parser -> IO a) -> IO a
57withParser bs f = allocaBytes parserSize $ \p ->
89withParser bs f = allocaBytes parserSize $ \p ->
@@ -399,3 +431,8 @@ decode :: MonadFailure YamlException m
399 => B.ByteString
431 => B.ByteString
400 -> IO (m [Event])
432 -> IO (m [Event])
401decode bs = withParser bs parserParse
433decode bs = withParser bs parserParse
435+decodeFile :: MonadFailure YamlException m
438+decodeFile fp = withFileParser fp parserParse