Creates an instance of +Magic::Database+ using given database file and flags
# File lib/magic/database.rb, line 7 def initialize(*args) options = args.last.is_a?(Hash) ? args.pop : {} # extract options database = options.delete(:database) open(*args) load(database) end
Determine type of given string
# File lib/magic/database.rb, line 44 def buffer(string) result = Api.magic_buffer(@magic_set, string, string.bytesize) if result.null? raise Exception, error else result.get_string(0) end end
Closes the database
# File lib/magic/database.rb, line 21 def close Api.magic_close(@magic_set) end
Returns the last error occured
# File lib/magic/database.rb, line 54 def error Api.magic_error(@magic_set) end
Determine type of a file at given path
# File lib/magic/database.rb, line 31 def file(filename) if !File.exists?(filename) raise Exception, "#{filename}: No such file or directory" end result = Api.magic_file(@magic_set, filename.to_s) if result.null? raise Exception, error else result.get_string(0) end end
Sets the flags
# File lib/magic/database.rb, line 59 def flags=(*flags) @flags = calculate_flags(*flags) Api.magic_setflags(@magic_set, @flags) end
Loads given database file (or default if nil
given)
# File lib/magic/database.rb, line 26 def load(database = nil) Api.magic_load(@magic_set, database) end
Opens magic db using given flags
# File lib/magic/database.rb, line 15 def open(*flags) @flags = calculate_flags(*flags) @magic_set = Api.magic_open(@flags) end
# File lib/magic/database.rb, line 66 def calculate_flags(*flags) flags.inject(0) { |calculated, flag| calculated |= Constants::Flag.const_get(flag.to_s.upcase) } end