{-# 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.

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


{-# LINE 48 "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 67 "src/Ganeti/Curl/Internal.hsc" #-}
  alignment _ = alignment (undefined :: CInt)
  peek ptr = do
    msg <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) ptr
{-# LINE 70 "src/Ganeti/Curl/Internal.hsc" #-}
    handle <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) ptr
{-# LINE 71 "src/Ganeti/Curl/Internal.hsc" #-}
    result <- ((\hsc_ptr -> peekByteOff hsc_ptr 16)) ptr
{-# LINE 72 "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 75 "src/Ganeti/Curl/Internal.hsc" #-}
    ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) ptr handle
{-# LINE 76 "src/Ganeti/Curl/Internal.hsc" #-}
    ((\hsc_ptr -> pokeByteOff hsc_ptr 16)) ptr ((fromIntegral $ fromEnum result)::CInt)
{-# LINE 77 "src/Ganeti/Curl/Internal.hsc" #-}

-- | Minimum buffer size for 'CurlErrorBuffer'.
errorBufferSize :: Int
errorBufferSize = (256)
{-# LINE 81 "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 102 "src/Ganeti/Curl/Internal.hsc" #-}
toMsgCode (1) = CurlMsgDone
{-# LINE 103 "src/Ganeti/Curl/Internal.hsc" #-}
toMsgCode v = CurlMsgUnknown v

-- | Convert a CurlMsgCode to a CInt.
fromMsgCode :: CurlMsgCode -> CInt
fromMsgCode CurlMsgNone = (0)
{-# LINE 108 "src/Ganeti/Curl/Internal.hsc" #-}
fromMsgCode CurlMsgDone = (1)
{-# LINE 109 "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 116 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (0)                 = CurlmOK
{-# LINE 117 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (1)         = CurlmBadHandle
{-# LINE 118 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (2)    = CurlmBadEasyHandle
{-# LINE 119 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (3)      = CurlmOutOfMemory
{-# LINE 120 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (4)     = CurlmInternalError
{-# LINE 121 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (5)         = CurlmBadSocket
{-# LINE 122 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode (6)     = CurlmUnknownOption
{-# LINE 123 "src/Ganeti/Curl/Internal.hsc" #-}
toMCode v = CurlmUnknown v