summaryrefslogtreecommitdiff
path: root/support/splint/scripts/bindx.pl
blob: ace62429ba615d4688d0c711196598bb70aa0f8d (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/perl

open FILE, "$ARGV[0]" or die "Cannot open input file $ARGV[0]\n";
open FILE_OUT, ">$ARGV[1]" or die "Cannot open input file $ARGV[1]\n";

sub lex_order (\@\@) { # lexicographic ordering

    my (@string1) = @{shift @_};
    my (@string2) = @{shift @_};

    my ($i);

    $i = 0;

    while ( $string1[$i] == $string2[$i] && $i <= $#string1 && $i <= $#string2 ) { $i++ }

    if ( $i > $#string1 || $i > $#string2 ) { 
   
	return $#string1 <=> $#string2;

    }

    return ( $string1[$i] <=> $string2[$i] );

}

sub numerically { $b <=> $a; }

sub alphabetically { # lexicographic ordering based on the ASCII order defined in @main_order

    my (@chars1) = map { $main_order{$_} } split //, $a;
    my (@chars2) = map { $main_order{$_} } split //, $b;

    return lex_order @chars1, @chars2;

}

sub lexicographically { # lexicographic ordering for numeric sequences separated by spaces

    my (@chars1) = split / /, $a;
    my (@chars2) = split / /, $b;

    return lex_order @chars1, @chars2;
}

$alphabet = "\#\$\%^&*<>[]{}()+-=_|\\,:;~`.?!\'\"\@0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
@main_set = split //, $alphabet;
map { $main_order{$_} = index $alphabet, $_ } @main_set; # inefficient ...

$ldelim[1] = "\\["; $rdelim[1] = "]";
$ldelim[0] = "\\("; $rdelim[0] = ")";
$ldelim[5] = "\\e"; $rdelim[5] = "e";
$ldelim[4] = "\\f"; $rdelim[4] = "f";
$ldelim[3] = "\\g"; $rdelim[3] = "g";

while (<FILE>) {

    $input = $_;

    if ( $input =~ /\\(.)TI\s*([0-9]+)\s*\{(\\[a-z]+)\s*\{(.+)\}\}\{(.+)\}\{(.+)\}\{(.+)\}\n/ ) {

	($domain, $rank, $type, $key, $nspace, $pageno, $ref) = ($1, $2, $3, $4, $5, $6, $7);
        $key =~ s/\{([0-9]+)\}/pack "c1", $1/eg;

        push @{$index{$domain}{$key}{refs}}, "$ref $rank";
        $index{$domain}{$key}{nspace} = $nspace;
        $index{$domain}{$key}{type} = $type;

    }

}

$i = 0;

foreach $domain (sort keys %index ) {

    if ( $i > 0) {

	print FILE_OUT "\\indexseparator{$domain}{$i}\n";

    }

    $i++;

    foreach $key ( sort alphabetically keys %{$index{$domain}} ) {

        %ref_list = ();
	map { exists $ref_list{$_} ? ($ref_list{$_}++) : ($ref_list{$_} = 0) } @{$index{$domain}{$key}{refs}};

        @ref_list = sort lexicographically keys %ref_list;
        @ref_list = map { @r = split / /, $_; "$ldelim[$r[1]]$r[0]$rdelim[$r[1]]" } @ref_list;

	$ukey = $key;
	$ukey =~ s/(.)/"\{".(unpack "c1", $1)."\}"/eg; 
    
	print FILE_OUT "\\GI{$index{$domain}{$key}{nspace}}{$index{$domain}{$key}{type}}{$ukey}, ", 
             (join ', ', @ref_list), ". \% $key, sec nos. ", (join ', ', @ref_list ), "\n";
	
    }
}