Created
December 28, 2015 18:58
-
-
Save autarch/d7153a6371f430289c10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
use NativeCall; | |
unit class File::LibMagic; | |
my @flags = < | |
MAGIC_APPLE | |
MAGIC_CHECK | |
MAGIC_COMPRESS | |
MAGIC_CONTINUE | |
MAGIC_DEBUG | |
MAGIC_DEVICES | |
MAGIC_ERROR | |
MAGIC_MIME | |
MAGIC_MIME_ENCODING | |
MAGIC_MIME_TYPE | |
MAGIC_NONE | |
MAGIC_NO_CHECK_APPTYPE | |
MAGIC_NO_CHECK_CDF | |
MAGIC_NO_CHECK_COMPRESS | |
MAGIC_NO_CHECK_ELF | |
MAGIC_NO_CHECK_ENCODING | |
MAGIC_NO_CHECK_SOFT | |
MAGIC_NO_CHECK_TAR | |
MAGIC_NO_CHECK_TEXT | |
MAGIC_NO_CHECK_TOKENS | |
MAGIC_PRESERVE_ATIME | |
MAGIC_RAW | |
MAGIC_SYMLINK | |
>; | |
BEGIN { | |
for @flags -> $flag { | |
my $m = my method () is native('magic') { * }; | |
$m.set_name($flag); | |
$?CLASS.^add_method( $flag, $m ); | |
} | |
} | |
my class Cookie is repr('CPointer') { | |
method new (int $flags) returns Pointer { | |
return open($flags); | |
} | |
sub open (int) returns Pointer is native('magic') { * } | |
method DESTROY { | |
self.close | |
} | |
method close (Pointer) is native('magic') { * } | |
method magic-descriptor (int $flags, IO::Handle $file) returns Str { | |
self.magic_setflags($flags); | |
return magic_descriptor( self, $file.native-descriptor ); | |
} | |
sub magic_descriptor (Pointer, int) returns Str is native('magic') { * } | |
method magic-file (int $flags, Str $filename) returns Str { | |
self.magic_setflags($flags); | |
return magic_file( self, $filename ); | |
} | |
sub magic_file (Pointer, Str) returns Str is native('magic') { * } | |
method magic-buffer (int $flags, Str $buffer) returns Str { | |
self.magic_setflags($flags); | |
my $blob = $buffer.encode('UTF-8'); | |
return magic_buffer( self, $blob, $blob.elems ); | |
} | |
sub magic_buffer (Pointer, Str, int) returns Str is native('magic') { * } | |
method magic_setflags (Pointer, int) is native('magic') { * } | |
} | |
has Cookie $!handle; | |
has int $!flags; | |
submethod BUILD (int :$!flags = 0) { | |
$!flags = $!flags +^ self.MAGIC_MIME +^ self.MAGIC_MIME_TYPE +^ self.MAGIC_MIME_ENCODING; | |
$!handle = Cookie.new($!flags); | |
return; | |
} | |
method info-from-filename (Str $filename, int $flags = 0) returns Hash { | |
return self!info-using( 'magic-file', $flags, $filename ); | |
} | |
method info-from-handle (IO::Handle $handle, int $flags = 0) returns Hash { | |
return self!info-using( 'magic-descriptor', $flags, $handle ); | |
} | |
method info-from-buffer (Str $buffer, int $flags = 0) returns Hash { | |
return self!info-using( 'magic-buffer', $flags, $buffer ); | |
} | |
method !info-using(Str $method, *@args) returns Hash { | |
my $description = $!handle.$method( $!flags +| self.MAGIC_MIME, |@args ); | |
my $mime-type = $!handle.$method( $!flags +| self.MAGIC_MIME_TYPE, |@args ); | |
my $encoding = $!handle.$method( $!flags +| self.MAGIC_MIME_ENCODING, |@args ); | |
return %( | |
description => $description, | |
mime-type => $mime-type, | |
encoding => $encoding, | |
mime-type-with-encoding => self!mime-type-with-encoding( $mime-type, $encoding ), | |
); | |
} | |
method !mime-type-with-encoding ($mime-type, $encoding) returns Str { | |
return $mime-type unless $encoding; | |
return "$mime-type; charset=$encoding"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment