xref: /aoo42x/main/sc/workben/celltrans/parse.py (revision a0428e9e)
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 localeNames.has_key(code):
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 not self.funcList.has_key(funcName):
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 = self.funcList.keys()
66        funcs.sort()
67        chars += "// pre instantiations of localized function names\n"
68        for func in funcs:
69            for item in self.funcList[func]:
70                chars += "static const sal_Unicode " + self.getLocaleFuncVarName(func, item) + "[] = {\n"
71                chars += "    "
72                isFirst = True
73                # Dump the UTF-16 bytes.
74                for uval in item[0]:
75                    if isFirst:
76                        isFirst = False
77                    else:
78                        chars += ", "
79                    chars += "0x%.4X"%uval
80
81                # Don't forget to null-terminate the string.
82                if not isFirst:
83                    chars += ", "
84                chars += "0x0000"
85
86                chars += "};\n"
87
88        # map item instantiations
89        chars += "\n"
90        chars += "static const TransItem p" + self.locale.capitalize() + "[] = {\n"
91        for func in funcs:
92            for item in self.funcList[func]:
93                chars += "    "
94                chars += "{%s, \"%s\", %s},\n"%(self.getLocaleFuncVarName(func, item),
95                                                getAscii(item[1]),
96                                                "oc"+func.capitalize())
97
98        chars += "    {NULL, NULL, ocNone}\n"
99        chars += "};\n\n"
100
101        # addToMap call
102        chars += "addToMap(%s, %s);\n"%(
103            "p"+self.locale.capitalize(), "a"+self.locale.capitalize())
104
105        return chars
106
107class Parser(object):
108
109    def __init__ (self, args):
110        # default input & output files.
111        self.infile = "./keywords_utf16.txt"
112        self.outfile = "../../source/core/tool/cellkeywords.inl"
113
114        if len(args) >= 2:
115            self.infile = args[1]
116        if len(args) >= 3:
117            self.outfile = args[2]
118
119    def getDByte (self):
120        # Assume little endian.
121        bh = ord(self.bytes[self.i])
122        bl = ord(self.bytes[self.i+1])
123        dbyte = bl*256 + bh
124        self.i += 2
125        return dbyte
126
127    def parseLine (self):
128        buf = []
129        while self.i < self.size:
130            dbyte = self.getDByte()
131            if dbyte == 0x000A:
132                break
133            buf.append(dbyte)
134        return buf
135
136    def dumpBuf (self, buf, linefeed=True):
137        for item in buf:
138            sys.stdout.write(chr(item))
139        if linefeed:
140            print ''
141
142    def parse (self):
143
144        file = open(self.infile, 'r')
145        self.bytes = file.read()
146        file.close()
147
148        self.size = len(self.bytes)
149        self.i = 0
150
151        localeList = []  # stores an array of locale data objects.
152        funcName = None
153        word = []
154        wordPair = []
155
156        while self.i < self.size:
157            dbyte = self.getDByte()
158            if dbyte == 0xFEFF and self.i == 2:
159                # unicode signature - ignore it.
160                pass
161            elif dbyte == 0x0024:
162                # $ - locale name
163                buf = self.parseLine()
164                locale = getAscii(buf)
165                localeList.append(LocaleData(locale))
166
167            elif dbyte == 0x0040:
168                # @ - function name
169                buf = self.parseLine()
170                funcName = getAscii(buf)
171
172            elif dbyte == 0x002C:
173                # , - comma separator
174                if len(word) > 0:
175                    wordPair.append(word)
176                    word = []
177            elif dbyte == 0x000A:
178                # linefeed
179                if len(word) > 0:
180                    wordPair.append(word)
181                    word = []
182                if len(wordPair) >= 2:
183                    localeList[-1].addKeywordMap(funcName, wordPair[0], wordPair[1])
184                wordPair = []
185            elif dbyte in [0x0009, 0x0020]:
186                # whitespace - ignore it.
187                pass
188            else:
189                word.append(dbyte)
190
191        chars = "// This file has been automatically generated.  Do not hand-edit this!\n"
192        for obj in localeList:
193            chars += "\n" + obj.dumpCode()
194
195        # Write to output file.
196        file = open(self.outfile, 'w')
197        file.write(chars)
198        file.close()
199
200if __name__=='__main__':
201    parser = Parser(sys.argv)
202    parser.parse()
203
204