1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
{- -*- 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.Operations(-- * Guessing the type
magicFile, magicStdin,
magicString, magicCString,
-- * Other operations
magicSetFlags, magicCompile)
where
import Foreign.Ptr
import Foreign.C.String
import Magic.Types
import Foreign.C.Types
import Data.Word
import Foreign.C.String
import Foreign.C.Error
import Magic.Utils
import Magic.TypesLL
import Foreign.Marshal.Utils
{- | Calls the Magic system on the specified file. -}
magicFile :: Magic -> FilePath -> IO String
magicFile magic fp =
withMagicPtr magic (\cmagic ->
withCString fp (\cfp ->
do res <- throwErrorIfNull "magicFile" magic (magic_file cmagic cfp)
peekCString res
)
)
{- | Calls the Magic system on stdin. -}
magicStdin :: Magic -> IO String
magicStdin magic =
withMagicPtr magic (\cmagic ->
do res <- throwErrorIfNull "magicStdin" magic (magic_file cmagic nullPtr)
peekCString res
)
{- | Calls the Magic system to process the given String. Please note:
it is not evaluated lazily. -}
magicString :: Magic -> String -> IO String
magicString m s = withCStringLen s (magicCString m)
{- | Lower-level function used to call the Magic system to process a C
string. -}
magicCString :: Magic -> CStringLen -> IO String
magicCString magic (cstr, len) =
withMagicPtr magic (\cmagic ->
do res <- throwErrorIfNull "magicCString" magic (magic_buffer cmagic cstr (fromIntegral len))
peekCString res
)
{- | Change the flags on an already-created object. -}
magicSetFlags :: Magic -> [MagicFlag] -> IO ()
magicSetFlags m mfl = withMagicPtr m (\cmagic ->
checkIntError "magicSetFlags" m $ magic_setflags cmagic flags)
where flags = flaglist2int mfl
{- | Compile the colon-separated list of database file(s). The compiled files
created have .mgc added to the names of the argument.
-}
magicCompile :: Magic -- ^ Object to use
-> Maybe String -- ^ Colon separated list of databases, or Nothing for default
-> IO ()
magicCompile m mstr = withMagicPtr m (\cm ->
case mstr of
Nothing -> worker cm nullPtr
Just x -> withCString x (worker cm)
)
where worker cm cs = checkIntError "magicCompile" m $ magic_compile cm cs
foreign import ccall unsafe "magic.h magic_file"
magic_file :: Ptr CMagic -> CString -> IO CString
foreign import ccall unsafe "magic.h magic_buffer"
magic_buffer :: Ptr CMagic -> CString -> #{type size_t} -> IO CString
foreign import ccall unsafe "magic.h magic_setflags"
magic_setflags :: Ptr CMagic -> CInt -> IO CInt
foreign import ccall unsafe "magic.h magic_compile"
magic_compile :: Ptr CMagic -> CString -> IO CInt
|