aboutsummaryrefslogtreecommitdiffstats
path: root/learn-your-telephone-number.c
blob: 9aa0f38d7c60cda63669f8f62d1c4022416b1af5 (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
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
/* See LICENSE file for copyright and license details. */
#include <alloca.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define t(...) do { if (__VA_ARGS__) goto fail; } while (0)


static const char *argv0;


static char *
compare(const char *restrict e, const char *restrict c)
{
	size_t en = strlen(e);
	size_t cn = strlen(c);
	size_t **matrix = alloca((en + 1) * sizeof(size_t *));
	char **path_matrix = alloca((en + 1) * sizeof(char *));
	size_t ei, ci, ri = 0;
	char* rc = NULL;
	size_t d, l, u, lu;
	int ch;

	for (ei = 0; ei <= en; ei++) {
		matrix[ei] = alloca((cn + 1) * sizeof(size_t));
		matrix[ei][0] = ei;
		path_matrix[ei] = alloca((cn + 1) * sizeof(char));
		path_matrix[ei][0] = '|';
	}
	for (ci = 0; ci <= cn; ci++) {
		matrix[0][ci] = ci;
		path_matrix[0][ci] = '-';
	}
	path_matrix[0][0] = '\0';

	e--, c--;

	for (ei = 1; ei <= en; ei++) {
		for (ci = 1; ci <= cn; ci++) {
			d = matrix[ei - 1][ci - 1];
			l = matrix[ei + 0][ci - 1];
			u = matrix[ei - 1][ci + 0];
			lu = 1 + (l < u ? l : u);
			ch = (e[ei] != c[ci]);
			d += (size_t)ch;
			matrix[ei][ci] = d < lu ? d : lu;
			path_matrix[ei][ci] = d < lu ? (ch ? '\\' : '=') : (l < u ? '-' : '|');
		}
	}

	t (!(rc = malloc((en + cn + 2) * sizeof(char))));

#if 0
	for (ei = 0; ei <= en; ei++) {
		for (ci = 0; ci <= cn; ci++)
			printf("%zu%s", matrix[ei][ci], ci == cn ? "" :
			       path_matrix[ei][ci + 1] == '-' ? "\033[1;32m-\033[m" : " ");
		printf("\n");
		if (ei < en)
			for (ci = 0; ci < cn; ci++)
				printf("%s%s", path_matrix[ei + 1][ci + 0] == '|' ? "\033[1;31m|\033[m" : " ",
				       path_matrix[ei + 1][ci + 1] == '\\' ? "\033[1;33m\\\033[m" :
				       path_matrix[ei + 1][ci + 1] == '=' ? "\033[1;39m\\\033[m" : " ");
		printf("\n");
	}
#endif

	rc[ri++] = '\0';
	for (ei = en, ci = cn; ei + ci;) {
		switch (path_matrix[ei][ci]) {
		case '=':   ei--, ci--;  rc[ri++] = '=';  break;
		case '\\':  ei--, ci--;  rc[ri++] = '!';  break;
		case '|':   ei--;        rc[ri++] = '-';  break;
		case '-':         ci--;  rc[ri++] = '+';  break;
		default:
			abort();
		}
	}
	rc[ri] = '\0';

fail:
	return rc;
}


int
main(int argc, char *argv[])
{
	char *correct;
	char *entered = NULL;
	size_t n = 0;
	void *new;
	char *w;
	char *r;
	char *path = NULL;
	char *path_;
	char *p;
	int rc = 0;

	argv0 = argc ? argv[0] : "learn-your-telephone-number";
	if (argc != 2)
		goto usage;

	correct = alloca((2 * strlen(argv[1]) + 1) * sizeof(char));
	for (w = correct, r = argv[1]; *r; r++) {
		if      (isdigit(*r)) *w++ = *r;
		else if (isalpha(*r)) goto usage;
		else if (*r == '+')   *w++ = '0', *w++ = '0';
	}
	*w = '\0';

	t (fprintf(stdout, "Enter your telephone number: ") < 0);
	t (fflush(stdout));
	t (errno = 0, getline(&entered, &n, stdin) < 0);
	n = strlen(entered) - 1;
	entered[n] = '\0';

	t (!(new = realloc(entered, (2 * n + 1) * sizeof(char))));
	entered = new;
	memmove(r = (entered + n), w = (entered), n + 1);
	for (; *r; r++) {
		if      (isdigit(*r)) *w++ = *r;
		else if (*r == '+')   *w++ = '0', *w++ = '0';
	}
	*w = '\0';

	if (!strcmp(entered, correct))
		goto done;
	t (!(path = compare(entered, correct)));

	path_ = strchr(path + 1, '\0');
	t (fprintf(stdout, "\033[A\033[KEntered: ") < 0);
	for (p = entered; *--path_;) {
		switch (*path_) {
		case '=': t (fprintf(stdout, "\033[39m%c", *p++) < 0); break;
		case '!': t (fprintf(stdout, "\033[31m%c", *p++) < 0); break;
		case '+': t (fprintf(stdout, " ")                < 0); break;
		case '-': t (fprintf(stdout, "\033[31m%c", *p++) < 0); break;
		}
	}
	t (fprintf(stdout, "\033[m\n") < 0);

	path_ = strchr(path + 1, '\0');
	t (fprintf(stdout, "Correct: ") < 0);
	for (p = correct; *--path_;) {
		switch (*path_){
		case '=': t (fprintf(stdout, "\033[39m%c", *p++) < 0); break;
		case '!': t (fprintf(stdout, "\033[32m%c", *p++) < 0); break;
		case '+': t (fprintf(stdout, "\033[32m%c", *p++) < 0); break;
		case '-': t (fprintf(stdout, " ")                < 0); break;
		}
	}
	t (fprintf(stdout, "\033[m\n") < 0);

	rc = 1;
done:
	free(entered);
	free(path);
	return rc;
fail:
	rc = 1;
	if (!errno)
		goto done;
	perror(argv0);
	rc = 2;
	goto done;
usage:
	fprintf(stderr, "usage: %s your-telephone-number\n", argv0);
	return 2;
}