Internal.hsc 4.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
{-# LANGUAGE ForeignFunctionInterface #-}
{-# OPTIONS_GHC -fno-warn-deprecated-flags #-}
-- the above is needed due to the fact that hsc2hs generates code also
-- compatible with older compilers; see
-- http://hackage.haskell.org/trac/ghc/ticket/3844

{-| Hsc2hs definitions for 'Storable' interfaces.

-}

{-

Copyright (C) 2013 Google Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

-}

module Ganeti.Curl.Internal
  ( CurlMsgCode(..)
  , toMsgCode
  , fromMsgCode
  , CurlMsg(..)
  , errorBufferSize
  , CurlMCode(..)
  , toMCode
  ) where

import Foreign
import Foreign.C.Types

import Network.Curl

#include <curl/curl.h>

-- | Data representing a @CURLMSG@ enum.
data CurlMsgCode = CurlMsgNone
                 | CurlMsgDone
                 | CurlMsgUnknown CInt -- ^ Haskell specific code for
                                       -- unknown codes
                   deriving (Show, Eq)

-- | Data representing a @struct CURLMsg@.
data CurlMsg = CurlMsg
  { cmMessage :: CurlMsgCode -- ^ The message type
  , cmHandle  :: CurlH       -- ^ The internal curl handle to which it applies
  , cmResult  :: CurlCode    -- ^ The message-specific result
  }

-- | Partial 'Storable' instance for 'CurlMsg'; we do not extract all
-- fields, only the one we are interested in.
instance Storable CurlMsg where
  sizeOf    _ = (#size CURLMsg)
  alignment _ = alignment (undefined :: CInt)
  peek ptr = do
    msg <- (#peek CURLMsg, msg) ptr
    handle <- (#peek CURLMsg, easy_handle) ptr
    result <- (#peek CURLMsg, data.result) ptr
    return $ CurlMsg (toMsgCode msg) handle (toCode result)
  poke ptr (CurlMsg msg handle result) = do
    (#poke CURLMsg, msg) ptr (fromMsgCode msg)
    (#poke CURLMsg, easy_handle) ptr handle
    (#poke CURLMsg, data.result) ptr ((fromIntegral $ fromEnum result)::CInt)

-- | Minimum buffer size for 'CurlErrorBuffer'.
errorBufferSize :: Int
errorBufferSize = (#const CURL_ERROR_SIZE)

-- | Multi interface error codes.
data CurlMCode = CurlmCallMultiPerform
               | CurlmOK
               | CurlmBadHandle
               | CurlmBadEasyHandle
               | CurlmOutOfMemory
               | CurlmInternalError
               | CurlmBadSocket
               | CurlmUnknownOption
               | CurlmUnknown CInt -- ^ Haskell specific code denoting
                                   -- undefined codes (e.g. when
                                   -- libcurl has defined new codes
                                   -- that are not implemented yet)
                 deriving (Show, Eq)

-- | Convert a CInt CURLMSG code (as returned by the C library) to a
-- 'CurlMsgCode'. When an unknown code is received, the special
-- 'CurlMsgUnknown' constructor will be used.
toMsgCode :: CInt -> CurlMsgCode
toMsgCode (#const CURLMSG_NONE) = CurlMsgNone
toMsgCode (#const CURLMSG_DONE) = CurlMsgDone
toMsgCode v = CurlMsgUnknown v

-- | Convert a CurlMsgCode to a CInt.
fromMsgCode :: CurlMsgCode -> CInt
fromMsgCode CurlMsgNone = (#const CURLMSG_NONE)
fromMsgCode CurlMsgDone = (#const CURLMSG_DONE)
fromMsgCode (CurlMsgUnknown v) = v

-- | Convert a CInt CURLMcode (as returned by the C library) to a
-- 'CurlMCode'. When an unknown code is received, the special
-- 'CurlmUnknown' constructor will be used.
toMCode :: CInt -> CurlMCode
toMCode (#const CURLM_CALL_MULTI_PERFORM) = CurlmCallMultiPerform
toMCode (#const CURLM_OK)                 = CurlmOK
toMCode (#const CURLM_BAD_HANDLE)         = CurlmBadHandle
toMCode (#const CURLM_BAD_EASY_HANDLE)    = CurlmBadEasyHandle
toMCode (#const CURLM_OUT_OF_MEMORY)      = CurlmOutOfMemory
toMCode (#const CURLM_INTERNAL_ERROR)     = CurlmInternalError
toMCode (#const CURLM_BAD_SOCKET)         = CurlmBadSocket
toMCode (#const CURLM_UNKNOWN_OPTION)     = CurlmUnknownOption
toMCode v = CurlmUnknown v