| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# Copy C source files (i.e. *.[ch]) from $src to $dest, first |
| 4 |
# stripping out uses of the macro BGD_DECLARE(<type>). A line must |
| 5 |
# begin with 'BGD_DECLARE' for it to be considered a use. |
| 6 |
|
| 7 |
use strict; |
| 8 |
use warnings; |
| 9 |
|
| 10 |
use File::Basename; |
| 11 |
|
| 12 |
my ($src, $dest) = @ARGV; |
| 13 |
|
| 14 |
die "Invalid arguments: nobgd.pl <src-dir> <dest-dir>\n" |
| 15 |
unless ($src && $dest && -d $src && -d $dest); |
| 16 |
|
| 17 |
for my $file (glob("$src/*.c"), glob("$src/*.h")) { |
| 18 |
do {local $| = 1; print "."}; |
| 19 |
fixup($file, $dest); |
| 20 |
} |
| 21 |
print "\n"; |
| 22 |
|
| 23 |
sub fixup { |
| 24 |
my ($src, $destDir) = @_; |
| 25 |
my $dest = $destDir . "/" . basename($src); |
| 26 |
|
| 27 |
my $content = slurp($src); |
| 28 |
$content =~ s{^ BGD_DECLARE \( ([^)]+) \)}{$1}gmx; |
| 29 |
unslurp($dest, $content); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
sub slurp { |
| 34 |
my ($filename) = @_; |
| 35 |
local $/; # no file separator |
| 36 |
my $data; |
| 37 |
|
| 38 |
open my $fh, "<", $filename |
| 39 |
or die "Unable to read file '$filename'.\n"; |
| 40 |
$data = <$fh>; |
| 41 |
close($fh); |
| 42 |
|
| 43 |
return $data; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
sub unslurp { |
| 48 |
my ($filename, $data) = @_; |
| 49 |
|
| 50 |
die "Refusing to overwrite file '$filename'\n" if -f $filename; |
| 51 |
|
| 52 |
open my $fh, ">", $filename |
| 53 |
or die "Unable to open '$filename' for writing.\n"; |
| 54 |
print {$fh} $data |
| 55 |
or die "Error writing file '$filename'\n"; |
| 56 |
close ($fh); |
| 57 |
} |