LibreOffice Module filter (master) 1
gentoken.py
Go to the documentation of this file.
1# This file is part of the LibreOffice project.
2#
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6#
7
8import sys
9
10tokenfile_name = sys.argv[1]
11hxx_name = sys.argv[2]
12gperf_name = sys.argv[3]
13
14gperf_header = r"""%language=C++
15%global-table
16%null-strings
17%struct-type
18struct xmltoken
19{
20 const char *name; sal_Int32 nToken;
21}
22%%
23"""
24
25tokens = {}
26
27with open(tokenfile_name) as tokenfile:
28 for line in tokenfile:
29 line = line.strip()
30 if line:
31 arr = line.split()
32 if len(arr) < 2:
33 t = "XML_" + arr[0]
34 t = t.replace('-', '_').replace('.', '_').replace(':', '_')
35 t = t.replace('+', 'PLUS')
36 arr.append(t)
37 tokens[arr[0]] = arr[1].upper()
38
39hxx = open(hxx_name, 'w')
40gperf = open(gperf_name, 'w', newline='\n')
41
42gperf.write(gperf_header)
43
44hxx.write("#ifndef INCLUDED_AUTOGEN_TOKEN_HXX\n")
45hxx.write("#define INCLUDED_AUTOGEN_TOKEN_HXX\n\n")
46hxx.write("#include <sal/types.h>\n\n" )
47
48i = 0;
49for token in sorted(tokens.keys()):
50 i += 1;
51 hxx.write("const sal_Int32 {} = {};\n".format(tokens[token], i))
52 gperf.write("{},{}\n".format(token, tokens[token]))
53
54gperf.write("%%\n")
55hxx.write("const sal_Int32 XML_TOKEN_COUNT = {};\n".format(i))
56hxx.write("const sal_Int32 XML_TOKEN_INVALID = -1;\n\n")
57hxx.write("#endif\n")
58
59hxx.close()
60gperf.close()