File: join-c-comments.pl

package info (click to toggle)
clisp 1:2.41-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 49,804 kB
  • ctags: 16,291
  • sloc: lisp: 75,912; ansic: 49,247; xml: 24,289; asm: 21,993; sh: 11,234; fortran: 6,692; cpp: 2,660; objc: 2,481; makefile: 2,355; perl: 164; sed: 55
file content (83 lines) | stat: -rw-r--r-- 1,730 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/perl -w

# After comment5 replaces #-style comments with normal C comments,
# this script finds consecutive lines of comments (each with their own
# set of /**/) and turns each chunk of comments into a single block
# comment.

use strict;

my @line_buffer = ();
my $indent = undef;
my $header_comment = 1;
my $add_to_header_comment = undef;

while (<>) {
  m!^#line 1 ".*?\.d"$! and next;
  if (m!^(\s*)/\*.*\*/\s*$!) {
    my $i = length($1);
    if (not defined $indent) {
      $indent = $i;
    }

    if ($i != $indent) {
      dump_line_buffer();
      $indent = $i;
    }

    push @line_buffer, $_;

  } else {
    dump_line_buffer();
    print;
  }
}
dump_line_buffer();


sub dump_line_buffer {
  if (@line_buffer) {
    if ($header_comment) {
      $add_to_header_comment and push @line_buffer, $add_to_header_comment;
      $header_comment = 0;
      tidy_comment(@line_buffer);
    } elsif ($#line_buffer == 0) {
      print $line_buffer[0];
    } else {
      compact_comment(@line_buffer);
    }
    @line_buffer = ();
    $indent = undef;
  }
}


sub compact_comment {
  my (@line_buffer) = @_;
  print " " x $indent;
  print "/*";
  for (my $i = 0; $i <= $#line_buffer; $i++) {
    my $line = $line_buffer[$i];
    my ($guts) = $line =~ m!/\*(.*?)\s*\*/!;
    unless ($i == 0) { print " " x $indent; }
    print "$guts";
    if ($i < $#line_buffer) { print "\n"; }
  }
  print " */\n";
}

sub tidy_comment {
  my (@line_buffer) = @_;
  print " " x $indent;
  print "/*\n";
  for (my $i = 0; $i <= $#line_buffer; $i++) {
    my $line = $line_buffer[$i];
    my ($guts) = $line =~ m!/\*(.*?)\s*\*/!;
    print " " x $indent;
    print " * $guts\n";
  }
  print " " x $indent;
  print " */\n";
}

__END__