module System.Locale.SetLocale ( Category(..)
, categoryToCInt
, setLocale
)
where
import Data.Typeable (Typeable)
import Foreign.C ( peekCString
, CString
, withCString
, CInt(CInt)
)
import Foreign.Ptr (nullPtr)
data Category = LC_ALL
| LC_COLLATE
| LC_CTYPE
| LC_MESSAGES
| LC_MONETARY
| LC_NUMERIC
| LC_TIME
deriving (Bounded, Enum, Eq, Ord, Read, Show, Typeable)
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
foreign import ccall "locale.h setlocale" c_setlocale :: CInt -> CString -> IO CString
setLocale :: Category -> Maybe String -> IO (Maybe String)
setLocale c Nothing = c_setlocale (categoryToCInt c) nullPtr >>= checkReturn
setLocale c (Just locale) = (withCString locale $ c_setlocale $ categoryToCInt c)
>>= checkReturn
checkReturn :: CString -> IO (Maybe String)
checkReturn r
| r == nullPtr = return Nothing
| otherwise = fmap Just $ peekCString r