summaryrefslogtreecommitdiffstats
path: root/wlite/to_c.c
blob: acebab8e6f976e636426ad5e54e5801a9d9f97f9 (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
/*
 * $Id$
 *
 * Copyright (C) 2003  Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Original Author: Adrian Havill <havill@redhat.com>
 *
 * Contributors:
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#include "to_c.h"

const char *const arrayfmt = "const %s %s[%zu] = {";
const char *const arraynfmt = "const size_t %s_n = %zu;\n";

void set_bit(wchar_t c, wlite_bitarray_t_ *bits, int bit) {
    size_t i;
    int j;

    if (c <= WLITE_WCHAR_MAX) {
        i = c / WLITE_BITARRAY_N_;
        j = c % WLITE_BITARRAY_N_;
        if (bit) {
            bits[i] |= ((wlite_bitarray_t_) 1 << j);
        }
        else bits[i] &= ~((wlite_bitarray_t_) 1 << j);
    }
}

void add_map(wchar_t c, wchar_t to, wlite_map_t_ *map[], size_t *n) {
    if (c > WLITE_WCHAR_MAX) {
        fprintf(stderr, "ignoring high char: %lX\n", c);
        return;
    }
    if (to > WLITE_WCHAR_MAX) {
        fprintf(stderr, "ignoring high char: %lX\n", to);
        return;
    }
    *map = realloc(*map, (*n + 1) * sizeof(wlite_map_t_));
    if (*map == NULL) {
        perror(NULL);
        exit(EXIT_FAILURE);
    }
    (*map)[*n].from = c;
    (*map)[*n].to = to;
    ++*n;
}

void add_wc(wchar_t c, wlite_wc_t_ *wide[], size_t *n) {
    if (c > WLITE_WCHAR_MAX) {
        fprintf(stderr, "ignoring high char: %lX\n", (unsigned long) c);
        return;
    }
    *wide = realloc(*wide, (*n + 1) * sizeof(wlite_wc_t_));
    if (*wide == NULL) {
        perror(NULL);
        exit(EXIT_FAILURE);
    }
    (*wide)[*n] = (wlite_wc_t_) c;
    *n += 1;
}

void print_bits(FILE *stream, const wlite_bitarray_t_ *bits, const char *name) {
    size_t x;
    const size_t max = (WLITE_WCHAR_MAX + 1) / WLITE_BITARRAY_N_;
    const size_t hex_per_line = 6;

    fprintf(stream, arrayfmt, WLITE_ID2STR_(wlite_bitarray_t_), name, max);
    fputc('\n', stream);
    for (x = 0; x < max; x++) {
        if (x % hex_per_line == 0)
            fputs("  ", stream);
        fprintf(stream, "%0#10lx", (unsigned long) bits[x]);
        if (x + 1 < max)
            fputc(',', stream);
        if (((x + 1) % hex_per_line) == 0) {
            unsigned long bit_1 = (x - hex_per_line + 1) * WLITE_BITARRAY_N_;
#if WLITE_XBMP_CHAR
            const char *const fmt = " //U-%06lX\n";
#else
            const char *const fmt = " //U+%04lX\n";
#endif
            fprintf(stream, fmt, bit_1);
        }
    }
    if (x % hex_per_line != 0)
        fputc('\n', stream);
    fputs("};\n\n", stream);
}

void print_maps(FILE *stream, wlite_map_t_ map[], const char *name, size_t n) {
    size_t i;
#if WLITE_XBMP_CHAR
    const char *const fmt = "{ %0#8lx, %0#8lx }";
    const char *const cmt = "\\U%08lX";
#else
    const char *const fmt = "{ %0#6lx, %0#6lx }";
    const char *const cmt = "  \\u%04lX  ";
#endif

    qsort(map, n, sizeof(wlite_map_t_), wlite_map_t_cmp_);
    fprintf(stream, arraynfmt, name, n);
    fprintf(stream, arrayfmt, WLITE_ID2STR_(wlite_map_t_), name, n);
    for (i = 0; i < n; i++) {
        unsigned long from = (unsigned long) map[i].from;
        unsigned long to = (unsigned long) map[i].to;

        fputs("\n    ", stream);
        fprintf(stream, fmt, from, to);
        fputs(i + 1 < n ? "," : " ", stream);
        if (fprintf(stream, " // (%10lc", (wint_t) from) < 0) {
            if (errno == EILSEQ) {
                fprintf(stream, cmt, from);
                errno = 0;
            }
            else {
                perror(NULL);
                exit(EXIT_FAILURE);
            }
        }
        if (fprintf(stream, "->%-10lc)", (wint_t) to) < 0) {
            if (errno == EILSEQ) {
                fprintf(stream, cmt, to);
                fputc(')', stream);
                errno = 0;
            }
            else {
                perror(NULL);
                exit(EXIT_FAILURE);
            }
        }
    }
    fputs("\n};\n\n", stream);
}

void print_wcs(FILE *stream, wlite_wc_t_ *wide, const char *name, size_t n) {
    size_t i;
    const size_t hex_per_line = 8;
    const char* const type = WLITE_ID2STR_(wlite_wc_t_);

    qsort(wide, n, sizeof(wlite_wc_t_), wlite_wc_t_cmp_);
    fprintf(stream, arraynfmt, name, n);
    fprintf(stream, arrayfmt, type, name, n);
    for (i = 0; i < n; i++) {
        unsigned long wc = (unsigned long) wide[i];

        if (i % hex_per_line == 0)
            fputs("\n    ", stream);
#if WLITE_XBMP_CHAR
            fprintf(stream, "%0#8lx", wc);
#else
            fprintf(stream, "%0#6lx", wc);
#endif
        if (i + 1 < n)
            fputc(',', stream);
    }
    if (i % hex_per_line != 0)
        fputc('\n', stream);
    fputs("};\n\n", stream);
}