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
|
/* main.cpp - reader: RSVP-style terminal word reader */
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <string>
#include <thread>
#include <vector>
#include "../include/reader.h"
int
main(int argc, char *argv[])
{
EditorCfg *cfg;
FILE *fp;
long size;
std::string content;
std::vector<std::string> data;
size_t i;
std::string *k;
SCPCQueue<char> queue(1024);
cfg = (EditorCfg *)malloc(sizeof(EditorCfg));
if (!cfg)
die("[err]: malloc failed\n");
cfg->speed = 250000;
int& speed = cfg->speed;
enable_raw(cfg);
init_editor(cfg);
if (argc < 2)
die("[err]: no file provided!\n");
if (argc > 3)
die("[err]: too many arguments!\n");
if (argc == 3) {
if (!is_number(argv[2]))
die("[err]: invalid speed interval provided!\n");
speed = atoi(argv[2]);
}
fp = fopen(argv[1], "r");
if (!fp)
die("[err]: fopen failed\n");
fseek(fp, 0, SEEK_END);
size = ftell(fp);
rewind(fp);
content.resize(size, '\0');
fread(&content[0], 1, size, fp);
fclose(fp);
split(content, data, " \t\r\n,;:!?");
std::thread input_thread(producer, std::ref(queue));
for (;;) {
for (i = 0; i < data.size(); i++) {
k = &data[i];
if (k->empty())
continue;
window_size_callback(cfg);
editor_refresh_scrn(cfg);
consumer(queue, cfg);
{
int mid = (int)k->size() / 2;
int remaining = (int)k->size() - mid - 1;
const char *cstr = k->c_str();
const char message[] = "Current delay: ";
char buf[32];
int len{};
write(STDOUT_FILENO, cstr, mid);
write(STDOUT_FILENO, "\x1b[31m", 5);
write(STDOUT_FILENO, cstr + mid, 1);
write(STDOUT_FILENO, "\x1b[m", 3);
write(STDOUT_FILENO, cstr + mid + 1, remaining);
write(STDOUT_FILENO, "\x1b[H", 3);
write(STDOUT_FILENO, message, sizeof(message)-1);
len = snprintf(buf, sizeof(buf), "%d", cfg->speed);
write(STDOUT_FILENO, buf, len);
}
usleep(speed);
}
}
free(cfg);
return 0;
}
|