-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhx-comm-edit.c
More file actions
154 lines (145 loc) · 4.21 KB
/
hx-comm-edit.c
File metadata and controls
154 lines (145 loc) · 4.21 KB
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
/* hx-comm-edit.c: Comment/uncomment stream from Helix editor */
#include <stdio.h>
#include <stdlib.h>
#define LANG_MAX_LENGTH 20
#define COMM_START_MAX_LENGTH 10
#define COMM_END_MAX_LENGTH 10
FILE *file;
int lang_length, start_length, end_length;
void open_file(const char *argv);
int get_lang(char *lang, const char *argv);
int extract_comm(char *comm, int switcher);
void get_comm(char *start, char *end, int lang_l, char *lang,
const char *argv);
void edit_comm(char *start, char *end);
int main(int argc, char **argv) {
char lang[LANG_MAX_LENGTH];
char start[COMM_START_MAX_LENGTH];
char end[COMM_END_MAX_LENGTH];
lang_length=get_lang(lang, argv[1]);
get_comm(start, end, lang_length, lang, argv[2]);
edit_comm(start, end);
return 0;
}
void open_file(const char *argv) {
if ((file = fopen(argv, "r")) == NULL) {
fprintf(stderr, "Can't open file.\n");
exit(1);
}
}
int get_lang(char *lang, const char *argv) {
int chr, i = 0;
open_file(argv);
while ((chr = getc(file)) != EOF && chr != '\n') {
lang[i] = chr; ++i;
}
lang[i] = '\0';
fclose(file);
return i;
}
int extract_comm(char *comm, int switcher) {
int chr, count = 0;
if (switcher) {
comm[count] = ' '; ++count;
}
while ((chr = getc(file)) == '\t' || chr == ' ');
comm[count] = chr;
while ((chr = getc(file)) != '\t' && chr != ' '
&& chr != '\n' && chr != EOF) {
++count; comm[count] = chr;
}
if (!switcher) {
++count; comm[count] = ' ';
}
++count; comm[count] = '\0';
if (switcher) end_length = count;
else start_length = count;
return chr;
}
void get_comm(char *start, char *end, int lang_l, char *lang,
const char *argv) {
int chr, count = 0, flag = 0;
open_file(argv);
while ((chr = getc(file)) != EOF) {
if (chr == lang[count]) {
++flag; ++count;
if (flag == lang_l) {
chr = extract_comm(start, 0);
if (chr == '\n' || chr == EOF) {
end_length = 0; end[0] = '\0';
break;
} else {
extract_comm(end, 1);
break;
}
} else {
continue;
}
} else if (chr == '*') {
chr = extract_comm(start, 0);
if (chr == '\n' || chr == EOF) {
end_length = 0; end[0] = '\0';
} else {
extract_comm(end, 1);
}
} else {
while ((chr = getc(file)) != '\n');
count = 0; flag = 0;
}
}
fclose(file);
}
void edit_comm(char *start, char *end) {
int chr, start_count = 0, start_done = 0,
end_get = 0, end_count = 0, end_done = 0,
unget_chr = 0, chk_nst = 0, nst_count = 0;
while (unget_chr || (chr = getc(stdin)) != EOF) {
unget_chr = 0;
if (chr == '\n' || chr == '\r') {
if (end_length && start_done && !end_get && !chk_nst)
for (int i = 0; i < end_length; ++i)
putc(end[i], stdout);
putc(chr, stdout);
if (chr == '\r') putc(getc(stdin),stdout);
start_count = start_done = end_get = 0;
end_count = end_done = chk_nst = nst_count = 0;
continue;
}
if (!start_done) {
if (chr == start[start_count]) {
++start_count;
if (start_count == start_length) {
start_done = 1; end_get = 1;
}
} else if (chr == '\t' || chr == ' ') putc(chr, stdout);
else {
for (int i = 0; i < start_length; ++i)
putc(start[i], stdout);
if (start_count) for (int i = 0; i < start_count; ++i)
putc(start[i], stdout);
start_done = 1; unget_chr = 1;
}
} else if ((chr == end[end_count]) && end_length &&
!end_done && end_get && !nst_count) {
++end_count;
if (end_count == end_length) {
end_done = 1;
if (chk_nst) for (int i = 0; i < end_length; ++i)
putc(end[i], stdout);
}
} else if (end_count) {
for (int i = 0; i < end_count; ++i) putc(end[i], stdout);
end_count = 0; unget_chr = 1;
} else {
putc(chr, stdout);
if (end_length && !chk_nst && chr == start[nst_count]) {
++nst_count;
if (nst_count == start_length) {
chk_nst = 1; nst_count = 0;
}
} else nst_count = 0;
}
}
if (start_done && !end_done && !end_get && !chk_nst)
for (int i = 0; i < end_length; ++i) putc(end[i], stdout);
}