Class: LibmagicRb
- Inherits:
-
Object
- Object
- LibmagicRb
- Defined in:
- lib/libmagic_rb/version.rb,
ext/libmagic/magic.c
Defined Under Namespace
Classes: FileClosedError, FileNotFound, FileUnreadable, InvalidDBError, IsDirError
Constant Summary collapse
- VERSION =
"0.1.0"- MAGIC_VERSION =
rb_str_new_cstr(version)
Instance Attribute Summary collapse
-
#closed ⇒ Object
(also: #closed?)
readonly
Attributes.
- #db ⇒ Object
- #file ⇒ Object
- #mode ⇒ Object readonly
Class Method Summary collapse
-
.check(args) ⇒ Object
Singleton Methods.
- .lsmodes ⇒ Object
- .lsparams ⇒ Object
Instance Method Summary collapse
-
#check ⇒ Object
Check for file mimetype.
-
#close ⇒ Object
Close database.
-
#getparam ⇒ Object
Get and set params.
-
#initialize(args) ⇒ Object
constructor
LibmagicRb.new().
-
#load ⇒ Object
Load database.
-
#magic_buffer ⇒ Object
Miscellaneous.
- #magic_list ⇒ Object
- #mode ⇒ Object readonly
-
#setflags ⇒ Object
Set modes dynamically.
- #setparam ⇒ Object
Constructor Details
#initialize(args) ⇒ Object
LibmagicRb.new()
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'ext/libmagic/magic.c', line 106 VALUE rb_libmagicRb_initialize(volatile VALUE self, volatile VALUE args) { // Database Path if(!RB_TYPE_P(args, T_HASH)) { rb_raise(rb_eArgError, "Expected hash as argument.") ; } VALUE argDBPath = rb_hash_aref(args, ID2SYM(rb_intern("db"))) ; char *databasePath ; if (RB_TYPE_P(argDBPath, T_NIL)) { databasePath = NULL ; rb_ivar_set(self, rb_intern("@db"), Qnil) ; } else if (!RB_TYPE_P(argDBPath, T_STRING)) { rb_raise(rb_eArgError, "Database name must be an instance of String.") ; } else { databasePath = StringValuePtr(argDBPath) ; rb_ivar_set(self, rb_intern("@db"), argDBPath) ; } // File path VALUE argFilePath = rb_hash_aref(args, ID2SYM(rb_intern("file"))) ; if (RB_TYPE_P(argFilePath, T_NIL)) { rb_raise(rb_eArgError, "Expected `file:\" key as a string, got nil instead") ; } else if (!RB_TYPE_P(argFilePath, T_STRING)) { rb_raise(rb_eArgError, "Filename must be an instance of String.") ; } rb_ivar_set(self, rb_intern("@file"), argFilePath) ; // Modes VALUE argModes = rb_hash_aref(args, ID2SYM(rb_intern("mode"))) ; unsigned int modes ; if(RB_TYPE_P(argModes, T_NIL)) { modes = MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK ; } else if (!RB_TYPE_P(argModes, T_FIXNUM)) { rb_raise(rb_eArgError, "Modes must be an instance of Integer. Check LibmagicRb.constants() or LibmagicRb.lsmodes().") ; } else { modes = FIX2UINT(argModes) ; } rb_ivar_set(self, rb_intern("@mode"), UINT2NUM(modes)) ; rb_ivar_set(self, rb_intern("@closed"), Qfalse) ; RB_UNWRAP() ; magic_setflags(*, modes) ; return self ; } |
Instance Attribute Details
#closed ⇒ Object (readonly) Also known as: closed?
Attributes
#db ⇒ Object
#file ⇒ Object
#mode ⇒ Object (readonly)
Class Method Details
.check(args) ⇒ Object
Singleton Methods
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 103 104 |
# File 'ext/libmagic/magic.c', line 48 VALUE _check_(volatile VALUE obj, volatile VALUE args) { if(!RB_TYPE_P(args, T_HASH)) { rb_raise(rb_eArgError, "Expected hash as argument.") ; } // Database Path VALUE argDBPath = rb_hash_aref(args, ID2SYM(rb_intern("db"))) ; char *databasePath ; if (RB_TYPE_P(argDBPath, T_NIL)) { databasePath = NULL ; } else if (!RB_TYPE_P(argDBPath, T_STRING)) { rb_raise(rb_eArgError, "Database name must be an instance of String.") ; } else { databasePath = StringValuePtr(argDBPath) ; } // File path VALUE argFilePath = rb_hash_aref(args, ID2SYM(rb_intern("file"))) ; if (RB_TYPE_P(argFilePath, T_NIL)) { rb_raise(rb_eArgError, "Expected `file:\" key as a string, got nil instead") ; } else if (!RB_TYPE_P(argFilePath, T_STRING)) { rb_raise(rb_eArgError, "Filename must be an instance of String.") ; } char *checkPath = StringValuePtr(argFilePath) ; // Modes VALUE argModes = rb_hash_aref(args, ID2SYM(rb_intern("mode"))) ; unsigned int modes ; if(RB_TYPE_P(argModes, T_NIL)) { modes = MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK ; } else if (!RB_TYPE_P(argModes, T_FIXNUM)) { rb_raise(rb_eArgError, "Modes must be an instance of Integer. Check LibmagicRb.constants() or LibmagicRb.lsmodes().") ; } else { modes = FIX2UINT(argModes) ; } // Checks struct magic_set *magic = magic_open(modes) ; // Check if the database is a valid file or not // Raises ruby error which will return. fileReadable(checkPath) ; if(databasePath) { magic_validate_db(magic, databasePath) ; } magic_load(magic, databasePath) ; const char *mt = magic_file(magic, checkPath) ; VALUE retVal = mt ? rb_str_new_cstr(mt) : Qnil ; magic_close(magic) ; return retVal ; } |
.lsmodes ⇒ Object
.lsparams ⇒ Object
Instance Method Details
#check ⇒ Object
Check for file mimetype
#close ⇒ Object
Close database
#getparam ⇒ Object
Get and set params
#load ⇒ Object
Load database
#magic_buffer ⇒ Object
Miscellaneous
#magic_list ⇒ Object
#mode= ⇒ Object (readonly)
#setflags ⇒ Object
Set modes dynamically