-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
544 lines (483 loc) · 13.6 KB
/
Copy pathmain.c
File metadata and controls
544 lines (483 loc) · 13.6 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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include "attribute.h"
#include "writeToFile.h"
#include <ctype.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
FILE *createFile(char *className);
void CLI(FILE *f, char *className);
int getNDirToCheck(char *path);
unsigned int getAttNumber(char *className) {
unsigned int n;
char question[256];
snprintf(question, sizeof(question), "How many attributes for %s?\n", className);
printf("%s", question);
scanf("%u", &n);
return n;
}
char *getAttVis() {
int vis;
printf("What is the visibility of the attributes? (default: friendly)\n");
printf("1. public\n");
printf("2. private\n");
printf("3. protected\n");
scanf("%d", &vis);
switch (vis) {
case 1:
return "public";
case 2:
return "private";
case 3:
return "protected";
default:
return "";
}
}
char *getCustomType() {
char *type = malloc(sizeof(char) * 256);
if (!type) {
fprintf(stderr, "Memory allocation failed for customType\n");
exit(1);
}
printf("Custom data type:\n");
scanf("%s", type);
return type;
}
char *getAttType(char *attName) {
int type;
printf("Data type of attribute \"%s\"? (default: Object)\n", attName);
printf("1. int\n");
printf("2. double\n");
printf("3. String\n");
printf("4. boolean\n");
printf("5. Custom\n");
scanf("%d", &type);
switch (type) {
case 1:
return strdup("int");
case 2:
return strdup("double");
case 3:
return strdup("String");
case 4:
return strdup("boolean");
case 5:
return getCustomType();
default:
return strdup("Object");
}
}
char *getAttName(int i) {
char *name = malloc(sizeof(char) * 256);
if (!name) {
fprintf(stderr, "Memory allocation failed for attributeName\n");
exit(1);
}
printf("Name of the attribute n°%d:\n", i + 1);
scanf("%s", name);
name[0] = tolower((unsigned char)name[0]);
return name;
}
void getClassName(char *className) {
printf("What is the name of the class?\n");
scanf("%s", className);
className[0] = toupper((unsigned char)className[0]);
}
void getParentClassName(char *className) {
printf("What is the name of the parent class?\n");
scanf("%s", className);
className[0] = toupper((unsigned char)className[0]);
}
int askBoolean(const char *text) {
char boolean;
printf("%s (y/n)\n", text);
scanf(" %c", &boolean);
switch (boolean) {
case 'y':
return 1;
case 'n':
return 0;
default:
return 0;
}
}
char *getPathFromSrc() {
int size = 1024;
char cwd[size];
if (getcwd(cwd, size) == NULL) {
return NULL;
}
char *p = strstr(cwd, "src/");
if (!p) {
return NULL;
}
return strdup(p);
}
int getExtendsLine(char *filePath) {
FILE *file = fopen(filePath, "r");
if (!file) {
printf("Could not open file\n");
exit(1);
}
char line[256];
int lineNumber = 1;
while (fgets(line, sizeof(line), file)) {
if (strstr(line, "class") && strstr(line, "{")) {
fclose(file);
return lineNumber;
}
lineNumber++;
}
fclose(file);
return -1;
}
char *parseParentExtends(char *filePath) {
int extendsLine = getExtendsLine(filePath);
if (extendsLine == -1) {
return NULL;
}
FILE *file = fopen(filePath, "r");
if (!file) {
printf("Could not open file\n");
exit(1);
}
char line[256];
int currentLine = 1;
while (fgets(line, sizeof(line), file)) {
if (currentLine == extendsLine) {
break;
}
currentLine++;
}
if (currentLine != extendsLine) {
fclose(file);
return NULL;
}
char *parentClass = malloc(256);
if (!parentClass) {
fprintf(stderr, "Memory allocation failed for parentClass\n");
fclose(file);
exit(1);
}
char *extendsPos = strstr(line, "extends");
if (!extendsPos) {
fclose(file);
free(parentClass);
return NULL;
}
char *afterExtends = extendsPos + 7;
sscanf(afterExtends, "%s", parentClass);
fclose(file);
return parentClass;
}
char *stripAttLine(char *line) {
char *modifiers[] = {"private ", "protected ", "public ", "final "};
int nModifiers = 4;
int found = 1;
while (found) {
found = 0;
for (int i = 0; i < nModifiers; i++) {
int len = strlen(modifiers[i]);
if (strncmp(line, modifiers[i], len) == 0) {
memmove(line, line + len, strlen(line + len) + 1);
found = 1;
}
}
}
return line;
}
char *parseParentAttributes(char *filePath, int startLine) {
FILE *file = fopen(filePath, "r");
if (!file) {
printf("Could not open file\n");
exit(1);
}
char *parentAtt = malloc(256);
if (!parentAtt) {
fprintf(stderr, "Memory allocation failed for parentAtt\n");
fclose(file);
exit(1);
}
int currentLine = 1;
while (fgets(parentAtt, 256, file)) {
if (currentLine == startLine) {
break;
}
currentLine++;
}
if (currentLine != startLine) {
fclose(file);
free(parentAtt);
return NULL;
}
int len = strlen(parentAtt);
while (len > 0 && (parentAtt[len - 1] == '\n' || parentAtt[len - 1] == '\r' || parentAtt[len - 1] == ';')) {
len--;
parentAtt[len] = '\0';
}
while (*parentAtt == ' ' || *parentAtt == '\t') {
memmove(parentAtt, parentAtt + 1, strlen(parentAtt));
}
// Skip static attributes
if (strstr(parentAtt, "static ")) {
fclose(file);
free(parentAtt);
return strdup("__SKIP__");
}
stripAttLine(parentAtt);
// Not an attribute line
if (parentAtt[0] == '\0' || parentAtt[0] == '}' || strstr(parentAtt, "(")) {
fclose(file);
free(parentAtt);
return NULL;
}
fclose(file);
return parentAtt;
}
classAttribute getParentAttribute(char *line) {
struct classAttribute attribute;
char *attType = malloc(256);
if (!attType) {
fprintf(stderr, "Memory allocation failed for attType\n");
exit(1);
}
char *attName = malloc(256);
if (!attName) {
fprintf(stderr, "Memory allocation failed for attName\n");
exit(1);
}
sscanf(line, "%s %s", attType, attName);
attribute.attType = attType;
attribute.attName = attName;
return attribute;
}
char *findFileInDirs(char *path, int nDirToCheck, char *parent) {
char *classFileName = malloc(strlen(parent) + 6);
if (!classFileName) {
fprintf(stderr, "Memory allocation failed for classFileName\n");
exit(1);
}
strcpy(classFileName, parent);
strcat(classFileName, ".java");
if (nDirToCheck < 1) {
free(classFileName);
int isCreated = askBoolean("The parent class dosen't exist or wasn't found, do you want to create it?");
if (isCreated == 1) {
FILE *f = createFile(parent);
if (f == NULL) {
printf("Error: the file already exists or cannot be created.\n");
return NULL;
}
char *p = getPathFromSrc();
if (!p) {
p = strdup("./");
}
CLI(f, parent);
fclose(f);
printf("Parent ok, going back to child\n");
char *result = findFileInDirs("./", getNDirToCheck(p), parent);
free(p);
return result;
} else {
return NULL;
}
}
DIR *dir = opendir(path);
if (!dir) {
fprintf(stderr, "Error while opening directory %s", path);
exit(1);
}
struct dirent *file;
file = readdir(dir);
while (file) {
if (strcmp(file->d_name, classFileName) == 0) {
break;
}
file = readdir(dir);
}
closedir(dir);
if (file) {
char *filePath = malloc(strlen(path) + strlen(classFileName) + 1);
if (!filePath) {
fprintf(stderr, "Memory allocation failed for filePath");
exit(1);
}
strcpy(filePath, path);
strcat(filePath, classFileName);
free(classFileName);
return filePath;
} else {
free(classFileName);
char backPath[] = "../";
char *newPath = malloc(strlen(path) + strlen(backPath) + 1);
if (!newPath) {
fprintf(stderr, "Memory allocation failed for newPath");
exit(1);
}
strcpy(newPath, backPath);
strcat(newPath, path);
char *result = findFileInDirs(newPath, nDirToCheck - 1, parent);
free(newPath);
return result;
}
}
char **searchParentsFilePaths(char *path, char *parent, int nDirToCheck, int *nFiles) {
char **filePaths = malloc(sizeof(char *) * 256);
if (!filePaths) {
fprintf(stderr, "Memory allocation failed for filePaths\n");
exit(1);
}
*nFiles = 0;
char *filePath = findFileInDirs(path, nDirToCheck, parent);
if (!filePath) {
free(filePaths);
return NULL;
}
// Check if this parent extends another class
char *grandParent = parseParentExtends(filePath);
if (grandParent) {
int nAncestorFiles = 0;
char **ancestorPaths = searchParentsFilePaths(path, grandParent, nDirToCheck, &nAncestorFiles);
free(grandParent);
if (ancestorPaths) {
for (int i = 0; i < nAncestorFiles; i++) {
int dst = *nFiles;
filePaths[dst] = ancestorPaths[i];
*nFiles = dst + 1;
}
free(ancestorPaths);
}
}
// Add current parent after ancestors
filePaths[(*nFiles)++] = filePath;
return filePaths;
}
int getNDirToCheck(char *path) {
int counter = 0;
for (int i = 0; path[i]; i++) {
if (path[i] == '/')
counter++;
}
return counter;
}
classAttribute *getParentsAttributes(char *parent, int *nParentAtt) {
char *p = getPathFromSrc();
if (!p) {
p = strdup("./");
}
struct classAttribute *attributeList = malloc(sizeof(classAttribute) * 256);
if (!attributeList) {
fprintf(stderr, "Memory allocation failed for attributeList\n");
exit(1);
}
int nFiles = 0;
char *startPath = strdup("./");
char **filePaths = searchParentsFilePaths(startPath, parent, getNDirToCheck(p), &nFiles);
free(startPath);
free(p);
if (!filePaths) {
free(attributeList);
return NULL;
}
int totalAtt = 0;
for (int f = 0; f < nFiles; f++) {
int startLine = getExtendsLine(filePaths[f]) + 1;
int j = 0;
char *line;
while ((line = parseParentAttributes(filePaths[f], startLine + j)) != NULL) {
if (strcmp(line, "__SKIP__") == 0) {
free(line);
j++;
continue;
}
attributeList[totalAtt] = getParentAttribute(line);
free(line);
totalAtt++;
j++;
}
free(filePaths[f]);
}
free(filePaths);
*nParentAtt = totalAtt;
return attributeList;
}
char *getPackageName() {
char *p = getPathFromSrc();
if (p) {
char *pkg = p + 4;
if (*pkg == '\0') {
free(p);
return NULL;
}
char *result = malloc(sizeof(char) * 256);
if (!result) {
fprintf(stderr, "Memory allocation failed for packageNameResult\n");
free(p);
exit(1);
}
strcpy(result, pkg);
free(p);
for (int i = 0; result[i]; i++) {
if (result[i] == '/')
result[i] = '.';
}
return result;
} else {
return NULL;
}
}
FILE *createFile(char *className) {
char fullPath[2048] = "./";
strcat(fullPath, className);
strcat(fullPath, ".java");
return fopen(fullPath, "wx");
}
void CLI(FILE *f, char *className) {
char *packageName = getPackageName();
char parentClassName[512] = "";
char question[512];
snprintf(question, sizeof(question), "Is %s class inherited?", className);
int isInherited = askBoolean(question);
classAttribute *parentAttList = NULL;
int parentAttCount = 0;
if (isInherited == 1) {
getParentClassName(parentClassName);
parentAttList = getParentsAttributes(parentClassName, &parentAttCount);
}
char *attVis = "";
unsigned int attNumber = getAttNumber(className);
if (attNumber > 0) {
attVis = getAttVis();
}
classAttribute *classAttList = malloc(sizeof(classAttribute) * attNumber);
if (!classAttList) {
fprintf(stderr, "Memory allocation failed for attributeList\n");
exit(1);
}
for (unsigned int i = 0; i < attNumber; i++) {
classAttList[i].attName = getAttName(i);
classAttList[i].attType = getAttType(classAttList[i].attName);
}
writeToFile(f, className, packageName, attVis, attNumber, classAttList, isInherited, parentClassName, parentAttList,
parentAttCount);
for (unsigned int i = 0; i < attNumber; i++) {
free(classAttList[i].attName);
free(classAttList[i].attType);
}
free(classAttList);
free(packageName);
}
int main(void) {
char className[512];
getClassName(className);
FILE *f = createFile(className);
if (f == NULL) {
printf("Error: the file already exists or cannot be created.\n");
return 1;
}
CLI(f, className);
fclose(f);
return 0;
}