os/ossrv/lowlevellibsandfws/apputils/stringtools/stringtable.pl
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
# Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
# All rights reserved.
sl@0
     3
# This component and the accompanying materials are made available
sl@0
     4
# under the terms of "Eclipse Public License v1.0"
sl@0
     5
# which accompanies this distribution, and is available
sl@0
     6
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
#
sl@0
     8
# Initial Contributors:
sl@0
     9
# Nokia Corporation - initial contribution.
sl@0
    10
#
sl@0
    11
# Contributors:
sl@0
    12
#
sl@0
    13
# Description:
sl@0
    14
# stringtable.pl - Makes a string table .cpp and .h file from a .st file
sl@0
    15
# This tool takes a valid .st file as an input parameter and converts it into a .h and a .cpp file
sl@0
    16
# 
sl@0
    17
#
sl@0
    18
sl@0
    19
use strict;
sl@0
    20
sl@0
    21
# Removes the extension from the filename
sl@0
    22
$ARGV[0] =~ m/(.*)\..*/;
sl@0
    23
my $root = $1;
sl@0
    24
$root =~ m/(.*[\\\/])([^\\\/]*)$/;
sl@0
    25
sl@0
    26
my $path_to_file = ("$1");
sl@0
    27
my $filename = lc("$2");
sl@0
    28
my $header_to_include = lc("$filename.h");
sl@0
    29
sl@0
    30
# Open the input filename
sl@0
    31
open (IN, "<$ARGV[0]" ) or die ("Error: $ARGV[0] $!");
sl@0
    32
sl@0
    33
# Parse header comments
sl@0
    34
my $input;
sl@0
    35
my @comments;
sl@0
    36
