{-# LINE 1 "Magic/Utils.hsc" #-}
{- -*- Mode: haskell; -*-
Haskell magic Interface
Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>

This code is under a 3-clause BSD license; see COPYING for details.
-}

{- |
   Module     : Magic.Utils
   Copyright  : Copyright (C) 2005 John Goerzen
   License    : BSD

   Maintainer : Joey Hess,
   Maintainer : id\@joeyh.name
   Stability  : provisional
   Portability: portable

Utils

Written by John Goerzen, jgoerzen\@complete.org
-}

module Magic.Utils (flaglist2int, fromMagicPtr, withMagicPtr, checkIntError,
                    throwErrorIfNull, decodeOsPath)
where

import Foreign
import Foreign.C.Error
import Foreign.C.String
import Magic.TypesLL
import Magic.Types
import Foreign.C.Types
import Control.Concurrent.MVar
import System.OsPath.Types
import System.OsString.Internal.Types
import System.OsString.Encoding
import Data.Type.Coercion
import qualified Data.ByteString.Short as SBS

flaglist2int :: [MagicFlag] -> CInt
flaglist2int :: [MagicFlag] -> CInt
flaglist2int [MagicFlag]
mfl =
    (CInt -> MagicFlag -> CInt) -> CInt -> [MagicFlag] -> CInt
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\CInt
c MagicFlag
f -> CInt
c CInt -> CInt -> CInt
forall a. Bits a => a -> a -> a
.|. (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> (MagicFlag -> Int) -> MagicFlag -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MagicFlag -> Int
forall a. Enum a => a -> Int
fromEnum (MagicFlag -> CInt) -> MagicFlag -> CInt
forall a b. (a -> b) -> a -> b
$ MagicFlag
f)) CInt
0 [MagicFlag]
mfl

fromMagicPtr :: String -> IO (Ptr CMagic) -> IO Magic
fromMagicPtr :: String -> IO (Ptr CMagic) -> IO Magic
fromMagicPtr String
caller IO (Ptr CMagic)
action =
    do ptr <- String -> IO (Ptr CMagic) -> IO (Ptr CMagic)
forall a. String -> IO (Ptr a) -> IO (Ptr a)
throwErrnoIfNull String
caller IO (Ptr CMagic)
action
       m <- newForeignPtr magic_close ptr
       mvar <- newMVar m
       return (Magic mvar)

throwErrorIfNull :: String -> Ptr CMagic -> IO (Ptr a) -> IO (Ptr a)
throwErrorIfNull :: forall a. String -> Ptr CMagic -> IO (Ptr a) -> IO (Ptr a)
throwErrorIfNull String
caller Ptr CMagic
m IO (Ptr a)
action =
    do res <- IO (Ptr a)
action
       if res == nullPtr
          then throwError caller m
          else return res

withMagicPtr :: Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr :: forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr (Magic MVar (ForeignPtr CMagic)
mvar) Ptr CMagic -> IO a
a = MVar (ForeignPtr CMagic) -> (ForeignPtr CMagic -> IO a) -> IO a
forall a b. MVar a -> (a -> IO b) -> IO b
withMVar MVar (ForeignPtr CMagic)
mvar ((ForeignPtr CMagic -> IO a) -> IO a)
-> (ForeignPtr CMagic -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \ForeignPtr CMagic
m -> ForeignPtr CMagic -> (Ptr CMagic -> IO a) -> IO a
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CMagic
m Ptr CMagic -> IO a
a

throwError :: String -> Ptr CMagic -> IO a
throwError :: forall a. String -> Ptr CMagic -> IO a
throwError String
caller Ptr CMagic
cmagic =
               do errormsg <- Ptr CMagic -> IO CString
magic_error Ptr CMagic
cmagic
                  if errormsg /= nullPtr
                     then do em <- peekCString errormsg
                             fail $ caller ++ ": " ++ em
                     else fail $ caller ++ ": got error code but no error message"

checkIntError :: String -> Ptr CMagic -> IO CInt -> IO ()
checkIntError :: String -> Ptr CMagic -> IO CInt -> IO ()
checkIntError String
caller Ptr CMagic
cmagic IO CInt
action = 
    do res <- IO CInt
action
       if res == 0
          then return ()
          else throwError caller cmagic

decodeOsPath :: OsPath -> IO (SBS.ShortByteString)
decodeOsPath :: OsPath -> IO ShortByteString
decodeOsPath OsPath
p = case Either
  (Coercion OsChar WindowsChar, Coercion OsPath WindowsString)
  (Coercion OsChar PosixChar, Coercion OsPath PosixString)
coercionToPlatformTypes of
    Right (Coercion OsChar PosixChar
_, Coercion OsPath PosixString
co) -> ShortByteString -> IO ShortByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (PosixString -> ShortByteString
getPosixString (Coercion OsPath PosixString -> OsPath -> PosixString
forall a b. Coercion a b -> a -> b
coerceWith Coercion OsPath PosixString
co OsPath
p))
    -- libmagic built for windows with MinGW by eg
    -- https://github.com/nscaife/file-windows does not use
    -- wide bytes as are in WindowsString, so convert it using
    -- the locale encoding
    Left (Coercion OsChar WindowsChar
_, Coercion OsPath WindowsString
co) ->
            String -> IO ShortByteString
encodeWithBasePosix' (String -> IO ShortByteString) -> IO String -> IO ShortByteString
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ShortByteString -> IO String
decodeWithBaseWindows
                (WindowsString -> ShortByteString
getWindowsString (Coercion OsPath WindowsString -> OsPath -> WindowsString
forall a b. Coercion a b -> a -> b
coerceWith Coercion OsPath WindowsString
co OsPath
p))

foreign import ccall unsafe "magic.h &magic_close"
  magic_close :: FunPtr (Ptr CMagic -> IO ())

foreign import ccall unsafe "magic.h magic_error"
  magic_error :: Ptr CMagic -> IO CString