{-# LINE 1 "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 QuasiQuotes #-}

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.OsString
import System.OsPath.Types
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 :: [MagicFlag] -> IO Magic
magicOpen [MagicFlag]
mfl =
    String -> IO (Ptr CMagic) -> IO Magic
fromMagicPtr String
"magicOpen" (CInt -> IO (Ptr CMagic)
magic_open CInt
flags)
    where flags :: CInt
flags = [MagicFlag] -> CInt
flaglist2int [MagicFlag]
mfl

{- | Load the system's default magic database. -}
magicLoadDefault :: Magic -> IO ()
magicLoadDefault :: Magic -> IO ()
magicLoadDefault Magic
m = Magic -> (Ptr CMagic -> IO ()) -> IO ()
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
m ((Ptr CMagic -> IO ()) -> IO ()) -> (Ptr CMagic -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CMagic
cmagic ->
    String -> Ptr CMagic -> IO CInt -> IO ()
checkIntError String
"magicLoadDefault" Ptr CMagic
cmagic (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr CMagic -> CString -> IO CInt
magic_load Ptr CMagic
cmagic CString
forall a. Ptr a
nullPtr

{- | Load the specified magic databases. -}
magicLoad :: Magic -> NE.NonEmpty OsPath -> IO ()
magicLoad :: Magic -> NonEmpty OsPath -> IO ()
magicLoad Magic
m NonEmpty OsPath
l = Magic -> (Ptr CMagic -> IO ()) -> IO ()
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
m ((Ptr CMagic -> IO ()) -> IO ()) -> (Ptr CMagic -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CMagic
cmagic -> do
    sbs <- OsPath -> IO ShortByteString
decodeOsPath OsPath
s
    SBS.useAsCString sbs $ \CString
cs ->
     String -> Ptr CMagic -> IO CInt -> IO ()
checkIntError String
"magicLoad" Ptr CMagic
cmagic (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr CMagic -> CString -> IO CInt
magic_load Ptr CMagic
cmagic CString
cs
  where
    s :: OsPath
s = OsPath -> [OsPath] -> OsPath
intercalate [osstr|:|] (NonEmpty OsPath -> [OsPath]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty OsPath
l)

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