LIB/GTK: use ByteString instead of String for
This commit is contained in:
84
src/HSFM/GUI/Glib/GlibString.hs
Normal file
84
src/HSFM/GUI/Glib/GlibString.hs
Normal file
@@ -0,0 +1,84 @@
|
||||
{--
|
||||
HSFM, a filemanager written in Haskell.
|
||||
Copyright (C) 2016 Julian Ospald
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
version 2 as published by the Free Software Foundation.
|
||||
|
||||
This program 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
--}
|
||||
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||
|
||||
|
||||
module HSFM.GUI.Glib.GlibString where
|
||||
|
||||
|
||||
import qualified Data.ByteString as BS
|
||||
import Data.Encoding
|
||||
(
|
||||
decodeStrictByteString
|
||||
)
|
||||
import Data.Encoding.UTF8
|
||||
(
|
||||
UTF8(..)
|
||||
)
|
||||
import Data.Word8
|
||||
(
|
||||
_percent
|
||||
)
|
||||
import Foreign.C.String
|
||||
(
|
||||
CStringLen
|
||||
, CString
|
||||
)
|
||||
import Foreign.C.Types
|
||||
(
|
||||
CSize(..)
|
||||
)
|
||||
import Foreign.Marshal.Utils
|
||||
(
|
||||
maybePeek
|
||||
)
|
||||
import Foreign.Ptr
|
||||
(
|
||||
nullPtr
|
||||
, plusPtr
|
||||
)
|
||||
import System.Glib.UTFString
|
||||
|
||||
|
||||
|
||||
-- TODO: move this to its own module
|
||||
instance GlibString BS.ByteString where
|
||||
withUTFString = BS.useAsCString
|
||||
withUTFStringLen s f = BS.useAsCStringLen s (f . noNullPtrs)
|
||||
peekUTFString s = do
|
||||
len <- c_strlen s
|
||||
BS.packCStringLen (s, fromIntegral len)
|
||||
maybePeekUTFString = maybePeek peekUTFString
|
||||
peekUTFStringLen = BS.packCStringLen
|
||||
newUTFString = newUTFString . decodeStrictByteString UTF8
|
||||
newUTFStringLen = newUTFStringLen . decodeStrictByteString UTF8
|
||||
genUTFOfs = genUTFOfs . decodeStrictByteString UTF8
|
||||
stringLength = BS.length
|
||||
unPrintf s = BS.intercalate "%%" (BS.split _percent s)
|
||||
|
||||
|
||||
foreign import ccall unsafe "string.h strlen" c_strlen
|
||||
:: CString -> IO CSize
|
||||
|
||||
|
||||
noNullPtrs :: CStringLen -> CStringLen
|
||||
noNullPtrs (p, 0) | p == nullPtr = (plusPtr p 1, 0)
|
||||
noNullPtrs s = s
|
||||
|
||||
Reference in New Issue
Block a user