| 1 | package Lingua::Ogmios::FileManager; | ||
| 2 | |||
| 3 | 16 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 16 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 16 | ▶ | use File::MMagic; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 16 | ▶ | use Lingua::Ogmios::Annotations; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | |||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ($class, $MNFile) = @_; | ||
| 12 | |||
| 13 | my $FM = { | ||
| 14 | 'MagicNumberFilename' => $MNFile, | ||
| 15 | 'MagicNumbers' => undef, | ||
| 16 | }; | ||
| 17 | |||
| 18 | bless $FM, $class; | ||
| 19 | |||
| 20 | $FM->_load_MagicNumber; | ||
| 21 | |||
| 22 | return($FM); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub getMagicNumberFilename { | ||
| 26 | my ($self) = @_; | ||
| 27 | |||
| 28 | return($self->{'MagicNumberFilename'}); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub setMagicNumberFilename { | ||
| 32 | my ($self, $filename) = @_; | ||
| 33 | |||
| 34 | $self->{'MagicNumberFilename'} = $filename; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | sub _load_MagicNumber | ||
| 40 | { | ||
| 41 | my $self = shift; | ||
| 42 | my $MNFile = $self->getMagicNumberFilename; | ||
| 43 | |||
| 44 | my $mm = new File::MMagic; # use internal magic file | ||
| 45 | |||
| 46 | print STDERR "Loading complementary magic number ... "; | ||
| 47 | |||
| 48 | if (open FILEM, $MNFile) { | ||
| 49 | |||
| 50 | my $line; | ||
| 51 | |||
| 52 | while($line = <FILEM>) { | ||
| 53 | chomp $line; | ||
| 54 | $line =~ s/\s*\#.*//; | ||
| 55 | $line =~ s/^\s*//; | ||
| 56 | |||
| 57 | if ($line ne "") { | ||
| 58 | $mm->addMagicEntry($line); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | print STDERR "done\n"; | ||
| 62 | |||
| 63 | } else { | ||
| 64 | warn "$MNFile: no such file or directory\n"; | ||
| 65 | warn "No more Magic Number definition is loaded\n"; | ||
| 66 | |||
| 67 | } | ||
| 68 | $self->{'MagicNumbers'} = $mm; | ||
| 69 | |||
| 70 | return($mm); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub getType { | ||
| 74 | my ($self, $file) = @_; | ||
| 75 | |||
| 76 | my $xmlns; | ||
| 77 | |||
| 78 | if (-d $file) { | ||
| 79 | return('directory'); | ||
| 80 | } | ||
| 81 | if (-f $file) { | ||
| 82 | |||
| 83 | my $mm = $self->{'MagicNumbers'}; | ||
| 84 | |||
| 85 | print STDERR "Determining the type of the file " . $file . ": "; | ||
| 86 | |||
| 87 | my $type = $mm->checktype_filename($file); | ||
| 88 | |||
| 89 | if ($file =~ /.ppt$/i) { | ||
| 90 | $type = "application/powerpoint"; | ||
| 91 | warn "Getting the type thanks to the extension\n"; | ||
| 92 | } | ||
| 93 | if ($file =~ /.xls$/i) { | ||
| 94 | $type = "application/vnd.ms-excel"; | ||
| 95 | warn "Getting the type thanks to the extension\n"; | ||
| 96 | } | ||
| 97 | # if msword may be it should be relevant to check the extension, to better determine the type | ||
| 98 | $type =~ s/;.*//; | ||
| 99 | if (($type eq "message/rfc822") || ($file =~ /^x-system\/x-unix;/)) { | ||
| 100 | if ($file =~ /.tex$/i) { | ||
| 101 | $type = "text/x-tex"; | ||
| 102 | warn "Getting the type thanks to the extension\n"; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | if ($type eq 'text/xml') { | ||
| 106 | $xmlns = Lingua::Ogmios::Annotations::getNamespace($file); | ||
| 107 | $type .= " ns=$xmlns"; | ||
| 108 | } | ||
| 109 | print STDERR "Type file: $type\n"; | ||
| 110 | return($type); | ||
| 111 | |||
| 112 | } else { | ||
| 113 | warn "Unknown type\n"; | ||
| 114 | return('unknown'); | ||
| 115 | } | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub convert { | ||
| 121 | |||
| 122 | } | ||
| 123 | |||
| 124 | sub XMLconvert { | ||
| 125 | |||
| 126 | } | ||
| 127 | |||
| 128 | 1; | ||
| 129 | |||
| 130 | __END__ | ||