Object
Map flag values to their names (Integer => :name).
Map flag names to their values (:name => Integer).
defined in b5be901 (2010-01-28, 5.05), but broken until 38e0136 (2013-08-15, 5.15)
Extract “simple” MIME type.
Clear our instance cache.
# File lib/filemagic.rb, line 66 def clear! @fm.each_value(&:close).clear end
Converts flags to integer
static VALUE rb_magic_flags(VALUE klass, VALUE flags) { VALUE map = rb_const_get(cFileMagic, rb_intern("FLAGS_BY_SYM")), f, g; int i = MAGIC_NONE, j; if (TYPE(flags) != T_ARRAY) { rb_raise(rb_eTypeError, "wrong argument type %s (expected Array)", rb_obj_classname(flags)); } for (j = 0; j < RARRAY_LEN(flags); j++) { f = rb_ary_entry(flags, j); switch (TYPE(f)) { case T_SYMBOL: if (RTEST(g = rb_hash_aref(map, f))) { f = g; /* fall through */ } else { f = rb_funcall(f, rb_intern("inspect"), 0); rb_raise(rb_eArgError, "%s: %s", NIL_P(g) ? "no such flag" : "flag not available", StringValueCStr(f)); break; } case T_FIXNUM: i |= NUM2INT(f); break; default: rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or Symbol)", rb_obj_classname(f)); } } return INT2FIX(i); }
Provide a “magic singleton”.
# File lib/filemagic.rb, line 55 def fm(*flags) options = flags.last.is_a?(Hash) ? flags.pop : {} if fm = @fm[key = [flags = flags(flags), options]] return fm unless fm.closed? end @fm[key] = new(flags, options) end
# File lib/filemagic.rb, line 91 def magic_version(default = MAGIC_VERSION) default != '0' ? default : user_magic_version || auto_magic_version || [default, 'unknown'] end
Just a short-cut to open with the mime flag set.
# File lib/filemagic.rb, line 87 def mime(*flags, &block) open(:mime, *flags, &block) end
static VALUE rb_magic_new(int argc, VALUE *argv, VALUE klass) { VALUE obj, args[2]; magic_t ms; if (rb_block_given_p()) { rb_warn( "FileMagic.new() does not take a block; use FileMagic.open() instead"); } if (argc > 0 && TYPE(args[1] = argv[argc - 1]) == T_HASH) { argc--; } else { args[1] = rb_hash_new(); } args[0] = rb_magic_flags(klass, rb_ary_new4(argc, argv)); if ((ms = magic_open(NUM2INT(args[0]))) == NULL) { rb_raise(rb_FileMagicError, "failed to initialize magic cookie (%d)", errno || -1); } if (magic_load(ms, NULL) == -1) { rb_raise(rb_FileMagicError, "failed to load database: %s", magic_error(ms)); } obj = Data_Wrap_Struct(klass, 0, rb_magic_free, ms); rb_obj_call_init(obj, 2, args); return obj; }
Just like new, but takes an optional block, in which case close is called at the end and the value of the block is returned.
# File lib/filemagic.rb, line 72 def open(*flags) fm = new(*flags) if block_given? begin yield fm ensure fm.close end else fm end end
Frees resources allocated
static VALUE rb_magic_close(VALUE self) { magic_t ms; if (RTEST(rb_magic_closed_p(self))) { return Qnil; } GetMagicSet(self, ms); rb_magic_free(ms); /* This keeps rb_magic_free from trying to free closed objects */ DATA_PTR(self) = NULL; rb_iv_set(self, "closed", Qtrue); return Qnil; }
static VALUE rb_magic_closed_p(VALUE self) { return rb_attr_get(self, rb_intern("closed")); }
Get the flags as array of symbols
static VALUE rb_magic_getflags(VALUE self) { VALUE ary = rb_ary_new(); VALUE map = rb_const_get(cFileMagic, rb_intern("FLAGS_BY_INT")); int i = NUM2INT(rb_attr_get(self, rb_intern("iflags"))), j = 0; while ((i -= j) > 0) { j = pow(2, (int)(log(i) / log(2))); rb_ary_unshift(ary, rb_hash_aref(map, INT2FIX(j))); } return ary; }
Set flags on the ms object
static VALUE rb_magic_setflags(VALUE self, VALUE flags) { magic_t ms; GetMagicSet(self, ms); rb_iv_set(self, "iflags", flags = rb_magic_flags(CLASS_OF(self), rb_Array(flags))); return INT2FIX(magic_setflags(ms, NUM2INT(flags))); }
# File lib/filemagic.rb, line 128 def inspect super.insert(-2, closed? ? ' (closed)' : '') end
Generated with the Darkfish Rdoc Generator 2.