summaryrefslogtreecommitdiff
path: root/tree.h
blob: 86a9521b228e18fcb0e341697e0c87b3ffeb960b (plain)
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
#pragma once

#include <stddef.h>
#include <stdint.h>


// The data field is a compact representation of a
// [(string, struct tree_node*)] list. The strings are all non-empty.
// Representation: for every list element, first a null-terminated sequence of
// chars (the string), then sizeof(void*) bytes (the pointer, potentially
// NULL). After the last list element, an additional 0 byte, which could be
// interpreted as an empty string, but instead means the end of the list.
struct tree_node {
  size_t total_files;
  size_t total_dirs;
  size_t total_size;  // sum of on-disk sizes (in bytes)
  uint8_t *data;
};

// A single element in a child list.
struct tree_pair {
  const char *name;  // ownership is retained by the producer of this tree_pair
  struct tree_node *sub;  // can be NULL
};

// The names are copied out of 'pairs'.
struct tree_node* tree_make(const struct tree_pair *pairs, size_t npairs, size_t disk_size);

// Frees recursively.
void tree_free(struct tree_node *node);

size_t tree_num_children(const struct tree_node *node);

struct tree_pair tree_index(struct tree_node *node, size_t index);

struct tree_iter { struct tree_node *node; size_t off; };
struct tree_iter tree_iter_start(struct tree_node *node);
// Returns whether there was another child
bool tree_iter_next(struct tree_iter *iter, struct tree_pair *dst);