From 6104c0ee7257ae99e5187f56593442ca9f04084b Mon Sep 17 00:00:00 2001 From: Oskar Date: Sun, 31 Mar 2024 21:30:11 +0200 Subject: Started integrating the input system. Mainly trying to integrate the 'singles' so far because they will be the easiest. --- Makefile | 2 +- input.c | 39 ------ input.h | 4 +- startmode.c | 172 +++------------------------ test/Makefile | 17 +-- test/oldtests/validate_check_imm.c | 36 ++++++ test/oldtests/validate_check_p_m.c | 37 ++++++ test/oldtests/validate_imm_numbers.c | 36 ++++++ test/oldtests/validate_plus_continue_tests.c | 30 +++++ test/validate_check_imm.c | 36 ------ test/validate_check_p_m.c | 37 ------ test/validate_imm_numbers.c | 36 ------ test/validate_plus_continue_tests.c | 30 ----- 13 files changed, 160 insertions(+), 352 deletions(-) create mode 100644 test/oldtests/validate_check_imm.c create mode 100644 test/oldtests/validate_check_p_m.c create mode 100644 test/oldtests/validate_imm_numbers.c create mode 100644 test/oldtests/validate_plus_continue_tests.c delete mode 100644 test/validate_check_imm.c delete mode 100644 test/validate_check_p_m.c delete mode 100644 test/validate_imm_numbers.c delete mode 100644 test/validate_plus_continue_tests.c diff --git a/Makefile b/Makefile index cf87f01..cd95d71 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ clean: tests: echo "CC 7ed.c functions.c startmode.c editmode.c ---> test/$(TESTTARGET)" - $(CC) $(CFLAGS_TESTBIN) 7ed.c functions.c startmode.c editmode.c -o test/$(TESTTARGET) + $(CC) $(CFLAGS_TESTBIN) 7ed.c functions.c startmode.c editmode.c input.c i_validation.c -o test/$(TESTTARGET) install: cp $(TARGET) $(INSTALL_DIRECTORY) diff --git a/input.c b/input.c index 1f2f81e..6895573 100644 --- a/input.c +++ b/input.c @@ -112,42 +112,3 @@ int smode_input(char *single, char **multiple, uint64_t focus) { // This functio return _FAIL; } - -int main () { - - // running smode_input will do a few things: - /* - scenario 1: If you input a valid multiple then the function return multiple - and it also returns the valid string to the char pointer you provide to the function - - scenario 2: If you input an invalid string, then it will return _FAIL which is basically just '?' - nothing will be returned to multiple nor single - - scenario 3: if you input a valid single then it will return _SINGLE - and it will also return the valid single character to the char you provide to the function - - scenario 4: if you input an invalid single then it will return _FAIL which is basically just '?' - nothing will be returned to multiple nor single - this scenario is the same as scenario 3. - */ - - char *multiple; - char single; - uint64_t focus = 1; - int si_ret = smode_input(&single, &multiple, focus); - - if (si_ret == _SINGLE) { - fprintf(stdout, "single\n"); - fprintf(stdout, "%c", single); - } - if (si_ret == _MULTIPLE) { - fprintf(stdout, "multiple\n"); - fprintf(stdout, "%s", multiple); - free(multiple); - } - if (si_ret == _FAIL) { - fprintf(stdout, "?\n"); - } - - -} \ No newline at end of file diff --git a/input.h b/input.h index 94c746d..e509ee7 100644 --- a/input.h +++ b/input.h @@ -15,4 +15,6 @@ #define _FAIL '?' // final return value from smode_input to indicate an invalid #define _VALID 0 // this may only be used to mark as valid or invalid from the validate functions #define _INVALID -1 // this may only be used to mark as valid or invalid from the validate functions - // _FAIL and _INVALID are sorta tied to eachother \ No newline at end of file + // _FAIL and _INVALID are sorta tied to eachother + +int smode_input(char *single, char **multiple, uint64_t focus); diff --git a/startmode.c b/startmode.c index 821610a..ff209eb 100644 --- a/startmode.c +++ b/startmode.c @@ -4,6 +4,8 @@ #include #include #include "7ed.h" +#include "input.h" +#include "i_validation.h" #include int ncat(char filename[]) { @@ -39,169 +41,27 @@ int display_name_linecount(char *filename) { int startmode(char filename[]) { // The entry to the program. Count lines and display the count. Also show which file is being edited. - uint64_t Flines; int dnl = display_name_linecount(filename); if (dnl == 1) { return EXIT_FAILURE; } uint64_t focus = 1; // The focus variable. Which is the actual line number we have "selected" - while(1) { // The main loop to get the "UI" started - firstwhile: - - int ret = count_lines_in_file(filename, &Flines); - if (ret == 1) { - return EXIT_FAILURE; - } - - fprintf(stdout, "(%lu): ", focus); - char command = getchar(); - if (command == '\n') { - continue; - } - - while ('\n' != getchar()); - - switch (command) { - case 'L': - case 'l': // L is how we change out "focus" - uint64_t Lfocus = 0; - char buf[1024]; - int success; - - do { - fprintf(stdout, "(L): "); - if (!fgets(buf, 1024, stdin)) { - fprintf(stderr, "Too many characters\n"); - break; - } - if (buf[0] == '\n') { - goto firstwhile; // If the user presses enter then we just goto start of the main loop - } - if (buf[0] == '+') { // if user inputs + then increment by 1 - Lfocus = focus+1; - break; - } - if (buf[0] == '-') { // If user inputs - then decrement by 1 - Lfocus = focus-1; - break; - } - - char *endptr; - - Lfocus = strtol(buf, &endptr, 10); - errno = 0; - if (errno == ERANGE) { - fprintf(stderr, "Sorry, this number is too small or too large.\n"); - success = 0; - } - else if (endptr == buf) { - // no character was read - success = 0; - } - else if (*endptr && *endptr != '\n') { - // *endptr is neither end of string nor newline, - // so we didn't convert the *whole* input - success = 0; - } - - else { - success = 1; - } - - } while (!success); - - if (Lfocus < 1 || Lfocus > Flines) { - fprintf(stderr, "L is either less than 1 or more than %lu\n", Flines); - } else { - focus = Lfocus; - } - - break; - case 'P': - case 'p': - - char *line; - size_t start; - int ret = get_line(filename, focus, &line, &start); - if (ret == 1) { - return EXIT_FAILURE; - } - fprintf(stdout, "%s", line); - free(line); - - break; - case 'E': - case 'e': - - editmode(filename, focus); - - break; - case 'C': - case 'c': - - int dnl = display_name_linecount(filename); - if (dnl == 1) { - return EXIT_FAILURE; - } - - break; - case 'Q': - case 'q': - return EXIT_SUCCESS; - break; - case 'a': - case 'A': - ncat(filename); - break; - case 'n': - case 'N': { - - new_line(filename, focus); // create new line after the current focus - - break; } - case 'X': - case 'x': - - int choic = choice(); - if (choic == 0) { - remove_line_contents(filename, focus); - } - break; - case 'D': - case 'd': { - int choic = choice(); - if (choic == 1) { - break; - } - int increment = 0; - if (focus == 1) { // checks if its line 1. This is so that we can remove the newline properly. - increment++; - } - int rlc = remove_line_contents(filename, focus); - if (rlc == 1) { - return 1; - } - - int dsn = delete_specified_newline(filename, focus+increment); - if (dsn == 1) { - return 1; - } - if (focus == 1) { - break; - } - focus--; - - break; } - default: - fprintf(stdout, "?\n"); - - } - - - + char *multiple; + char single; + int smode_input_ret = smode_input(&single, &multiple, focus); + if (smode_input_ret == _SINGLE) { + fprintf(stdout, "single\n"); + fprintf(stdout, "%c\n", single); + } + if (smode_input_ret == _MULTIPLE) { + fprintf(stdout, "multiple\n"); + fprintf(stdout, "%s", multiple); + free(multiple); + } + if (smode_input_ret == _FAIL) { + fprintf(stdout, "?\n"); } - return EXIT_SUCCESS; } diff --git a/test/Makefile b/test/Makefile index 4011915..ec61306 100644 --- a/test/Makefile +++ b/test/Makefile @@ -10,14 +10,6 @@ all: 7ed-TESTING clean: cd .. && make clean - rm -f vcpm - echo "rm -f vcpm" - rm -f vcimm - echo "rm -f vcimm" - rm -f vpct - echo "rm -f vpct" - rm -f vimmn - echo "rm -f vimmn" echo "Cleaned." smode: @@ -26,11 +18,4 @@ smode: alltests: cd .. && make tests - $(CC) $(CFLAGS_ALLTESTS) validate_check_p_m.c ../i_validation.c -o vcpm - echo "CC ---> vcpm" - $(CC) $(CFLAGS_ALLTESTS) validate_check_imm.c ../i_validation.c -o vcimm - echo "CC ---> vcimm" - $(CC) $(CFLAGS_ALLTESTS) validate_plus_continue_tests.c ../i_validation.c -o vpct - echo "CC ---> vpct" - $(CC) $(CFLAGS_ALLTESTS) validate_imm_numbers.c ../i_validation.c -o vimmn - echo "CC ---> vimmn" + echo "Done." diff --git a/test/oldtests/validate_check_imm.c b/test/oldtests/validate_check_imm.c new file mode 100644 index 0000000..48867f9 --- /dev/null +++ b/test/oldtests/validate_check_imm.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include "../7ed.h" +#include "../i_validation.h" +#include "../input.h" +#include + +// Test for validate_check_imm + +int main () { + uint64_t focus = 1; + char smode_buf[SMODE_MAX_SIZE]; // Smode buffer + fprintf(stdout, "(%lu): ", focus); // UI + fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input + + int ret = validate_check_imm(smode_buf); + + if (ret == _IMM_NUMBER) { + printf("imm number\n"); + } + if(ret == _INVALID) { + printf("?\n"); + } + if(ret == _VALID) { + printf("valid\n"); + } + if(ret == _NA) { + printf("_NA\n"); + } + + return 0; + +} \ No newline at end of file diff --git a/test/oldtests/validate_check_p_m.c b/test/oldtests/validate_check_p_m.c new file mode 100644 index 0000000..fe54bd9 --- /dev/null +++ b/test/oldtests/validate_check_p_m.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include "../7ed.h" +#include "../i_validation.h" +#include "../input.h" +#include + +// Test for validate_check_p_m + +int main () { + uint64_t focus = 1; + char smode_buf[SMODE_MAX_SIZE]; // Smode buffer + fprintf(stdout, "(%lu): ", focus); // UI + fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input + + int ret = validate_check_p_m(smode_buf); + + if(ret == _NA) { + printf("NA\n"); + } + if(ret == _PLUS_CONTINUE) { + printf("plus continue\n"); + } + + if(ret == _INVALID) { + printf("?\n"); + } + if(ret == _VALID) { + printf("valid\n"); + } + + return 0; + +} \ No newline at end of file diff --git a/test/oldtests/validate_imm_numbers.c b/test/oldtests/validate_imm_numbers.c new file mode 100644 index 0000000..35e0e32 --- /dev/null +++ b/test/oldtests/validate_imm_numbers.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include "../7ed.h" +#include "../i_validation.h" +#include "../input.h" +#include + +// Test for validate_check_imm + +int main () { + uint64_t focus = 1; + char smode_buf[SMODE_MAX_SIZE]; // Smode buffer + fprintf(stdout, "(%lu): ", focus); // UI + fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input + + int ret = validate_imm_numbers(smode_buf); + + if (ret == _IMM_NUMBER) { + printf("imm number\n"); + } + if(ret == _INVALID) { + printf("?\n"); + } + if(ret == _VALID) { + printf("valid\n"); + } + if(ret == _NA) { + printf("_NA\n"); + } + + return 0; + +} \ No newline at end of file diff --git a/test/oldtests/validate_plus_continue_tests.c b/test/oldtests/validate_plus_continue_tests.c new file mode 100644 index 0000000..b8c9769 --- /dev/null +++ b/test/oldtests/validate_plus_continue_tests.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include "../7ed.h" +#include "../i_validation.h" +#include "../input.h" +#include + +// Test for validate_plus_continue + +int main () { + uint64_t focus = 1; + char smode_buf[SMODE_MAX_SIZE]; // Smode buffer + fprintf(stdout, "(%lu): ", focus); // UI + fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input + + int ret = validate_plus_continue(smode_buf); + + if(ret == _INVALID) { + printf("?\n"); + } + if(ret == _VALID) { + printf("valid\n"); + } + + return 0; + +} \ No newline at end of file diff --git a/test/validate_check_imm.c b/test/validate_check_imm.c deleted file mode 100644 index 48867f9..0000000 --- a/test/validate_check_imm.c +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include -#include -#include -#include "../7ed.h" -#include "../i_validation.h" -#include "../input.h" -#include - -// Test for validate_check_imm - -int main () { - uint64_t focus = 1; - char smode_buf[SMODE_MAX_SIZE]; // Smode buffer - fprintf(stdout, "(%lu): ", focus); // UI - fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input - - int ret = validate_check_imm(smode_buf); - - if (ret == _IMM_NUMBER) { - printf("imm number\n"); - } - if(ret == _INVALID) { - printf("?\n"); - } - if(ret == _VALID) { - printf("valid\n"); - } - if(ret == _NA) { - printf("_NA\n"); - } - - return 0; - -} \ No newline at end of file diff --git a/test/validate_check_p_m.c b/test/validate_check_p_m.c deleted file mode 100644 index fe54bd9..0000000 --- a/test/validate_check_p_m.c +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include -#include -#include -#include "../7ed.h" -#include "../i_validation.h" -#include "../input.h" -#include - -// Test for validate_check_p_m - -int main () { - uint64_t focus = 1; - char smode_buf[SMODE_MAX_SIZE]; // Smode buffer - fprintf(stdout, "(%lu): ", focus); // UI - fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input - - int ret = validate_check_p_m(smode_buf); - - if(ret == _NA) { - printf("NA\n"); - } - if(ret == _PLUS_CONTINUE) { - printf("plus continue\n"); - } - - if(ret == _INVALID) { - printf("?\n"); - } - if(ret == _VALID) { - printf("valid\n"); - } - - return 0; - -} \ No newline at end of file diff --git a/test/validate_imm_numbers.c b/test/validate_imm_numbers.c deleted file mode 100644 index 35e0e32..0000000 --- a/test/validate_imm_numbers.c +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include -#include -#include -#include "../7ed.h" -#include "../i_validation.h" -#include "../input.h" -#include - -// Test for validate_check_imm - -int main () { - uint64_t focus = 1; - char smode_buf[SMODE_MAX_SIZE]; // Smode buffer - fprintf(stdout, "(%lu): ", focus); // UI - fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input - - int ret = validate_imm_numbers(smode_buf); - - if (ret == _IMM_NUMBER) { - printf("imm number\n"); - } - if(ret == _INVALID) { - printf("?\n"); - } - if(ret == _VALID) { - printf("valid\n"); - } - if(ret == _NA) { - printf("_NA\n"); - } - - return 0; - -} \ No newline at end of file diff --git a/test/validate_plus_continue_tests.c b/test/validate_plus_continue_tests.c deleted file mode 100644 index b8c9769..0000000 --- a/test/validate_plus_continue_tests.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include -#include -#include -#include "../7ed.h" -#include "../i_validation.h" -#include "../input.h" -#include - -// Test for validate_plus_continue - -int main () { - uint64_t focus = 1; - char smode_buf[SMODE_MAX_SIZE]; // Smode buffer - fprintf(stdout, "(%lu): ", focus); // UI - fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input - - int ret = validate_plus_continue(smode_buf); - - if(ret == _INVALID) { - printf("?\n"); - } - if(ret == _VALID) { - printf("valid\n"); - } - - return 0; - -} \ No newline at end of file -- cgit v1.2.3