Class: Magic

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/ffi-magic.rb,
lib/ffi-magic/version.rb

Constant Summary collapse

NONE =
0x000000
DEBUG =
0x000001
0x000002
COMPRESS =
0x000004
DEVICES =
0x000008
MIME_TYPE =
0x000010
CONTINUE =
0x000020
CHECK =
0x000040
PRESERVE_ATIME =
0x000080
RAW =
0x000100
ERROR =
0x000200
MIME_ENCODING =
0x000400
MIME =
MIME_TYPE | MIME_ENCODING
APPLE =
0x000800
NO_CHECK_COMPRESS =
0x001000
NO_CHECK_TAR =
0x002000
NO_CHECK_SOFT =
0x004000
NO_CHECK_APPTYPE =
0x008000
NO_CHECK_ELF =
0x010000
NO_CHECK_TEXT =
0x020000
NO_CHECK_CDF =
0x040000
NO_CHECK_TOKENS =
0x100000
NO_CHECK_ENCODING =
0x200000
Error =
Class.new(StandardError)
VERSION =
"2011.04.22"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags = NONE, path = nil) ⇒ Magic

Returns a new instance of Magic.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ffi-magic.rb', line 57

def initialize(flags = NONE, path = nil)
  @flags = flags
  @path = path

  unless @cookie = magic_open(@flags)
    raise Error, "failed to initialize magic cookie"
  end

  if magic_load(@cookie, @path) != 0
    raise Error, "failed to load database: #{magic_error(@cookie)}"
  end

  begin
    ObjectSpace.define_finalizer(self){ close }
  rescue
  end
end

Class Method Details

.compile(path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ffi-magic.rb', line 45

def self.compile(path)
  cookie = magic_open(NONE)

  unless magic_compile(cookie, path) == 0
    raise Error, "failed compile: #{magic_error(cookie)}"
  end

  magic_close(cookie)

  true
end

Instance Method Details

#buffer(input) ⇒ Object



83
84
85
86
# File 'lib/ffi-magic.rb', line 83

def buffer(input)
  input.force_encoding(Encoding::BINARY)
  magic_buffer(@cookie, input, input.bytesize)
end

#closeObject



103
104
105
106
# File 'lib/ffi-magic.rb', line 103

def close
  magic_close(@cookie) if @cookie
  @cookie = nil
end

#file(path) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/ffi-magic.rb', line 75

def file(path)
  unless msg = magic_file(@cookie, path)
    raise Error, "failed lookup: #{magic_error(@cookie)}"
  end

  msg
end

#flags=(flags) ⇒ Object



88
89
90
91
# File 'lib/ffi-magic.rb', line 88

def flags=(flags)
  @flags = flags
  magic_setflags(@cookie, flags)
end

#loadObject



97
98
99
100
101
# File 'lib/ffi-magic.rb', line 97

def load
  unless magic_load(@cookie, path) == 0
    raise Error, "failed load: #{magic_error(@cookie)}"
  end
end

#valid?(path = nil) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/ffi-magic.rb', line 93

def valid?(path = nil)
  magic_check(@cookie, path) == 0
end