-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.c
214 lines (176 loc) · 6.41 KB
/
project.c
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
#include "stdio.h"
#include "string.h"
#define FLUSH while (getchar() != '\n') // replaces fflush on linux
int searchThroughString (char *string, char *pattern);
int readFromFile (const char *finput, const char *foutput);
int readFromStdin (const char *foutput);
void writeResults (const char *fileout, int ifCount, int elseCount, int linesCount);
int main (int argc, char const *argv[]) {
int optionChoosen = 0, read = 0;
while (1) {
printf("Изберете начин на работа \n");
printf("1 - Четене от файл, изход във файл \n");
printf("2 - Четене от файл, изход на екрана \n");
printf("3 - Четене от клавиатура, изход на екрана \n");
printf("4 - Четене от клавиатура, във файл \n");
read = scanf("%d", &optionChoosen);
if (read > 0) {
if ((optionChoosen > 0) && (optionChoosen < 5)) {
printf("Вие избрахте опция %d\n", optionChoosen);
break;
} else {
printf("Няма такава опция\n");
}
} else {
printf("Неправилен вход\n");
}
FLUSH;
}
switch (optionChoosen) {
case 1:
if (argc >= 3) {
int fileLength = strlen(argv[1]);
if ( (argv[1][fileLength - 2] == '.') && ( (argv[1][fileLength - 1] == 'c') || (argv[1][fileLength - 1] == 'C') ) ) {
readFromFile(argv[1], argv[2]);
} else {
printf("Въведете файл с разишернието на езика C\n");
return 0;
}
} else {
printf("Не сте въвели файлове за четене и писане\n");
return 0;
}
break;
case 2:
if (argc >= 2) {
int fileLength = strlen(argv[1]);
if ( (argv[1][fileLength - 2] == '.') && ( (argv[1][fileLength - 1] == 'c') || (argv[1][fileLength - 1] == 'C') ) ) {
readFromFile(argv[1], NULL);
} else {
printf("Въведете файл с разишернието на езика C\n");
return 0;
}
} else {
printf("Не сте въвели файл за четене\n");
return 0;
}
break;
case 3:
readFromStdin(NULL);
break;
case 4:
if (argc >= 2) {
readFromStdin(argv[1]);
} else {
printf("Не сте въвели файл за писане\n");
return 0;
}
break;
};
return 0;
}
int readFromStdin (const char *foutput) {
// input read vars
char *line = NULL;
size_t len = 0;
ssize_t charactersRead;
// comment vars
const char pattern[3] = "/*";
char *token;
int inComment = 0;
// result vars
int ifCount = 0;
int elseCount = 0;
int linesCount = 0;
while ((charactersRead = getline(&line, &len, stdin)) != -1) {
linesCount += 1;
token = strtok(line, pattern);
if (token != NULL) {
while( token != NULL ) {
if (strcmp(token, line) && searchThroughString(line, "/*")) {
inComment = 1;
break;
} else if (strcmp(token, line) && searchThroughString(line, "*/")) {
inComment = 0;
break;
}
if (!inComment) {
ifCount += searchThroughString(token, "if");
elseCount += searchThroughString(token, "else");
}
token = strtok(NULL, pattern);
if (token != NULL) inComment = !inComment ;
}
}
}
writeResults(foutput, ifCount, elseCount, linesCount - 1);
return 0;
}
int readFromFile (const char *finput, const char *foutput) {
if (!finput) {
printf("Не сте въвели файл за четене\n");
return 0;
}
FILE *input;
// file read vars
char *line = NULL;
size_t len = 0;
ssize_t charactersRead;
// comment vars
const char pattern[3] = "/*";
char *token;
int inComment = 0;
// result vars
int ifCount = 0;
int elseCount = 0;
int linesCount = 0;
input = fopen(finput, "r");
while ((charactersRead = getline(&line, &len, input)) != -1) {
linesCount += 1;
token = strtok(line, pattern);
if (token != NULL) {
printf("inComment: %d Token: %s\n", inComment, token);
printf("Equal: %d\n", strcmp(token, line));
while( token != NULL ) {
if (strcmp(token, line) && searchThroughString(line, "/*")) {
inComment = 1;
break;
} else if (strcmp(token, line) && searchThroughString(line, "*/")) {
inComment = 0;
break;
}
if (!inComment) {
ifCount += searchThroughString(token, "if");
elseCount += searchThroughString(token, "else");
}
token = strtok(NULL, pattern);
if (token != NULL) inComment = !inComment ;
}
}
}
fclose(input);
writeResults(foutput, ifCount, elseCount, linesCount);
return 0;
}
void writeResults (const char *fileout, int ifCount, int elseCount, int linesCount) {
if (fileout) {
FILE *fp;
fp = fopen(fileout, "w");
fprintf(fp, "Брой редове %d\n", linesCount);
fprintf(fp, "Брой if-ове %d\n", ifCount - elseCount);
fprintf(fp, "Брой if/else-ове %d\n", elseCount);
fclose(fp);
} else {
printf("Брой редове %d\n", linesCount);
printf("Брой if-ове %d\n", ifCount - elseCount);
printf("Брой if/else-ове %d\n", elseCount);
}
}
int searchThroughString (char *string, char *pattern) {
int count = 0;
int length = strlen(pattern);
if (!length) return 0;
for (string = strstr(string, pattern); string; string = strstr(string + length, pattern))
++count;
return count;
}