{-# LANGUAGE DeriveDataTypeable, EmptyDataDecls, ForeignFunctionInterface #-}
-- |
-- Module      : Data.Text.ICU.Convert.Internal
-- Copyright   : (c) Bryan O'Sullivan 2009
--
-- License     : BSD-style
-- Maintainer  : bos@serpentine.com
-- Stability   : experimental
-- Portability : GHC
--
-- Low-level character set types and functions.

module Data.Text.ICU.Convert.Internal
    (
      Converter(..)
    , UConverter
    , getName
    , withConverter
    ) where

import Data.Text.ICU.Error.Internal (UErrorCode, handleError)
import Data.Typeable (Typeable)
import Foreign.C.String (CString, peekCString)
import Foreign.ForeignPtr (ForeignPtr, withForeignPtr)
import Foreign.Ptr (Ptr)
import System.IO.Unsafe (unsafePerformIO)

data UConverter

-- | Character set converter type.  /Note/: this structure is not
-- thread safe. It is /not/ safe to use value of this type
-- simultaneously from multiple threads.
data Converter = Converter {-# UNPACK #-} !(ForeignPtr UConverter)
                 deriving (Converter -> Converter -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Converter -> Converter -> Bool
$c/= :: Converter -> Converter -> Bool
== :: Converter -> Converter -> Bool
$c== :: Converter -> Converter -> Bool
Eq, Typeable)

instance Show Converter where
    show :: Converter -> String
show Converter
c = String
"Converter " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Converter -> String
getName Converter
c)

withConverter :: Converter -> (Ptr UConverter -> IO a) -> IO a
{-# INLINE withConverter #-}
withConverter :: forall a. Converter -> (Ptr UConverter -> IO a) -> IO a
withConverter (Converter ForeignPtr UConverter
cnv) Ptr UConverter -> IO a
action = forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr UConverter
cnv Ptr UConverter -> IO a
action

-- | Gets the internal, canonical name of the converter.
getName :: Converter -> String
getName :: Converter -> String
getName Converter
cnv = forall a. IO a -> a
unsafePerformIO forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall a. Converter -> (Ptr UConverter -> IO a) -> IO a
withConverter Converter
cnv forall a b. (a -> b) -> a -> b
$ \Ptr UConverter
ptr ->
    CString -> IO String
peekCString forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError (Ptr UConverter -> Ptr UErrorCode -> IO CString
ucnv_getName Ptr UConverter
ptr)

foreign import ccall unsafe "hs_text_icu.h __hs_ucnv_getName" ucnv_getName
    :: Ptr UConverter -> Ptr UErrorCode -> IO CString