-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.c
134 lines (131 loc) · 2.74 KB
/
write.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
#include <fastdb.h>
#include <fastdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int max(int a, int b)
{
return a > b ? a : b;
}
node_t *createNode()
{
return (node_t *)malloc(sizeof(node_t));
}
node_t *createRNode(char *name)
{
node_t *node = createNode();
node->size = 0;
node->uid = hashName(name);
node->linked = false;
node->hasChild = false;
node->access = READ_WRITE;
node->isPointer = false;
node->saved = false;
node->cap = 0;
node->ca = NULL;
node->children = malloc(32 * sizeof(uintptr_t));
strcpy(node->name, name);
return node;
}
uintptr_t writeNode(node_t *node, FILE *db)
{
if (node == NULL)
{
printf("CANNOT WRITE NULL NODE");
return (uintptr_t)NULL;
}
node->saved = true;
fseek(db, 0, SEEK_END);
node->addr = ftell(db);
saveNode(node, db);
return node->addr;
}
void saveNode(node_t *node, FILE *db)
{
if (node == NULL)
{
printf("CANNOT SAVE NULL NODE");
return;
}
if (node->hasChild)
{
if (node->ca == NULL || node->size >= node->cap)
{
fseek(db, 0, SEEK_END);
node->ca = ftell(db);
node->cap += 20;
}
fseek(db, node->ca, SEEK_SET);
fwrite((char *)(node->children), 1, node->cap * sizeof(uintptr_t), db);
}
fseek(db, node->addr, SEEK_SET);
fwrite(node, 1, sizeof(node_t), db);
}
void linkNode(node_t *parent, node_t *child, FILE *db)
{
if (parent == NULL || child == NULL)
{
printf("CANNOT LINK NULL NODE");
return;
}
if (findChildAddr(parent, child->name, db) != 1)
{
printf("%s%s%s%s%s", "Child '", child->name, "'' already exists in '", parent->name, "'");
return;
}
if (child->saved == false)
writeNode(child, db);
parent->children[parent->size++] = child->addr;
parent->hasChild = true;
child->parent = (uintptr_t *)parent->addr;
child->linked = true;
saveNode(parent, db);
saveNode(child, db);
}
node_t *createDB(FILE *db_file)
{
node_t *db = createRNode("root");
writeNode(db, db_file);
return db;
}
int writeContent(node_t *node, char *content, FILE *db)
{
if (node->access != WRITE && node->access != READ_WRITE)
{
return -1;
}
if (node->hasChild == true)
{
printf("%s%s%s", "Node '", node->name, "' cannot hold values since its a parent node");
return -2;
}
fseek(db, 0, SEEK_END);
node->content = (char *)ftell(db);
int n = strlen(content);
node->size = n;
fwrite(content, 1, n + 1, db);
saveNode(node, db);
return 0;
}
void deleteNode(node_t *node, FILE *db)
{
if (node == NULL)
{
printf("CANNOT DELETE NULL NODE!");
return;
}
node_t *parent = getNodeFromAddr((node_t *)(node->parent), db);
for (int i = 0; i < parent->size; i++)
{
if (parent->children[i] == node->addr)
{
for (int j = i; j < parent->size; j++)
{
parent->children[j] = parent->children[j + 1];
}
parent->size--;
break;
}
}
saveNode(parent, db);
}