big rework.

rename afterwards, but only if something changed and no error occured


git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@951 379699f6-c40d-0410-875b-85095c16579e
stable
jochen 26 years ago
parent 610bc2106e
commit 03694a3b2e
  1. 141
      jode/jcpp

@ -52,89 +52,122 @@ for (@ARGV) {
} }
for (@files) { for (@files) {
my $file = $_; # Number of nested #if directives. Initially 0, will be increased
my $branch = "none"; # on every #if directive and decreased on every #endif directive.
my $level = 0; my $level = 0;
rename "$file", "$file.orig" or do {
# The number of the outermost level, whose #if directive was
# false. This is 0, if there wasn't an false #if directive, yet.
# As long as it is != 0, we comment every line, and ignore
# directives except for increasing/decreasing $level.
my $falselevel = 0;
# Tells if an error occured and the transformation shouldn't
# be done.
my $error = 0;
my $changes = 0;
my $file = $_;
open OLD, "<$file" or do {
print STDERR "Can't open file $file\n"; print STDERR "Can't open file $file\n";
next; next;
}; };
open OLD, "<$file.orig"; open NEW, ">$file.tmp" or do {
open NEW, ">$file"; print STDERR "Can't open tmp file $file.tmp\n";
next;
};
my $linenr = 0; my $linenr = 0;
LINE:
while (<OLD>) { while (<OLD>) {
$linenr++; $linenr++;
if (/^\/\/\/\#/) { if (m'^///#') {
# This is a directive. First we print it out. # This is a directive. First we print it out.
print NEW $_; print NEW $_;
if (/^\/\/\/\#ifdef (\S*)$/) { if (m'^///# *if') {
my $label=$1; $level++;
# An ifdef directive, look if -D is defined, but only if (m'^///# *ifdef (\S*)$') {
# if we are not in a false branch. In a false branch # If there was an outer false #if directive, we ignore the
# we simply increase level. # condition.
if ($branch eq "false") { next LINE if ($falselevel);
$level++;
} else { my $label=$1;
if (defined $defs{$label}) {
$branch = "true"; # An ifdef directive, look if -D is defined.
} else { $falselevel = $level
$branch = "false"; unless (defined $defs{$label});
$level = 0; } elsif (m'^///# *ifndef (\S*)$') {
} # If there was an outer false #if directive, we ignore the
} # condition.
} elsif (/^\/\/\/\#ifndef (\S*)$/) { next LINE if ($falselevel);
my $label=$1;
# An ifndef directive, look if -D is defined, but only my $label=$1;
# if we are not in a false branch. In a false branch # An ifndef directive, look if -D is defined
# we simply increase level. $falselevel = $level
if ($branch eq "false") { if (defined $defs{$label});
$level++;
} else {
if (defined $defs{$label}) {
$branch = "false";
$level = 0;
} else {
$branch = "true";
}
} }
} elsif (/^\/\/\/\#else/) { } elsif (m'^///# *else') {
# An else directive. We switch from true to false and # An else directive. We switch from true to false and
# if level is zero we switch from false to true # if level is falselevel we switch from false to true
if ($branch eq "true") { if ($level == 0) {
$branch = "false";
$level = 0;
} elsif ($branch eq "false") {
$branch = "true" if ($level == 0);
} else {
# An else outside of any directives; warn. # An else outside of any directives; warn.
print STDERR "$file: $linenr: ignoring unmatched $_"; print STDERR "$file: $linenr: unmatched $_";
$error = 1;
} elsif ($falselevel == $level) {
$falselevel = 0;
} elsif ($falselevel == 0) {
$falselevel = $level;
} }
} elsif (/^\/\/\/\#endif/) { } elsif (m'^///# *endif') {
if ($branch eq "none") { # set $falselevel to 0, if the false branch is over now.
# An endif outside of any directives; warn. $falselevel = 0 if ($falselevel == $level);
print STDERR "$file: $linenr: ignoring unmatched $_"; # decrease level.
} elsif ($branch eq "false") { if ($level == 0) {
$branch = "true" if ($level-- == 0); print STDERR "$file: $linenr: unmatched $_";
$error = 1;
} else {
$level--;
} }
} else { } else {
print STDERR "$file: $linenr: ignoring unknown directive $_"; print STDERR "$file: $linenr: ignoring unknown directive $_";
$error = 1;
} }
} elsif (/^\/\/\/(.*)/) { } elsif (m'^///(.*)') {
$line = $1; $line = $1;
if ($branch eq "true") { if ($falselevel == 0 && $level > 0) {
# remove comments in true branch; # remove comments in true branch, but not in outermost level.
print NEW "$line\n"; print NEW "$line\n";
$changes = 1;
} else { } else {
# print out the full line with comments: # print out the full line with comments:
print NEW $_; print NEW $_;
} }
} else { } else {
if ($branch eq "false") { if ($falselevel != 0) {
# add comments in false branch # add comments in false branch
print NEW "///$_"; print NEW "///$_";
$changes = 1;
} else { } else {
print NEW $_; print NEW $_;
} }
} }
} }
if ($level != 0 || $falselevel != 0) {
# something got wrong
print STDERR "$file: unmatched directives: level $level, ".
"falselevel $falselevel\n";
$error = 1;
}
if ($error == 0) {
if ($changes == 0) {
unlink "$file.tmp";
} else {
(rename "$file", "$file.orig"
and rename "$file.tmp", "$file")
or print STDERR "$file: Couldn't rename files.\n";
}
} else {
print STDERR "$file: errors occured, file not transformed.\n";
}
} }

Loading…
Cancel
Save