on master help sign in
Added basic decoding from file
commit22ced4da973f15537a82fa3da7d30e027e214ac0 authorMichael Snoyman <michael@snoyman.com> dateSun 10 Jan 2010, 20:11 (17 years ago) parent4a7cc88
4 files changed+52
Data/Object/Yaml.hs+7 Data/Object/Yaml/Internal.hs+1 Data/Object/Yaml/Lib.hs+37 c/helper.c+7
Data/Object/Yaml.hs+7
@@ -10,6 +10,8 @@ module Data.Object.Yaml
1010 YamlDoc (..)
1111 , readYamlDoc
1212 , writeYamlDoc
13+ -- * Reading directly from files
14+ , readYamlObject
1315 -- * 'YamlObject' definition
1416 , YamlScalar (..)
1517 , Tag (..)
@@ -65,6 +67,11 @@ readYamlDoc = fmap YamlDoc . B.readFile
6567writeYamlDoc :: FilePath -> YamlDoc -> IO ()
6668writeYamlDoc fp = B.writeFile fp . unYamlDoc
6769
70+readYamlObject :: FilePath -> IO (Object YamlScalar YamlScalar)
71+readYamlObject fp = do
72+ es <- join $ decodeFile fp
73+ convertAttemptWrap es
74+
6875data YamlScalar = YamlScalar
6976 { value :: ByteString
7077 , tag :: Tag
Data/Object/Yaml/Internal.hs+1
@@ -86,5 +86,6 @@ data YamlException =
8686 | YamlPrematureEventStreamEnd
8787 | YamlNonScalarKey
8888 | YamlInvalidStartingEvent Event
89+ | YamlFileNotFound FilePath
8990 deriving (Show, Typeable)
9091instance Exception YamlException
Data/Object/Yaml/Lib.hs+37
@@ -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
c/helper.c+7
@@ -107,3 +107,10 @@ int get_scalar_style(yaml_event_t *e)
107107{
108108 return e->data.scalar.style;
109109}
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+}