summaryrefslogtreecommitdiffstats
path: root/wlite/to_c-UnicodeData.c
blob: d9e1a00608d7f2e39a0dea7f8fa9003ab4ee55b3 (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
/*
 * $Id$
 *
 * Copyright (C) 2002  Red Hat, Inc.
 *
 * This program 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 program 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 program; 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 <locale.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "to_c.h"

char *get_field(const char *line, unsigned n) {
    const char *semi;
    char *field = NULL;
    size_t length;
    unsigned i;

    for (i = 0, semi = line; i < n; i++, semi++) {
        if ((semi = strchr(semi, ';')) == NULL) {
            fprintf(stderr, "can't get field #%u from '%s'\n", n, line);
            break;
        }
    }
    length = strcspn(semi, ";");
    if ((field = malloc(length + 1)) == NULL) {
        perror(NULL);
        exit(EXIT_FAILURE);
    }
    strncpy(field, semi, length);
    field[length] = '\0';
    return field;
}

int main(int argc, char **argv) {
    wlite_wc_t_ *wlite_punct = NULL; size_t punctn = 0;
    wlite_map_t_ *wlite_fixw = NULL; size_t fixw_n = 0;

    if (setlocale(LC_CTYPE, "") == NULL) {
        fputs("couldn't set locale for LC_CTYPE\n", stderr);
    }
    while (!feof(stdin)) {
        unsigned long codepoint, decomp;
        char line[512] = { 0 }, *comment, *field;

        if (fgets(line, sizeof(line), stdin) == NULL) {
            if (ferror(stdin)) {
                perror(NULL);
                exit(EXIT_FAILURE);
            }
            break;
        }
        if ((comment = strchr(line, '#')) != NULL) {
            strcpy(comment, "\n");
        }

        /* get character in question */

        if (sscanf(line, " %lX ;", &codepoint) != 1) continue;

        /* get character category */

        if ((field = get_field(line, 2)) != NULL) {
            if (field[0] == 'P') {
                fprintf(stderr, "adding punctuation U+%04lX\n", codepoint);
                add_wc((wchar_t) codepoint, &wlite_punct, &punctn);
            }
            free(field);
        }

        /* get character decomposition */

        if ((field = get_field(line, 5)) != NULL) {
            if (strstr(field, "<narrow>") != NULL) {
                if (sscanf(field, "<narrow> %lX ;", &decomp) != 1) {
                    fprintf(stderr, "can't scan field: %s", field);
                }
                else {
                    fprintf(stderr, "adding halfwidth U+%04lX -> U+%04lX\n",
                            codepoint, decomp);
                    add_map(codepoint, decomp, &wlite_fixw, &fixw_n);
                }
                   
            }
            free(field);
        }

        if ((field = get_field(line, 5)) != NULL) {
            if (strstr(field, "<wide>") != NULL) {
                if (sscanf(field, "<wide> %lX ;", &decomp) != 1) {
                    fprintf(stderr, "can't scan field: %s", field);
                }
                else {
                    fprintf(stderr, "adding fullwidth U+%04lX -> U+%04lX\n",
                            codepoint, decomp);
                    add_map(codepoint, decomp, &wlite_fixw, &fixw_n);
                }
                   
            }
            free(field);
        }

    }

    fprintf(stdout, "#include \"%s\"\n", "wlite_config.h");

    print_wcs(stdout, wlite_punct, WLITE_ID2STR_(wlite_punct), punctn);
    print_maps(stdout, wlite_fixw, WLITE_ID2STR_(wlite_fixw), fixw_n);

    return 0;
}