sl@0: # Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: # All rights reserved.
sl@0: # This component and the accompanying materials are made available
sl@0: # under the terms of "Eclipse Public License v1.0"
sl@0: # which accompanies this distribution, and is available
sl@0: # at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: #
sl@0: # Initial Contributors:
sl@0: # Nokia Corporation - initial contribution.
sl@0: #
sl@0: # Contributors:
sl@0: #
sl@0: # Description:
sl@0: # stringtable.pl - Makes a string table .cpp and .h file from a .st file
sl@0: # This tool takes a valid .st file as an input parameter and converts it into a .h and a .cpp file
sl@0: # 
sl@0: #
sl@0: 
sl@0: use strict;
sl@0: 
sl@0: # Removes the extension from the filename
sl@0: $ARGV[0] =~ m/(.*)\..*/;
sl@0: my $root = $1;
sl@0: $root =~ m/(.*[\\\/])([^\\\/]*)$/;
sl@0: 
sl@0: my $path_to_file = ("$1");
sl@0: my $filename = lc("$2");
sl@0: my $header_to_include = lc("$filename.h");
sl@0: 
sl@0: # Open the input filename
sl@0: open (IN, "<$ARGV[0]" ) or die ("Error: $ARGV[0] $!");
sl@0: 
sl@0: # Parse header comments
sl@0: my $input;
sl@0: my @comments;
sl@0: while ($input = <IN>) {
sl@0: 	# Comments starting with '#' not to be included in header file
sl@0: 	next if ($input =~ /^\s*\#/);  
sl@0: 
sl@0: 	chomp($input);
sl@0: 
sl@0: 	# Save comments containing '!' or stop looking if something else is found
sl@0: 	if ($input =~ /\!(.*)/) {
sl@0: 		push(@comments, "$1\n");
sl@0:   	} else {
sl@0:     		last;
sl@0:   	}
sl@0: }
sl@0: 
sl@0: my $folding;
sl@0: # Check that line is of the right format and get the table name.
sl@0: $input =~ m/(f*stringtable) ([A-Za-z0-9_]+)/ or die ("Error: $ARGV[0] is not a stringtable file");
sl@0: my $name = $2;
sl@0: if ($1 eq "stringtable") {
sl@0: 	$folding = "ETrue";
sl@0: 	} else {
sl@0: 	$folding = "EFalse";
sl@0: 	}
sl@0: 
sl@0: # Open the output filenames
sl@0: open (CPP, ">"."$path_to_file"."$filename".".cpp" ) or die ("Error: $ARGV[0] Can't open cpp file");
sl@0: open (HEADER, ">"."$path_to_file"."$filename".".h" ) or die ("Error: $ARGV[0] Can't open header file");
sl@0: 
sl@0: my $sourcename = $ARGV[0];
sl@0: $sourcename =~ s/^(.*epoc32)/epoc32/i;
sl@0: 
sl@0: # Output the preambles
sl@0: print CPP <<EOD;
sl@0: // Autogenerated from $sourcename by the stringtable tool - Do not edit
sl@0: #include <e32std.h>
sl@0: #include <stringpool.h>
sl@0: #include <stringtablesupport.h>
sl@0: #include "$header_to_include"
sl@0: #ifdef _DEBUG
sl@0: #undef _DEBUG
sl@0: #endif
sl@0: 
sl@0: EOD
sl@0: 
sl@0: print HEADER <<EOD;
sl@0: // Autogenerated from $sourcename by the stringtable tool - Do not edit
sl@0: 
sl@0: #ifndef STRINGTABLE_$name
sl@0: #define STRINGTABLE_$name
sl@0: 
sl@0: #include <stringpool.h>
sl@0: 
sl@0: struct TStringTable;
sl@0: 
sl@0: EOD
sl@0: 
sl@0: # If there are no header comments, use the default comment, otherwise, add the header comments
sl@0: if (@comments == 0) {
sl@0: 	print HEADER "/** A String table */\n";
sl@0: 	} else {
sl@0: 	print HEADER @comments;
sl@0: 	}
sl@0: 
sl@0: print HEADER <<EOD;
sl@0: class $name 
sl@0: 	{
sl@0: public:
sl@0: 	enum TStrings
sl@0: 		{
sl@0: EOD
sl@0: 
sl@0: my $count = 0;
sl@0: my $outputcomma = 0;
sl@0: my @commentlines;
sl@0: 
sl@0: #Now process file
sl@0: until (eof IN) {
sl@0:     # Treat lines with a '#' as the first character as comments and skip them, ignoring 0 or more whitespaces before it.
sl@0:     # If # is not the first character, then treat them as part of the string.
sl@0:     do 
sl@0:       {
sl@0:       $input = readline (*IN);
sl@0: 	    chomp($input);
sl@0:       } while ($input =~ m/^\s*\#/);
sl@0: 
sl@0: 	if ($input =~ m/\!(.*)/) {
sl@0: 		# Save lines that start with '!' to print later
sl@0: 		push(@commentlines, "\t\t$1\n");
sl@0: 	} else {
sl@0: 		# Parse the line
sl@0: 		$input =~ m/([A-Za-z0-9]+)\s*(.*)/ or die ("Error: $ARGV[0] badly formed at \"$input\"");
sl@0: 
sl@0: 		# If a comma needs to be printed, print it
sl@0: 		if ($outputcomma == 1) {
sl@0: 			print HEADER ",\n";
sl@0: 			$outputcomma = 0;
sl@0: 		}
sl@0: 		# Print pending comments and clear the comments array
sl@0: 		if (@commentlines > 0) {
sl@0: 			print HEADER @commentlines;
sl@0: 			@commentlines = '';
sl@0: 		}
sl@0: 
sl@0: 		# Form a version of the string that can go in a comment. This
sl@0: 		# can't contain /* or */
sl@0: 		my $enum = $1;
sl@0: 		my $string = $2;
sl@0: 		$string =~ s/\s+$//;	# remove any trailing whitespace
sl@0: 		my $comment = $string;
sl@0: 		$comment =~ s|/\*|/ \*|g;
sl@0: 		$comment =~ s|\*/|\* /|g;
sl@0: 
sl@0: 		# Increment the count
sl@0: 		$count++;
sl@0: 		print HEADER "\t\t/** $comment */\n";
sl@0: 		print HEADER "\t\t$enum";
sl@0: 		print CPP "_STLIT8(K$count, \"$string\");\n";
sl@0: 		$outputcomma = 1;
sl@0: 	}
sl@0: }
sl@0: 
sl@0: # Print closing comments
sl@0: if (@commentlines > 0) {
sl@0: 	print HEADER "\n";
sl@0: 	print HEADER @commentlines;
sl@0: 	@commentlines = '';
sl@0: } else {
sl@0: 	print HEADER "\n";
sl@0: }
sl@0: 
sl@0: #Output intermediate boilerplate in the cpp
sl@0: print CPP <<EOD;
sl@0: 
sl@0: // Intermediate
sl@0: const void * const KStringPointers[] =
sl@0: 	{
sl@0: EOD
sl@0: 
sl@0: # And the end of the headers
sl@0: print HEADER <<EOD;
sl@0: 		};
sl@0: 	static const TStringTable Table;	
sl@0: 	};
sl@0: 
sl@0: #endif // STRINGTABLE_$name
sl@0: 
sl@0: EOD
sl@0: 
sl@0: #Output the table of pointers to the cpp file
sl@0: my $total = $count;
sl@0: 
sl@0: for ($count = 1; $count <= $total ; $count++) {
sl@0:     if ($count > 1) {
sl@0: 	print CPP ",\n";
sl@0:     }
sl@0:     print CPP "\t(const void*)&K$count";
sl@0: };
sl@0: 
sl@0: #The last of the boilerplate
sl@0: print CPP <<EOD;
sl@0: 
sl@0: 	};
sl@0: 
sl@0: const TStringTable ${name}::Table = {$total, KStringPointers, $folding};
sl@0: EOD