summaryrefslogtreecommitdiff
path: root/memch/memch.c
blob: caf65c336cc76f5f47a13298202a37e802eadd40 (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
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
/* 
 * Help memorize Chinese poetry/prose
 * Copyright (c) 2024 Runxi Yu <https://runxiyu.org>
 * SPDX-License-Identifier: BSD-2-Clause
 */

// FIXME: There's something wrong with the buffer length handling
//        I'm a bit sick so I don't want to figure out why right now
//        I should probably use libutf rather than wide characters anyway

#define _XOPEN_SOURCE_EXTENDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <wchar.h>
#include <ncurses.h>
#include <time.h>
#include <signal.h>
#include <locale.h>
#include <unistd.h>
#include <fcntl.h>

#define NORMAL 1
#define WRONG 2
#define CORRECT 3
#define BLANK 4
#define HALF 5
#define ACTIVE 6

const wchar_t PUNCTS[] = L"\uff1a\uff1b\u3002\uff01\uff1f\uff0c\u201c\u201d\u2018\u2019";	// ascii quotes don't appear in Chinese text
// const wchar_t PUNCTS[] = L",."; // why would you ever use this

void poof(int sig)
{
	endwin();
	exit(0);
}

void warn(const char *progname, const char *s)
{
	fprintf(stderr, "%s: %s\n", progname, s);
}

struct segment {
	wchar_t *text;
	bool display;
	wchar_t *ending_punctuation;
	int color;
};

void print_segments(struct segment *segments, int segment_count)
{
	int i;

	for (i = 0; i < segment_count; i++) {
		attron(COLOR_PAIR(segments[i].color));
		if (segments[i].display) {
			printw("%ls", segments[i].text);
		} else {
			printw("*");
		}
		attroff(COLOR_PAIR(segments[i].color));
		printw("%ls", segments[i].ending_punctuation);
	}
}

struct segment *parse_paragraph_to_segments(const wchar_t *t,
					    bool first_display,
					    int *segment_count)
{
	int len = wcslen(t);
	struct segment *segments;
	int i = 0, punct_idx = 0, text_idx = 0;
	bool current_display = first_display;
	wchar_t current_punctuation[10] = L"";
	wchar_t current_text[1000] = L"";

	if (len == 0)
		return NULL;

	segments = malloc(sizeof(*segments) * len);
	*segment_count = 0;

	for (;;) {
		if (wcschr(PUNCTS, t[i])) {
			current_punctuation[punct_idx++] = t[i++];
		} else if (punct_idx > 0) {
			current_punctuation[punct_idx] = L'\0';
			segments[*segment_count].text = wcsdup(current_text);
			segments[*segment_count].display = current_display;
			segments[*segment_count].ending_punctuation =
			    wcsdup(current_punctuation);
			segments[*segment_count].color =
			    current_display ? NORMAL : BLANK;
			(*segment_count)++;
			current_punctuation[0] = L'\0';
			punct_idx = 0;
			current_text[0] = L'\0';
			text_idx = 0;
			current_display = !current_display;
		} else {
			current_text[text_idx++] = t[i++];
		}

		if (i >= len) {	// FIXME
			current_text[text_idx] = L'\0';
			segments[*segment_count].text = wcsdup(current_text);
			segments[*segment_count].display = current_display;
			segments[*segment_count].ending_punctuation =
			    wcsdup(current_punctuation);
			segments[*segment_count].color =
			    current_display ? NORMAL : BLANK;
			(*segment_count)++;
			break;
		}
	}

	return segments;
}

int main(int argc, char *argv[])
{
	setlocale(LC_ALL, "");

	signal(SIGINT, poof);

	FILE *file;
	struct segment *segments = NULL;
	wchar_t line[1000];
	int segment_count = 0;
	int specified_line = -1;
	int current_line = 0;
	int i, n;
	int wrongs = 0;
	int corrects = 0;

	if (argc < 2) {
		warn(argv[0],
		     "Missing argument: Path to file to be memorized.");
		return 22;
	}

	file = fopen(argv[1], "r");
	if (!file) {
		warn(argv[0], "Cannot find file.");
		return 2;
	}

	if (argc > 2)
		specified_line = atoi(argv[2]) - 1;

	while (fgetws(line, sizeof(line) / sizeof(wchar_t), file)) {
		if (specified_line == -1 || current_line == specified_line) {
			line[wcscspn(line, L"\n")] = 0;
			if (wcslen(line) > 0) {
				struct segment *parsed_segments;
				int parsed_count;
				parsed_segments =
				    parse_paragraph_to_segments(line,
								rand() % 2,
								&parsed_count);
				segments =
				    realloc(segments,
					    sizeof(struct segment) *
					    (segment_count + parsed_count));
				for (i = 0; i < parsed_count; i++)
					segments[segment_count + i] =
					    parsed_segments[i];
				segment_count += parsed_count;
				free(parsed_segments);
			}
		}
		current_line++;
		if (specified_line != -1 && current_line > specified_line)
			break;
	}

	fclose(file);

	if (segment_count == 0) {
		warn(argv[0], "Empty segments list after parsing.");
		return 79;
	}

	if (segments[0].display) {
		if (segment_count > 1)
			segments[1].color = ACTIVE;
	} else {
		segments[0].color = ACTIVE;
	}

	initscr();
	start_color();
	init_pair(NORMAL, COLOR_WHITE, COLOR_BLACK);
	init_pair(WRONG, COLOR_RED, COLOR_BLACK);
	init_pair(CORRECT, COLOR_GREEN, COLOR_BLACK);
	init_pair(BLANK, COLOR_YELLOW, COLOR_BLACK);
	init_pair(HALF, COLOR_MAGENTA, COLOR_BLACK);
	init_pair(ACTIVE, COLOR_CYAN, COLOR_BLACK);

	cbreak();
	noecho();
	keypad(stdscr, TRUE);

	for (i = 0; i < segment_count; i++) {
		struct segment *segment = &segments[i];
		wchar_t got[1000];

		clear();
		print_segments(segments, segment_count);
		printw("\n\n");
		refresh();

		if (!segment->display) {
			mvprintw(LINES - 2, 0, "* ");
			echo();
			getn_wstr(got, sizeof(got) - 1);
			noecho();

			if (wcscmp(got, segment->text) == 0) {
				segment->display = true;
				segment->color = CORRECT;
				corrects++;
			} else if (wcslen(got) == 0) {
				segment->display = true;
				segment->color = WRONG;
				wrongs++;
			} else {
				mvprintw(LINES - 2, 0, "Retry: ");
				echo();
				getnstr((char *)got, sizeof(got) - 1);
				noecho();

				if (wcscmp(got, segment->text) == 0) {
					segment->display = true;
					segment->color = CORRECT;
					corrects++;
				} else {
					segment->display = true;
					segment->color = WRONG;
					wrongs++;
				}
			}

			n = i;
			while (n < segment_count && segments[n].display)
				n++;
			if (n < segment_count)
				segments[n].color = ACTIVE;
		}
	}

	clear();
	print_segments(segments, segment_count);
	printw("\n\n");
	printw("Summary:\n\tCorrect: %d\n\tIncorrect: %d\n", corrects, wrongs);
	refresh();

	getch();
	endwin();

	for (int i = 0; i < segment_count; i++) {
		free(segments[i].text);
		free(segments[i].ending_punctuation);
	}
	free(segments);
	// Yes, the OS will free things, but I'm kinda convinced that this is
	// good practice anyways, and there's about no harm.

	return 0;
}