PRODUCTNAME -> %PRODUCTNAME
[LibreOffice.git] / solenv / bin / generate-tokens.py
blob2dc3c3fd4cfc017fb250348509eaef62ed54fddb
1 # This file is part of the LibreOffice project.
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/.
7 # This file incorporates work covered by the following license notice:
9 # Licensed to the Apache Software Foundation (ASF) under one or more
10 # contributor license agreements. See the NOTICE file distributed
11 # with this work for additional information regarding copyright
12 # ownership. The ASF licenses this file to you under the Apache
13 # License, Version 2.0 (the "License"); you may not use this file
14 # except in compliance with the License. You may obtain a copy of
15 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 import sys, re
20 infile_name = sys.argv[1]
21 idfile_out_name = sys.argv[2]
22 namefile_out_name = sys.argv[3]
23 gperffile_out_name = sys.argv[4]
25 idfile = open(idfile_out_name, 'w')
26 namefile = open(namefile_out_name, 'w')
27 gperffile = open(gperffile_out_name, 'w')
29 gperffile.write("""%language=C++
30 %global-table
31 %null-strings
32 %struct-type
33 struct xmltoken {
34 const char *name;
35 sal_Int32 nToken;
38 """)
40 token_count = 0;
41 tokens = {}
43 with open(infile_name) as infile:
44 for line in infile:
45 line = line.strip()
46 # check for valid characters
47 if not re.match(r'[a-zA-Z0-9-_]+$', line):
48 sys.exit("Error: invalid character in token '{}'".format(line));
49 cur_id = "XML_" + line;
50 # we have two ids with similar names("cut-offs" and "cut_offs")
51 if cur_id == "XML_cut_offs":
52 cur_id = "cut_offs2";
53 cur_id = cur_id.replace('-', '_')
54 tokens[line] = cur_id
55 idfile.write("const sal_Int32 {} = {};\n".format(cur_id, token_count))
56 namefile.write("\"{}\",\n".format(line));
57 gperffile.write("{},{}\n".format(line, cur_id));
58 token_count += 1
60 idfile.write("const sal_Int32 XML_TOKEN_COUNT = {};\n".format(token_count))
61 gperffile.write("%%\n")
63 idfile.close()
64 namefile.close()
65 gperffile.close()
67 def fix_linefeeds(fname):
68 # Gperf requires LF newlines, not CRLF, even on Windows.
69 # Making this work on both Python 2 and 3 is difficult.
70 # When Python 2 is dropped, delete this and add
71 # newline = '\n' to the open() calls above.
72 with open(fname, 'rb') as ifile:
73 d = ifile.read()
74 d = d.replace(b'\r', b'')
75 with open(fname, 'wb') as ofile:
76 ofile.write(d)
78 fix_linefeeds(idfile_out_name)
79 fix_linefeeds(namefile_out_name)
80 fix_linefeeds(gperffile_out_name)