[Namazu-cvs 862] r272 - in MMagic/trunk: . t

cvsd @ vaj.namazu.org cvsd @ vaj.namazu.org
2006年 7月 1日 (土) 17:11:10 JST


Author: knok
Date: 2006-07-01 17:11:09 +0900 (Sat, 01 Jul 2006)
New Revision: 272

Added:
   MMagic/trunk/t/06-conthook.t
Modified:
   MMagic/trunk/ChangeLog
   MMagic/trunk/MMagic.pm
Log:
Added Container Hook system.
closes: #4.


Modified: MMagic/trunk/ChangeLog
===================================================================
--- MMagic/trunk/ChangeLog	2006-07-01 07:33:15 UTC (rev 271)
+++ MMagic/trunk/ChangeLog	2006-07-01 08:11:09 UTC (rev 272)
@@ -1,3 +1,9 @@
+2006-07-01  NOKUBI Takatsugu  <knok @ daionet.gr.jp>
+
+	* MMagic.pm (checktype_container): Added container check.
+	  (Powerd by CodeFest 2006 Akihabara
+	   https://members.fsij.org/trac/codefestakihabara2006)
+
 2006-06-26  NOKUBI Takatsugu  <knok @ daionet.gr.jp>
 
 	* Applied subclass patch, made by Michael Hendricks.

Modified: MMagic/trunk/MMagic.pm
===================================================================
--- MMagic/trunk/MMagic.pm	2006-07-01 07:33:15 UTC (rev 271)
+++ MMagic/trunk/MMagic.pm	2006-07-01 08:11:09 UTC (rev 272)
@@ -422,6 +422,8 @@
 	     '\.html$' => 'text/html',
 	     '\.htm$' => 'text/html',
     };
+    # content hook
+    $self->{chook} = {};
     return bless $self, $class;
 }
 
@@ -491,6 +493,13 @@
     readMagicEntry($self->{magic}, $self->{MF});
 }
 
+sub addContainerHook {
+    my $self = shift;
+    my $mtype = shift;
+    my $funcref = shift;
+    $self->{chook}->{$mtype} = $funcref;
+}
+
 # Not implimented.
 #
 #sub readMagicFile {
@@ -618,6 +627,9 @@
 
     return 'application/octet-stream' if (length($data) <= 0);
 
+    $mtype = checktype_contents($self, $data);
+    return $mtype unless $mtype eq "";
+
     $mtype = checktype_magic($self, $data);
 
     # 4) check if it's text or binary.
@@ -631,6 +643,17 @@
     return $mtype;
 }
 
+sub checktype_container {
+    my $self = shift;
+    my $data = shift;
+    my $href = $self->{chook};
+    foreach my $mtype (keys %$href) {
+	my $ret = &{$href->{$mtype}}($self, $data);
+	return $ret if $ret ne "";
+    }
+    return "";
+}
+
 sub checktype_magic {
     my $self = shift;
     my $data = shift;

Added: MMagic/trunk/t/06-conthook.t
===================================================================
--- MMagic/trunk/t/06-conthook.t	2006-07-01 07:33:15 UTC (rev 271)
+++ MMagic/trunk/t/06-conthook.t	2006-07-01 08:11:09 UTC (rev 272)
@@ -0,0 +1,19 @@
+# perl-test
+# $Id$
+
+use strict;
+use Test;
+
+BEGIN { plan tests => 1 };
+
+use File::MMagic;
+
+my $ans = "text/plain; conthook";
+my $magic = File::MMagic->new();
+$magic->addContainerHook($ans, sub {
+	my $self = shift;
+	my $data = shift;
+	return "text/plain; conthook" if $data =~ /conthook/;
+	return ""; });
+my $ret = $magic->checktype_container('text conthook');
+ok($ret eq $ans);




Namazu-cvs メーリングリストの案内