Skip to content

Commit

Permalink
screened 2025-01-10 08:26:19+00:00
Browse files Browse the repository at this point in the history
  • Loading branch information
bfrk committed Jan 10, 2025
1 parent 95096dc commit 377189b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
2 changes: 1 addition & 1 deletion darcs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ Library
System.Posix.IO
cpp-options: -DWIN32
c-sources: src/win32/send_email.c
build-depends: Win32 >= 2.4.0 && < 2.14,
build-depends: Win32 >= 2.5.0 && < 2.15,
-- exclude 1.3.8 .. 1.3.8.4 due to bug on windows
directory >= 1.2.7 && < 1.3.8 || >= 1.3.8.5 && < 1.4
else
Expand Down
24 changes: 5 additions & 19 deletions src/Darcs/Util/Encoding/Win32.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,24 @@ import Foreign ( castPtr, allocaArray0 )
import Foreign.C
( CInt(..), peekCWStringLen, withCWStringLen )
import System.Win32
( CodePage, nullPtr, getConsoleCP, getACP
, LPCSTR, LPWSTR, LPCWSTR, LPBOOL, DWORD )
( CodePage, nullPtr, getCurrentCodePage

Check failure on line 38 in src/Darcs/Util/Encoding/Win32.hs

View workflow job for this annotation

GitHub Actions / windows-2022 / cabal-3.12 / ghc-8.4.4

Module ‘System.Win32’ does not export ‘getCurrentCodePage’

Check failure on line 38 in src/Darcs/Util/Encoding/Win32.hs

View workflow job for this annotation

GitHub Actions / windows-2022 / cabal-3.12 / ghc-9.10.1

Module ‘System.Win32’ does not export ‘getCurrentCodePage’.
, wideCharToMultiByte, multiByteToWideChar

Check failure on line 39 in src/Darcs/Util/Encoding/Win32.hs

View workflow job for this annotation

GitHub Actions / windows-2022 / cabal-3.12 / ghc-8.4.4

Module ‘System.Win32’ does not export ‘wideCharToMultiByte’

Check failure on line 39 in src/Darcs/Util/Encoding/Win32.hs

View workflow job for this annotation

GitHub Actions / windows-2022 / cabal-3.12 / ghc-9.10.1

Module ‘System.Win32’ does not export ‘wideCharToMultiByte’.
)

#include <windows_cconv.h>

-- | Encode a Unicode 'String' into a 'ByteString' suitable for the current
-- console.
encode :: String -> IO B.ByteString
encode str = getCodePage >>= flip unicodeToCodePage str
encode str = getCurrentCodePage >>= flip unicodeToCodePage str

-- | Convert a 'ByteString' from the console's encoding into a Unicode 'String'.
decode :: B.ByteString -> IO String
decode str = getCodePage >>= flip codePageToUnicode str
decode str = getCurrentCodePage >>= flip codePageToUnicode str

------------------------
-- Multi-byte conversion

foreign import WINDOWS_CCONV "WideCharToMultiByte" wideCharToMultiByte
:: CodePage -> DWORD -> LPCWSTR -> CInt -> LPCSTR -> CInt
-> LPCSTR -> LPBOOL -> IO CInt

unicodeToCodePage :: CodePage -> String -> IO B.ByteString
unicodeToCodePage cp wideStr = withCWStringLen wideStr $ \(wideBuff, wideLen) -> do
-- first, ask for the length without filling the buffer.
Expand All @@ -66,9 +63,6 @@ unicodeToCodePage cp wideStr = withCWStringLen wideStr $ \(wideBuff, wideLen) ->
fmap fromEnum $ wideCharToMultiByte cp 0 wideBuff (toEnum wideLen)
(castPtr outBuff) outSize nullPtr nullPtr

foreign import WINDOWS_CCONV "MultiByteToWideChar" multiByteToWideChar
:: CodePage -> DWORD -> LPCSTR -> CInt -> LPWSTR -> CInt -> IO CInt

codePageToUnicode :: CodePage -> B.ByteString -> IO String
codePageToUnicode cp bs = B.useAsCStringLen bs $ \(inBuff, inLen) -> do
-- first ask for the size without filling the buffer.
Expand All @@ -77,11 +71,3 @@ codePageToUnicode cp bs = B.useAsCStringLen bs $ \(inBuff, inLen) -> do
allocaArray0 (fromEnum outSize) $ \outBuff -> do
outSize' <- multiByteToWideChar cp 0 inBuff (toEnum inLen) outBuff outSize
peekCWStringLen (outBuff, fromEnum outSize')

getCodePage :: IO CodePage
getCodePage = do
conCP <- getConsoleCP
if conCP > 0
then return conCP
else getACP

0 comments on commit 377189b

Please sign in to comment.