用perl實現的文件頭注釋工具


最近想開源一套軟件,對於一些代碼都要在文件頭上加上固定格式的版本信息。自己用perl寫了一個小工具,拿出來分享。

下載地址:http://files.cnblogs.com/xjon/comments.zip

 

  1 #!/usr/bin/perl -W
  2 #
  3 # Copyright (c) 2014 Nijie. All rights reserved.
  4 # License: GPL-2
  5 #
  6 # File: comments.pl  用於增加c/c++文件注釋
  7 # Create by Nijie 2014.07.23
  8 #
  9 
 10 use strict;
 11 use warnings;
 12 use File::Copy;
 13 
 14 my $author='Nijie';            # 作者 - 需修改
 15 my $createyear='2014';        # 版權時間 - 需修改
 16 my $date="2014.07.23";        # 文件創建時間 - 需修改
 17 
 18 # 以下版權信息 - 需修改
 19 my $comments="// Created by $author on $date.
 20 // Copyright (c) $createyear $author. All rights reserved.
 21 // Use of this source code is governed by a GPL-2 license that can be found in the LICENSE file. 
 22 //
 23 ";
 24 
 25 my @filterDir = ("./lib3rd/");    # 需要過濾的文件,比如不屬於你開發的目錄等 - 需修改
 26 
 27 my %filterList = ();
 28 foreach (@filterDir)
 29 {
 30     $filterList{$_} = 1;
 31 }
 32 
 33 my ($dircnt, $filecnt) = (0, 0);
 34 
 35 sub lsr_s($) {
 36     my $cwd = shift;
 37     my @dirs = ($cwd.'/');
 38 
 39     my ($dir, $file);
 40     while ($dir = pop(@dirs)) {
 41         if (exists($filterList{$dir}))
 42         {
 43             print "skip dir : $dir\n";
 44             next;
 45         }
 46         local *DH;
 47         print "open $dir\n";
 48         if (!opendir(DH, $dir)) {
 49             warn "Cannot opendir $dir: $! $^E";
 50             next;
 51         }
 52         foreach (readdir(DH)) {
 53             if ($_ eq '.' || $_ eq '..') {
 54                 next;
 55             }
 56             $file = $dir.$_;         
 57             if (!-l $file && -d _) {
 58                 $file .= '/';
 59                 push(@dirs, $file);
 60             }
 61             process($file, $dir);
 62         }
 63         closedir(DH);
 64     }
 65 }
 66 
 67 sub process($) {
 68     my $file = shift;
 69     print "process $file";
 70     if (substr($file, length($file)-1, 1) eq '/') {
 71         $dircnt++;
 72     }
 73     else {
 74         $filecnt++;
 75         if ($file =~ /\.(h$|c$|cpp$|hpp$|cc$)/)
 76         {
 77             addComments($file);
 78         }
 79     }
 80     print "\n";
 81 }
 82 
 83 sub addComments($)
 84 {
 85     my $line = 0;
 86     my $file = shift;
 87     open FILE, $file or die 'die ($!)';
 88     open OUTFILE, ">$file~~~" or die "Open the file $file~~~ failed!\n";
 89 
 90     print OUTFILE $comments;
 91     while (<FILE>)
 92     {
 93         print OUTFILE $_;
 94     }
 95     close FILE;
 96     close OUTFILE;
 97     move("$file~~~", $file);
 98 }
 99 
100 lsr_s('.');
101 print "$filecnt files, $dircnt directory.\n";
102 `pause`;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM