    {-# 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 : John Goerzen,
       Maintainer : jgoerzen\@complete.org
       Stability  : provisional
       Portability: portable

    Initialization and shutdown for magic programs

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

    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 Foreign.Marshal.Utils

    {- | 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
    cmagic ->
        String -> Magic -> IO CInt -> IO ()
    checkIntError String
    "magicLoadDefault" Magic
    m (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 database(s).  The given string may contain
    multiple colon-separated pathnames. -}
    magicLoad :: Magic -> String -> IO ()
    magicLoad :: Magic -> String -> IO ()
    magicLoad Magic
    m String
    s = Magic -> (Ptr CMagic -> IO ()) -> IO ()
    forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
    withMagicPtr Magic
    m (\Ptr CMagic
    cmagic ->
        String -> (CString -> IO ()) -> IO ()
    forall a. String -> (CString -> IO a) -> IO a
    withCString String
    s (\CString
    cs ->
         String -> Magic -> IO CInt -> IO ()
    checkIntError String
    "magicLoad" Magic
    m (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))

    foreign import ccall unsafe "magic.h magic_open"
      magic_open :: CInt -> IO (Ptr CMagic)

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