summaryrefslogtreecommitdiffstats
path: root/lib/kformula/scripts/oper-dict.py
blob: 70f7868c905766c455cff16de4596c9f00689021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#! /usr/bin/env python

"""This file is part of the KDE project
   Copyright (C) 2006 Alfredo Beaumont Sainz <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
 
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
 
   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
"""
import codecs
import sys
import string
import time
import os

###
# If operator element's attributes change in the future, just update this list
# and the attr dictionary below
attr_list = [
	'lspace',
	'rspace',
	'maxsize',
	'minsize',
	'fence',
	'separator',
	'stretchy',
	'symmetric',
	'largeop',
	'movablelimits',
	'accent'
	]
	

def write_header( f ):
	print('''//
// Created: ''' + time.ctime(time.time()) + '''
//      by: ''' + os.path.basename( sys.argv[0] ) + '''
//    from: ''' + os.path.basename( sys.argv[1] ) + '''
//
// WARNING! All changes made in this file will be lost!

/* This file is part of the KDE project
   Copyright (C) 2006 Alfredo Beaumont Sainz <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
 
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
 
   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/
''', file=f)

def write_h( f ):
	print('''
#ifndef OPERATORDICTIONARY_H
#define OPERATORDICTIONARY_H

#include "kformuladefs.h"

KFORMULA_NAMESPACE_BEGIN
	
struct DictionaryKey
{
    int operator==( const DictionaryKey& right ) const {
        if ( qstrcmp( name, right.name ) || qstrcmp( form, right.form ) ) {
            return false;
        }
        return true;
    }
    const char* name;
    const char* form;
};

struct OperatorDictionary {
    static int size();
    int operator<( const DictionaryKey& right ) const {
        int equal = qstrcmp( key.name, right.name );
        if ( equal != 0 ) {
            return equal < 0;
        }
        return qstrcmp( key.form, right.form ) < 0;
    }
    const DictionaryKey key;
	const char* lspace;
	const char* rspace;
	const char* maxsize;
	const char* minsize;
	bool fence;
	bool separator;
	bool stretchy;
	bool symmetric;
	bool largeop;
	bool movablelimits;
	bool accent;
};
	
extern const OperatorDictionary operators[];

KFORMULA_NAMESPACE_END

#endif // OPERATORDICTIONARY_H
''', file=f)

def write_cc( fr, fw ):
	print('''
#include "operatordictionary.h"

KFORMULA_NAMESPACE_BEGIN

const OperatorDictionary operators[] = {''', file=fw)

	entities = get_entities()
	parse( fr, fw, entities )
	
	print('''
};

// Needed since sizeof is a macro and we cannot be used until size is known
int OperatorDictionary::size()
{
    return sizeof( operators ) / sizeof( OperatorDictionary );
}

KFORMULA_NAMESPACE_END
	''', file=fw)

def get_entities():
	# First, read entity list into a dict
	fd = open( 'entity.list' )
	entities = {}
	for line in fd.readlines():
		fields = line.split()
		entities[fields[0]] = string.atoi( fields[1], 16 )
	fd.close()
	return entities

def key_cmp( a, b ):

	if a[0] < b[0]:
		return -1
	if a[0] > b[0]:
		return 1

	if a[1] < b[1]:
		return -1
	if a[1] > b[1]:
		return 1
	print('WARNING: Same key in operator dictionary: ' + a[0] + ', ' + b[0])
	return 0

def parse( fr, fw, entities ):
	entries = []
	line = fr.readline()
	while line != "":
		if line[0] == '"':
			###
			# If operator element's attributes or default values change in the future,
			# just update this dictionary and the list at the beginning of the file
			attr_dict = {
				attr_list[0]: '"thickmathspace"',
				attr_list[1]: '"thickmathspace"',
				attr_list[2]: '"infinity"',
				attr_list[3]: '"1"',
				attr_list[4]: '"false"',
				attr_list[5]: '"false"',
				attr_list[6]: '"false"',
				attr_list[7]: '"true"',
				attr_list[8]: '"false"',
				attr_list[9]: '"false"',
				attr_list[10]: '"false"'
				}
			fields = line.split()
			name = string.replace( fields[0], '&amp;', '&' )
			fields.pop(0) # Remove name
			entities_found = True
			while True:
				begin = string.find( name, '&' )
				end = string.find( name, ';' )
				if begin == -1 or end == -1:
					break
				###
				# TODO: Support multicharacter entities, should also be supported by
				# application. The best solution would probably to map to a single
				# character provided by the font in the private area of Unicode
				entity_name = name[begin + 1:end]
				if entity_name in entities :
					name = name.replace( '&' + entity_name + ';', chr(entities[entity_name]));
				else:
					entities_found = False
					break
			if entities_found:
				form = string.split( fields[0], '=' )[1]
				fields.pop(0) # Remove form
				for f in fields:
					attr, value = string.split( f, '=' )
					if attr not in attr_dict :
						print('Unsupported attribute: ' + attr)
						print('If it is valid, update attribute dictionary')
						sys.exit(-1)
					# Spec has a typo, fix it
					if string.count( value, '"' ) == 3:
						value = value[:-1]
					attr_dict[attr] = value
				entries.append( [name, form, attr_dict] )
		line = fr.readline()
	entries.sort( key_cmp, None, True )

	while True:
		e = entries.pop()
		print('     { {' + e[0] + ', ' + e[1] + '},', file=fw)
		d = e[2]
		for a in attr_list:
			# Convert, at least, bool values
			value = d[a]
			if value == '"true"' or value == '"false"':
				value = string.strip( value, '"' )
			print('\t\t' + value, end=' ', file=fw)
			if a != attr_list[len(attr_list) - 1]:
				print(',', file=fw)
		print('}', end=' ', file=fw)
		if len( entries ) == 0:
			break
		print(',\n', file=fw)

if __name__ == '__main__':
	fh = open( '../operatordictionary.h', 'w' )
	write_header( fh )
	write_h( fh )
	fh.close()
	fcc = codecs.open( '../operatordictionary.cpp', 'w', 'utf-8' )
	write_header( fcc )
	fr = open( sys.argv[1] )
	write_cc( fr , fcc )
	fcc.close()
	fr.close()