mahoro.c in mahoro-0.2 vs mahoro.c in mahoro-0.3

- old
+ new

@@ -6,25 +6,23 @@ #include <magic.h> #ifndef RSTRING_LEN # define RSTRING_LEN(s)->len #endif +#ifndef RSTRING_PTR +# define RSTRING_PTR(s)->ptr +#endif -struct MagicCookie -{ - magic_t cookie; -}; - static VALUE cMahoro; static VALUE eMahoroError; static void mahoro_free(ptr) - struct MagicCookie *ptr; + void *ptr; { - magic_close(ptr->cookie); - xfree(ptr); + if (ptr) + magic_close((magic_t)ptr); } static VALUE mahoro_allocate(klass) VALUE klass; @@ -37,18 +35,17 @@ int argc; VALUE *argv, self; { int flags = MAGIC_NONE; char *path = 0; - struct MagicCookie *ptr; magic_t cookie; VALUE vpath, vflags; switch(rb_scan_args(argc, argv, "02", &vflags, &vpath)) { case 2: if(!NIL_P(vpath)) { - path = StringValuePtr(vpath); + path = StringValueCStr(vpath); } /* fallthrough */ case 1: flags = FIX2INT(vflags); break; @@ -61,25 +58,23 @@ if(magic_load(cookie, path)) { rb_raise(eMahoroError, "failed to load database: %s", magic_error(cookie)); } - ptr = ALLOC(struct MagicCookie); - ptr->cookie = cookie; - DATA_PTR(self) = ptr; + DATA_PTR(self) = cookie; return self; } static VALUE mahoro_file(self, path) VALUE self, path; { const char *msg; - magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie; + magic_t cookie = (magic_t)DATA_PTR(self); - if(!(msg = magic_file(cookie, StringValuePtr(path)))) { + if(!(msg = magic_file(cookie, StringValueCStr(path)))) { rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie)); } return rb_str_new2(msg); } @@ -87,25 +82,27 @@ static VALUE mahoro_buffer(self, input) VALUE self, input; { const char *msg; - magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie; + magic_t cookie = (magic_t)DATA_PTR(self); - if(!(msg = magic_buffer(cookie, StringValuePtr(input), - RSTRING_LEN(StringValue(input))))) { + StringValue(input); + + if(!(msg = magic_buffer(cookie, RSTRING_PTR(input), + RSTRING_LEN(input)))) { rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie)); } return rb_str_new2(msg); } static VALUE mahoro_set_flags(self, flags) VALUE self, flags; { - magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie; + magic_t cookie = (magic_t)DATA_PTR(self); return INT2FIX(magic_setflags(cookie, FIX2INT(flags))); } static VALUE @@ -113,16 +110,16 @@ int argc; VALUE *argv, self; { char *path = 0; VALUE vpath; - magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie; + magic_t cookie = (magic_t)DATA_PTR(self); switch(rb_scan_args(argc, argv, "01", &vpath)) { case 1: - if(!NIL_P(vpath)) { - path = StringValuePtr(vpath); + if(!NIL_P(vpath)) { + path = StringValueCStr(vpath); } break; } if(!magic_check(cookie, path)) { @@ -131,31 +128,38 @@ return Qfalse; } } static VALUE -mahoro_compile(klass, path) - VALUE klass, path; +mahoro_compile(self, path) + VALUE self, path; { - magic_t cookie = magic_open(MAGIC_NONE); + magic_t cookie = (magic_t)DATA_PTR(self); - if(magic_compile(cookie, StringValuePtr(path))) { + if(magic_compile(cookie, StringValueCStr(path))) { rb_raise(eMahoroError, "failed compile: %s", magic_error(cookie)); } - magic_close(cookie); - return Qtrue; } static VALUE +mahoro_s_compile(klass, path) + VALUE klass, path; +{ + VALUE m = rb_funcall(klass, rb_intern("new"), 0, 0); + + return mahoro_compile(m, path); +} + +static VALUE mahoro_load(self, path) VALUE self, path; { - magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie; + magic_t cookie = (magic_t)DATA_PTR(self); - if(magic_load(cookie, StringValuePtr(path))) { + if(magic_load(cookie, StringValueCStr(path))) { rb_raise(eMahoroError, "failed load: %s", magic_error(cookie)); } return self; } @@ -163,29 +167,154 @@ void Init_mahoro(void) { cMahoro = rb_define_class("Mahoro", rb_cObject); eMahoroError = rb_define_class_under(cMahoro, "Error", rb_eStandardError); - rb_const_set(cMahoro, rb_intern("NONE"), INT2FIX(MAGIC_NONE)); - rb_const_set(cMahoro, rb_intern("DEBUG"), INT2FIX(MAGIC_DEBUG)); - rb_const_set(cMahoro, rb_intern("SYMLINK"), INT2FIX(MAGIC_SYMLINK)); - rb_const_set(cMahoro, rb_intern("COMPRESS"), INT2FIX(MAGIC_COMPRESS)); - rb_const_set(cMahoro, rb_intern("DEVICES"), INT2FIX(MAGIC_DEVICES)); - rb_const_set(cMahoro, rb_intern("MIME"), INT2FIX(MAGIC_MIME)); - rb_const_set(cMahoro, rb_intern("CONTINUE"), INT2FIX(MAGIC_CONTINUE)); - rb_const_set(cMahoro, rb_intern("CHECK"), INT2FIX(MAGIC_CHECK)); - rb_const_set(cMahoro, rb_intern("PRESERVE_ATIME"), + /* No special handling, the default */ + rb_define_const(cMahoro, "NONE", INT2FIX(MAGIC_NONE)); + + /* print debugging messages to stderr */ + rb_define_const(cMahoro, "DEBUG", INT2FIX(MAGIC_DEBUG)); + + /* Follow symlinks */ + rb_define_const(cMahoro, "SYMLINK", INT2FIX(MAGIC_SYMLINK)); + + /* Check inside compressed files */ + rb_define_const(cMahoro, "COMPRESS", INT2FIX(MAGIC_COMPRESS)); + + /* Look at the contents of devices */ + rb_define_const(cMahoro, "DEVICES", INT2FIX(MAGIC_DEVICES)); + +#ifdef MAGIC_MIME_TYPE + /* + * Return only the MIME type + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "MIME_TYPE", INT2FIX(MAGIC_MIME_TYPE)); +#endif + + /* Return all matches */ + rb_define_const(cMahoro, "CONTINUE", INT2FIX(MAGIC_CONTINUE)); + + /* + * Check the magic database for consistency and + * print warnings to stderr + */ + rb_define_const(cMahoro, "CHECK", INT2FIX(MAGIC_CHECK)); + + /* preserve access time of files analyzed */ + rb_define_const(cMahoro, "PRESERVE_ATIME", INT2FIX(MAGIC_PRESERVE_ATIME)); - rb_const_set(cMahoro, rb_intern("RAW"), INT2FIX(MAGIC_RAW)); - rb_const_set(cMahoro, rb_intern("ERROR"), INT2FIX(MAGIC_ERROR)); + /* + * Don't translate unprintable characters to a \\ooo octal + * representation + */ + rb_define_const(cMahoro, "RAW", INT2FIX(MAGIC_RAW)); + + /* + * Treat operating system errors while trying to open files + * and follow symlinks as real errors, instead of printing + * them in the magic buffer. + */ + rb_define_const(cMahoro, "ERROR", INT2FIX(MAGIC_ERROR)); + +#ifdef MAGIC_MIME_ENCODING + /* + * Return a MIME encoding, instead of a textual description. + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "MIME_ENCODING", INT2FIX(MAGIC_MIME_ENCODING)); +#endif + + /* return both MIME type and encoding */ + rb_define_const(cMahoro, "MIME", INT2FIX(MAGIC_MIME)); + +#ifdef MAGIC_APPLE + /* + * Return both Apple creator and type. + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "APPLE", INT2FIX(MAGIC_APPLE)); +#endif + +#ifdef MAGIC_NO_CHECK_COMPRESS + /* + * Don't check for or inside compressed files. + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "NO_CHECK_COMPRESS", + INT2FIX(MAGIC_NO_CHECK_COMPRESS)); +#endif + +#ifdef MAGIC_NO_CHECK_TAR + /* + * Don't examine tar files. + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "NO_CHECK_TAR", INT2FIX(MAGIC_NO_CHECK_TAR)); +#endif + +#ifdef MAGIC_NO_CHECK_SOFT + /* + * Don't consult magic files. + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "NO_CHECK_SOFT", INT2FIX(MAGIC_NO_CHECK_SOFT)); +#endif + +#ifdef MAGIC_NO_CHECK_APPTYPE + /* + * Don't check application type (EMX only). + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "NO_CHECK_APPTYPE", + INT2FIX(MAGIC_NO_CHECK_APPTYPE)); +#endif + +#ifdef MAGIC_NO_CHECK_ELF + /* + * Don't check for ELF details. + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "NO_CHECK_ELF", INT2FIX(MAGIC_NO_CHECK_ELF)); +#endif + +#ifdef MAGIC_NO_CHECK_ASCII + /* + * Don't check for various types of ASCII text files. + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "NO_CHECK_TEXT", + INT2FIX(MAGIC_NO_CHECK_ASCII)); +#endif + +#ifdef MAGIC_NO_CHECK_TOKENS + /* + * Don't check for known tokens inside ASCII files. + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "NO_CHECK_TOKENS", + INT2FIX(MAGIC_NO_CHECK_TOKENS)); +#endif + +#ifdef MAGIC_NO_CHECK_ENCODING + /* + * Don't check for text encodings. + * This constant may not be defined on older systems. + */ + rb_define_const(cMahoro, "NO_CHECK_ENCODING", + INT2FIX(MAGIC_NO_CHECK_ENCODING)); +#endif + rb_define_alloc_func(cMahoro, mahoro_allocate); rb_define_method(cMahoro, "initialize", mahoro_initialize, -1); rb_define_method(cMahoro, "file", mahoro_file, 1); rb_define_method(cMahoro, "buffer", mahoro_buffer, 1); rb_define_method(cMahoro, "flags=", mahoro_set_flags, 1); rb_define_method(cMahoro, "valid?", mahoro_check, -1); - rb_define_singleton_method(cMahoro, "compile", mahoro_compile, 1); + rb_define_singleton_method(cMahoro, "compile", mahoro_s_compile, 1); + rb_define_method(cMahoro, "compile", mahoro_compile, 1); rb_define_method(cMahoro, "load", mahoro_load, 1); } /* arch-tag: mahoro */