Created
January 2, 2009 17:44
-
-
Save d0k/42612 to your computer and use it in GitHub Desktop.
perl script to create empty source files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# ./cheader.pl foo.h bar.c qux.py | |
# creates empty source files | |
use strict; | |
sub cheader { | |
my $guard = uc shift; | |
$guard =~ tr/\.\/-/_/; | |
return "#ifndef $guard\n#define $guard\n\n#endif /* $guard */\n"; | |
} | |
sub cfile { | |
my $name = shift; | |
$name =~ s/\..*?$//x; # remove extension | |
my @found = grep { /^$name\.h+p*$/ix } @ARGV; | |
return if @found == 0; | |
return '#include "', @found, "\"\n"; | |
} | |
sub rmext { | |
s/\..*?$//x; | |
return $_; | |
} | |
my %filetypes = ( | |
qr/\.h+p*$/ix => \&cheader, | |
qr/\.c+p*$/ix => \&cfile, | |
qr/\.sh$/x => sub { "#!/bin/sh\n" }, | |
qr/\.pl$/x => sub { "#!/usr/bin/perl\n" }, | |
qr/\.py$/x => sub { "#!/usr/bin/env python\n" }, | |
qr/\.rb$/x => sub { "#!/usr/bin/env ruby\n" }, | |
qr/\.java$/x => sub { "public class ".rmext." {\n}\n" }, | |
qr/\.cs$/x => sub { "using System;\n\npublic class ".rmext." {\n}\n" }, | |
); | |
foreach (@ARGV) { | |
if (! -e) { # file exists? | |
open my $f, '>', $_ or warn $_.': '.$!."\n"; | |
while (my ($regex, $outsub) = each %filetypes) { | |
print {$f} $outsub->($_) if $_ =~ $regex; | |
} | |
close $f; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment