summaryrefslogtreecommitdiff
path: root/input.c
blob: c94aaa30afc3c4ef104231452562b13b9d303df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "7ed.h"
#include "input.h"
#include "i_validation.h"
#include <stdint.h>

int clfstdin_doubleprint; // variable accessed via extern int in startmode so that it does not print '?' 2 times in a row

int check_length_fix_stdin(char *smode_buf) {

    int sbl = 0;
    for ( ; sbl < SMODE_MAX_INPUT_SIZE ; sbl++) {
        if(smode_buf[sbl] == '\0' || smode_buf[sbl] == '\n') {
            break;
        }
    }
    if (sbl >= SMODE_MAX_INPUT_SIZE) {
        char c;
        while ((c = getchar()) != '\n');
        fprintf (stdout, "?\n");
        clfstdin_doubleprint = 0;
        //fprintf (stderr, "sbl > SMODE_MAX_SIZE\n"); // debug code
        return _FAIL;
    }

    return 0;
}

int smode_input(char *single, char **multiple, uint64_t focus) { // This function is for input then calls the appropriate validator 

    // char *single is for p, e, c, q, a
    // char **multiple is for L, n, x and d. Although it can be expanded to be used in p and e. 

    char smode_buf[SMODE_MAX_SIZE] = { '\0' }; // Smode buffer
    fprintf(stdout, "(%lu): ", focus); // UI
    fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input
    if(check_length_fix_stdin(smode_buf) == _FAIL) { return _FAIL; }

    switch (smode_buf[0]) {
                // from L to D there will be the 'Multiple' options. In their respective cases i will check if the input is valid or not.
                // I will not make the program clean the input because that could lead to assumptions.
        case 'l':
        case 'L': {
            
            int chk = validate_LN(smode_buf, MODE_L);
            if (chk == _INVALID) {
                return _FAIL;
            }
            *multiple = (char *)malloc(strlen(smode_buf) + 1);
            strcpy(*multiple, smode_buf);
            return _MULTIPLE;

        break; }
        case 'n':
        case 'N': {
            
            int chk = validate_LN(smode_buf, MODE_N);
            if (chk == _INVALID) {
                return _FAIL;
            }
            *multiple = (char *)malloc(strlen(smode_buf) + 1);
            strcpy(*multiple, smode_buf);
            return _MULTIPLE;

        break; }
        case 'x':
        case 'X': {

            // X will work with MODE_N mode for now. I may update in the future to use MODE_L but for now i will only use MODE_N for simplicity sake.
            int chk = validate_LN(smode_buf, MODE_N);
            if (chk == _INVALID) {
                return _FAIL;
            }
            *multiple = (char *)malloc(strlen(smode_buf) + 1);
            strcpy(*multiple, smode_buf);
            return _MULTIPLE;

        break; }
        case 'd':
        case 'D': {
            
            // X will work with MODE_N mode for now. I may update in the future to use MODE_L but for now i will only use MODE_N for simplicity sake.
            int chk = validate_LN(smode_buf, MODE_N);
            if (chk == _INVALID) {
                return _FAIL;
            }
            *multiple = (char *)malloc(strlen(smode_buf) + 1);
            strcpy(*multiple, smode_buf);
            return _MULTIPLE;

        break; }// singles below this point
        case 'p':
        case 'P':
            *single = smode_buf[0];
            return _SINGLE;
        break;
        case 'e':
        case 'E':
            *single = smode_buf[0];
            return _SINGLE;
        break;
        case 'c':
        case 'C':
            *single = smode_buf[0];
            return _SINGLE;
        break;
        case 'q':
        case 'Q':
            *single = smode_buf[0];
            return _SINGLE;
        break;
        case 'a':
        case 'A':
            *single = smode_buf[0];
            return _SINGLE;
        break;
        case '\n':
            return _RETURN;
        break;

    }

    return _FAIL;

}