Skip to content

Commit

Permalink
fix encoding issue (#99) (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
kukimik committed Jun 3, 2021
1 parent c37a828 commit d52f12e
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/Erd/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,33 @@ where

import Erd.ER

import Control.Monad (when)
import Control.Exception (bracket_)
import Control.Monad (when,unless)
import Data.List (find)
import Data.Maybe
import Data.Text.Lazy hiding (find, map, reverse)
import Data.Text.Lazy.IO
import System.IO (Handle)
import System.IO (Handle,hIsTerminalDevice,hSetEncoding,
hGetEncoding,utf8)
import Text.Parsec
import Text.Parsec.Erd.Parser (AST (..), GlobalOptions (..), document)
import Text.Printf (printf)

loadER :: String -> Handle -> IO (Either String ER)
loadER fpath f = do
s <- hGetContents f
case parse (do { (opts, ast) <- document; return $ toER opts ast}) fpath s of
Left err -> return $ Left $ show err
Right err@(Left _) -> return err
Right (Right er) -> return $ Right er
Just initialEncoding <- hGetEncoding f
isTerminalDevice <- hIsTerminalDevice f
let setEncodingIfNeeded = unless isTerminalDevice . hSetEncoding f
bracket_
(setEncodingIfNeeded utf8)
(setEncodingIfNeeded initialEncoding)
(do
s <- hGetContents f
case parse (do { (opts, ast) <- document; return $ toER opts ast}) fpath s of
Left err -> return $ Left $ show err
Right err@(Left _) -> return err
Right (Right er) -> return $ Right er
)

-- | Converts a list of syntactic categories in an entity-relationship
-- description to an ER representation. If there was a problem with the
Expand Down

0 comments on commit d52f12e

Please sign in to comment.