directory_tree.c 1.56 KB
Newer Older
1
#include <assert.h>
Adam Blank's avatar
Adam Blank committed
2
#include <stdio.h>
3
#include <stdlib.h>
Adam Blank's avatar
Adam Blank committed
4
#include <string.h>
5
#include <sys/stat.h>
Adam Blank's avatar
Adam Blank committed
6
7
8

#include "directory_tree.h"

9
10
11
12
13
void init_node(node_t *node, char *name, node_type_t type) {
    if (!strlen(name)) {
        name = strdup("ROOT");
    }
    node->name = name;
14
    assert(node->name != NULL);
Adam Blank's avatar
Adam Blank committed
15
16
17
    node->type = type;
}

18
file_node_t *init_file_node(char *name, uint64_t size, char *contents) {
19
20
21
22
    file_node_t *node = malloc(sizeof(file_node_t));
    assert(node != NULL);
    init_node((node_t *) node, name, FILE_TYPE);
    node->size = size;
23
24
    node->contents = contents;
    return node;
Adam Blank's avatar
Adam Blank committed
25
26
}

27
directory_node_t *init_directory_node(char *name) {
28
29
30
31
32
    directory_node_t *node = malloc(sizeof(directory_node_t));
    assert(node != NULL);
    init_node((node_t *) node, name, DIRECTORY_TYPE);
    node->num_children = 0;
    node->children = NULL;
33
    return node;
Adam Blank's avatar
Adam Blank committed
34
35
}

36
37
38
void add_child_directory_tree(directory_node_t *dnode, node_t *child) {
    (void)dnode;
    (void)child;
Adam Blank's avatar
Adam Blank committed
39
40
}

41
42
void print_directory_tree(node_t *node) {
    (void)node;
Adam Blank's avatar
Adam Blank committed
43
44
}

45
46
47

void create_directory_tree(node_t *node) {
    (void)node;
Adam Blank's avatar
Adam Blank committed
48
49
50
}

void free_directory_tree(node_t *node) {
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    if (node->type == FILE_TYPE) {
        file_node_t *fnode = (file_node_t *)node;
        free(node->name);
        free(fnode->contents);
        free(node);
    }
    else {
        directory_node_t *dnode = (directory_node_t *)node;
        for (size_t i = 0; i < dnode->num_children; i++) {
            free_directory_tree(dnode->children[i]);
        }
        free(node->name);
        free(dnode->children);
        free(node);
    }
Adam Blank's avatar
Adam Blank committed
66
}