forked from xiaoyifang/goldendict-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.cc
199 lines (148 loc) · 5.28 KB
/
generate.cc
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
/* This file is (c) 2008-2009 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
// This program generates the required .cc files to perform Unicode foldings,
// parsing the Unicode standards' text files.
// Normally, we issue rebuilds manually in case either the generator or
// the data files were updated to avoid the hurdles an end user might get
// into while compiling the program.
#include <stdio.h>
#include <map>
#include <string>
using std::map;
using std::u32string;
struct Node
{
map< char32_t, Node > nodes;
char32_t tail;
Node():
tail( 0 )
{
}
};
void indent( FILE * outf, size_t steps )
{
for ( unsigned x = steps; x--; )
fprintf( outf, " " );
}
void handleForest( FILE * outf, map< char32_t, Node > const & forest, size_t prev, size_t steps )
{
indent( outf, steps );
fprintf( outf, "if ( size > %u )\n", prev );
indent( outf, steps );
fprintf( outf, "{\n" );
indent( outf, steps + 1 );
fprintf( outf, "switch( in[ %u ] )\n", prev );
indent( outf, steps + 1 );
fprintf( outf, "{\n" );
for ( map< char32_t, Node >::const_iterator i = forest.begin(); i != forest.end(); ++i ) {
indent( outf, steps + 2 );
fprintf( outf, "case 0x%x:\n", i->first );
if ( i->second.nodes.size() )
handleForest( outf, i->second.nodes, prev + 1, steps + 3 );
indent( outf, steps + 3 );
if ( i->second.tail )
fprintf( outf, "consumed = %u; return 0x%x;\n", prev + 1, i->second.tail );
else
fprintf( outf, "consumed = 1; return *in;\n" );
}
indent( outf, steps + 1 );
fprintf( outf, "}\n" );
indent( outf, steps );
fprintf( outf, "}\n" );
}
int main()
{
// Case folding
{
FILE * inf = fopen( "CaseFolding.txt", "r" );
if ( !inf ) {
fprintf( stderr, "Failed to open CaseFolding.txt\n" );
return 1;
}
char buf[ 4096 ];
map< char32_t, u32string > foldTable;
map< char32_t, char32_t > simpleFoldTable;
while ( fgets( buf, sizeof( buf ), inf ) ) {
if ( *buf == '#' )
continue; // A comment
unsigned long in, out[ 4 ];
char type;
unsigned totalOut;
if ( sscanf( buf, "%lx; %c; %lx %lx %lx %lx;", &in, &type, out, out + 1, out + 2, out + 3 ) == 6 ) {
fprintf( stderr,
"Four output chars ecountered in CaseFolding.txt, which we expected"
"the file didn't have, make changes into the program.\n" );
return 1;
}
if ( sscanf( buf, "%lx; %c; %lx %lx %lx;", &in, &type, out, out + 1, out + 2 ) == 5 )
totalOut = 3;
else if ( sscanf( buf, "%lx; %c; %lx %lx;", &in, &type, out, out + 1 ) == 4 )
totalOut = 2;
else if ( sscanf( buf, "%lx; %c; %lx;", &in, &type, out ) == 3 )
totalOut = 1;
else {
fprintf( stderr, "Erroneous input in CaseFolding.txt: %s\n", buf );
return 1;
}
switch ( type ) {
case 'C':
if ( totalOut != 1 ) {
fprintf( stderr, "C-record has more than one output char in CaseFolding.txt: %s\n", buf );
return 1;
}
simpleFoldTable[ in ] = out[ 0 ];
// fall-through
case 'F': {
u32string result( totalOut, 0 );
for ( unsigned x = 0; x < totalOut; ++x ) {
result[ x ] = out[ x ];
}
foldTable[ in ] = result;
} break;
case 'S': {
if ( totalOut != 1 ) {
fprintf( stderr, "S-record has more than one output char in CaseFolding.txt: %s\n", buf );
return 1;
}
simpleFoldTable[ in ] = out[ 0 ];
} break;
case 'T':
// Ignore special foldings
break;
default:
fprintf( stderr, "Unknown folding type encountered: %c\n", type );
return 1;
}
}
fclose( inf );
// Create an outfile
FILE * outf = fopen( "inc_case_folding.hh", "w" );
if ( !outf ) {
fprintf( stderr, "Failed to create outfile\n" );
return 1;
}
fprintf( outf, "// This file was generated automatically. Do not edit directly.\n\n" );
fprintf( outf, "#pragma once\n\n" );
fprintf( outf, "enum { foldCaseMaxOut = 3 };\n\n" );
fprintf( outf, "inline size_t foldCase( char32_t in, char32_t * out )\n{\n switch( in )\n {\n" );
for ( map< char32_t, u32string >::const_iterator i = foldTable.begin(); i != foldTable.end(); ++i ) {
if ( i->second.size() == 1 )
fprintf( outf, " case 0x%x: *out = 0x%x; return 1;\n", i->first, i->second[ 0 ] );
else {
fprintf( outf, " case 0x%x: ", i->first );
for ( unsigned x = 0; x < i->second.size(); ++x )
fprintf( outf, "out[ %u ] = 0x%x; ", x, i->second[ x ] );
fprintf( outf, "return %u;\n", i->second.size() );
}
}
fprintf( outf, " default: *out = in; return 1;\n" );
fprintf( outf, " }\n}\n\n" );
fprintf( outf, "char32_t foldCaseSimple( char32_t in )\n{\n switch( in )\n {\n" );
for ( map< char32_t, char32_t >::const_iterator i = simpleFoldTable.begin(); i != simpleFoldTable.end(); ++i )
fprintf( outf, " case 0x%x: return 0x%x;\n", i->first, i->second );
fprintf( outf, " default: return in;\n" );
fprintf( outf, " }\n}\n" );
fclose( outf );
}
return 0;
}