Skip to content

Commit 96b426e

Browse files
committed
Use vector in place of linked list (WIP)
1 parent f70b61e commit 96b426e

File tree

12 files changed

+122
-54
lines changed

12 files changed

+122
-54
lines changed

err

Whitespace-only changes.

frame.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
#include <wchar.h>
2222

2323
#define NUM_THREADS 1
24+
#define VERTICAL L"┃"
25+
#define HORIZONTAL L"━"
2426

2527
// constants
26-
const wchar_t HORIZONTAL = 0x2501;
27-
const wchar_t VERTICAL = 0x2503;
28+
// const wchar_t HORIZONTAL = 0x2501;
2829
const wchar_t CORNERS[] = { 0x250f, 0x2513, 0x251b, 0x2517 };
2930

3031
void *update_bounds();
@@ -70,24 +71,26 @@ void write_text(char *text, rect_t *r) {
7071
}
7172
}
7273

73-
int write_strlist(string *bank, rect_t *r, int start_index, int end_index) {
74+
int write_strlist(strlist *bank, rect_t *r, int start_index, int end_index) {
7475
for (int y0 = r->y0 + 1; y0 < r->y1; y0++) {
7576
move(y0, r->x0 + 1);
7677
for (int i = r->x0 + 1; i < r->x1 - 1; i++)
7778
addch(' ');
7879
}
7980

80-
string *curr;
8181
int counter = start_index;
8282

8383
// Write characters
8484
move(r->y0 + 1, r->x0 + 1);
8585
int wc = 0, y, x; // word count at first line
86-
for (curr = get_string(bank, start_index); curr->next != NULL;
87-
curr = curr->next) {
86+
string *curr;
87+
88+
for (int i = 0; i < (int)bank->len; i++) {
89+
curr = strlist_get(bank, i);
90+
// fprintf(stderr, "%s\n", curr->val);
8891
getyx(stdscr, y, x);
89-
/* printf("%d, %d\n", y, x); */
9092

93+
// Move to next line if word goes past end of screen
9194
if (curr->len + 1 >= r->x1 - x) {
9295
x = r->x0 + 1;
9396
y++;
@@ -128,7 +131,7 @@ void draw_rect(rect_t *r) {
128131
// make horizontal lines for top and bottom
129132
hor_line[0] = CORNERS[0];
130133
for (int i = 1; i < width - 1; i++)
131-
hor_line[i] = HORIZONTAL;
134+
hor_line[i] = HORIZONTAL[0];
132135
hor_line[width - 1] = CORNERS[1];
133136
hor_line[width] = L'\0';
134137

@@ -139,7 +142,7 @@ void draw_rect(rect_t *r) {
139142
mvaddwstr(r->y1, r->x0, hor_line);
140143

141144
cchar_t vertical_bar;
142-
setcchar(&vertical_bar, &VERTICAL, 0, 0, NULL);
145+
setcchar(&vertical_bar, L"┃", 0, 0, NULL);
143146
for (int y = r->y0 + 1; y < r->y1; y++) {
144147
mvadd_wch(y, r->x0, &vertical_bar);
145148
mvadd_wch(y, r->x1, &vertical_bar);

frame.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef FRAME_H
2+
#define FRAME_H
3+
14
#include "strlist.h"
25

36
typedef struct {
@@ -11,6 +14,8 @@ typedef struct {
1114
void init_frame();
1215
void draw_rect(rect_t *);
1316
void write_text(char *, rect_t *);
14-
int write_strlist(string *bank, rect_t *r, int start_index, int end_index);
17+
int write_strlist(strlist *bank, rect_t *r, int start_index, int end_index);
1518
void refresh_frame();
1619
void del_frame();
20+
21+
#endif

m

32.8 KB
Binary file not shown.

typingtest.c renamed to main.c

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <assert.h>
12
#include <ncurses.h>
23
#include <pthread.h>
34
#include <stdio.h>
@@ -9,7 +10,7 @@
910

1011
#include "frame.h"
1112

12-
#define MAX_WORD_LEN 20
13+
#define MAX_WORD_LEN 100
1314
#define NUM_THREADS 2
1415
#define STRLIST_SIZE 200
1516

@@ -28,7 +29,7 @@ typedef struct _windowsize {
2829
} windowsize;
2930

3031
static strlist load_word_bank(FILE *bank_file, char word_delimiter);
31-
void load_text_from_bank(char *text, string *word_bank, int start_index,
32+
void load_text_from_bank(char *text, strlist *word_bank, int start_index,
3233
int end_index);
3334

3435
// threads
@@ -43,10 +44,10 @@ void update_time_box(rect_t *r, windowsize *size);
4344

4445
void print_rect(rect_t *r);
4546

46-
void calculate_stats(typingtest_results *results, long time, string *word_bank,
47+
void calculate_stats(typingtest_results *results, long time, strlist *word_bank,
4748
string *last_word, int errors);
4849
void show_results(typingtest_results *results, windowsize *);
49-
void final_screen(int timediff, string *word_bank, string *curr_word,
50+
void final_screen(int timediff, strlist *word_bank, string *curr_word,
5051
int num_errors, windowsize *curr_size);
5152
void print_help();
5253
int is_option(char *str, int pos);
@@ -122,10 +123,11 @@ int main(int argc, char **argv) {
122123
// update time in the background
123124
pthread_create(&pids[0], NULL, update_time, &now);
124125

125-
words_in_first_line = write_strlist(word_bank, &main_box, offset, -1);
126+
words_in_first_line = write_strlist(&word_bank, &main_box, offset, -1);
126127
/* return 1; */
127128

128-
string *curr_word = word_bank->next;
129+
int word_index = 0;
130+
string *curr_word = strlist_get(&word_bank, word_index++);
129131
curr_word->style = A_BOLD;
130132
// 27 == ESC
131133
c = getch();
@@ -137,7 +139,7 @@ int main(int argc, char **argv) {
137139
timef(timestr, &start, &now, TIMER_LEN);
138140
write_text(timestr, &time_box);
139141
if (now.tv_sec - start.tv_sec >= TIMER_LEN) {
140-
final_screen(now.tv_sec - start.tv_sec, word_bank, curr_word, num_errors,
142+
final_screen(now.tv_sec - start.tv_sec, &word_bank, curr_word, num_errors,
141143
&curr_size);
142144
goto EXIT;
143145
}
@@ -161,7 +163,8 @@ int main(int argc, char **argv) {
161163
cp = 0;
162164
typed_word[cp] = '\0';
163165

164-
curr_word = curr_word->next;
166+
assert(word_index < word_bank.len);
167+
curr_word = strlist_get(&word_bank, word_index++);
165168
curr_word->style = A_BOLD;
166169

167170
word_count++;
@@ -177,7 +180,7 @@ int main(int argc, char **argv) {
177180
}
178181

179182
write_text(typed_word, &text_box);
180-
words_in_first_line = write_strlist(word_bank, &main_box, offset, -1);
183+
words_in_first_line = write_strlist(&word_bank, &main_box, offset, -1);
181184
/* printf("%d\n", words_in_first_line); */
182185
if (word_count == words_in_first_line) {
183186
offset += word_count;
@@ -188,7 +191,7 @@ int main(int argc, char **argv) {
188191

189192
EXIT : {
190193
del_frame();
191-
free_strlist(word_bank);
194+
strlist_delete(&word_bank);
192195
for (int i = 0; i < NUM_THREADS; i++)
193196
pthread_cancel(pids[i]);
194197
}
@@ -202,41 +205,43 @@ EXIT : {
202205
* @param start_index The index to start at
203206
* @param end_index The index to end at. Use -1 for None.
204207
*/
205-
void load_text_from_bank(char *text, string *word_bank, int start_index,
206-
int end_index) {
207-
int i = 0, counter = start_index;
208-
string *curr;
209-
for (curr = get_string(word_bank, start_index); curr->next != NULL;
210-
curr = curr->next) {
211-
strcpy(text + i, curr->val);
212-
i += curr->len;
213-
text[i++] = ' ';
214-
215-
if (counter++ == end_index)
216-
break;
217-
}
218-
text[i] = '\0';
219-
}
208+
// void load_text_from_bank(char *text, strlist *word_bank, int start_index,
209+
// int end_index) {
210+
// int i = 0, counter = start_index;
211+
// string *curr;
212+
// for (curr = get_string(word_bank, start_index); curr->next != NULL;
213+
// curr = curr->next) {
214+
// strcpy(text + i, curr->val);
215+
// i += curr->len;
216+
// text[i++] = ' ';
217+
//
218+
// if (counter++ == end_index)
219+
// break;
220+
// }
221+
// text[i] = '\0';
222+
// }
220223

221224
static strlist load_word_bank(FILE *bank_file, char word_delimiter) {
222225
// TODO optimize size
223226
strlist bank = strlist_new(STRLIST_SIZE);
224227
// int len = lseek(bank_file, 0, SEEK_END);
225228
// void *data = mmap(0, len, PROT_READ, MAP_PRIVATE, bank_file, 0);
226229

230+
// TODO Move outside function
227231
fseek(bank_file, 0, SEEK_END);
228232
long fsize = ftell(bank_file);
229233
fseek(bank_file, 0, SEEK_SET); /* same as rewind(f); */
230234
char *buf = malloc(fsize + 1);
231235
fread(buf, fsize, 1, bank_file);
232-
buf[fsize] = 0;
236+
buf[fsize] = '\0';
233237

234238
char *b = buf;
235239
int len = 0;
236240
while (*b != '\0') {
237241
if (*b == word_delimiter) {
238242
*b = '\0';
239-
strlist_append(&bank, (string){ b - len, 0, 0 });
243+
// Add 1 because it includes the delimeter
244+
strlist_append(&bank, (string){ .val = b - len + 1, .len = len - 1, .style = 0 });
240245
len = 0;
241246
}
242247
len++;
@@ -292,7 +297,7 @@ void print_rect(rect_t *r) {
292297
printf("Rectangle(%d, %d, %d, %d)\n", r->x0, r->y0, r->x1, r->y1);
293298
}
294299

295-
void calculate_stats(typingtest_results *results, long time, string *word_bank,
300+
void calculate_stats(typingtest_results *results, long time, strlist *word_bank,
296301
string *last_word, int errors) {
297302
/*
298303
* double wpm;
@@ -304,13 +309,14 @@ void calculate_stats(typingtest_results *results, long time, string *word_bank,
304309

305310
int words_typed = 0;
306311
int chars_typed = 0;
307-
for (string *w = word_bank->next;
308-
w->next != NULL && strcmp(w->val, last_word->val) != 0; w = w->next) {
312+
int i = 0;
313+
for (string *s = strlist_get(word_bank, i);
314+
i < (int)word_bank->len && strcmp(s->val, last_word->val) != 0; i++) {
309315
words_typed++;
310-
chars_typed += w->len;
316+
chars_typed += s->len;
311317
}
312318

313-
printf("last: %s\n", get_string(word_bank, words_typed)->val);
319+
printf("last: %s\n", strlist_get(word_bank, words_typed)->val);
314320

315321
results->wpm = 60.0 * words_typed / time;
316322
results->cps = (double)chars_typed / time;
@@ -348,7 +354,7 @@ void show_results(typingtest_results *results, windowsize *size) {
348354
mvaddstr(result_box.y1 + 2, result_box.x0 + 2, "Press ESC to exit.");
349355
}
350356

351-
void final_screen(int timediff, string *word_bank, string *curr_word,
357+
void final_screen(int timediff, strlist *word_bank, string *curr_word,
352358
int num_errors, windowsize *curr_size) {
353359
int c;
354360
typingtest_results results;

makefile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ all: $(PROG)
44

55
# Tell make about the file dependencies
66
HEAD = strlist.h frame.h
7-
OBJ = strlist.o frame.o strlist_test.o typingtest.o
7+
OBJ = strlist.o frame.o main.o
88

99
# special libraries This can be blank
1010
LIB = -lncurses
@@ -14,7 +14,7 @@ LIB = -lncurses
1414
CC = clang
1515
DEBUG = -g -fsanitize=address
1616
CSTD =
17-
WARN = -Wall -Wextra -Werror
17+
WARN = -Wall -Wextra #-Werror
1818
CDEFS =
1919
CFLAGS = -I. $(DEBUG) $(WARN) $(CSTD) $(CDEFS)
2020

@@ -25,7 +25,7 @@ $(PROG): $(OBJ)
2525
$(CC) $(CFLAGS) $(OBJ) $(LIB) -o $@
2626

2727

28-
.PHONY: clean test_strlist install
28+
.PHONY: clean test install
2929

3030
clean:
3131
rm -f $(OBJ) $(BIN)
@@ -37,5 +37,8 @@ install: $(PROG)
3737
rm -f /usr/local/bin/tterm
3838
ln ./typingterm /usr/local/bin/tterm
3939

40-
strlist_test: $(OBJ)
40+
strlist_test: strlist.o strlist_test.o
4141
$(CC) $^ -o $@ $(CFLAGS)
42+
43+
test: strlist_test
44+
./strlist_test

misc_test.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#define _XOPEN_SOURCE_EXTENDED
2+
3+
#include <ncurses.h>
4+
#include <wchar.h>
5+
6+
7+
const wchar_t VERTICAL = 0x2503;
8+
9+
int main() {
10+
int h[2] = L"━";
11+
printf("%d, %d\n%x, %x\n%c, %c\n", h[0], h[1], h[0], h[1], h[0], h[1]);
12+
}

strlist.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ strlist strlist_new(uintptr_t size) {
2626

2727
void strlist_delete(strlist *s) {
2828
free(s->vec);
29-
free(s);
3029
}
3130

32-
string strlist_get(strlist *s, uintptr_t index) {
31+
string *strlist_get(strlist *s, uintptr_t index) {
3332
if (index >= s->len) {
3433
fprintf(stderr, "index out of bounds: %lu >= len %lu\n", index, s->len);
3534
exit(EXIT_FAILURE);
3635
}
37-
return s->vec[index];
36+
return &s->vec[index];
3837
}
3938

4039
uintptr_t strlist_append(strlist *l, string s) {
@@ -76,10 +75,11 @@ static void strlist_resize(strlist *s, uintptr_t size) {
7675

7776
void strlist_shuffle(strlist *s) {
7877
srand(getpid());
79-
for (int i = 0; i < s->len; i++) {
78+
const int len = s->len;
79+
for (int i = 0; i < len; i++) {
8080
int rand_pos = (double)s->len * rand() / RAND_MAX;
81-
string curr = strlist_get(s, i);
82-
string r = strlist_get(s, rand_pos);
81+
string curr = *strlist_get(s, i);
82+
string r = *strlist_get(s, rand_pos);
8383
// swap
8484
string tmp = curr;
8585
strlist_set(s, i, r);

strlist.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef STRLIST_H
2+
#define STRLIST_H
3+
14
#include <stdint.h>
25

36
typedef struct _string {
@@ -16,7 +19,7 @@ typedef struct _strlist {
1619
strlist strlist_new(uintptr_t size);
1720
void strlist_delete(strlist *);
1821

19-
string strlist_get(strlist *s, uintptr_t index);
22+
string *strlist_get(strlist *s, uintptr_t index);
2023
uintptr_t strlist_set(strlist *l, uintptr_t index, string s);
2124
uintptr_t strlist_append(strlist *l, string s);
2225
void strlist_shuffle(strlist *s);
@@ -34,3 +37,4 @@ void strlist_shuffle(strlist *s);
3437
// void free_string(string *);
3538
// void free_strlist(string *);
3639
// void free_string(string *str);
40+
#endif

strlist_test

55.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)