module Language.C.Data.InputStream (
InputStream, readInputStream,inputStreamToString,inputStreamFromString,
takeByte, takeChar, inputStreamEmpty, takeChars,
countLines,
)
where
import Data.Word
#ifndef NO_BYTESTRING
import Data.ByteString (ByteString)
import qualified Data.ByteString as BSW
import qualified Data.ByteString.Char8 as BSC
#else
import qualified Data.Char as Char
#endif
readInputStream :: FilePath -> IO InputStream
inputStreamToString :: InputStream -> String
inputStreamFromString :: String -> InputStream
takeByte :: InputStream -> (Word8, InputStream)
takeChar :: InputStream -> (Char, InputStream)
inputStreamEmpty :: InputStream -> Bool
takeChars :: Int -> InputStream -> [Char]
countLines :: InputStream -> Int
#ifndef NO_BYTESTRING
type InputStream = ByteString
takeByte bs = BSW.head bs `seq` (BSW.head bs, BSW.tail bs)
takeChar bs = BSC.head bs `seq` (BSC.head bs, BSC.tail bs)
inputStreamEmpty = BSW.null
#ifndef __HADDOCK__
takeChars !n bstr = BSC.unpack $ BSC.take n bstr --leaks
#endif
readInputStream = BSW.readFile
inputStreamToString = BSC.unpack
inputStreamFromString = BSC.pack
countLines = length . BSC.lines
#else
type InputStream = String
takeByte bs
| Char.isLatin1 c = let b = fromIntegral (Char.ord c) in b `seq` (b, tail bs)
| otherwise = error "takeByte: not a latin-1 character"
where c = head bs
takeChar bs = (head bs, tail bs)
inputStreamEmpty = null
takeChars n str = take n str
readInputStream = readFile
inputStreamToString = id
inputStreamFromString = id
countLines = length . lines
#endif