while ($input = <IN>) {
sl@0
    37
	# Comments starting with '#' not to be included in header file
sl@0
    38
	next if ($input =~ /^\s*\#/);  
sl@0
    39
sl@0
    40
	chomp($input);
sl@0
    41
sl@0
    42
	# Save comments containing '!' or stop looking if something else is found
sl@0
    43
	if ($input =~ /\!(.*)/) {
sl@0
    44
		push(@comments, "$1\n");
sl@0
    45
  	} else {
sl@0
    46
    		last;
sl@0
    47
  	}
sl@0
    48
}
sl@0
    49
sl@0
    50
my $folding;
sl@0
    51
# Check that line is of the right format and get the table name.
sl@0
    52
$input =~ m/(f*stringtable) ([A-Za-z0-9_]+)/ or die ("Error: $ARGV[0] is not a stringtable file");
sl@0
    53
my $name = $2;
sl@0
    54
if ($1 eq "stringtable") {
sl@0
    55
	$folding = "ETrue";
sl@0
    56
	} else {
sl@0
    57
	$folding = "EFalse";
sl@0
    58
	}
sl@0
    59
sl@0
    60
# Open the output filenames
sl@0
    61
open (CPP, ">"."$path_to_file"."$filename".".cpp" ) or die ("Error: $ARGV[0] Can't open cpp file");
sl@0
    62
open (HEADER, ">"."$path_to_file"."$filename".".h" ) or die ("Error: $ARGV[0] Can't open header file");
sl@0
    63
sl@0
    64
my $sourcename = $ARGV[0];
sl@0
    65
$sourcename =~ s/^(.*epoc32)/epoc32/i;
sl@0
    66
sl@0
    67
# Output the preambles
sl@0
    68
print CPP <<EOD;
sl@0
    69
// Autogenerated from $sourcename by the stringtable tool - Do not edit
sl@0
    70
#include <e32std.h>
sl@0
    71
#include <stringpool.h>
sl@0
    72
#include <stringtablesupport.h>
sl@0
    73
#include "$header_to_include"
sl@0
    74
#ifdef _DEBUG
sl@0
    75
#undef _DEBUG
sl@0
    76
#endif
sl@0
    77
sl@0
    78
EOD
sl@0
    79
sl@0
    80
print HEADER <<EOD;
sl@0
    81
// Autogenerated from $sourcename by the stringtable tool - Do not edit
sl@0
    82
sl@0
    83
#ifndef STRINGTABLE_$name
sl@0
    84
#define STRINGTABLE_$name
sl@0
    85
sl@0
    86
#include <stringpool.h>
sl@0
    87
sl@0
    88
struct TStringTable;
sl@0
    89
sl@0
    90
EOD
sl@0
    91
sl@0
    92
# If there are no header comments, use the default comment, otherwise, add the header comments
sl@0
    93
if (@comments == 0) {
sl@0
    94
	print HEADER "/** A String table */\n";
sl@0
    95
	} else {
sl@0
    96
	print HEADER @comments;
sl@0
    97
	}
sl@0
    98
sl@0
    99
print HEADER <<EOD;
sl@0
   100
class $name 
sl@0
   101
	{
sl@0
   102
public:
sl@0
   103
	enum TStrings
sl@0
   104
		{
sl@0
   105
EOD
sl@0
   106
sl@0
   107
my $count = 0;
sl@0
   108
my $outputcomma = 0;
sl@0
   109
my @commentlines;
sl@0
   110
sl@0
   111
#Now process file
sl@0
   112
until (eof IN) {
sl@0
   113
    # Treat lines with a '#' as the first character as comments and skip them, ignoring 0 or more whitespaces before it.
sl@0
   114
    # If # is not the first character, then treat them as part of the string.
sl@0
   115
    do 
sl@0
   116
      {
sl@0
   117
      $input = readline (*IN);
sl@0
   118
	    chomp($input);
sl@0
   119
      } while ($input =~ m/^\s*\#/);
sl@0
   120
sl@0
   121
	if ($input =~ m/\!(.*)/) {
sl@0
   122
		# Save lines that start with '!' to print later
sl@0
   123
		push(@commentlines, "\t\t$1\n");
sl@0
   124
	} else {
sl@0
   125
		# Parse the line
sl@0
   126
		$input =~ m/([A-Za-z0-9]+)\s*(.*)/ or die ("Error: $ARGV[0] badly formed at \"$input\"");
sl@0
   127
sl@0
   128
		# If a comma needs to be printed, print it
sl@0
   129
		if ($outputcomma == 1) {
sl@0
   130
			print HEADER ",\n";
sl@0
   131
			$outputcomma = 0;
sl@0
   132
		}
sl@0
   133
		# Print pending comments and clear the comments array
sl@0
   134
		if (@commentlines > 0) {
sl@0
   135
			print HEADER @commentlines;
sl@0
   136
			@commentlines = '';
sl@0
   137
		}
sl@0
   138
sl@0
   139
		# Form a version of the string that can go in a comment. This
sl@0
   140
		# can't contain /* or */
sl@0
   141
		my $enum = $1;
sl@0
   142
		my $string = $2;
sl@0
   143
		$string =~ s/\s+$//;	# remove any trailing whitespace
sl@0
   144
		my $comment = $string;
sl@0
   145
		$comment =~ s|/\*|/ \*|g;
sl@0
   146
		$comment =~ s|\*/|\* /|g;
sl@0
   147
sl@0
   148
		# Increment the count
sl@0
   149
		$count++;
sl@0
   150
		print HEADER "\t\t/** $comment */\n";
sl@0
   151
		print HEADER "\t\t$enum";
sl@0
   152
		print CPP "_STLIT8(K$count, \"$string\");\n";
sl@0
   153
		$outputcomma = 1;
sl@0
   154
	}
sl@0
   155
}
sl@0
   156
sl@0
   157
# Print closing comments
sl@0
   158
if (@commentlines > 0) {
sl@0
   159
	print HEADER "\n";
sl@0
   160
	print HEADER @commentlines;
sl@0
   161
	@commentlines = '';
sl@0
   162
} else {
sl@0
   163
	print HEADER "\n";
sl@0
   164
}
sl@0
   165
sl@0
   166
#Output intermediate boilerplate in the cpp
sl@0
   167
print CPP <<EOD;
sl@0
   168
sl@0
   169
// Intermediate
sl@0
   170
const void * const KStringPointers[] =
sl@0
   171
	{
sl@0
   172
EOD
sl@0
   173
sl@0
   174
# And the end of the headers
sl@0
   175
print HEADER <<EOD;
sl@0
   176
		};
sl@0
   177
	static const TStringTable Table;	
sl@0
   178
	};
sl@0
   179
sl@0
   180
#endif // STRINGTABLE_$name
sl@0
   181
sl@0
   182
EOD
sl@0
   183
sl@0
   184
#Output the table of pointers to the cpp file
sl@0
   185
my $total = $count;
sl@0
   186
sl@0
   187
for ($count = 1; $count <= $total ; $count++) {
sl@0
   188
    if ($count > 1) {
sl@0
   189
	print CPP ",\n";
sl@0
   190
    }
sl@0
   191
    print CPP "\t(const void*)&K$count";
sl@0
   192
};
sl@0
   193
sl@0
   194
#The last of the boilerplate
sl@0
   195
print CPP <<EOD;
sl@0
   196
sl@0
   197
	};
sl@0
   198
sl@0
   199
const TStringTable ${name}::Table = {$total, KStringPointers, $folding};
sl@0
   200
EOD