File: wizard.pl

package info (click to toggle)
libfile-type-perl 0.22-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 464 kB
  • sloc: perl: 1,729; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,280 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl

use warnings;
use strict;

use lib './lib','../lib';

use IO::File;
use File::Type::Builder;

my $in  = new IO::File;
my $out = new IO::File "> cases.pl";
die "No output file!" unless defined ($out);

my ($line, $count) = (0, 0);
my $build = File::Type::Builder->new();

if ($in->open("< mime-magic")) {
  while (<$in>) {
    $line++;
    
    my $data = $_;
    chomp $data;

    # special case for a couple of lines that are unparsable
    next if ($data =~ m/Content-Type/);

    my $parsed = $build->parse_magic($data, $line);
    if (!defined $parsed) {
      # warn "Skipping line $line\n";
      next;
    }
    
    # output to new line
    if ($parsed->{pattern_type} eq 'string') {
      my $code = $build->string($parsed);    
      next unless defined($code);

      print $out $code;
      $count++;
    
    } elsif ($parsed->{pattern_type} =~ m/^be/) {
      my $code;
      if ($parsed->{pattern_type} eq 'beshort') {
        $code = $build->be($parsed, 2);      
      }
      if ($parsed->{pattern_type} eq 'belong') {
        $code = $build->be($parsed, 4);
      }      
      next unless defined($code);

      print $out $code;
      $count++;
    }      

    
  }
}

print "Read $line lines. Written $count conditions.\n";

exit;

__END__