hirt-0.0.1.0: Input.hs
{-# LANGUAGE OverloadedStrings #-}
module Input where
import Types
import Parse
import Data.Char
import Data.Attoparsec.Text (parseOnly)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Text.CSV
fromCSV :: CSV -> [(Contestant, Task, Points)]
fromCSV xs = [(c,t,r > 0) | (c,t,r) <- concatMap row xs']
where
row (name:ys) = zipWith (resp name) tasks ys
resp name task res = (T.pack name, task, readRes res)
hasNames = (t1 == "") || (t1 == "name")
where t1 = (filter (not . isSpace) . head . head) xs
xs' | hasNames = tail xs
| otherwise = xs
tasks :: [T.Text]
tasks | hasNames = (map T.pack . tail . head) xs
| otherwise = map (T.pack . reverse . take 2 . (++ repeat '0') . reverse . show) [1::Int ..]
readRes x = read x :: Int
readResponses :: FilePath -> IO [(Contestant, Task, Points)]
readResponses name = do
s <- T.readFile name
case parseOnly responsesParser s of
Right r -> return r
Left _ -> case parseCSV name . T.unpack $ s of
Right csv -> return $ fromCSV csv
Left _ -> error $ "could not parse " ++ name