-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpastevents.c
138 lines (119 loc) · 3.09 KB
/
pastevents.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
#include "headers.h"
void pastevents_store(char command[1000][1000], char *directory)
{
char dir[4096];
strcpy(dir, directory);
strcat(dir, "/pastevents.txt");
int len = 0;
for (int i = 0; i < 1000; i++)
{
if (strcmp(command[i], "NULL") == 0)
{
len = i;
break;
}
}
int i;
for (i = 0; i < strlen(command[len - 1]); i++)
{
if (command[len - 1][i] == '\n')
break;
}
command[len - 1][i] = '\n';
command[len - 1][i + 1] = '\0';
char command_1[4096] = {'\0'};
for (int i = 0; i < 1000; i++)
{
if (strcmp(command[i], "NULL") == 0)
break;
strcat(command_1, command[i]);
strcat(command_1, " ");
}
command_1[strlen(command_1) - 1] = '\0';
FILE *store = fopen(dir, "r");
if (store == NULL)
{
fprintf(stderr, "\x1b[31mError opening file\x1b[0m\n");
return;
}
char lines[15][4096] = {'\0'};
int lineCount = 0;
while (fgets(lines[lineCount], 4096, store) != NULL && lineCount < 16)
lineCount++;
fclose(store);
strcpy(dir, directory);
strcat(dir, "/pastevents.txt");
store = fopen(dir, "w");
if (lineCount < 15)
{
for (int i = 0; i < lineCount; i++)
fputs(lines[i], store);
if (lineCount > 0 && (strcmp(lines[lineCount - 1], command_1) != 0))
fputs(command_1, store);
else if (lineCount == 0)
fputs(command_1, store);
}
else
{
if (strcmp(lines[lineCount - 1], command_1) != 0)
{
for (int i = 1; i < lineCount; i++)
fputs(lines[i], store);
fputs(command_1, store);
}
else
{
for (int i = 0; i < lineCount; i++)
fputs(lines[i], store);
}
}
fclose(store);
return;
}
void display_pastevents(char *directory)
{
char dir[4096];
strcpy(dir, directory);
strcat(dir, "/pastevents.txt");
FILE *store = fopen(dir, "r");
if (store == NULL)
{
fprintf(stderr, "\x1b[31mError opening file\x1b[0m\n");
return;
}
char one_line[4096];
while (fgets(one_line, 4096, store) != NULL)
printf("%s", one_line);
fclose(store);
}
void delete_pastevents(char *directory)
{
char dir[4096];
strcpy(dir, directory);
strcat(dir, "/pastevents.txt");
FILE *store = fopen(dir, "w");
if (store == NULL)
{
fprintf(stderr, "\x1b[31mError opening file\x1b[0m\n");
return;
}
fclose(store);
}
void execute_pastevents(int index, char *directory, char prevdir[4096])
{
char dir[4096];
strcpy(dir, directory);
strcat(dir, "/pastevents.txt");
FILE *store = fopen(dir, "r");
if (store == NULL)
{
fprintf(stderr, "\x1b[31mError opening file\x1b[0m\n");
return;
}
char lines[15][4096] = {'\0'};
int lineCount = 0;
while (fgets(lines[lineCount], 4096, store) != NULL && lineCount < 16)
lineCount++;
fclose(store);
find_input(lines[lineCount - index], directory, prevdir);
}