module Data.CSV.Table.Types (
Table (..)
, Row (..)
, Col (..)
, RowInfo
, TField (..)
, Order (..)
, getCols
, getRows
, lookupCol
, fromFile
, fromString
, toFile
) where
import Text.Printf
import Text.CSV
import System.FilePath
import Control.Applicative ((<$>))
import Data.Maybe
import Data.List (sort, elemIndex)
import qualified Data.Map.Strict as M
newtype Col = C Field deriving (Eq, Ord, Show)
newtype Row = R [Field] deriving (Eq, Ord, Show)
type RowInfo = [(Col, Field)]
data Table = T { dim :: Int, cols :: [Col], body :: [Row]}
getCols t = [c | C c <- cols t]
getRows t = [r | R r <- body t]
lookupCol :: Col -> RowInfo -> Field
lookupCol c cxs = fromMaybe err $ lookup c cxs
where
err = printf "lookupCol: cannot find %s in %s" (show c) (show cxs)
data TField = FStr | FInt | FDbl
deriving (Eq, Ord, Show)
data Order = Asc | Dsc
deriving (Eq, Ord, Show)
fromCSV :: CSV -> Table
fromCSV [] = error "fromCSV: Empty CSV with no rows!"
fromCSV (r:rs) = T n cs b
where
n = length r
cs = [C x | x <- r]
b = mapMaybe (makeRow n) $ zip [0..] rs
makeRow :: Int -> (Int, Record) -> Maybe Row
makeRow n (i, xs)
| length xs == n = Just $ R xs
| empty xs = Nothing
| otherwise = error $ printf "Row %d does not have %d columns:\n%s" i n (show xs)
empty :: Record -> Bool
empty = null . unwords
toCSV :: Table -> CSV
toCSV t = [c | C c <- cols t] : [xs | R xs <- body t]
toFile :: FilePath -> Table -> IO ()
toFile f = writeFile f . show
fromFile :: FilePath -> IO Table
fromFile f = fromString f <$> readFile f
fromString :: FilePath -> String -> Table
fromString fp s = fromCSV $ parseCSV' fp s
parseCSV' fp s = case parseCSV fp s of
Right c -> c
Left e -> error $ printf "parseCSV': %s" (show e)
instance Show Table where
show = printCSV . toCSV