libmagic-1.3: Magic/Init.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.Init
Copyright : Copyright (C) 2005 John Goerzen
License : BSD
Maintainer : Joey Hess,
Maintainer : id\@joeyh.name
Stability : provisional
Portability: portable
Initialization for magic programs
Written by John Goerzen, jgoerzen\@complete.org
-}
{-# LANGUAGE OverloadedStrings #-}
module Magic.Init(magicOpen, magicLoad, magicLoadDefault)
where
import Foreign.Ptr
import Foreign.C.String
import Magic.Types
import Foreign.C.Types
import Magic.Utils
import Magic.TypesLL
import qualified Data.List.NonEmpty as NE
import System.OsPath.Types
import System.OsString.Internal.Types
import Data.Type.Coercion
import qualified Data.ByteString.Short as SBS
{- | Create a Magic object. You must call either 'magicLoadDefault'
or 'magicLoad' after this.
-}
magicOpen :: [MagicFlag] -> IO Magic
magicOpen mfl =
fromMagicPtr "magicOpen" (magic_open flags)
where flags = flaglist2int mfl
{- | Load the system's default magic database. -}
magicLoadDefault :: Magic -> IO ()
magicLoadDefault m = withMagicPtr m $ \cmagic ->
checkIntError "magicLoadDefault" cmagic $ magic_load cmagic nullPtr
{- | Load the specified magic databases. -}
magicLoad :: Magic -> NE.NonEmpty OsPath -> IO ()
magicLoad m l = withMagicPtr m $ \cmagic ->
SBS.useAsCString s $ \cs ->
checkIntError "magicLoad" cmagic $ magic_load cmagic cs
where
s = SBS.intercalate ":" (map conv (NE.toList l))
conv p = case coercionToPlatformTypes of
Left (_, co) -> getWindowsString (coerceWith co p)
Right (_, co) -> getPosixString (coerceWith co p)
foreign import ccall unsafe "magic.h magic_open"
magic_open :: CInt -> IO (Ptr CMagic)
foreign import ccall safe "magic.h magic_load"
magic_load :: Ptr CMagic -> CString -> IO CInt