-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.c
357 lines (265 loc) · 5.22 KB
/
list.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
NodePtr create_node(char* name, off_t offset, NodePtr prev, NodePtr next)
{
NodePtr new_node = malloc(sizeof(node));
if (new_node == NULL)
{
return NULL;
}
new_node->name = name;
new_node->offset = offset;
new_node->prev = prev;
new_node->next = next;
return new_node;
}
void node_destroy(NodePtr* node)
{
free((*node)->name);
free(*node);
node = NULL;
}
Stack_List* create_List(void)
{
Stack_List* new_List = malloc(sizeof(Stack_List));
if (new_List == NULL)
{
perror("malloc");
return NULL;
}
new_List->head = NULL;
new_List->tail = NULL;
new_List->size = 0;
return new_List;
}
Stack_List* copy_List(Stack_List* list)
{
if (is_Empty(list))
{
return NULL;
}
/* create the new list */
Stack_List* copy_list = create_List();
if (copy_list == NULL)
{
return NULL;
}
char* new_name = malloc((strlen(list->head->name) + 1) * sizeof(char));
if (new_name == NULL)
{
perror("malloc() error");
free(copy_list);
return NULL;
}
strcpy(new_name, list->head->name);
/* copy the first node */
copy_list->head = create_node(new_name, list->head->offset, NULL, NULL);
if (copy_list->head == NULL)
{
perror("malloc() error in new_node()");
free(new_name);
free(copy_list);
return NULL;
}
copy_list->tail = copy_list->head;
int len = list->size;
/* if the list has more than 1 nodes, copy the rest of them */
if (len > 1)
{
NodePtr temp = list->head->next;
NodePtr temp_copy = copy_list->head;
int i = 1;
/* iterate to copy the nodes */
for (; i < len; i++)
{
char* temp_name = malloc((strlen(temp->name) + 1) * sizeof(char));
if (temp_name == NULL)
{
perror("malloc() error");
free(copy_list);
return NULL;
}
strcpy(temp_name, temp->name);
temp_copy->next = create_node(temp_name, temp->offset, temp_copy, NULL);
if (temp_copy->next == NULL)
{
perror("malloc() error in new_node()");
free(temp_name);
free(copy_list);
return NULL;
}
temp = temp->next;
temp_copy = temp_copy->next;
}
/* set the tail */
copy_list->tail = temp_copy;
}
copy_list->size = len;
return copy_list;
}
int is_Empty(Stack_List* list)
{
return list->size == 0;
}
uint get_Stack_List_Size(Stack_List* list)
{
return list->size;
}
int is_in_Root(Stack_List* list)
{
return list->size == 1;
}
int Stack_List_Push(Stack_List* list, char* name, off_t offset)
{
if (is_Empty(list))
{
NodePtr new_node = create_node(name, offset, NULL, NULL);
if (new_node == NULL)
{
return 0;
}
list->head = new_node;
list->tail = new_node;
}
else
{
NodePtr new_node = create_node(name, offset, list->tail, NULL);
if (new_node == NULL)
{
return 0;
}
list->tail->next = new_node;
list->tail = new_node;
}
list->size++;
return 1;
}
int Stack_List_Pop(Stack_List* list)
{
if (is_Empty(list))
{
return 0;
}
if (list->size == 1)
{
node_destroy(&(list->tail));
list->head = NULL;
list->tail = NULL;
}
else
{
NodePtr temp = list->tail->prev;
node_destroy(&(list->tail));
list->tail = temp;
temp->next = NULL;
}
list->size--;
return 1;
}
int Stack_List_Peek(Stack_List* list, char** name, off_t* offset)
{
if (is_Empty(list))
{
return 0;
}
strcpy(*name, list->tail->name);
*offset = list->tail->offset;
return 1;
}
off_t Stack_List_Peek_offset(Stack_List* list)
{
if (is_Empty(list))
{
return (off_t) 0;
}
return list->tail->offset;
}
int Stack_List_Print(Stack_List* list)
{
if (is_Empty(list))
{
return 0;
}
NodePtr temp = list->head;
while (temp != NULL)
{
printf("Directory: %s offset: %lu\n", temp->name, temp->offset);
temp = temp->next;
}
return 1;
}
int Stack_List_Print_Path(Stack_List* list)
{
if (is_Empty(list))
{
return 0;
}
NodePtr temp = list->head;
while (temp != NULL)
{
printf("/%s", temp->name);
temp = temp->next;
}
printf("\n");
return 1;
}
int Stack_List_Print_Directories(Stack_List* list, int n)
{
if (list == NULL || n <= 0)
{
return 0;
}
uint number_of_prints = n;
if (list->size < number_of_prints)
{
number_of_prints = list->size;
}
NodePtr temp = list->tail;
int i = 1;
for (; i < number_of_prints; i++)
{
temp = temp->prev;
}
// [0;34m Blue
// [1;34m Bold Blue
/* set print colour to Bold Blue */
printf("\033[1;34m");
if (list->size > number_of_prints)
{
printf("~");
}
while (number_of_prints > 0)
{
printf("/%s", temp->name);
temp = temp->next;
number_of_prints--;
}
/* set print colour back to normal */
printf("\033[0m");
return 1;
}
int Stack_List_Empty(Stack_List* list)
{
if (list == NULL || is_Empty(list))
{
return 0;
}
while (Stack_List_Pop(list));
return 1;
}
int Stack_List_Destroy(Stack_List** list)
{
if (*list == NULL)
{
return 0;
}
while (Stack_List_Pop(*list));
(*list)->head = NULL;
(*list)->tail = NULL;
(*list)->size = 0;
free(*list);
*list = NULL;
return 1;
}