#!/usr/bin/perl # updateJniCode.pl {SWIG .i file} # This script reads a SWIG .i file, looking for comment blocks of the form: # # /* # {someFile}: { # // C++ code here # } # */ # # It inserts the C++ code into the file specified. if (@ARGV != 1) { print "Usage:\n"; print "updateJniCode.pl {SWIG input file}\n"; } elsif (open(SWIGFILE, "<$ARGV[0]")) { my %updates; my $fileName = ""; while() { chomp; if (($inBlock == 0) && (/[\/\*\s]*update\s*([\w-\.]*):/)) { $fileName = $1; #$updates{$1} = ""; $inBlock = 1; #print "Found file $fileName to update!\n"; } elsif (($inBlock == 1) && (index($_, "}") >= 0)) { $inBlock = 0; } elsif ($inBlock == 1) { $updates{$fileName} .= $_ . "\n"; } } close SWIGFILE; if (%updates) { foreach $srcFile (keys(%updates)) { if (open(INFILE, "<$srcFile")) { if (open(OUTFILE, ">$srcFile" . ".NEW")) { print "Updating file $srcFile...\n"; my $wroteBlock = 0; my $skipToGuard = 0; if (index($srcFile, "\.h") > 0) { $skipToGuard = 1; } while () { chomp; if (($wroteBlock == 0) && ($skipToGuard == 0)) { print OUTFILE $updates{$srcFile}; $wroteBlock = 1; } elsif (($skipToGuard == 1) && ((index($_, "#define") >= 0) || (index($_, "#pragma once") >= 0))) { $skipToGuard = 0; } print OUTFILE $_; print OUTFILE "\n"; } close INFILE; close OUTFILE; unlink $srcFile; rename $srcFile . ".NEW", $srcFile; } else { print "ERROR: Cannot open file $srcFile" . ".NEW for writing...\n"; print "ERROR: File $srcFile will not be updated.\n"; } } else { print "ERROR: Cannot open file $srcFile for reading...\n"; print "ERROR: File $srcFile will not be updated.\n"; } } } }