"Fossies" - the Fresh Open Source Software Archive

Member "pp-2.14.3/src/UTF8.hs" (24 Nov 2021, 2713 Bytes) of package /linux/privat/pp-2.14.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Haskell source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "UTF8.hs": 2.14.2_vs_2.14.3.

    1 {- PP
    2 
    3 Copyright (C) 2015-2021 Christophe Delord
    4 
    5 http://cdelord.fr/pp
    6 
    7 This file is part of PP.
    8 
    9 PP is free software: you can redistribute it and/or modify
   10 it under the terms of the GNU General Public License as published by
   11 the Free Software Foundation, either version 3 of the License, or
   12 (at your option) any later version.
   13 
   14 PP is distributed in the hope that it will be useful,
   15 but WITHOUT ANY WARRANTY; without even the implied warranty of
   16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17 GNU General Public License for more details.
   18 
   19 You should have received a copy of the GNU General Public License
   20 along with PP.  If not, see <http://www.gnu.org/licenses/>.
   21 -}
   22 
   23 module UTF8 ( setUTF8Encoding
   24             , readFileUTF8
   25             , writeFileUTF8
   26             , hWriteFileUTF8
   27             , runProcessUTF8
   28             , readProcessUTF8
   29             )
   30 where
   31 
   32 import Control.Monad
   33 import System.IO
   34 import System.Exit
   35 import qualified System.IO.Strict as SIO
   36 import System.Process
   37 
   38 -- setUTF8Encoding sets UTF8 as the current encoding of a file handle
   39 setUTF8Encoding :: Handle -> IO ()
   40 setUTF8Encoding h = hSetEncoding h utf8
   41 
   42 -- "readFileUTF8 name" reads an UTF-8 file.
   43 -- If name is "-", it reads stdin.
   44 readFileUTF8 :: FilePath -> IO String
   45 readFileUTF8 "-" = getContents
   46 readFileUTF8 name = do
   47     h <- openFile name ReadMode
   48     hSetEncoding h utf8
   49     -- the file must not be read lazily
   50     -- (in some case we want to be able to read files
   51     -- that have been previously produced by the same document)
   52     content <- SIO.hGetContents h
   53     hClose h
   54     return content
   55 
   56 -- "writeFileUTF8 name content" writes an UTF-8 file.
   57 -- If name is "-", it writes to stdout.
   58 writeFileUTF8 :: FilePath -> String -> IO ()
   59 writeFileUTF8 "-" content = putStr content
   60 writeFileUTF8 name content = do
   61     handle <- openBinaryFile name WriteMode
   62     hWriteFileUTF8 handle content
   63     hClose handle
   64 
   65 -- "hwriteFileUTF8 handle content" writes an UTF-8 file.
   66 hWriteFileUTF8 :: Handle -> String -> IO ()
   67 hWriteFileUTF8 handle content = do
   68     hSetEncoding handle utf8
   69     hPutStr handle content
   70 
   71 -- "runrocessUTF8 cmd arg" executes "cmd args"
   72 runProcessUTF8 :: String -> [String] -> IO ()
   73 runProcessUTF8 cmd args = void $ readProcessUTF8 cmd args
   74 
   75 -- "readProcessUTF8 cmd arg" executes "cmd args"
   76 -- and returns the standard output produced by the command.
   77 readProcessUTF8 :: String -> [String] -> IO String
   78 readProcessUTF8 cmd args = do
   79     (_, Just hOut, _, hProc) <- createProcess (proc cmd args) { std_out = CreatePipe }
   80     hSetEncoding hOut utf8
   81     out <- SIO.hGetContents hOut
   82     code <- waitForProcess hProc
   83     case code of
   84         ExitSuccess -> return out
   85         ExitFailure _ -> exitWith code