1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
-- GIMP Toolkit (GTK) UTF aware string marshalling
--
-- Author : Axel Simon
--
-- Created: 22 June 2001
--
-- Copyright (c) 1999..2002 Axel Simon
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- |
-- Maintainer : gtk2hs-users@lists.sourceforge.net
-- Stability : provisional
-- Portability : portable (depends on GHC)
--
-- This module adds CString-like functions that handle UTF8 strings.
--
module System.Glib.UTFString (
GlibString(..),
readUTFString,
readCString,
withUTFStrings,
withUTFStringArray,
withUTFStringArray0,
peekUTFStringArray,
peekUTFStringArray0,
readUTFStringArray0,
UTFCorrection,
ofsToUTF,
ofsFromUTF,
glibToString,
stringToGlib,
DefaultGlibString,
GlibFilePath(..),
withUTFFilePaths,
withUTFFilePathArray,
withUTFFilePathArray0,
peekUTFFilePathArray0,
readUTFFilePathArray0
) where
import Codec.Binary.UTF8.String
import Control.Applicative ((<$>))
import Control.Monad (liftM)
import Data.Char (ord, chr)
import Data.Maybe (maybe)
import Data.String (IsString)
import Data.Monoid (Monoid)
import System.Glib.FFI
import qualified Data.Text as T (replace, length, pack, unpack, Text)
import qualified Data.Text.Foreign as T
(withCStringLen, peekCStringLen)
import Data.ByteString (useAsCString)
import Data.Text.Encoding (encodeUtf8)
class (IsString s, Monoid s, Show s) => GlibString s where
-- | Like 'withCString' but using the UTF-8 encoding.
--
withUTFString :: s -> (CString -> IO a) -> IO a
-- | Like 'withCStringLen' but using the UTF-8 encoding.
--
withUTFStringLen :: s -> (CStringLen -> IO a) -> IO a
-- | Like 'peekCString' but using the UTF-8 encoding.
--
peekUTFString :: CString -> IO s
-- | Like 'maybePeek' 'peekCString' but using the UTF-8 encoding to retrieve
-- UTF-8 from a 'CString' which may be the 'nullPtr'.
--
maybePeekUTFString :: CString -> IO (Maybe s)
-- | Like 'peekCStringLen' but using the UTF-8 encoding.
--
peekUTFStringLen :: CStringLen -> IO s
-- | Like 'newCString' but using the UTF-8 encoding.
--
newUTFString :: s -> IO CString
-- | Like Define newUTFStringLen to emit UTF-8.
--
newUTFStringLen :: s -> IO CStringLen
-- | Create a list of offset corrections.
--
genUTFOfs :: s -> UTFCorrection
-- | Length of the string in characters
--
stringLength :: s -> Int
-- Escape percent signs (used in MessageDialog)
unPrintf :: s -> s
-- GTK+ has a lot of asserts that the ptr is not NULL even if the length is 0
-- Until they fix this we need to fudge pointer values to keep the noise level
-- in the logs.
noNullPtrs :: CStringLen -> CStringLen
noNullPtrs (p, 0) | p == nullPtr = (plusPtr p 1, 0)
noNullPtrs s = s
instance GlibString [Char] where
withUTFString = withCAString . encodeString
withUTFStringLen s f = withCAStringLen (encodeString s) (f . noNullPtrs)
peekUTFString = liftM decodeString . peekCAString
maybePeekUTFString = liftM (maybe Nothing (Just . decodeString)) . maybePeek peekCAString
peekUTFStringLen = liftM decodeString . peekCAStringLen
newUTFString = newCAString . encodeString
newUTFStringLen = newCAStringLen . encodeString
genUTFOfs str = UTFCorrection (gUO 0 str)
where
gUO n [] = []
gUO n (x:xs) | ord x<=0x007F = gUO (n+1) xs
| ord x<=0x07FF = n:gUO (n+1) xs
| ord x<=0xFFFF = n:n:gUO (n+1) xs
| otherwise = n:n:n:gUO (n+1) xs
stringLength = length
unPrintf s = s >>= replace
where
replace '%' = "%%"
replace c = return c
foreign import ccall unsafe "string.h strlen" c_strlen
:: CString -> IO CSize
instance GlibString T.Text where
withUTFString = useAsCString . encodeUtf8
withUTFStringLen s f = T.withCStringLen s (f . noNullPtrs)
peekUTFString s = do
len <- c_strlen s
T.peekCStringLen (s, fromIntegral len)
maybePeekUTFString = maybePeek peekUTFString
peekUTFStringLen = T.peekCStringLen
newUTFString = newUTFString . T.unpack -- TODO optimize
newUTFStringLen = newUTFStringLen . T.unpack -- TODO optimize
genUTFOfs = genUTFOfs . T.unpack -- TODO optimize
stringLength = T.length
unPrintf = T.replace "%" "%%"
glibToString :: T.Text -> String
glibToString = T.unpack
stringToGlib :: String -> T.Text
stringToGlib = T.pack
-- | Like like 'peekUTFString' but then frees the string using g_free
--
readUTFString :: GlibString s => CString -> IO s
readUTFString strPtr = do
str <- peekUTFString strPtr
g_free strPtr
return str
-- | Like 'peekCString' but then frees the string using @g_free@.
--
readCString :: CString -> IO String
readCString strPtr = do
str <- peekCAString strPtr
g_free strPtr
return str
foreign import ccall unsafe "g_free"
g_free :: Ptr a -> IO ()
-- | Temporarily allocate a list of UTF-8 'CString's.
--
withUTFStrings :: GlibString s => [s] -> ([CString] -> IO a) -> IO a
withUTFStrings hsStrs = withUTFStrings' hsStrs []
where withUTFStrings' :: GlibString s => [s] -> [CString] -> ([CString] -> IO a) -> IO a
withUTFStrings' [] cs body = body (reverse cs)
withUTFStrings' (s:ss) cs body = withUTFString s $ \c ->
withUTFStrings' ss (c:cs) body
-- | Temporarily allocate an array of UTF-8 encoded 'CString's.
--
withUTFStringArray :: GlibString s => [s] -> (Ptr CString -> IO a) -> IO a
withUTFStringArray hsStr body =
withUTFStrings hsStr $ \cStrs -> do
withArray cStrs body
-- | Temporarily allocate a null-terminated array of UTF-8 encoded 'CString's.
--
withUTFStringArray0 :: GlibString s => [s] -> (Ptr CString -> IO a) -> IO a
withUTFStringArray0 hsStr body =
withUTFStrings hsStr $ \cStrs -> do
withArray0 nullPtr cStrs body
-- | Convert an array (of the given length) of UTF-8 encoded 'CString's to a
-- list of Haskell 'String's.
--
peekUTFStringArray :: GlibString s => Int -> Ptr CString -> IO [s]
peekUTFStringArray len cStrArr = do
cStrs <- peekArray len cStrArr
mapM peekUTFString cStrs
-- | Convert a null-terminated array of UTF-8 encoded 'CString's to a list of
-- Haskell 'String's.
--
peekUTFStringArray0 :: GlibString s => Ptr CString -> IO [s]
peekUTFStringArray0 cStrArr = do
cStrs <- peekArray0 nullPtr cStrArr
mapM peekUTFString cStrs
-- | Like 'peekUTFStringArray0' but then free the string array including all
-- strings.
--
-- To be used when functions indicate that their return value should be freed
-- with @g_strfreev@.
--
readUTFStringArray0 :: GlibString s => Ptr CString -> IO [s]
readUTFStringArray0 cStrArr | cStrArr == nullPtr = return []
| otherwise = do
cStrs <- peekArray0 nullPtr cStrArr
strings <- mapM peekUTFString cStrs
g_strfreev cStrArr
return strings
foreign import ccall unsafe "g_strfreev"
g_strfreev :: Ptr a -> IO ()
-- | Offset correction for String to UTF8 mapping.
--
newtype UTFCorrection = UTFCorrection [Int] deriving Show
ofsToUTF :: Int -> UTFCorrection -> Int
ofsToUTF n (UTFCorrection oc) = oTU oc
where
oTU [] = n
oTU (x:xs) | n<=x = n
| otherwise = 1+oTU xs
ofsFromUTF :: Int -> UTFCorrection -> Int
ofsFromUTF n (UTFCorrection oc) = oFU n oc
where
oFU n [] = n
oFU n (x:xs) | n<=x = n
| otherwise = oFU (n-1) xs
type DefaultGlibString = T.Text
class fp ~ FilePath => GlibFilePath fp where
withUTFFilePath :: fp -> (CString -> IO a) -> IO a
peekUTFFilePath :: CString -> IO fp
instance GlibFilePath FilePath where
withUTFFilePath = withUTFString . T.pack
peekUTFFilePath f = T.unpack <$> peekUTFString f
withUTFFilePaths :: GlibFilePath fp => [fp] -> ([CString] -> IO a) -> IO a
withUTFFilePaths hsStrs = withUTFFilePath' hsStrs []
where withUTFFilePath' :: GlibFilePath fp => [fp] -> [CString] -> ([CString] -> IO a) -> IO a
withUTFFilePath' [] cs body = body (reverse cs)
withUTFFilePath' (fp:fps) cs body = withUTFFilePath fp $ \c ->
withUTFFilePath' fps (c:cs) body
withUTFFilePathArray :: GlibFilePath fp => [fp] -> (Ptr CString -> IO a) -> IO a
withUTFFilePathArray hsFP body =
withUTFFilePaths hsFP $ \cStrs -> do
withArray cStrs body
withUTFFilePathArray0 :: GlibFilePath fp => [fp] -> (Ptr CString -> IO a) -> IO a
withUTFFilePathArray0 hsFP body =
withUTFFilePaths hsFP $ \cStrs -> do
withArray0 nullPtr cStrs body
peekUTFFilePathArray0 :: GlibFilePath fp => Ptr CString -> IO [fp]
peekUTFFilePathArray0 cStrArr = do
cStrs <- peekArray0 nullPtr cStrArr
mapM peekUTFFilePath cStrs
readUTFFilePathArray0 :: GlibFilePath fp => Ptr CString -> IO [fp]
readUTFFilePathArray0 cStrArr | cStrArr == nullPtr = return []
| otherwise = do
cStrs <- peekArray0 nullPtr cStrArr
fps <- mapM peekUTFFilePath cStrs
g_strfreev cStrArr
return fps
|