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 .
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++
43 with
open(infile_name
) as infile
:
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":
53 cur_id
= cur_id
.replace('-', '_')
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
));
60 idfile
.write("const sal_Int32 XML_TOKEN_COUNT = {};\n".format(token_count
))
61 gperffile
.write("%%\n")
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
:
74 d
= d
.replace(b
'\r', b
'')
75 with
open(fname
, 'wb') as ofile
:
78 fix_linefeeds(idfile_out_name
)
79 fix_linefeeds(namefile_out_name
)
80 fix_linefeeds(gperffile_out_name
)