cgal/Scripts/scripts/cgal_conditional_include

108 lines
2.7 KiB
Perl
Executable File

#!/usr/bin/env perl
# usage: cgal_conditional_include file1 ...
# Author: Geert-Jan Giezeman.
# Protects CGAL and other include directives in the following way:
# #include <CGAL/Handle>
#
# is converted to
#
# #ifndef CGAL_HANDLE_H
# #include <CGAL/Handle>
# #endif
# #include <stdlib.h>
#
# is converted to
#
# #ifndef CGAL_PROTECT_STDLIB_H
# #include <stdlib.h>
# #define CGAL_PROTECT_STDLIB_H
# #endif
# The only filenames that are not protected are the ones that figure in
# the following list (with .h suffix added).
@non_protected_files = (
'LEDA/REDEFINE_NAMES',
'LEDA/UNDEFINE_NAMES',
'assert');
$tmpfilename = "CondIncl$$";
FILE:
foreach $filename (@ARGV) {
if (!open("infile", $filename)) {
print STDERR "Could not open ${filename} for reading\n$!\n";
next FILE;
}
open("tmpfile", ">$tmpfilename")
|| die "Could not open temp file for writing\n$!\n";
$last = '';
Line:
while (<infile>) {
if (($leading_space, $basename) = m|^([\s]*)#[\s]*include[\s]*<CGAL/(.*)\.h>[\s]*|)
{
# protect CGAL header files
$protect_name = $basename;
$protect_name =~ s'/'_'g;
$protect_name =~ tr/a-z/A-Z/;
$protect_name = "CGAL_" . $protect_name . '_H';
$protect_line = '#ifndef ' . $protect_name . "\n";
if ( $last !~ m|^[\s]*$protect_line|) {
print tmpfile $leading_space;
print tmpfile $protect_line;
# print tmpfile "#include <CGAL/$basename.h>\n";
print tmpfile $_;
print tmpfile $leading_space;
print tmpfile "#endif // $protect_name\n";
} else {
print tmpfile $_;
}
$last = '';
} elsif (($leading_space, $basename) = m|^([\s]*)#[\s]*include[\s]*<(.*)\.h>[\s]*|) {
# protect other header files
foreach $special_name (@non_protected_files) {
if ($basename =~ m#^$special_name$#) {
print tmpfile $_;
$last = '';
next Line;
}
}
$protect_name = $basename;
$protect_name =~ s'/'_'g;
$protect_name =~ tr/a-z/A-Z/;
$protect_name = 'CGAL_PROTECT_' . $protect_name . '_H';
$protect_line = '#ifndef ' . $protect_name . "\n";
if ( $last !~ m|^[\s]*$protect_line|) {
print tmpfile $leading_space;
print tmpfile $protect_line;
print tmpfile $_;
print tmpfile $leading_space;
print tmpfile "#define $protect_name\n";
print tmpfile $leading_space;
print tmpfile "#endif // $protect_name\n";
} else {
print tmpfile $_;
}
$last = '';
} else {
# copy other lines
print tmpfile $_;
$last = $_;
}
}
close infile;
close tmpfile;
if ($?) {
print STDERR "Conversion of ${filename} failed\n$!\n";
next FILE;
}
if (!rename($tmpfilename,${filename})) {
print STDERR "Cannot rename $tmpfilename to ${filename}.\n$!\n";
print STDERR "Conversion of ${filename} failed\n";
}
}