1#! /usr/bin/perl
2
3print "Replace C++ comments with clean C comments: //text  =>  /* text */\n";
4print "Written by Stas Degteff 2:5080/102\n\n";
5if( $#ARGV<0 ){
6print <<USAGE;
7  Usage:
8
9     $0 *.c *.h
10USAGE
11}
12
13for( $i=0; $i<=$#ARGV; $i++ ){
14  if ( ! open FF, "$ARGV[$i]") { print "Can't open $ARGV[$i]!\n"; next; }
15  open OO, ">cpp2c.tmp" || die "Can't create/open cpp2c.tmp! Abort.\n";
16  print "Process $ARGV[$i]\n";
17  $commented=0;
18  while( <FF> ){
19    $commented=1 if( /\/\*/ );  # C comment start line
20    $commented=0 if( /\*\// );  # C comment end line
21
22    # replace // with /* */
23    s%//([^\015\012]*)%/* $1 */% if( !$commented );
24
25    # remove duplicated "*/"
26    while( m%\*/.*\*/% ){
27      s%\*/(.*)\*/% $1*/%;
28    }
29
30    print OO "$_";
31  }
32  close FF;
33  close OO;
34  rename $ARGV[$i], "$ARGV[$i].bak";
35  rename "cpp2c.tmp", $ARGV[$i];
36}