-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake-edit-distance-c.pl
executable file
·117 lines (107 loc) · 2.51 KB
/
make-edit-distance-c.pl
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/home/ben/software/install/bin/perl
# This makes all the different edit distance C files from a master
# template.
use warnings;
use strict;
use Template;
use FindBin '$Bin';
use File::Compare;
use C::Utility qw/linein lineout/;
use Deploy 'do_system';
my $tt = Template->new (
ABSOLUTE => 1,
INCLUDE_PATH => [
$Bin,
],
);
my $file = 'edit-distance';
my %vars;
$vars{compare_c1_c2} = 'c1 == c2';
$vars{insert_cost} = 1;
$vars{delete_cost} = 1;
$vars{substitute_cost} = 1;
my @cfiles;
for my $type (qw/char int/) {
# This is stupid, keeping it like this until ed-trans.c.tmpl is
# all sorted out.
$vars{type} = "unsigned $type";
if ($type eq 'int') {
$vars{type} = 'int';
}
$vars{function} = "distance_$type";
$vars{ed_type} = "$type";
$vars{stem} = "$type";
my $notf = '';
my $base = "$file-$vars{stem}";
# This is the macro used in the .h file as a double-inclusion
# guard.
my $wrapper = "$base-h";
$wrapper =~ s/-/_/g;
$wrapper = uc $wrapper;
$vars{wrapper} = $wrapper;
if ($type eq 'int') {
$vars{length} = 'ulength';
$vars{value} = 'unicode';
$vars{dic} = 'idic';
}
elsif ($type eq 'char') {
$vars{length} = 'length';
$vars{value} = 'text';
$vars{dic} = 'adic';
}
else {
die "Bad type $type";
}
my $cfile = "$base.c";
do_file ($tt, "$file.c.tmpl", \%vars, $cfile);
push @cfiles, $cfile;
$vars{function} .= "_trans";
$vars{stem} .= "-trans";
my $tcfile = "ed-trans-$type.c";
do_file ($tt, "ed-trans.c.tmpl", \%vars, $tcfile);
push @cfiles, $tcfile;
}
# Write "config.h" from "config".
open my $config, "<", "$Bin/config" or die $!;
my %config;
while (<$config>) {
if (/^\s*#/ || /^\s*$/) {
next;
}
if (! /([A-Z_]+)\s+(.*)/) {
die "$.: Bad line $_";
}
my ($key, $value) = ($1, $2);
$config{$key} = $value;
}
close $config or die $!;
open my $cfgh, ">", "$Bin/config.h" or die $!;
my $w = 'TEXT_FUZZY_CONFIG';
print $cfgh <<EOF;
#ifndef $w
#define $w
EOF
for my $key (sort keys %config) {
print $cfgh "#define $key $config{$key}\n";
}
print $cfgh <<EOF;
#endif /* ndef $w */
EOF
close $cfgh or die $!;
for my $cfile (@cfiles) {
do_system ("cfunctions $cfile");
}
exit;
sub do_file
{
my ($tt, $infile, $vars, $outfile) = @_;
if (-f $outfile) {
chmod 0777, $outfile;
unlink $outfile;
}
my $text = linein ($infile);
$tt->process (\$text, $vars, \my $textout)
or die '' . $tt->error ();
lineout ($textout, $outfile);
chmod 0444, $outfile;
}