{-# LINE 1 "src/Ganeti/Curl/Internal.hsc" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LINE 2 "src/Ganeti/Curl/Internal.hsc" #-}
{-# 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.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-}

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

import Foreign
import Foreign.C.Types

import Network.Curl


{-# LINE 57 "src/Ganeti/Curl/Internal.hsc" #-}

-- | 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    _ = ((24))
{-# LINE 76 "src/Ganeti/Curl/Internal.hsc" #-}
  alignment _ = alignment (undefined :: CInt)
  peek ptr = do
    msg <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) ptr
{-# LINE 79 "src/Ganeti/Curl/Internal.hsc" #-}
    handle <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) ptr
{-# LINE 80 "src/Ganeti/Curl/Internal.hsc" #-}
    result <- ((\hsc_ptr -> peekByteOff hsc_ptr 16)) ptr
{-# LINE 81 "src/Ganeti/Curl/Internal.hsc" #-}
    return $ CurlMsg (toMsgCode msg) handle (toCode result)
  poke ptr (CurlMsg msg handle result) = do
    ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) ptr (fromMsgCode msg)
{-# LINE 84 "src/Ganeti/Curl/Internal.hsc" #-}
    ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) ptr handle
{-# LINE 85 "src/Ganeti/Curl/Internal.hsc" #-}
    ((\hsc_ptr -> pokeByteOff hsc_ptr 16)) ptr ((fromIntegral $ fromEnum result)::CInt)
{-# LINE 86 "src/Ganeti/Curl/Internal.hsc" #-}

-- | Minimum buffer size for 'CurlErrorBuffer'.
errorBufferSize :: Int
errorBufferSize = (256)
{-# LINE 90 "src/Ganeti/Curl/Internal.hsc" #-}

-- | 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 (0) = CurlMsgNone
{-# LINE 111 "src/Ganeti/Curl/Internal.hsc" #-}
toMsgCode (1) = CurlMsgDone
{-# LINE 112 "src/Ganeti/Curl/Internal.hsc" #-}
toMsgCode v = CurlMsgUnknown v

-- | Convert a CurlMsgCode to a CInt.
fromMsgCode :: CurlMsgCode -> CInt
fromMsgCode CurlMsgNone = (0)
{-# LINE 117 "src/Ganeti/Curl/Internal.hsc" #-}
fromMsgCode CurlMsgDone = (1)
{-# LINE 118 "src/Ganeti/Curl/Internal.hsc" #-}
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 (-1) = CurlmCallMultiPerform
{-# LINE 125 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (0)                 = CurlmOK
{-# LINE 126 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (1)         = CurlmBadHandle
{-# LINE 127 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (2)    = CurlmBadEasyHandle
{-# LINE 128 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (3)      = CurlmOutOfMemory
{-# LINE 129 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (4)     = CurlmInternalError
{-# LINE 130 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (5)         = CurlmBadSocket
{-# LINE 131 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (6)     = CurlmUnknownOption
{-# LINE 132 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode v = CurlmUnknown v