| 1 | import PMInstance |
| 2 | import PMDefaults |
| 3 | import ProposalMatcher |
| 4 | import System.IO |
| 5 | import Data.Array.IArray |
| 6 | import Data.Array.Unboxed |
| 7 | import ArrayStuff |
| 8 | import Text.CSV |
| 9 | |
| 10 | -- Command-line interface with simple tab-separated input/output formats. |
| 11 | -- ./match <example.in |
| 12 | |
| 13 | -- pretty silly but it does the job |
| 14 | swapTabCommaIn s = map (\c -> if c == '\t' then ',' else if c == ',' then '\t' else c) s |
| 15 | removeQuotes s = filter (\c -> not (c == '"')) s |
| 16 | |
| 17 | -- Some versions of Text.CSV do not use or accept a trailing newline; compensate for that. |
| 18 | removeTrailingNewline s = if not (null s) && last s == '\n' then init s else s |
| 19 | addTrailingNewline s = if not (null s) && last s /= '\n' then s ++ ['\n'] else s |
| 20 | |
| 21 | parseTSV fname str = case parseCSV fname (swapTabCommaIn $ removeTrailingNewline str) of |
| 22 | Left pe -> Left pe |
| 23 | Right ll -> Right $ map (map swapTabCommaIn) ll |
| 24 | printTSV ll = addTrailingNewline $ removeQuotes $ swapTabCommaIn $ printCSV $ map (map swapTabCommaIn) ll |
| 25 | |
| 26 | main = do |
| 27 | incsv <- hGetContents stdin |
| 28 | -- handle errors another day, or let the platform do it |
| 29 | let Right inll = parseTSV "standard input" incsv |
| 30 | let loadList = head inll |
| 31 | let numRvrs = length loadList |
| 32 | let loadA = listArray (0, numRvrs-1) (map read loadList) |
| 33 | let numProps = length (tail inll) |
| 34 | -- explicit type on the next line appears to be necessary |
| 35 | let pxarr = listOfListsToArray2D (tail inll) :: Array (Int,Int) String |
| 36 | -- careful, we end up transposing the array in here |
| 37 | let prefA = funcArray ((0,0), (numRvrs-1,numProps-1)) (\(i,j) -> read (pxarr ! (j, i))) |
| 38 | let theInst = PMInstance numRvrs numProps loadA prefA |
| 39 | let PMatching theMatching = doMatching pmDefaults theInst |
| 40 | hPutStr stdout $ printTSV $ map (\(i, j) -> map show [i, j]) theMatching |