Warning, /acts/Traccc/extras/ccl_partitioning/src/CsvIo.hs is written in an unsupported language. File is not indexed.
0001 {-# LANGUAGE BangPatterns #-}
0002 {-# LANGUAGE LambdaCase #-}
0003
0004 module CsvIo (getCsvCells) where
0005
0006 import System.Exit (exitFailure)
0007 import System.IO
0008 import Data.ByteString (ByteString, hGetSome, empty)
0009 import Data.Csv.Incremental
0010 import Data (Cell)
0011
0012 feed :: (ByteString -> Parser Cell) -> Handle -> IO (Parser Cell)
0013 feed k csvFile = do
0014 hIsEOF csvFile >>= \case
0015 True -> return $ k empty
0016 False -> k <$> hGetSome csvFile 4096
0017
0018 getCsvCells :: String -> IO [Either String Cell]
0019 getCsvCells fn = do
0020 withFile fn ReadMode $ \ csvFile -> do
0021 let loop !_ (Fail _ errMsg) = do putStrLn errMsg; exitFailure
0022 loop acc (Many rs k) = loop (acc ++ rs) =<< feed k csvFile
0023 loop acc (Done rs) = do return (acc ++ rs)
0024 loop [] (decode HasHeader)