3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 # This file incorporates work covered by the following license notice:
11 # Licensed to the Apache Software Foundation (ASF) under one or more
12 # contributor license agreements. See the NOTICE file distributed
13 # with this work for additional information regarding copyright
14 # ownership. The ASF licenses this file to you under the Apache
15 # License, Version 2.0 (the "License"); you may not use this file
16 # except in compliance with the License. You may obtain a copy of
17 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 localeNames
= {'fr': 'French', 'hu': 'Hungarian', 'de': 'German'}
23 def getLocaleName (code
):
25 if code
in localeNames
:
26 return localeNames
[code
]
28 return "(unknown locale)"
36 class LocaleData(object):
37 def __init__ (self
, locale
):
41 def addKeywordMap (self
, funcName
, localeName
, engName
):
42 if funcName
not in self
.funcList
:
43 self
.funcList
[funcName
] = []
45 self
.funcList
[funcName
].append([localeName
, engName
])
47 def getLocaleFuncVarName (self
, func
, pair
):
48 return func
.lower() + "_" + getAscii(pair
[1]).lower() + "_" + self
.locale
54 chars
+= "// " + "-"*75 + "\n"
55 chars
+= "// %s language locale (automatically generated)\n"%getLocaleName
(self
.locale
)
56 chars
+= "// " + "-"*75 + "\n"
57 chars
+= "static const Locale a" + self
.locale
.capitalize() + "(OUString(\""
59 chars
+= "\"), OUString(), OUString());\n\n"
61 # pre instantiations of localized function names.
62 funcs
= sorted(self
.funcList
.keys())
63 chars
+= "// pre instantiations of localized function names\n"
65 for item
in self
.funcList
[func
]:
66 chars
+= "static const sal_Unicode " + self
.getLocaleFuncVarName(func
, item
) + "[] = {\n"
69 # Dump the UTF-16 bytes.
75 chars
+= "0x%.4X"%uval
77 # Don't forget to null-terminate the string.
84 # map item instantiations
86 chars
+= "static const TransItem p" + self
.locale
.capitalize() + "[] = {\n"
88 for item
in self
.funcList
[func
]:
90 chars
+= "{%s, \"%s\", %s},\n"%(self
.getLocaleFuncVarName(func
, item
),
92 "oc"+func
.capitalize())
94 chars
+= " {NULL, NULL, ocNone}\n"
98 chars
+= "addToMap(%s, %s);\n"%(
99 "p"+self
.locale
.capitalize(), "a"+self
.locale
.capitalize())
103 class Parser(object):
105 def __init__ (self
, args
):
106 # default input & output files.
107 self
.infile
= "./keywords_utf16.txt"
108 self
.outfile
= "../../source/core/tool/cellkeywords.inl"
111 self
.infile
= args
[1]
113 self
.outfile
= args
[2]
116 # Assume little endian.
117 bh
= self
.bytes
[self
.i
]
118 bl
= self
.bytes
[self
.i
+1]
120 dbyte
= ord(bl
)*256 + ord(bh
)
126 def parseLine (self
):
128 while self
.i
< self
.size
:
129 dbyte
= self
.getDByte()
135 def dumpBuf (self
, buf
, linefeed
=True):
137 sys
.stdout
.write(chr(item
))
143 file = open(self
.infile
, 'rb')
144 self
.bytes
= file.read()
147 self
.size
= len(self
.bytes
)
150 localeList
= [] # stores an array of locale data objects.
155 while self
.i
< self
.size
:
156 dbyte
= self
.getDByte()
157 if dbyte
== 0xFEFF and self
.i
== 2:
158 # unicode signature - ignore it.
160 elif dbyte
== 0x0024:
162 buf
= self
.parseLine()
163 locale
= getAscii(buf
)
164 localeList
.append(LocaleData(locale
))
166 elif dbyte
== 0x0040:
168 buf
= self
.parseLine()
169 funcName
= getAscii(buf
)
171 elif dbyte
== 0x002C:
172 # , - comma separator
174 wordPair
.append(word
)
176 elif dbyte
== 0x000A:
179 wordPair
.append(word
)
181 if len(wordPair
) >= 2:
182 localeList
[-1].addKeywordMap(funcName
, wordPair
[0], wordPair
[1])
184 elif dbyte
in [0x0009, 0x0020]:
185 # whitespace - ignore it.
190 chars
= "// This file has been automatically generated. Do not hand-edit this!\n"
191 for obj
in localeList
:
192 chars
+= "\n" + obj
.dumpCode()
194 # Write to output file.
195 file = open(self
.outfile
, 'w')
199 if __name__
=='__main__':
200 parser
= Parser(sys
.argv
)