CachegrindParse.hs 1.11 KB
Newer Older
Ben Gamari's avatar
Ben Gamari committed
1 2 3 4 5 6 7 8
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module CachegrindParse where

import Data.Maybe
import qualified Data.Map as M

newtype EventName = EventName { getEventName :: String }
Ben Gamari's avatar
Ben Gamari committed
9
                  deriving (Show, Eq, Ord)
Ben Gamari's avatar
Ben Gamari committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

parse :: FilePath -> IO (M.Map EventName Integer)
parse fname = parse' <$> readFile fname

parse' :: String -> M.Map EventName Integer
parse' content =
    M.fromList $ zip summary events
  where
    events = case mapMaybe isEventList $ lines content of
               [] -> error "No event list found"
               [x] -> x
               _ -> error "More than one event list found"
    summary = case mapMaybe isSummaryList $ lines content of
               [] -> error "No event summary found"
               [x] -> x
               _ -> error "More than one event summary found"

    isEventList :: String -> Maybe [Integer]
    isEventList line
      | "summary:" : rest <- words line = Just $ map read rest
      | otherwise = Nothing

    isSummaryList :: String -> Maybe [EventName]
    isSummaryList line
      | "events:" : rest <- words line = Just $ map EventName rest
      | otherwise = Nothing