aboutsummaryrefslogtreecommitdiffstats
path: root/src/cnki_outline_tree.c
blob: cf59d10456ce7f325fbb9b8d578ac2373f3e9196 (plain) (blame)
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
/*
 * Copyright (c) 2020-2021, yzrh <yzrh@noema.org>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdlib.h>

#include "cnki.h"

int
cnki_outline_tree(object_outline_tree_t **outline_tree,
	object_outline_t **outline, int *ids)
{
	if (*outline_tree != NULL || *outline == NULL)
		return 1;

	int pos = 0;

	*outline_tree = malloc(sizeof(object_outline_tree_t));

	if (*outline_tree == NULL)
		return 1;

	object_outline_tree_t *tree = *outline_tree;

	tree->id = ids[pos++];
	tree->item = NULL;
	tree->up = NULL;
	tree->left = NULL;
	tree->right = NULL;

	object_outline_t *ptr = *outline;
	while (ptr != NULL) {
		if (tree->item == NULL ||
			ptr->depth == tree->item->depth) {
			while (tree->left != NULL)
				tree = tree->left;

			tree->left = malloc(sizeof(object_outline_tree_t));

			if (tree->left == NULL)
				return 1;

			tree->left->id = ids[pos++];
			tree->left->item = ptr;
			tree->left->up = tree;
			tree->left->left = NULL;
			tree->left->right = NULL;

			tree = tree->left;
		} else if (ptr->depth == tree->item->depth + 1) {
			tree->right = malloc(sizeof(object_outline_tree_t));

			if (tree->right == NULL)
				return 1;

			tree->right->id = ids[pos++];
			tree->right->item = ptr;
			tree->right->up = tree;
			tree->right->left = NULL;
			tree->right->right = NULL;

			tree = tree->right;
		} else {
			tree = tree->up;
			continue;
		}
		ptr = ptr->next;
	}

	return 0;
}