aboutsummaryrefslogtreecommitdiffstats
path: root/UnicodeData.parse.c
blob: 173fb6a6c4fb853a38fcbddde4e9a68bc9bda0cf (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "parse-common.c"


struct segment {
	uint32_t first, last;
};


static struct segment *segments = NULL;
static size_t nsegments = 0u;
static int ret = 0;


static char *
split(char **text)
{
	char *field = *text;
	if (field) {
		*text = strchr(*text, ';');
		if (*text)
			*(*text)++ = '\0';
	}
	return field;
}


static int
output_character_data(char *text)
{
	char *field;

	if (!(field = split(&text))) return 0; /* codepoint */

	if (!(field = split(&text))) return 0;
	ret |= printf(".name = \"%s\"", field);

	if (!(field = split(&text))) return 0;
	ret |= printf(", .general_category = GENERAL_CATEGORY_%s", field);

	if (!(field = split(&text))) return 0;
	ret |= printf(", .canonical_combining_classes = %su", field);

	if (!(field = split(&text))) return 0;
	ret |= printf(", .bidirectional_category = BIDIRECTIONAL_CATEGORY_%s", field);

	if (!(field = split(&text))) return 0; /* TODO Character Decomposition Mapping */
	if (!(field = split(&text))) return 0; /* TODO Decimal digit value */
	if (!(field = split(&text))) return 0; /* TODO Digit value */
	if (!(field = split(&text))) return 0; /* TODO Numeric value */

	if (!(field = split(&text))) return 0;
	ret |= printf(", .bidirectional_mirrored = BIDIRECTIONAL_MIRRORED_%s", field);

	if (!(field = split(&text))) return 0;
	ret |= printf(", .unicode_1_name = \"%s\"", field);

	if (!(field = split(&text))) return 0; /* ISO 10646 comment field -- always empty as of Unicode 5.2.0 */

	if (!(field = split(&text))) return 0; /* TODO Uppercase Mapping */
	if (!(field = split(&text))) return 0; /* TODO Lowercase Mapping */
	if (!(field = split(&text))) return 0; /* TODO Titlecase Mapping */

	return 1;
}


static void
parse_line(char *text, size_t lineno)
{
	unsigned long int codepoint;
	char *end;

	if (!isxdigit(*text)) {
	malformat:
		fprintf(stderr, "%s: line %zu in is malformatted\n", argv0, lineno);
		exit(1);
	}

	errno = 0;

	codepoint = strtoul(text, &end, 16);
	if (errno || codepoint > ULTIMATE_CODEPOINT || *end != ';')
		goto malformat;

	if (!nsegments) {
	new_segment:
		ret |= printf("static const struct libcmap_codepoint_data data_from_%04lx[] = {\n", codepoint);
		segments = realloc(segments, ++nsegments * sizeof(*segments));
		if (!segments) {
			fprintf(stderr, "%s: realloc %zu: %s\n", argv0, nsegments * sizeof(*segments), strerror(errno));
			exit(1);
		}
		segments[nsegments - 1u].first = (uint32_t)codepoint;
		goto new_codepoint;
	} else if (segments[nsegments - 1u].last + 1u == (uint32_t)codepoint) {
		ret |= printf(",\n");
	new_codepoint:
		segments[nsegments - 1u].last = (uint32_t)codepoint;
		ret |= printf("\t{");
		if (!output_character_data(text))
			goto malformat;
		ret |= printf("}");
	} else {
		if ((uint32_t)codepoint < segments[nsegments - 1u].last) {
			fprintf(stderr, "%s: UnicodeData is not sorted\n", argv0);
			exit(1);
		}
		ret |= printf("\n};\n");
		goto new_segment;
	}
}


static int
output(void)
{
	size_t i;

	if (!nsegments) {
		fprintf(stderr, "%s: character list is empty\n", argv0);
		exit(1);
	}

	ret |= printf("\n};\n");

	ret |= printf("static const struct libcmap_codepoint_segment list[] = {\n");
	for (i = 0u; i < nsegments; i++) {
		if (i)
			ret |= printf(",\n");
		ret |= printf("\t{.range = {0x%04"PRIX32", 0x%04"PRIX32"}, .data = data_from_%04"PRIx32"}",
		              segments[i].first, segments[i].last, segments[i].first);
	}
	ret |= printf("\n};\n");

	free(segments);

	return ret;
}