1+ #include <assert.h>
12#include <ncurses.h>
23#include <pthread.h>
34#include <stdio.h>
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
3031static 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
4445void 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 );
4849void 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 );
5152void print_help ();
5253int 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
189192EXIT : {
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
221224static 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 ;
0 commit comments