module System.Locale.SetLocale (
Category(..),
categoryToCInt,
setLocale
) where
import Foreign.Ptr
import Foreign.C.Types
import Foreign.C.String
import Data.Typeable
data Category
= LC_ALL
| LC_COLLATE
| LC_CTYPE
| LC_MESSAGES
| LC_MONETARY
| LC_NUMERIC
| LC_TIME
deriving (Eq, Ord, Read, Show, Enum, Bounded)
instance Typeable Category where
typeOf _ = mkTyConApp (mkTyCon "System.Locale.SetLocale.Category") []
categoryToCInt :: Category -> CInt
categoryToCInt LC_ALL = 6
categoryToCInt LC_COLLATE = 3
categoryToCInt LC_CTYPE = 0
categoryToCInt LC_MESSAGES = 5
categoryToCInt LC_MONETARY = 4
categoryToCInt LC_NUMERIC = 1
categoryToCInt LC_TIME = 2
ptr2str :: Ptr CChar -> IO (Maybe String)
ptr2str p
| p == nullPtr = return Nothing
| otherwise = fmap Just $ peekCString p
str2ptr :: Maybe String -> (Ptr CChar -> IO a) -> IO a
str2ptr Nothing f = f nullPtr
str2ptr (Just s) f = withCString s f
foreign import ccall unsafe "locale.h setlocale" c_setlocale :: CInt -> Ptr CChar -> IO (Ptr CChar)
setLocale :: Category -> Maybe String -> IO (Maybe String)
setLocale cat str =
str2ptr str $ \p -> c_setlocale (categoryToCInt cat) p >>= ptr2str