    {-# LINE 1 "Database/TokyoCabinet/Error.hsc" #-}
    {-# LANGUAGE CPP, ForeignFunctionInterface #-}
    {-# LINE 2 "Database/TokyoCabinet/Error.hsc" #-}
    module Database.TokyoCabinet.Error
        (
        -- * Error code
          ECODE(..)
        -- * Utility function
        , errmsg
        , cintToError
        , errorToCInt
        -- * Other constants
        , cINT_MIN
        ) where

    import Foreign
    import Foreign.C.Types
    import Foreign.C.String

    {-# LINE 19 "Database/TokyoCabinet/Error.hsc" #-}

    -- | Represents error
    data ECODE =
        ESUCCESS | -- ^ success
        ETHREAD  | -- ^ threading error
        EINVALID | -- ^ invalid operation
        ENOFILE  | -- ^ file not found
        ENOPERM  | -- ^ no permission
        EMETA    | -- ^ invalid meta data
        ERHEAD   | -- ^ invalid record header
        EOPEN    | -- ^ open error
        ECLOSE   | -- ^ close error
        ETRUNC   | -- ^ trunc error
        ESYNC    | -- ^ sync error
        ESTAT    | -- ^ stat error
        ESEEK    | -- ^ seek error
        EREAD    | -- ^ read error
        EWRITE   | -- ^ write error
        EMMAP    | -- ^ mmap error
        ELOCK    | -- ^ lock error
        EUNLINK  | -- ^ unlink error
        ERENAME  | -- ^ rename error
        EMKDIR   | -- ^ mkdir error
        ERMDIR   | -- ^ rmdir error
        EKEEP    | -- ^ existing record
        ENOREC   | -- ^ no record found
        EMISC      -- ^ miscellaneous error
        deriving (Eq, Ord)

    instance Show ECODE where
        show e =  errmsg e ++ " (code:" ++ show (errorToCInt e) ++ ")"

    errorToCInt :: ECODE -> CInt
    errorToCInt ESUCCESS = 0
    {-# LINE 53 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ETHREAD  = 1
    {-# LINE 54 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt EINVALID = 2
    {-# LINE 55 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ENOFILE  = 3
    {-# LINE 56 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ENOPERM  = 4
    {-# LINE 57 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt EMETA    = 5
    {-# LINE 58 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ERHEAD   = 6
    {-# LINE 59 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt EOPEN    = 7
    {-# LINE 60 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ECLOSE   = 8
    {-# LINE 61 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ETRUNC   = 9
    {-# LINE 62 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ESYNC    = 10
    {-# LINE 63 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ESTAT    = 11
    {-# LINE 64 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ESEEK    = 12
    {-# LINE 65 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt EREAD    = 13
    {-# LINE 66 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt EWRITE   = 14
    {-# LINE 67 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt EMMAP    = 15
    {-# LINE 68 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ELOCK    = 16
    {-# LINE 69 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt EUNLINK  = 17
    {-# LINE 70 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ERENAME  = 18
    {-# LINE 71 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt EMKDIR   = 19
    {-# LINE 72 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ERMDIR   = 20
    {-# LINE 73 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt EKEEP    = 21
    {-# LINE 74 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt ENOREC   = 22
    {-# LINE 75 "Database/TokyoCabinet/Error.hsc" #-}
    errorToCInt EMISC    = 9999
    {-# LINE 76 "Database/TokyoCabinet/Error.hsc" #-}

    cintToError :: CInt -> ECODE
    cintToError 0 = ESUCCESS
    {-# LINE 79 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 1 = ETHREAD
    {-# LINE 80 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 2 = EINVALID
    {-# LINE 81 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 3 = ENOFILE
    {-# LINE 82 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 4 = ENOPERM
    {-# LINE 83 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 5 = EMETA
    {-# LINE 84 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 6 = ERHEAD
    {-# LINE 85 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 7 = EOPEN
    {-# LINE 86 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 8 = ECLOSE
    {-# LINE 87 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 9 = ETRUNC
    {-# LINE 88 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 10 = ESYNC
    {-# LINE 89 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 11 = ESTAT
    {-# LINE 90 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 12 = ESEEK
    {-# LINE 91 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 13 = EREAD
    {-# LINE 92 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 14 = EWRITE
    {-# LINE 93 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 15 = EMMAP
    {-# LINE 94 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 16 = ELOCK
    {-# LINE 95 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 17 = EUNLINK
    {-# LINE 96 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 18 = ERENAME
    {-# LINE 97 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 19 = EMKDIR
    {-# LINE 98 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 20 = ERMDIR
    {-# LINE 99 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 21 = EKEEP
    {-# LINE 100 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 22 = ENOREC
    {-# LINE 101 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError 9999 = EMISC
    {-# LINE 102 "Database/TokyoCabinet/Error.hsc" #-}
    cintToError _ = error "unknown error code"

    cINT_MIN :: CInt
    cINT_MIN = -2147483648
    {-# LINE 106 "Database/TokyoCabinet/Error.hsc" #-}

    -- | Convert error code to message string.
    errmsg :: ECODE -> String
    errmsg = unsafePerformIO . peekCString . c_tcerrmsg . errorToCInt

    foreign import ccall "tcerrmsg"
      c_tcerrmsg :: CInt -> CString