summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-03-31 21:30:11 +0200
committerOskar <[email protected]>2024-03-31 21:30:11 +0200
commit6104c0ee7257ae99e5187f56593442ca9f04084b (patch)
tree437957ae25f7bd1544f6c725425213244d94e704
parenta2fe6b4251181b65da4264c179c5df39c22159de (diff)
Started integrating the input system. Mainly trying to integrate the
'singles' so far because they will be the easiest.
-rw-r--r--Makefile2
-rw-r--r--input.c39
-rw-r--r--input.h4
-rw-r--r--startmode.c172
-rw-r--r--test/Makefile17
-rw-r--r--test/oldtests/validate_check_imm.c (renamed from test/validate_check_imm.c)0
-rw-r--r--test/oldtests/validate_check_p_m.c (renamed from test/validate_check_p_m.c)0
-rw-r--r--test/oldtests/validate_imm_numbers.c (renamed from test/validate_imm_numbers.c)0
-rw-r--r--test/oldtests/validate_plus_continue_tests.c (renamed from test/validate_plus_continue_tests.c)0
9 files changed, 21 insertions, 213 deletions
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 <string.h>
#include <errno.h>
#include "7ed.h"
+#include "input.h"
+#include "i_validation.h"
#include <stdint.h>
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/validate_check_imm.c b/test/oldtests/validate_check_imm.c
index 48867f9..48867f9 100644
--- a/test/validate_check_imm.c
+++ b/test/oldtests/validate_check_imm.c
diff --git a/test/validate_check_p_m.c b/test/oldtests/validate_check_p_m.c
index fe54bd9..fe54bd9 100644
--- a/test/validate_check_p_m.c
+++ b/test/oldtests/validate_check_p_m.c
diff --git a/test/validate_imm_numbers.c b/test/oldtests/validate_imm_numbers.c
index 35e0e32..35e0e32 100644
--- a/test/validate_imm_numbers.c
+++ b/test/oldtests/validate_imm_numbers.c
diff --git a/test/validate_plus_continue_tests.c b/test/oldtests/validate_plus_continue_tests.c
index b8c9769..b8c9769 100644
--- a/test/validate_plus_continue_tests.c
+++ b/test/oldtests/validate_plus_continue_tests.c