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
|
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "7ed.h"
#include <stdint.h>
int ncat(char filename[]) {
FILE *file;
file = fopen(filename,"r");
if (file == NULL) {
fprintf(stderr, "Cannot open file. ncat\n");
return 1;
}
char buffer[BUF_SZ_2];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, BUF_SZ_2, file)) > 0) {
fwrite(buffer, 1, bytes_read, stdout);
}
fclose(file);
return 0;
}
int startmode(char filename[]) {
uint64_t Flines;
int returnval = count_lines_in_file(filename, &Flines);
if (returnval == 1) {
return EXIT_FAILURE;
}
fprintf(stdout,"%s %lu lines\n", filename, Flines);
uint64_t focus = 1;
while(1) {
firstwhile:
int ret = count_lines_in_file(filename, &Flines); // update Flines every time
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':
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; // start the first while loop aka startmode
}
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':
uint64_t CFlines;
int returnval = count_lines_in_file(filename, &CFlines);
if (returnval == 1) {
return EXIT_FAILURE;
}
fprintf(stdout,"%s %zu lines\n", filename, CFlines);
break;
case 'Q':
case 'q':
return EXIT_SUCCESS;
break;
case 'a':
case 'A':
ncat(filename);
break;
case 'n':
case 'N': {
// The "focus" that the newline will be inserted afterwards
uint64_t new_line_pos_temp = 0; // temp
char buf[1024];
int success;
do {
fprintf(stdout, "Create a new line after: ");
if (!fgets(buf, 1024, stdin)) { // take input from user
fprintf(stderr, "Too many characters\n");
break;
}
char *endptr;
new_line_pos_temp = 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') {
success = 0;
}
else {
success = 1;
}
} while (!success);
new_line(filename, new_line_pos_temp);
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) {
return 1;
}
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");
}
}
return EXIT_SUCCESS;
}
|