File-MimeInfo
view release on metacpan or search on metacpan
lib/File/MimeInfo/Magic.pm view on Meta::CPAN
push @$ref, [
$o, $end, # offset, offset+length+range
$mdef, # boolean for mask
qr/$reg/sm, # the regex to match
undef # debug data
];
$$ref[-1][-1] = "$file line $line" if $DEBUG;
}
close MAGIC;
}
sub _find_branch { # finds last branch of tree of rules
my $i = shift;
my $ref = $magic[-1];
for (1..$i) { $ref = $$ref[-1] }
return $ref;
}
sub _mask_regex { # build regex based on mask
my ($v, $m) = @_;
my @v = split '', unpack "b*", $v;
my @m = split '', unpack "b*", $m;
my $re = '';
for (0 .. $#m) {
$re .= $m[$_] ? $v[$_] : '.' ;
# If $mask = 1 than ($input && $mask) will be same as $input
# If $mask = 0 than ($input && $mask) is always 0
# But $mask = 0 only makes sense if $value = 0
# So if $mask = 0 we ignore that bit of $input
}
return $re;
}
sub _escape_bytes { # used for debug output
my $string = shift;
if ($string =~ /[\x00-\x1F\x7F]/) {
$string = join '', map {
my $o = ord($_);
($o < 32) ? '^' . chr($o + 64) :
($o == 127) ? '^?' : $_ ;
} split '', $string;
}
return $string;
}
1;
__END__
=head1 NAME
File::MimeInfo::Magic - Determine file type with magic
=head1 SYNOPSIS
use File::MimeInfo::Magic;
my $mime_type = mimetype($file);
=head1 DESCRIPTION
This module inherits from L<File::MimeInfo>, it is transparent
to its functions but adds support for the freedesktop magic file.
Magic data is hashed when you need it for the first time.
If you want to force hashing earlier use the C<rehash()> function.
=head1 EXPORT
The method C<mimetype> is exported by default. The methods C<magic>,
C<inodetype>, C<globs> and C<describe> can be exported on demand.
=head1 METHODS
See also L<File::MimeInfo> for methods that are inherited.
=over 4
=item C<mimetype($file)>
Returns a mime-type string for C<$file>, returns undef on failure.
This method bundles C<inodetype()>, C<globs()> and C<magic()>.
Magic rules with an priority of 80 and higher are checked before
C<globs()> is called, all other magic rules afterwards.
If this doesn't work the file is read and the mime-type defaults
to 'text/plain' or to 'application/octet-stream' when the first ten chars
of the file match ascii control chars (white spaces excluded).
If the file doesn't exist or isn't readable C<undef> is returned.
If C<$file> is an object reference only C<magic> and the default method
are used. See below for details.
=item C<magic($file)>
Returns a mime-type string for C<$file> based on the magic rules,
returns undef on failure.
C<$file> can be an object reference, in that case it is supposed to have a
C<seek()> and a C<read()> method. This allows you for example to determine
the mimetype of data in memory by using L<IO::Scalar>.
Be aware that when using a filehandle or an C<IO::> object you need to set
the C<:utf8> binmode yourself if appropriate.
=item C<rehash()>
Rehash the data files. Glob and magic
information is preparsed when this method is called.
If you want to by-pass the XDG basedir system you can specify your database
directories by setting C<@File::MimeInfo::DIRS>. But normally it is better to
change the XDG basedir environment variables.
=item C<default>
=item C<describe>
=item C<extensions>
( run in 4.016 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )