xref: /aoo42x/main/sc/workben/celltrans/parse.py (revision 7d9fa7c3)
1#!/usr/bin/env python
2#**************************************************************
3#
4#  Licensed to the Apache Software Foundation (ASF) under one
5#  or more contributor license agreements.  See the NOTICE file
6#  distributed with this work for additional information
7#  regarding copyright ownership.  The ASF licenses this file
8#  to you under the Apache License, Version 2.0 (the
9#  "License"); you may not use this file except in compliance
10#  with the License.  You may obtain a copy of the License at
11#
12#    http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing,
15#  software distributed under the License is distributed on an
16#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17#  KIND, either express or implied.  See the License for the
18#  specific language governing permissions and limitations
19#  under the License.
20#
21#**************************************************************
22
23import sys
24
25localeNames = {'fr': 'French', 'hu': 'Hungarian', 'de': 'German'}
26def getLocaleName (code):
27    global localeNames
28    if code in localeNames:
29        return localeNames[code]
30    else:
31        return "(unknown locale)"
32
33def getAscii (ords):
34    ascii = ''
35    for c in ords:
36        ascii += chr(c)
37    return ascii
38
39class LocaleData(object):
40    def __init__ (self, locale):
41        self.locale = locale
42        self.funcList = {}
43
44    def addKeywordMap (self, funcName, localeName, engName):
45        if funcName not in self.funcList:
46            self.funcList[funcName] = []
47
48        self.funcList[funcName].append([localeName, engName])
49
50    def getLocaleFuncVarName (self, func, pair):
51        return func.lower() + "_" + getAscii(pair[1]).lower() + "_" + self.locale
52
53    def dumpCode (self):
54        chars = ""
55
56        # locale output
57        chars += "// " + "-"*75 + "\n"
58        chars += "// %s language locale (automatically generated)\n"%getLocaleName(self.locale)
59        chars += "// " + "-"*75 + "\n"
60        chars += "static const Locale a" + self.locale.capitalize() + "(OUString::createFromAscii(\""
61        chars += self.locale
62        chars += "\"), OUString(), OUString());\n\n"
63
64        # pre instantiations of localized function names.
65        funcs = sorted(self.funcList.keys())
66        chars += "// pre instantiations of localized function names\n"
67        for func in funcs:
68            for item in self.funcList[func]:
69                chars += "static const sal_Unicode " + self.getLocaleFuncVarName(func, item) + "[] = {\n"
70                chars += "    "
71                isFirst = True
72                # Dump the UTF-16 bytes.
73                for uval in item[0]:
74                    if isFirst:
75                        isFirst = False
76                    else:
77                        chars += ", "
78                    chars += "0x%.4X"%uval
79
80                # Don't forget to null-terminate the string.
81                if not isFirst:
82                    chars += ", "
83                chars += "0x0000"
84
85                chars += "};\n"
86
87        # map item instantiations
88        chars += "\n"
89        chars += "static const TransItem p" + self.locale.capitalize() + "[] = {\n"
90        for func in funcs:
91            for item in self.funcList[func]:
92                chars += "    "
93                chars += "{%s, \"%s\", %s},\n"%(self.getLocaleFuncVarName(func, item),
94                                                getAscii(item[1]),
95                                                "oc"+func.capitalize())
96
97        chars += "    {NULL, NULL, ocNone}\n"
98        chars += "};\n\n"
99
100        # addToMap call
101        chars += "addToMap(%s, %s);\n"%(
102            "p"+self.locale.capitalize(), "a"+self.locale.capitalize())
103
104        return chars
105
106class Parser(object):
107
108    def __init__ (self, args):
109        # default input & output files.
110        self.infile = "./keywords_utf16.txt"
111        self.outfile = "../../source/core/tool/cellkeywords.inl"
112
113        if len(args) >= 2:
114            self.infile = args[1]
115        if len(args) >= 3:
116            self.outfile = args[2]
117
118    def getDByte (self):
119        # Assume little endian.
120        bh = ord(self.bytes[self.i])
121        bl = ord(self.bytes[self.i+1])
122        dbyte = bl*256 + bh
123        self.i += 2
124        return dbyte
125
126    def parseLine (self):
127        buf = []
128        while self.i < self.size:
129            dbyte = self.getDByte()
130            if dbyte == 0x000A:
131                break
132            buf.append(dbyte)
133        return buf
134
135    def dumpBuf (self, buf, linefeed=True):
136        for item in buf:
137            sys.stdout.write(chr(item))
138        if linefeed:
139            print('')
140
141    def parse (self):
142
143        file = open(self.infile, 'r')
144        self.bytes = file.read()
145        file.close()
146
147        self.size = len(self.bytes)
148        self.i = 0
149
150        localeList = []  # stores an array of locale data objects.
151        funcName = None
152        word = []
153        wordPair = []
154
155        while self.i < self.size:
156            dbyte = self.getDByte()
157            if dbyte == 0xFEFF and self.i == 2:
158                # unicode signature - ignore it.
159                pass
160            elif dbyte == 0x0024:
161                # $ - locale name
162                buf = self.parseLine()
163                locale = getAscii(buf)
164                localeList.append(LocaleData(locale))
165
166            elif dbyte == 0x0040:
167                # @ - function name
168                buf = self.parseLine()
169                funcName = getAscii(buf)
170
171            elif dbyte == 0x002C:
172                # , - comma separator
173                if len(word) > 0:
174                    wordPair.append(word)
175                    word = []
176            elif dbyte == 0x000A:
177                # linefeed
178                if len(word) > 0:
179                    wordPair.append(word)
180                    word = []
181                if len(wordPair) >= 2:
182                    localeList[-1].addKeywordMap(funcName, wordPair[0], wordPair[1])
183                wordPair = []
184            elif dbyte in [0x0009, 0x0020]:
185                # whitespace - ignore it.
186                pass
187            else:
188                word.append(dbyte)
189
190        chars = "// This file has been automatically generated.  Do not hand-edit this!\n"
191        for obj in localeList:
192            chars += "\n" + obj.dumpCode()
193
194        # Write to output file.
195        file = open(self.outfile, 'w')
196        file.write(chars)
197        file.close()
198
199if __name__=='__main__':
200    parser = Parser(sys.argv)
201    parser.parse()
202