-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.c
72 lines (68 loc) · 1.45 KB
/
log.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
#define RBtree
#include "log.h"
void initialize(Node **T)
{
char line[MAX];
char *p;
char *key;
char *value;
line[0] = '\0';
log = fopen("log", "a+");
if(!log)
{
fprintf(stderr, "open log fail!\n");
exit(1);
}
while(fgets(line, MAX, log) && line[0] != '\n' && strlen(line))
{
p = strtok(line, "\t");
if(!strcmp(p, "i"))
{
p = strtok(NULL, "\t");
key = calloc(strlen(p)+1, sizeof(char));
strcpy(key, p);
p = strtok(NULL, "\t");
p[strlen(p)-1] = '\0';
value = calloc(strlen(p)+1, sizeof(char));
strcpy(value, p);
InsertNode(T, key, value);
}
else if(!strcmp(p, "d"))
{
p = strtok(NULL, "\t");
p[strlen(p)-1] = '\0';
*T = DeleteNode(*T, p);
}
else
{
fprintf(stderr, "initialize fali!\n");
exit(1);
}
}
}
char *log_make(char *d, char *key, char *value)
{
char text[MAX];
char *p;
text[0] = '\0';
strcat(text, d);
strcat(text, "\t");
strcat(text, key);
if(!strcmp(d, "i"))
{
strcat(text, "\t");
strcat(text, value);
}
strcat(text, "\n");
p = calloc(strlen(text)+1, sizeof(char));
strcpy(p, text);
return p;
}
void log_write(char *test)
{
write(fileno(log), test, strlen(test));
}
void log_close()
{
fclose(log);
}