diff --git a/Makefile b/Makefile index 0ac26fe..9a3fd7e 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,8 @@ LDLIBS = -lncurses all: typing_test debugger # Create executables -typing_test: typing_test.o string_helpers.o - $(CC) -o typing_test typing_test.o string_helpers.o $(LDLIBS) +typing_test: typing_test.o utilities.o + $(CC) -o typing_test typing_test.o utilities.o $(LDLIBS) debugger: debugger.o utilities.o $(CC) -o debugger debugger.o utilities.o diff --git a/debugger.c b/debugger.c index 0b9f650..b9dfcbb 100644 --- a/debugger.c +++ b/debugger.c @@ -7,9 +7,25 @@ #include "typing_test.h" int main() { - FILE *words; + FILE *words_file; + words_file = fopen("words.txt", "r"); + Word_array word_array, return_words; + int i, number_of_words; + /* Test parse words file and generate words */ + parse_words_file(words_file, &word_array); + number_of_words = word_array.number_of_words; + for (i = 0; i < number_of_words; i++) { + printf("Word List: %s %d\n", word_array.words[i].text, word_array.words[i].length); + } + + printf("Number of words in list: %d\n", word_array.number_of_words); + + int words_to_generate = 10; + printf("\nGenerating %d Words\n", words_to_generate); + + generate_words(words_to_generate, &word_array, &return_words); + for (i = 0; i < words_to_generate; i++) { + printf("Generated Word: %s %d\n", return_words.words[i].text, return_words.words[i].length); + } - words = fopen("words.txt", "r"); - Word_array word_array; - parse_words_file(words, &word_array); } \ No newline at end of file diff --git a/typing_test.c b/typing_test.c index 8a29a3a..6dde791 100644 --- a/typing_test.c +++ b/typing_test.c @@ -44,7 +44,7 @@ void print_centered_text_menu(WINDOW *win, int row, int target, char str[][MAX_S } } -void typing_ui(WINDOW *win, int level, int mode, FILE *word_file) { +void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array) { int run = 1, ch, i, words, win_x = win->_maxx; char str[1024]; @@ -88,7 +88,8 @@ void typing_ui(WINDOW *win, int level, int mode, FILE *word_file) { /* Main function. Creates main menu */ int main() { - FILE *words; + FILE *words_file; + Word_array *word_array; int cursor_x = 0, cursor_y = 0, run = 1; int ch, key; @@ -97,7 +98,7 @@ int main() { keypad(stdscr, TRUE); noecho(); - words = fopen("words.txt", "r"); + words_file = fopen("words.txt", "r"); if (has_colors() == FALSE) { print_centered_text(stdscr, 4, "Your terminal does not support color"); @@ -107,7 +108,7 @@ int main() { start_color(); } - if (words == NULL) { + if (words_file == NULL) { run = 0; init_color(COLOR_WHITE, 255, 255, 0); @@ -118,6 +119,8 @@ int main() { print_centered_text(stdscr, 6, "Press any key to exit.\n"); attroff(COLOR_PAIR(0)); ch = getch(); + } else { + parse_words_file(words_file, word_array); } while (run) { @@ -172,12 +175,15 @@ int main() { if (cursor_x == 0 && cursor_y == 2) { run = 0; } else if (cursor_y == 1 || cursor_y == 0) { - typing_ui(stdscr, cursor_x, cursor_y, words); + typing_ui(stdscr, cursor_x, cursor_y, &word_array); } } } + /* Exiting */ + free(word_array); + refresh(); endwin(); diff --git a/utilities.c b/utilities.c index a229ad0..119af9e 100644 --- a/utilities.c +++ b/utilities.c @@ -1,14 +1,27 @@ #include "utilities.h" +#include + +int generate_random(int lower, int upper, int c) { + time_t x; + srand(x); + return (rand() % (upper - lower + 1)) + lower; +} /* This will go through the word_array structure and find the requested amount of words */ -int generate_words(int num_words, Word_array *words, char *to_return) { - int i, k, number_words_in_txt; +int generate_words(int number_of_words, Word_array *words, Word_array *to_return) { + int i, k; - to_return = malloc(num_words * 64); /* Each word will be less than 64 chars */ + /* Remember to free me after the test ends! */ - for (i = 0; i < words->number_of_words; i++) { + to_return->number_of_words = number_of_words; + to_return->words = malloc(sizeof(Word) * number_of_words); + for (i = 0; i < number_of_words; i++) { + k = generate_random(0, words->number_of_words, i); + to_return->words[i].length = words->words[k].length; + to_return->words[i].text = malloc(words->words[k].length); + strcpy(to_return->words[i].text, words->words[k].text); } return SUCCESS; @@ -18,15 +31,29 @@ int generate_words(int num_words, Word_array *words, char *to_return) { /* This will dynamically allocate memory to a 2d char array of all the words in words.txt */ int parse_words_file(FILE *words_file, Word_array *words) { - int i, number_of_words = 0; - char buf[256]; + int i = 0, number_of_words = 0; + char buf[256], temp[256]; /* Counts number of lines in words file (Each line contains a unique word) */ while (fgets(buf, sizeof(buf), words_file) != NULL) { number_of_words++; } - printf("Number of words: %d\n", number_of_words); + words->number_of_words = number_of_words; + /* Allocates space for 'number_of_words' Words structures */ + words->words = malloc(sizeof(Word) * number_of_words); + + rewind(words_file); + + while (fgets(buf, sizeof(buf), words_file) != NULL) { + sscanf(buf, "%s", temp); + words->words[i].length = strlen(temp); + words->words[i].text = malloc(strlen(temp)); + strcpy(words->words[i].text, temp); + i++; + } + + fclose(words_file); return SUCCESS; } \ No newline at end of file diff --git a/utilities.h b/utilities.h index b87227b..917aaf7 100644 --- a/utilities.h +++ b/utilities.h @@ -5,10 +5,15 @@ #define SUCCESS 1 #define FAILURE 0 +typedef struct { + int length; + char *text; +} Word; + typedef struct { int number_of_words; - char words[]; + Word *words; } Word_array; -int generate_words(int num_words, Word_array *words, char *to_return); +int generate_words(int num_words, Word_array *words, Word_array *to_return); int parse_words_file(FILE *words_file, Word_array *words); \ No newline at end of file