diff options
Diffstat (limited to 'src/Utils/ScoresFile.hs')
-rw-r--r-- | src/Utils/ScoresFile.hs | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/Utils/ScoresFile.hs b/src/Utils/ScoresFile.hs index 4028efa..2f496bf 100644 --- a/src/Utils/ScoresFile.hs +++ b/src/Utils/ScoresFile.hs @@ -13,6 +13,7 @@ import System.FilePath (takeExtension) import Control.Monad (liftM) import Data.List.Split (splitOn) import Data.Maybe (fromJust) +import System.FilePath ((</>)) scoresToCSV :: ScoresList -> String scoresToCSV = unlines . foldr step [] @@ -21,27 +22,31 @@ scoresToCSV = unlines . foldr step [] (show theClass ++ "," ++ show x ++ "," ++ show y) : theLines -- no malformed CSV handling here yet! +-- this function currently doesn't work scoresFromCSV :: String -> ScoresList scoresFromCSV csv = foldr step [] (lines csv) where step line scores = (theClass, Score (read scoreString) (read timeString)) : scores where classString:scoreString:timeString:[] = splitOn "," line - theClass = fromJust $ lookupSariulClass ((head . read) classString) ((last . read) classString) + theClass = fromJust $ lookupSariulClass ((read . (:[]) . head) classString) ((read . (:[]) . last) classString) --- read to scores-XX.csv where XX is largest timestamp +-- read from scores-XX.csv where XX is largest timestamp readScoresFile :: IO (Maybe ScoresList) readScoresFile = do curDir <- getCurrentDirectory - filenames <- liftM (reverse . sort . filter isCSV) $ getDirectoryContents curDir + let dataDir = curDir </> "data" + filenames <- liftM (reverse . sort . filter isCSV) $ getDirectoryContents dataDir case filenames of [] -> return Nothing - _ -> Just . scoresFromCSV <$> readFile (head filenames) + _ -> Just . scoresFromCSV <$> readFile (dataDir </> head filenames) where isCSV path = takeExtension path == ".csv" -- writes to score-XX.csv where XX is unix timestamp: a simple-minded logging writeScoresFile :: ScoresList -> IO () writeScoresFile scores = do + curDir <- getCurrentDirectory + let dataDir = curDir </> "data" timestamp <- round <$> getPOSIXTime - let filename = "scores-" ++ show timestamp ++ ".csv" + let filename = dataDir </> ("scores-" ++ show timestamp ++ ".csv") writeFile filename (scoresToCSV scores) |