forked from MHeasell/rwe
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremove-empty-cpp-files.pl
More file actions
96 lines (71 loc) · 1.94 KB
/
Copy pathremove-empty-cpp-files.pl
File metadata and controls
96 lines (71 loc) · 1.94 KB
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
#!/usr/bin/env perl
use warnings;
use strict;
use File::Basename qw(basename dirname);
use File::Spec::Functions qw(catfile);
use File::Copy qw(move);
use File::Find;
my $dir = 'src';
my %empty_files;
find(\&process_file, $dir);
for my $f (keys %empty_files) {
unlink($f);
}
process_cmakelists('CMakeLists.txt', \%empty_files);
sub process_cmakelists {
my ($cmakelists, $files_to_remove) = @_;
my $tmp_name = "$cmakelists.remove-empty-cpp-files-tmp";
open(my $fh, '<', $cmakelists) or die "Failed to open $cmakelists: $!";
open(my $out, '>', $tmp_name) or die "Failed to open tmp file: $!";
while (my $line = <$fh>) {
chomp $line;
die "blarg" unless $line =~ /^\s*(.*?)\s*$/;
next if $files_to_remove->{$1};
print $out "$line\n";
}
close($fh);
close($out);
rename($tmp_name, $cmakelists);
return;
}
sub process_file {
my $path = $_;
return if ! -f $path;
return if not $path =~ /^.*\.cpp$/;
my $fullpath = $File::Find::name;
my $state = 'before';
open(my $fh, '<', $path) or die "Failed to open $path: $!";
while (my $line = <$fh>) {
chomp $line;
if ($state eq 'before') {
if ($line eq 'namespace rwe') {
$state = 'open';
}
}
elsif ($state eq 'open') {
if ($line eq '{') {
$state = 'in'
}
else {
die "Expected '{' in $fullpath at line $.";
}
}
elsif ($state eq 'in') {
if ($line eq '}') {
$empty_files{$fullpath} = 1;
}
return;
}
}
return;
}
__END__
=head1 NAME
remove-empty-cpp-files.pl - Remove empty .cpp files
=head1 SYNOPSIS
remove-empty-cpp-files.pl
=head1 DESCRIPTION
This script deletes empty cpp files and removes them
from CMakeLists.txt
These empty files can be created by the IDE
but then never actually used.