From 9f8a5bf1dafad446f3d12b1227fc91f8cca1c3a3 Mon Sep 17 00:00:00 2001
From: Oskar <assawin.h@gmail.com>
Date: Thu, 14 Mar 2024 20:35:11 +0100
Subject: Removed bug from N, very messy code but at least it works now

---
 README                         |   2 +-
 input.c                        |  37 ++++++++++++++++++++++++-------------
 test/smode                     | Bin 30640 -> 0 bytes
 test/testcases/smode_cases.txt |   5 +++++
 4 files changed, 30 insertions(+), 14 deletions(-)
 delete mode 100755 test/smode

diff --git a/README b/README
index b7bad36..6433fc4 100644
--- a/README
+++ b/README
@@ -4,4 +4,4 @@ Its a crude and simple line editor.
 WARNING: Do not use this program on any files that you dont want to risk damaging, deleting or overwriting. I can not gaurantee that this program will function as expected.
 
 TODO:
--Finish up the new input system
+-Finish the new input system.
diff --git a/input.c b/input.c
index 1a774c2..04a8e2e 100644
--- a/input.c
+++ b/input.c
@@ -167,26 +167,25 @@ int validate_N(char *smode_buf) {
 
     If focus is 1 and we say N-10 Then it will create 10 lines "before" 1 (out of scope for validate_N)
 
-    TODO:
-        - Make N+ valid again
-        - Make all '-'/negatives invalid. 
-        
     */
+    char nums_with_zero[] = "0123456789";
     char nums[] = "123456789";
     int N_num_flag = _INVALID;
     int plus_num_flag = _INVALID; //If true the if statement will validate if input is valid after the +
     int num_flag = _INVALID; // If true the if statement will validate if input is valid after L
-
     if (smode_buf[1] == '+') {
         plus_num_flag = _ONE;
         if (smode_buf[2] == '0') {
             return _INVALID;
         }
 
+        if (!((smode_buf[2] >= '0' && smode_buf[2] <= '9') || smode_buf[2] == '\n')) {
+            return _INVALID;
+        }
+
         for (int i = 0 ; i < 9 ; i++) { // Check if theres a number after +
-            if (smode_buf[2] == nums[i]) {
-                //printf("Its a number after + \n");
-                plus_num_flag = _VALID;
+            if (smode_buf[2] == nums[i]) {    
+                plus_num_flag = _VALID;                 
                 break;
             }
         }
@@ -195,9 +194,8 @@ int validate_N(char *smode_buf) {
     if (plus_num_flag == _VALID) { // Because there is a number after + or - then the flag is 0 and this code runs
         int plus_valid_flag = _INVALID;
         N_num_flag = _INVALID; // False
-        char nums_with_zero[] = "0123456789";
         // Check the rest with a loop. Start at 2
-        for (int i = 1 ; i < SMODE_MAX_INPUT_SIZE ; i++) {
+        for (int i = 2 ; i < SMODE_MAX_INPUT_SIZE ; i++) {
                                                 // Nested loop to check every element in nums_with_zero on the current smode element (i)(outer loop)
             for (int j = 0 ; j < 10 ; j++) {
 
@@ -210,6 +208,9 @@ int validate_N(char *smode_buf) {
                     }
                     break;
                 }
+                if(smode_buf[i+1] != nums_with_zero[j]) {
+                    return _INVALID;
+                }
 
             } //inner nested loop
             if (plus_valid_flag == _VALID) {
@@ -233,10 +234,10 @@ int validate_N(char *smode_buf) {
         }
     }
     
-    if (num_flag == 0) { // Start validating if there are numbers after L
+    if (num_flag == 0) { // Start validating if there are numbers after N
         int valid_flag = _INVALID;
         N_num_flag = _INVALID; // False
-        char nums_with_zero[] = "0123456789";
+        int Kflag = _INVALID;
         // Check the rest with a loop. Start at 2
         for (int i = 1 ; i < SMODE_MAX_INPUT_SIZE ; i++) {
                                                 // Nested loop to check every element in nums_with_zero on the current smode element (i)(outer loop)
@@ -249,6 +250,16 @@ int validate_N(char *smode_buf) {
                         valid_flag = _VALID;
                         break;
                     }
+                    Kflag = _INVALID;
+                    for (int k = 0 ; k < 10 ; k++) {
+                        if (smode_buf[i+1] == nums_with_zero[k]) { // We checked for newline earlier but since we didnt fine one
+                            Kflag = _VALID;                         // We check if theres a number. If theres a number then keep going
+                            break;                                  // If there is anything but a number then Return _INVALID
+                        }
+                    }
+                    if (Kflag == _INVALID) {
+                        return _INVALID;
+                    }
                     break;
                 }
 
@@ -258,13 +269,13 @@ int validate_N(char *smode_buf) {
                 break;
             }
             
-
         }//outer nested loop
     }
 
     if (smode_buf[1] == '\n') {
         return _VALID; // just return N
     }
+    
     if (plus_num_flag == _ONE) {
         return _VALID;
     }
diff --git a/test/smode b/test/smode
deleted file mode 100755
index 7d1e54e..0000000
Binary files a/test/smode and /dev/null differ
diff --git a/test/testcases/smode_cases.txt b/test/testcases/smode_cases.txt
index 3c2ec8a..57f7e41 100644
--- a/test/testcases/smode_cases.txt
+++ b/test/testcases/smode_cases.txt
@@ -24,3 +24,8 @@ L+023L		expected result: Invalid
 L23l		expected result: Invalid
 L-012		expected result: Invalid
 L-0L		expected result: Invalid
+L100+100	expected result: Invalid
+L+100+100	expected result: Invalid
+N100+100	expected result: Invalid
+N+100+100	expected result: Invalid
+
-- 
cgit v1.2.3