summaryrefslogtreecommitdiffstats
path: root/lib/kformula/scripts/bynames.py
blob: afb9ece551ed0de31aaf244dde933a42e99df2dd (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
#! /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 sys
import string
import time
import os

def write_header( f ):
	print >> f, '''//
// 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.
*/
'''

def write_h( f ):
	print >>f, '''
#ifndef ENTITIES_H
#define ENTITIES_H

#include "kformuladefs.h"

KFORMULA_NAMESPACE_BEGIN
	
struct entityMap {
    static int size();
    int operator<( const char* right ) const {
	    return qstrcmp( name, right ) < 0;
    }
	const char* name;
	const uint unicode;
};
	
extern const entityMap entities[];

KFORMULA_NAMESPACE_END

#endif // ENTITIES_H
'''

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

KFORMULA_NAMESPACE_BEGIN

const entityMap entities[] = {'''

	parse( fr, fw )
	
	print >> fw, '''
};

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

KFORMULA_NAMESPACE_END
	'''
	
def name_cmp( a, b ):

	if a[0] < b[0]:
		return -1
	if a[0] > b[0]:
		return 1
	print 'WARNING: Same name in entity: ' + a[0] + ', ' + b[0]
	return 0;

def parse( fr, fw ):
	line = fr.readline()
	while line != "" and string.find( line, '<pre>' ) == -1:
		line = fr.readline()
	pos = string.find( line, '<pre>' ) ### Ad-hoc detection
	if pos == -1:
		return
	line = line[pos + len('<pre>'):].strip() ### Ad-hoc detection
	entries = []
	while line != "" and string.find( line, ',' ) != -1:
		fields = line.split(',')
		name = fields[0].strip()
		number = fields[1].strip()
		###
		# 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
		if string.find( number, '-' ) == -1:
			entries.append( [name, '0x' + number[1:]] )
		line = fr.readline().strip()

	entries.sort( name_cmp, None, True )
	fd_list = open( 'entity.list', 'w' )
	while True:
		e = entries.pop()
		fd_list.write( e[0] + ' ' + e[1] + '\n')
		print >> fw, '     {"' + e[0] + '", ' + e[1] + '}',
		if len( entries ) == 0:
			break
		print >> fw, ','
	fd_list.close()
	
if __name__ == '__main__':
	fh = open( '../entities.h', 'w' )
	write_header( fh )
	write_h( fh )
	fh.close()
	fcc = open( '../entities.cc', 'w' )
	write_header( fcc )
	fr = open( sys.argv[1] )
	write_cc( fr , fcc )
	fcc.close()
	fr.close()