find-in-one-file.pl 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env perl
  2. # Locate duplicate includes
  3. # Copyright (c) 2005, Pavel Roskin
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. use strict;
  17. my %sys_includes;
  18. my %loc_includes;
  19. if ($#ARGV != 0) {
  20. print "Usage: dupincludes.pl file\n";
  21. exit 1;
  22. }
  23. my $filename = $ARGV[0];
  24. if (!open (FILE, "$filename")) {
  25. print "Cannot open file \"$filename\"\n";
  26. exit 1;
  27. }
  28. my $lineno=1;
  29. foreach (<FILE>) {
  30. if (/^\s*#\s*include\s*<(.*)>/) {
  31. if (defined $sys_includes{$1}) {
  32. print "$filename:$lineno: duplicate <$1> (line no ".$sys_includes{$1}.")\n";
  33. } else {
  34. $sys_includes{$1} = $lineno;
  35. }
  36. } elsif (/^\s*#\s*include\s*"(.*)"/) {
  37. if (defined $loc_includes{$1}) {
  38. print "$filename:$lineno: duplicate \"$1\" (line no ".$loc_includes{$1}.")\n";
  39. } else {
  40. $loc_includes{$1} = $lineno;
  41. }
  42. }
  43. $lineno++;
  44. }