X-Git-Url: https://mattmccutchen.net/match/match.git/blobdiff_plain/df3d7779b79713973898f91d7bd9d418bf41eebe..86e209954803086a9a880fe3cd538d2c866cb8d6:/program/Main.hs diff --git a/program/Main.hs b/program/Main.hs index 0bbfdda..d45755e 100644 --- a/program/Main.hs +++ b/program/Main.hs @@ -7,13 +7,21 @@ import Data.Array.Unboxed import ArrayStuff import Text.CSV +-- Command-line interface with simple tab-separated input/output formats. +-- ./match if c == '\t' then ',' else if c == ',' then '\t' else c) s removeQuotes s = filter (\c -> not (c == '"')) s -parseTSV fname str = case parseCSV fname (swapTabCommaIn str) of + +-- Some versions of Text.CSV do not use or accept a trailing newline; compensate for that. +removeTrailingNewline s = if not (null s) && last s == '\n' then init s else s +addTrailingNewline s = if not (null s) && last s /= '\n' then s ++ ['\n'] else s + +parseTSV fname str = case parseCSV fname (swapTabCommaIn $ removeTrailingNewline str) of Left pe -> Left pe Right ll -> Right $ map (map swapTabCommaIn) ll -printTSV ll = removeQuotes $ swapTabCommaIn $ printCSV $ map (map swapTabCommaIn) ll +printTSV ll = addTrailingNewline $ removeQuotes $ swapTabCommaIn $ printCSV $ map (map swapTabCommaIn) ll main = do incsv <- hGetContents stdin