summaryrefslogtreecommitdiff
path: root/process_multiples.c
blob: 10a565d1ab3bc5a9d98a5bfa1a69d41da019e758 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#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, int mode) { // Check if you are trying to modify or move to lines under 1 or lines that are over the total amount of lines in the file.
                                                                    // Mode N allows us to go to line 0 because N needs it.
    if (mode == MODE_N) { 
        if (focus == 0) {
            return _VALID;
        }
    }

    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) { // Go to a specific newline

    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, MODE_L) == _INVALID) {
        return focus;
    }

    return new_focus;

}

uint64_t call_L_plus_minus_continue(char *multiple, uint64_t focus, uint64_t Flines) { // Go up or down a number of lines

    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, MODE_L) == _INVALID) {
        return focus;
    }

    return new_focus;
}

uint64_t call_L_only(uint64_t focus, uint64_t Flines) { // Old L functionality.

    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, MODE_L);
    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) { // Go up or down by only 1
    switch (p_or_m) {
        case '+': {
            int cll = check_L_linecount(Flines, focus+1, MODE_L);
            if (cll == _INVALID) {
                return focus;
            }
            return focus+1;
        break; }
        case '-': {
            int cll = check_L_linecount(Flines, focus-1, MODE_L);
            if (cll == _INVALID) {
                return focus;
            }
            return focus-1;
        break; }
    }

    return _NA;
}

