codesnippets:filesystemioandstreams
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| codesnippets:filesystemioandstreams [2021/04/20 13:01] – created f2b216 | codesnippets:filesystemioandstreams [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ~~DISCUSSION ~~ | ||
| ====== Filesystem IO and streams ====== | ====== Filesystem IO and streams ====== | ||
| *example: | *example: | ||
| - | * code< | + | * code, module FileSystem<code Haskell> |
| {-| | {-| | ||
| Description : is to provides a generalised, | Description : is to provides a generalised, | ||
| Line 17: | Line 18: | ||
| * uniform types for identification of input and output streams (' | * uniform types for identification of input and output streams (' | ||
| - | * higher order functions (stream handler functions, | + | * higher order functions |
| * can not cause error by " | * can not cause error by " | ||
| - | * supports the following file system features: | + | |
| - | + | | |
| - | * deletion of files | + | |
| - | * reading | + | |
| - | + | | |
| - | * supports the following data types: | + | |
| - | + | * supports the following data types: | |
| - | * Octets | + | |
| + | | ||
| + | |||
| + | NOTE: There is no isolated read function like the write function, because this would lead to errors like " | ||
| + | Background: The function hGetContents needs an open file handle, that may be closed before the lazy String has been evaluated. | ||
| * example | * example | ||
| @ | @ | ||
| - | | + | import qualified |
| - | import qualified | + | import qualified |
| - | | + | main :: IO () |
| - | main = | + | main = |
| - | | + | |
| - | | + | |
| - | FS.writeToStream | + | |
| - | FS.writeToStream | + | |
| - | FS.writeToStream | + | FS.DataWriter |
| + | FS.DataWriter | ||
| + | ] | ||
| @ | @ | ||
| - | * copies the file "Spec.hs" in " | + | * copies the file "a.txt" in " |
| - | * sends to octal representation | + | * sends the file content of " |
| * will not cause uncaught exception caused by " | * will not cause uncaught exception caused by " | ||
| -} | -} | ||
| Line 52: | Line 59: | ||
| Inp(..), | Inp(..), | ||
| Out(..), | Out(..), | ||
| + | DataReader(..), | ||
| + | DataWriter(..), | ||
| removeFileIfExists, | removeFileIfExists, | ||
| - | | + | |
| - | | + | |
| ) where | ) where | ||
| Line 83: | Line 92: | ||
| | otherwise = Excp.throwIO e | | otherwise = Excp.throwIO e | ||
| - | readFromStream | + | data DataReader s = |
| - | readFromStream | + | DataReader { |
| - | readFromStream | + | rinp :: Inp, |
| + | rfRead :: (SysIo.Handle | ||
| + | |||
| + | data DataWriter s = | ||
| + | DataWriter { | ||
| + | rout :: Out, | ||
| + | rfWrite :: (SysIo.Handle -> s -> IO ()) } | ||
| + | |||
| + | processData :: (DataReader a) -> [DataWriter a] -> IO () | ||
| + | processData (DataReader | ||
| + | do | ||
| + | dt <- f SysIo.stdin | ||
| + | | ||
| + | processData (DataReader | ||
| do | do | ||
| hFile <- SysIo.openFile sFileName SysIo.ReadMode | hFile <- SysIo.openFile sFileName SysIo.ReadMode | ||
| - | | + | |
| + | processData' | ||
| SysIo.hClose hFile | SysIo.hClose hFile | ||
| - | return x | ||
| - | writeToStream | + | processData' |
| - | writeToStream | + | processData' |
| - | writeToStream | + | processData' |
| - | writeToStream | + | do |
| - | writeToStream | + | writeToData wrt x |
| + | processData' | ||
| + | |||
| + | writeToData :: (DataWriter a) -> a -> IO () | ||
| + | writeToData (DataWriter | ||
| + | writeToData (DataWriter | ||
| + | writeToData (DataWriter | ||
| + | writeToData (DataWriter | ||
| do | do | ||
| hFile <- SysIo.openFile sFileName SysIo.WriteMode | hFile <- SysIo.openFile sFileName SysIo.WriteMode | ||
| Line 102: | Line 131: | ||
| SysIo.hClose hFile | SysIo.hClose hFile | ||
| </ | </ | ||
| + | * code, use case of files system< | ||
| + | import qualified FileSystem as FS | ||
| + | import qualified System.IO as SysIo | ||
| + | |||
| + | main :: IO () | ||
| + | main = | ||
| + | FS.processData | ||
| + | (FS.DataReader (FS.FileInp " | ||
| + | [ | ||
| + | FS.DataWriter (FS.FileOut " | ||
| + | FS.DataWriter FS.StdOut SysIo.hPutStr, | ||
| + | FS.DataWriter FS.StdErr SysIo.hPutStr | ||
| + | ] | ||
| + | </ | ||
| + | * compiles, error and warning free, with compiler: GHC 8.10.4, using compiler option -Wall | ||
| + | * executes with output as file copy of " | ||
| + | * example, with conversions | ||
| + | * code:< | ||
| + | import qualified FileSystem as FS | ||
| + | import qualified System.IO as SysIo | ||
| + | import qualified Data.Char as Ch | ||
| + | |||
| + | main :: IO () | ||
| + | main = | ||
| + | FS.processData | ||
| + | (FS.DataReader (FS.FileInp " | ||
| + | [ | ||
| + | FS.DataWriter (FS.FileOut " | ||
| + | FS.DataWriter FS.StdOut hPutToLower, | ||
| + | FS.DataWriter FS.StdErr SysIo.hPutStr | ||
| + | ] | ||
| + | |||
| + | hGetToUpper :: SysIo.Handle -> IO String | ||
| + | hGetToUpper h = | ||
| + | do | ||
| + | s <- SysIo.hGetContents h | ||
| + | return (fmap Ch.toUpper s) | ||
| + | |||
| + | hPutToLower :: SysIo.Handle -> String -> IO () | ||
| + | hPutToLower h s = SysIo.hPutStr h (fmap Ch.toLower s) | ||
| + | </ | ||
| + | * input, file " | ||
| + | The quick brown fox jumps over the lazy dog. | ||
| + | </ | ||
| + | * output, in terminal:< | ||
| + | the quick brown fox jumps over the lazy dog. | ||
| + | THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. | ||
| + | </ | ||
| + | * output, file " | ||
| + | THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/filesystemioandstreams.1618916488.txt.gz · Last modified: (external edit)
