File: Strings2Resource.pl

package info (click to toggle)
pose 3.0a3-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,500 kB
  • ctags: 20,548
  • sloc: ansic: 72,579; cpp: 50,198; perl: 1,336; python: 1,242; sh: 363; makefile: 290
file content (195 lines) | stat: -rw-r--r-- 3,190 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# -*- mode: Perl; tab-width: 4 -*-

$started = 0;
$catenating = 0;
$id = "";
$text = "";
$language = "ENGLISH";

$rez_resource = 0;
$vcpp_resource = 0;
$cpp_source = 0;
$translate_high_ascii = 0;

$format = $ARGV[0];

if ($format eq "-format=rez_resource")
{
	$rez_resource = 1;
}

if ($format eq "-format=vcpp_resource")
{
	$vcpp_resource = 1;
	$translate_high_ascii = 1
}

if ($format eq "-format=cpp_source")
{
	$cpp_source = 1;
	$translate_high_ascii = 1
}


$stridx = 0;
$maxidx = 0;
%strkeys = ();
keys( %strkeys ) = 2000;

sub escape_quotes
{	
	if ($rez_resource or $cpp_source)
	{
		$text =~ s/\"/\\\"/g;		# Turn a " into \"
	}

	if ($vcpp_resource)
	{
		$text =~ s/\"/\"\"/g;		# Turn a " into ""
	}
}

sub translate_high_ascii
{
	if ($translate_high_ascii)
	{
		$text =~ s//\"/g;			# Turn  into "
		$text =~ s//\"/g;			# Turn  into "
		$text =~ s//\'/g;			# Turn  into '
		$text =~ s//\'/g;			# Turn  into '
		$text =~ s//\.\.\./g;		# Turn  into ...
		$text =~ s//\(TM)/g;		# Turn  into (TM)
	}
}

sub dump_if_catenating
{
	if ($catenating eq 1)
	{
		translate_high_ascii ();
		escape_quotes ();

		if ($rez_resource)
		{
			print "\t$id, \"$text\";";
		}

		if ($vcpp_resource)
		{
			print "\t$id\t\"$text\"";
		}

		if ($cpp_source)
		{
			print "\t\"$text\",";
			$strkeys{$id} = $stridx++;
		}

		$catenating = 0;
	}
}


while (chomp ($line = <STDIN>) )
{
	if ($line eq "START")
	{
		$started = 1;

		if ($rez_resource)
		{
			printf "type \'STRA\' {\n";
			printf "	integer = \$\$CountOf(SomeArray);\n";
			printf "	wide array SomeArray {\n";
			printf "		integer;\n";
			printf "		wstring;\n";
			printf "	};\n";
			printf "};\n";
			printf "\n";
			printf "resource \'STRA\' (1000) {{\n";
		}

		if ($vcpp_resource)
		{
			print "STRINGTABLE DISCARDABLE\n";
			print "BEGIN\n";
		}

		if ($cpp_source)
		{
			print "#include <map>\n";
			print "static map<int, const char*> _ResStrMap;\n";
			print "static const char* _ResStrTable[] = {\n";
		}
	}
	elsif ($line eq "END")
	{
		$started = 0;

		if ($rez_resource)
		{
			print "}};\n\n";
		}

		if ($vcpp_resource)
		{
			print "END\n\n";
		}

		if ($cpp_source)
		{
			print "\t0\n};\n\n";
			print "static void _AddOneString (long id, long index)\n";
			print "{\n";
			print "\t_ResStrMap[id] = _ResStrTable[index];\n";
			print "}\n";
			print "\n";
			print "static void _ResStrTableInit (void)\n";
			print "{\n";
			foreach $k (sort keys %strkeys)
			{
				print "\t_AddOneString ($k, $strkeys{$k});\n";
			}
			print "}\n";
			print "\n";
			print "const char* _ResGetString(int idx)\n";
			print "{\n";
			print "\tstatic int inited;\n";
			print "\tif (!inited)\n";
			print "\t{\n";
			print "\t\tinited = 1;\n";
			print "\t\t_ResStrTableInit();\n";
			print "\t}\n";
			print "\n";
			print "\treturn _ResStrMap[idx];\n";
			print "}\n";
		}
	}
	elsif ($started && $line =~ /^([A-Z]+)=(.+)/)
	{
		dump_if_catenating ();

		if ($1 eq "ID")
		{
			$id = $2;
		}
		elsif ($1 eq $language)
		{
			$text = $2;
			$catenating = 1;
		}
	}
	else
	{
		if ($catenating && substr ($line, 0, 1) eq "\t")
		{
			$text .= substr ($line, 1);
		}
		else
		{
			dump_if_catenating ();

			print "$line\n";
		}
	}
}