summaryrefslogtreecommitdiff
path: root/process_multiples.c
blob: 6c3b6b7e9d4a219ffcc76ee810ac58a60038cd31 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#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 check_L_linecount(uint64_t Flines, uint64_t focus) {

    if (focus < 1 || focus > Flines) {
        fprintf(stdout, "?\n");
        return _INVALID;
    }
    return _VALID;
}

uint64_t call_L_immediate(char *multiple, uint64_t focus, uint64_t Flines) {

    char new_multiple[32] = { '\0' };

    int i = 0;
    int j = 1;
    for( ; multiple[j] != '\n' ; i++, j++) {
        new_multiple[i] = multiple[j];
    }
    
    char *endptr;
    uint64_t new_focus = strtol(new_multiple, &endptr, 10);
    errno = 0;
    if (errno == ERANGE) {
        return focus;
    }
    if (endptr == new_multiple) {
        return focus;
    }
    
    if(check_L_linecount(Flines, new_focus) == _INVALID) {
        return focus;
    }

    return new_focus;

}

uint64_t call_L_plus_minus_continue(char *multiple, uint64_t focus, uint64_t Flines) {

    char new_multiple[32] = { '\0' };

    int i = 0;
    int j = 2;
    for( ; multiple[j] != '\n' ; i++, j++) {
        new_multiple[i] = multiple[j];
    }
    
    char *endptr;
    uint64_t new_focus = strtol(new_multiple, &endptr, 10);
    errno = 0;
    if (errno == ERANGE) {
        return focus;
    }
    if (endptr == new_multiple) {
        return focus;
    }
    
    if(multiple[1] == '+') { new_focus = focus+new_focus; }
    if(multiple[1] == '-') { 
        int64_t s_new_focus = (int64_t)new_focus;
        int64_t s_focus = (int64_t)focus;
        if((s_focus-s_new_focus) <= 0) {
            new_focus = 0;
        } else {
            new_focus = focus-new_focus;
        }
    }
    if(check_L_linecount(Flines, new_focus) == _INVALID) {
        return focus;
    }

    return new_focus;
}

uint64_t call_L_only(uint64_t focus, uint64_t Flines) {

    char buf[SMODE_MAX_SIZE] = { '\0' };
    fprintf(stdout, "(L): ");
    fgets(buf, SMODE_MAX_SIZE, stdin);
    if(check_length_fix_stdin(buf) == _FAIL) { return focus; return _FAIL; }

    if (buf[0] == '\n') {
        return focus;
    }

    char *endptr;
    uint64_t new_focus = strtol(buf, &endptr, 10);
    errno = 0;
    if (errno == ERANGE) {
        return focus;
    }
    if (endptr == buf) {
        return focus;
    }
    if (*endptr != '\n') {
        return focus;
    }

    int cll = check_L_linecount(Flines, new_focus);
    if (cll == _INVALID) {
        return focus;
    }

    return new_focus;
}

uint64_t call_L_plus_minus_only(uint64_t focus, char p_or_m, uint64_t Flines) {
    switch (p_or_m) {
        case '+': {
            int cll = check_L_linecount(Flines, focus+1);
            if (cll == _INVALID) {
                return focus;
            }
            return focus+1;
        break; }
        case '-': {
            int cll = check_L_linecount(Flines, focus-1);
            if (cll == _INVALID) {
                return focus;
            }
            return focus-1;
        break; }
    }

    return _NA;
}

uint64_t call_L(char *multiple, uint64_t focus, uint64_t Flines) {

    int imm = _IMM_NUMBER;

    if (multiple[1] == '\n') {
        imm = _NA;
        focus = call_L_only(focus, Flines);
        return focus;
    }

    if (multiple[1] == '+' || multiple[1] == '-') {
        imm = _NA;
        if (multiple[2] == '\n') { // This is where - and + only inputs happen and get processed
            focus = call_L_plus_minus_only(focus, multiple[1], Flines);
            return focus;
        }
        focus = call_L_plus_minus_continue(multiple, focus, Flines);
        return focus;

    }

    if (imm == _IMM_NUMBER) {
        focus = call_L_immediate(multiple, focus, Flines);
        return focus;
    }


    return focus;
}

int call_N(char *multiple) {

    fprintf(stdout, "%s\n", multiple);
    return 0;
}

int call_X(char *multiple) {

    fprintf(stdout, "%s\n", multiple);
    return 0;
}

int call_D(char *multiple) {

    fprintf(stdout, "%s\n", multiple);
    return 0;
}