#!/usr/sbin/perl -w
# This Perl script takes 2 or 3 files names : a mandatory C/C++ file,
# and a .java file and or a .properties file. It converts all lines
# starting with #define's to public static finals and puts them in the
# .java file in the corresponding class. Any lines that do not start
# with #define, are just added to the .java file. It puts any line that
# starts with #define in the .properties file, after converting it to
# look like a java resource. # The destination files, if given, must
# exist. All lines in the destinations file up to and including the
# separator line will be copied verbatim from the existing file to the
# new file. The existing file will be renamed to .O. The separator
# line looks like:
# /* DO NOT DELETE THIS LINE -- macrosCToJava.pl depends on it */
# or
# # DO NOT DELETE THIS LINE -- macrosCToJava.pl depends on it
# Disclaimer: It makes very few checks. It was written
# particularly for converting ci_clikeys.h to CLIKeys.java and
# PackageP.properties.
# get hack name
@dirs = split(/\//,$0);
$O = "$dirs[$#dirs]";
# init
$SEPARATORJAVARE = "/\\* DO NOT DELETE THIS LINE --";
$SEPARATORJAVA = "/* DO NOT DELETE THIS LINE -- ${O} depends on it */";
$SEPARATORPROPRE = "# DO NOT DELETE THIS LINE --";
$SEPARATORPROP = "# DO NOT DELETE THIS LINE -- ${O} depends on it";
# get args
$from = $ARGV[0];
$file1 = $ARGV[1];
$file2 = $ARGV[2];
if (!$file1) {
die "${0}: must specify a .java file, a .properties file, or both.\n";
}
if ($file1 =~ m/^(\S*?)\.java$/) {
$java = $file1;
} elsif ($file1 =~ /^(\S*?)\.properties$/) {
$prop = $file1;
} else {
die "${0}: $file1 has neither .java nor .properties extension.\n";
}
if ($file2) {
if ($file2 =~ m/^(\S*?)\.java$/) {
if (!$java) {
$java = $file2;
} else {
die "${0}: second file has same type as first.\n";
}
} elsif ($file2 =~ /^(\S*?)\.properties$/) {
if (!$prop) {
$prop = $file2;
} else {
die "${0}: second file has same type as first.\n";
}
} else {
die "${0}: $file2 has neither .java nor .properties extension.\n";
}
}
if ($java && ! -e $java) {
warn "${O}: First target file does not exist.\n";
}
if ($java && ! -w $java) {
warn "${O}: First target file not writable.\n";
}
if ($prop && ! -e $prop) {
warn "${O}: Second target file does not exist.\n";
}
if ($prop && ! -w $prop) {
warn "${O}: Second target file not writable.\n";
}
open(HFILE, $from);
if ($java) {
$tempJava = "${java}.$$";
open(NEWJAVAFILE, ">$tempJava");
#copy all lines above seperator into new Java file.
if (open(OLDJAVAFILE, "<$java")) {
while (<OLDJAVAFILE>) {
print NEWJAVAFILE $_;
last if (m/^$SEPARATORJAVARE/o);
}
close(OLDJAVAFILE);
} else {
print NEWJAVAFILE $SEPARATORJAVA, "\n";
}
print NEWJAVAFILE "\n";
}
if ($prop) {
$tempProp = "${prop}.$$";
open(NEWPROPFILE, ">$tempProp");
#copy all lines above seperator into new Properties file.
if (open(OLDPROPFILE, "<$prop")) {
while (<OLDPROPFILE>) {
print NEWPROPFILE $_;
last if (m/^$SEPARATORPROPRE/o);
}
close(OLDPROPFILE);
} else {
print NEWPROPFILE $SEPARATORPROP, "\n";
}
print NEWPROPFILE "\n";
}
while (<HFILE>) {
s/\s*$//; # remove whitespace at EOL
if (m/\s*?#define\s+(\S+)\s+(\S+)/) {
$macro = $1;
$value = $2;
# Remove CICLI from the beginning of any macro
$macro =~ s/^CICLI_//g;
# If value is all integers, then the type is int,otherwise it
# is a String
if ($value =~ m/^[0-9]*$/) {
$type = "int";
} else {
$type = "String";
}
$_ = " public static final $type $macro = $value;";
if ($java) {print NEWJAVAFILE $_,"\n";}
$value =~ s/\"([^\"]*)\"/$1/;
if ($prop) {print NEWPROPFILE "$macro = $value\n";}
}
}
if ($java) {print NEWJAVAFILE "\n}\n";}
close(HFILE);
if ($java) {
close(NEWJAVAFILE);
if ( -e $java) {
rename $java,"${java}.O";
}
rename $tempJava,$java;
}
if ($prop) {
close(NEWPROPFILE);
if (-e $prop) {
rename $prop,"${prop}.O";
}
rename $tempProp,$prop;
}