~alcinnz/fontconfig-pure

fcf1e371e4a7d55b5ae07b90172502bc1d3e8064 — Adrian Cochrane 2 years ago 92f6f9b
Remove file utility & throw memory exceptions for String utilities.
3 files changed, 8 insertions(+), 51 deletions(-)

D Graphics/Text/Font/Choose/File/Atomic.hs
M Graphics/Text/Font/Choose/Strings.hs
M fontconfig-pure.cabal
D Graphics/Text/Font/Choose/File/Atomic.hs => Graphics/Text/Font/Choose/File/Atomic.hs +0 -45
@@ 1,45 0,0 @@
module Graphics.Text.Font.Choose.File.Atomic where

import Foreign.ForeignPtr
import Foreign.Ptr (Ptr, FunPtr)
import Foreign.C.String (CString, peekCString, withCString)
import Control.Exception (bracket)

data Atomic'
type Atomic = ForeignPtr Atomic'
type Atomic_ = Ptr Atomic'

create :: String -> IO Atomic
create filename = withCString filename fcAtomicCreate >>= newForeignPtr fcAtomicDestroy
foreign import ccall "FcAtomicCreate" fcAtomicCreate :: CString -> IO Atomic_
foreign import ccall "&FcAtomicDestroy" fcAtomicDestroy :: FunPtr (Atomic_ -> IO ())

lock :: Atomic -> IO Bool
lock = flip withForeignPtr fcAtomicLock
foreign import ccall "FcAtomicLock" fcAtomicLock :: Atomic_ -> IO Bool

newFile :: Atomic -> IO String
newFile atomic = withForeignPtr atomic fcAtomicNewFile >>= peekCString
foreign import ccall "FcAtomicNewFile" fcAtomicNewFile :: Atomic_ -> IO CString

origFile :: Atomic -> IO String
origFile atomic = withForeignPtr atomic fcAtomicOrigFile >>= peekCString
foreign import ccall "FcAtomicOrigFile" fcAtomicOrigFile :: Atomic_ -> IO CString

replaceOrig :: Atomic -> IO Bool
replaceOrig = flip withForeignPtr fcAtomicReplaceOrig
foreign import ccall "FcAtomicReplaceOrig" fcAtomicReplaceOrig :: Atomic_ -> IO Bool

deleteNew :: Atomic -> IO ()
deleteNew = flip withForeignPtr fcAtomicDeleteNew
foreign import ccall "FcAtomicDeleteNew" fcAtomicDeleteNew :: Atomic_ -> IO ()

unlock :: Atomic -> IO ()
unlock = flip withForeignPtr fcAtomicUnlock
foreign import ccall "FcAtomicUnlock" fcAtomicUnlock :: Atomic_ -> IO ()

withLock :: Atomic -> IO () -> IO ()
withLock atomic cb = bracket (lock atomic) (const $ unlock atomic) inner
    where
      inner True = cb
      inner False = return ()

M Graphics/Text/Font/Choose/Strings.hs => Graphics/Text/Font/Choose/Strings.hs +7 -5
@@ 4,7 4,7 @@ module Graphics.Text.Font.Choose.Strings (StrSet, StrSet_, StrList, StrList_,

import Data.Set (Set)
import qualified Data.Set as Set
import Graphics.Text.Font.Choose.Result (throwNull)
import Graphics.Text.Font.Choose.Result (throwNull, throwFalse)

import Foreign.Ptr (Ptr, nullPtr)
import Foreign.C.String (CString, withCString, peekCString)


@@ 17,19 17,21 @@ data StrSet'
type StrSet_ = Ptr StrSet'

withNewStrSet :: (StrSet_ -> IO a) -> IO a
withNewStrSet = bracket fcStrSetCreate fcStrSetDestroy
withNewStrSet = bracket (throwNull <$> fcStrSetCreate) fcStrSetDestroy
foreign import ccall "FcStrSetCreate" fcStrSetCreate :: IO StrSet_
foreign import ccall "FcStrSetDestroy" fcStrSetDestroy :: StrSet_ -> IO ()

withStrSet :: StrSet -> (StrSet_ -> IO a) -> IO a
withStrSet strs cb = withNewStrSet $ \strs' -> do
    forM (Set.elems strs) $ flip withCString $ fcStrSetAdd strs'
    forM (Set.elems strs) $ \str ->
        throwFalse <$> (withCString str $ fcStrSetAdd strs')
    cb strs'
foreign import ccall "FcStrSetAdd" fcStrSetAdd :: StrSet_ -> CString -> IO Bool

withFilenameSet :: StrSet -> (StrSet_ -> IO a) -> IO a
withFilenameSet paths cb = withNewStrSet $ \paths' -> do
    forM (Set.elems paths) $ flip withCString $ fcStrSetAddFilename paths'
    forM (Set.elems paths) $ \path ->
        throwFalse <$> (withCString path $ fcStrSetAddFilename paths')
    cb paths'
foreign import ccall "FcStrSetAddFilename" fcStrSetAddFilename ::
    StrSet_ -> CString -> IO Bool


@@ 48,7 50,7 @@ data StrList'
type StrList_ = Ptr StrList'

withStrList :: StrSet_ -> (StrList_ -> IO a) -> IO a
withStrList strs = bracket (fcStrListCreate strs) fcStrListDone
withStrList strs = bracket (throwNull <$> fcStrListCreate strs) fcStrListDone
foreign import ccall "FcStrListCreate" fcStrListCreate :: StrSet_ -> IO StrList_
foreign import ccall "FcStrListDone" fcStrListDone :: StrList_ -> IO ()


M fontconfig-pure.cabal => fontconfig-pure.cabal +1 -1
@@ 56,7 56,7 @@ library
                Graphics.Text.Font.Choose.Strings, Graphics.Text.Font.Choose.Range,
                Graphics.Text.Font.Choose.LangSet, Graphics.Text.Font.Choose.Value,
                Graphics.Text.Font.Choose.Pattern, Graphics.Text.Font.Choose.FontSet,
                Graphics.Text.Font.Choose.Config, Graphics.Text.Font.Choose.File.Atomic
                Graphics.Text.Font.Choose.Config

  c-sources:    cbits/pattern.c