tuura / scenco-core

Collection of encoding algorithms for conditional graphs
Other
0 stars 1 forks source link

Stdin EOF in Haskell #9

Closed allegroCoder closed 8 years ago

allegroCoder commented 8 years ago

I am splitting the c++ encoding framework in multiple functions (loading scenarios and the custom opcodes, one per encoding algorithm).

I am facing with a problem when I have to read a number from stdin in Haskell.

getEncodingAlgorithm :: IO EncodingType
getEncodingAlgorithm = do
    putStrLn " "
    putStrLn "Algorithms available for encoding the graphs:"
    putStrLn "\t 1) Single-literal encoding"
    [...]
    putStr "Select the number of the algorithm you want to use: "
    hFlush stdout
    encodingId <- read <$> getLine
    return $ convertAlgorithm encodingId

the code above generates the following error message:

Select the number of the algorithm you want to use: Main: <stdin>: hGetLine: end of file

I tackled the problem opening a new handle, the code below works:

    newstdin <- openFile "/dev/tty" ReadMode
    encodingId <- read <$> (hGetLine newstdin)
    hClose newstdin

Though, I don't like this solution. I split up the c++ encoding framework over multiple functions (loading scenarios and opcodes, one function for each algorithm). After that the function for loading scenarios is called, the problem arises. @snowleopard do you know how to fix this?

snowleopard commented 8 years ago

I propose not to use interactive mode at all. Don't ask the user to input numbers, filenames, etc. Instead, use command line arguments. There are good libraries for parsing command line parameters in Haskell, the most commonly used being System.Console.GetOpt. An example of how I use it in Shaking up GHC project: https://github.com/snowleopard/shaking-up-ghc/blob/master/src/CmdLineFlag.hs.

allegroCoder commented 8 years ago

Thanks for the link! I will learn how to use command line parameters for the next part. Now I will try to skip the interactive mode, in order to automate the test.

However, a memo for myself: the problem should be related to the c++ function. I should double-check the function for loading the scenarios.

snowleopard commented 8 years ago

So, I think we can close this issue for now?