File: kmaptoqe.c

package info (click to toggle)
qemacs 0.3.1-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,040 kB
  • ctags: 3,226
  • sloc: ansic: 29,371; sh: 353; makefile: 338
file content (287 lines) | stat: -rw-r--r-- 7,243 bytes parent folder | download
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* convert yudit kmap files to qe binary internal format */

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

//#define TEST

#define NB_MAX 15000

typedef struct InputEntry {
    unsigned char input[10];
    int len;
    int output;
} InputEntry;

InputEntry inputs[NB_MAX];
int nb_inputs;
char kmap_names[300][128];
int kmap_offsets[300];
char name[128];
int table_start[NB_MAX], nb_starts;
int table_val[NB_MAX];
int is_chinese_cj, gen_table;
int freq[32];
FILE *outfile;
unsigned char outbuf[100000], *outbuf_ptr;

int sort_func(const void *a1, const void *b1)
{
    const InputEntry *a = a1;
    const InputEntry *b = b1;
    int val;

    if (is_chinese_cj) {
        val = a->input[0] - b->input[0];
        if (val != 0)
            return val;
    }
    return a->output - b->output;
}


int offset, write_data;

void put_byte(int b)
{
    if (write_data) 
        *outbuf_ptr++ = b;
    offset++;
}

void gen_map(void)
{
    int len, c, d, last, delta, k, j;
    int last_input0;

        last = 0;
        nb_starts = 0;
        last_input0 = 0;
        offset = 0;
        for(k=0;k<nb_inputs;k++) {
            unsigned char *input = inputs[k].input;
            int first, output;

            if (gen_table) {
                if (last_input0 != inputs[k].input[0]) {
                    if (last_input0 != 0)
                        put_byte(0);
                    last_input0 = inputs[k].input[0];
                    table_val[nb_starts] = last_input0;
                    table_start[nb_starts++] = offset;
                    last = 0;
                }
            }

            len = inputs[k].len;
            output = inputs[k].output;
            delta = output - last;
            last = output;

#ifdef TEST
            /* frequencies */
            if (delta > 30)
                delta = 30;
            else if (delta < -1)
                delta = -1;
            freq[delta + 1]++;
#endif

            if (is_chinese_cj) {
                assert(input[len-1] == ' ');
                len--;
            }

            first = 0;
            if (gen_table)
                first = 1;
            
            /* c = 0 : end of table 
               c = 1..0x1d : delta unicode
               c = 0x1e : unicode char follows.
               c = 0x1f : unicode mapping follows 
               bit 7 : eof and delta == 1
            */
            for(j=first;j<len;j++) {
                c = input[j];
                d = 0;
                if ((j == (len - 1) && delta == 1))
                    d = 0x80;
                if (c >= 0x20 && c <= 0x7f) {
                    put_byte(c | d);
                } else {
                    put_byte(d | 0x1f);
                    put_byte((c >> 8) & 0xff);
                    put_byte(c & 0xff);
                }
            }
            /* XXX: potential problem if first == len. We avoid it
               by forcing an emission of a unicode char */
            if (first == len)
                delta = 0;
            if (delta != 1) {
                if (delta >= 1 && delta <= 0x1d) {
                    put_byte(delta);
                } else {
                    put_byte(0x1e);
                    put_byte((output >> 8) & 0xff);
                    put_byte(output & 0xff);
                }
            }
        }
        put_byte(0);
}

int main(int argc, char **argv)
{
    char *filename;
    int i, j, k, col, line_num;
    FILE *f;
    char line[1024], *p;
    unsigned char *q;
    int c, size;
    int nb_kmaps;

    if (argc < 3) {
        printf("usage: kmaptoqe outfile kmaps...\n"
               "Convert yudit keyboard maps to qemacs compressed format\n");
        exit(1);
    }
    outfile = fopen(argv[1], "w");
    if (!outfile) {
        perror(argv[1]);
        exit(1);
    }
    outbuf_ptr = outbuf;

    memset(freq, 0, sizeof(freq));

    nb_kmaps = 0;
    for(i=2;i<argc;i++) {
        filename = argv[i];
        f = fopen(filename, "r");
        if (!f) {
            perror(filename);
            exit(1);
        }
        p = strrchr(filename, '/');
        if (!p)
            p = filename;
        else
            p++;
        q = name;
        while (*p != '.' && *p != '\0') {
            c = *p++;
            if (c == '-')
                c = '_';
            *q++ = c;
        }
        *q = '\0';
        strcpy(kmap_names[nb_kmaps], name);
        kmap_offsets[nb_kmaps] = outbuf_ptr - outbuf;
        nb_kmaps++;
        /* special compression for CJ */
        is_chinese_cj = !strcmp(name, "Chinese_CJ");
        gen_table = !strcmp(name, "Chinese_CJ") ||
            !strcmp(name, "TeX") || !strcmp(name, "SGML");

        col = 0;
        nb_inputs = 0;
        line_num = 0;
        for(;;) {
            if (fgets(line, sizeof(line), f) == NULL)
                break;
            line_num++;
            p = line;
            if (*p == '/')
                continue;
            while (*p != '\0' && *p != '\"') p++;
            if (*p != '\"')
                continue;
            p++;
            q = inputs[nb_inputs].input;
            for(;;) {
                while (isspace(*p))
                    p++;
                if (*p == '=') 
                    break;
                if (p[0] == '0' && p[1] == 'x') {
                    c = strtoul(p, &p, 0);
                } else {
                    c = *p++;
                }
                if (c >= 256) {
                    fprintf(stderr, "%s:%d: Invalid char %x %c\n", 
                            filename, line_num, c, c);
                }
                *q++ = c;
            }
            inputs[nb_inputs].len = q - inputs[nb_inputs].input;
            p++;
            inputs[nb_inputs].output = strtoul(p, NULL, 0);
            nb_inputs++;
        }

        qsort(inputs, nb_inputs, sizeof(InputEntry), sort_func);

        /* first pass to compute offsets */
        write_data = 0;
        gen_map();

        write_data = 1;

        /* write start map */
        put_byte(nb_starts | (is_chinese_cj ? 0x80 : 0));
        if (gen_table) {
            for(k=0;k<nb_starts;k++) {
                j = table_start[k];
                put_byte(table_val[k]);
                put_byte((j >> 16) & 0xff);
                put_byte((j >> 8) & 0xff);
                put_byte(j & 0xff);
            }
        }

        gen_map();

        fclose(f);
    }

#ifdef TEST
    for(k=0;k<32;k++) {
        fprintf(stderr, "freq[%d] = %d\n", k, freq[k]);
    }
#endif

    /* write header */
    size = 4;
    for(i=0;i<nb_kmaps;i++) {
        size += strlen(kmap_names[i]) + 1 + 4;
    }
    size += 4; /* last offset at zero */

    fwrite("kmap", 1, 4, outfile);

    for(i=0;i<nb_kmaps;i++) {
        int off;
        off = kmap_offsets[i] + size;
        fputc((off >> 24) & 0xff, outfile);
        fputc((off >> 16) & 0xff, outfile);
        fputc((off >> 8) & 0xff, outfile);
        fputc((off >> 0) & 0xff, outfile);
        fwrite(kmap_names[i], 1, strlen(kmap_names[i]) + 1, outfile);
    }
    fputc(0, outfile);
    fputc(0, outfile);
    fputc(0, outfile);
    fputc(0, outfile);
    
    fwrite(outbuf, 1, outbuf_ptr - outbuf, outfile);

    fclose(outfile);

    return 0;
}