uint64_t call_L(char *multiple, uint64_t focus, uint64_t Flines) { // Main L function to decide what to do.

    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_immediate(char *multiple, uint64_t Flines, char *filename) { 
    
    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 newline_insert_position = strtol(new_multiple, &endptr, 10);
    errno = 0;
    if (errno == ERANGE) {
        return 0;
    }
    if (endptr == new_multiple) {
        return 0;
    }
    
    if(check_L_linecount(Flines, newline_insert_position, MODE_N) == _INVALID) {
        return -1;
    }

    new_line(filename, newline_insert_position);

    return 0;

}

int call_N_plus_continue(char *multiple, uint64_t focus, char *filename) {

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

    int choice_yesno = 0;
    int i = 0;
    int j = 2;
    for( ; multiple[j] != '\n' ; i++, j++) {
        new_multiple[i] = multiple[j];
    }
    
    char *endptr;
    uint64_t newlines_to_add = strtol(new_multiple, &endptr, 10);
    errno = 0;
    if (errno == ERANGE) {
        return -1;
    }
    if (endptr == new_multiple) {
        return -1;
    }
    
    if (newlines_to_add > 1) {
        choice_yesno = choice();
    }
    if (choice_yesno == 1) { return -1; }
    
    for(uint64_t count = 0 ; count < newlines_to_add ; count++) { // This is very inefficient if you are adding thousands of lines but why would you wanna do that anyway?
        new_line(filename, focus);
    }

    return 0;

}

uint64_t call_N(char *multiple, uint64_t focus, uint64_t Flines, char *filename) {

    int imm = _IMM_NUMBER;

    if (multiple[1] == '\n') { // N will add just 1 line.
        imm = _NA;
        new_line(filename, focus);
        return 0;
    }

    if (multiple[1] == '+') { // N+ is the same as N. It will only add 1 line.
        imm = _NA;
        if (multiple[2] == '\n') {
            new_line(filename, focus);
            return 0;
        }

        call_N_plus_continue(multiple, focus, filename);
        return 0;
    }

    if (imm == _IMM_NUMBER) {
        call_N_immediate(multiple, Flines, filename);
        return 0;
    }

    return 0;
}

int call_X_plus_continue(char *multiple, uint64_t focus, char *filename, 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 amount_of_lines_to_remove = strtol(new_multiple, &endptr, 10);
    errno = 0;
    if (errno == ERANGE) {
        return -1;
    }
    if (endptr == new_multiple) {
        return -1;
    }

    if(check_L_linecount(Flines, focus+(amount_of_lines_to_remove-1) /*-1 because we are not doing currentline + amount of lines. That would be 1 too much*/, MODE_L) == _INVALID) {
        return -1; 
    }

    if (choice() == 1) { return -1; }

    for(uint64_t count = 0 ; count < amount_of_lines_to_remove ; count++) { // This is very inefficient
        remove_line_contents(filename, focus+count);
    }

    return 0;
}

int call_X_immediate(char *multiple, uint64_t Flines, char *filename) {

    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 remove_line_contents_position = strtol(new_multiple, &endptr, 10);
    errno = 0;
    if (errno == ERANGE) {
        return 0;
    }
    if (endptr == new_multiple) {
        return 0;
    }
    
    if(check_L_linecount(Flines, remove_line_contents_position, MODE_L) == _INVALID) {
        return -1;
    }

    if (choice() == 1) { return -1; }    

    remove_line_contents(filename, remove_line_contents_position);

    return 0;
}

int call_X(char *multiple, uint64_t focus, uint64_t Flines, char *filename) {
    
    int imm = _IMM_NUMBER;

    if (multiple[1] == '\n') { // X Will remove current line
        imm = _NA;
    
        if(choice() == 0) {
            remove_line_contents(filename, focus);
        }
        return 0;
    }

    if (multiple[1] == '+') {
        imm = _NA;
        if (multiple[2] == '\n') {
            if(choice() == 0) {
                remove_line_contents(filename, focus);
            }
            return 0;
        }
        call_X_plus_continue(multiple, focus, filename, Flines);

        return 0;
    }

    if (imm == _IMM_NUMBER) {
        call_X_immediate(multiple, Flines, filename);
        return 0;
    }

    return 0;
}

uint64_t call_D_plus_continue(char *multiple, uint64_t focus, uint64_t Flines, char *filename) {

    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 lines_and_newlines_to_remove = strtol(new_multiple, &endptr, 10);
    errno = 0;
    if (errno == ERANGE) {
        return focus;
    }
    if (endptr == new_multiple) {
        return focus;
    }

    if(check_L_linecount(Flines, focus+(lines_and_newlines_to_remove-1) /*-1 because we are not doing currentline + amount of lines. That would be 1 too much*/, MODE_L) == _INVALID) {
        return focus; 
    }

    if (choice() == 1) { return focus; }

    for(uint64_t count = 0 ; count < lines_and_newlines_to_remove ; count++) { // This is very inefficient
        remove_line_contents_and_newline(filename, focus);
    }

    return focus-1;

}

uint64_t call_D_immediate(char *multiple, uint64_t focus, uint64_t Flines, char *filename) {
    
    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 remove_line_and_newline_position = strtol(new_multiple, &endptr, 10);
    errno = 0;
    if (errno == ERANGE) {
        return focus;
    }
    if (endptr == new_multiple) {
        return focus;
    }
    
    if(check_L_linecount(Flines, remove_line_and_newline_position, MODE_L) == _INVALID) {
        return focus;
    }

    if (choice() == 1) { return focus; }    

    remove_line_contents_and_newline(filename, remove_line_and_newline_position);
    if (focus == 1 && remove_line_and_newline_position == 1) {
        return 1;
    }

    if (focus == remove_line_and_newline_position) {
        return focus-1;
    }

    return focus;
}

uint64_t call_D(char *multiple, uint64_t focus, uint64_t Flines, char *filename) {

    int imm = _IMM_NUMBER;
    uint64_t original_focus = focus;

    if (multiple[1] == '\n') { // D Will remove current line
        imm = _NA;
    
        if(choice() == 0) {
            remove_line_contents_and_newline(filename, focus);
            if (focus == 1) { return 1; }
            return focus-1;
        } else {
            return focus;
        }
        
    }

    if (multiple[1] == '+') {
        imm = _NA;
        if (multiple[2] == '\n') {
            if(choice() == 0) {
                remove_line_contents_and_newline(filename, focus);
                if (focus == 1) { return 1; }
                return focus-1;
            } else {
                return focus;
            }
        }
        focus = call_D_plus_continue(multiple, focus, Flines, filename);
        if (original_focus == 1) { return 1; }
        return focus;
    }

    if (imm == _IMM_NUMBER) {
        uint64_t calldimmediate; 
        calldimmediate = call_D_immediate(multiple, focus, Flines, filename);
        return calldimmediate;
    }

    return 0;
}