-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.c
66 lines (55 loc) · 1.67 KB
/
report.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// one Change contains the file path, number of changes in that file
typedef struct Changes {
int number;
char *fileName;
} Change;
Change *changes = NULL;
int currentChangesCount = 0;
// compare function comaring the number of updates in one change
int compare(const void *a, const void *b) {
int l = ((Change *)a)->number;
int r = ((Change *)b)->number;
return (l - r);
}
// report.txt generator
void initializeReport(char *fileName, char *targetString) {
// create the file
FILE *reportFile = fopen("report.txt", "w");
fprintf(reportFile,
"Target string: %s\nSearch begins in current folder: "
"%s\n\n------Report------\n"
"Updates\t\tFile Name\n",
targetString, fileName);
// qsort the array
qsort(changes, currentChangesCount, (sizeof(*changes)), compare);
for (int i = currentChangesCount - 1; i >= 0; i--) {
fprintf(reportFile, "%d\t\t\t%s\n", changes[i].number, changes[i].fileName);
}
// free memory
free(changes);
changes = NULL;
fclose(reportFile);
}
// add a change to the list with the name and number of updates
void addChange(char *fileName, int numberOfChanges) {
Change *temp = realloc(changes, (currentChangesCount + 1) * sizeof(*changes));
if (temp != NULL) {
changes = temp;
} else {
return;
}
Change *c = malloc(sizeof(Change));
c->number = numberOfChanges;
c->fileName = malloc(strlen(fileName) + 1);
if (NULL == c->fileName) {
perror("malloc for fileName failed");
exit(EXIT_FAILURE);
}
strcpy(c->fileName, fileName);
changes[currentChangesCount] = *c;
currentChangesCount++;
}