on master help sign in
Added basic decoding from file
22ced4dMichael Snoyman
Data/Object/Yaml.hs+7
@@ -10,6 +10,8 @@ module Data.Object.Yaml
10 YamlDoc (..)
10 YamlDoc (..)
11 , readYamlDoc
11 , readYamlDoc
12 , writeYamlDoc
12 , writeYamlDoc
13+ -- * Reading directly from files
14+ , readYamlObject
13 -- * 'YamlObject' definition
15 -- * 'YamlObject' definition
14 , YamlScalar (..)
16 , YamlScalar (..)
15 , Tag (..)
17 , Tag (..)
@@ -65,6 +67,11 @@ readYamlDoc = fmap YamlDoc . B.readFile
65writeYamlDoc :: FilePath -> YamlDoc -> IO ()
67writeYamlDoc :: FilePath -> YamlDoc -> IO ()
66writeYamlDoc fp = B.writeFile fp . unYamlDoc
68writeYamlDoc fp = B.writeFile fp . unYamlDoc
67
69
70+readYamlObject :: FilePath -> IO (Object YamlScalar YamlScalar)
71+readYamlObject fp = do
72+ es <- join $ decodeFile fp
73+ convertAttemptWrap es
74+
68data YamlScalar = YamlScalar
75data YamlScalar = YamlScalar
69 { value :: ByteString
76 { value :: ByteString
70 , tag :: Tag
77 , tag :: Tag
Data/Object/Yaml/Internal.hs+1
@@ -86,5 +86,6 @@ data YamlException =
86 | YamlPrematureEventStreamEnd
86 | YamlPrematureEventStreamEnd
87 | YamlNonScalarKey
87 | YamlNonScalarKey
88 | YamlInvalidStartingEvent Event
88 | YamlInvalidStartingEvent Event
89+ | YamlFileNotFound FilePath
89 deriving (Show, Typeable)
90 deriving (Show, Typeable)
90instance Exception YamlException
91instance Exception YamlException
Data/Object/Yaml/Lib.hs+37
@@ -12,6 +12,7 @@ module Data.Object.Yaml.Lib
12 -- * Higher level functions
12 -- * Higher level functions
13 , encode
13 , encode
14 , decode
14 , decode
15+ , decodeFile
15 ) where
16 ) where
16
17
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"
53 -> CULong
54 -> CULong
54 -> IO ()
55 -> IO ()
55
56
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+
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 ->
58 do
90 do
@@ -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
434+
435+decodeFile :: MonadFailure YamlException m
436+ => FilePath
437+ -> IO (m [Event])
438+decodeFile fp = withFileParser fp parserParse
c/helper.c+7
@@ -107,3 +107,10 @@ int get_scalar_style(yaml_event_t *e)
107{
107{
108 return e->data.scalar.style;
108 return e->data.scalar.style;
109}
109}
110+
111+int yaml_parser_set_input_filename(yaml_parser_t *parser, const char *filename)
112+{
113+ FILE *in = fopen(filename, "r");
114+ if (!in) return 0;
115+ yaml_parser_set_input_file(parser, in);
116+}