aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/ci_queue.c
blob: 0928ff90a9e769f4a2f50f270133c02a7de16cf3 (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
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
/*
 * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
 * This file is part of the "cimple" project.
 * For details, see https://github.com/egor-tensin/cimple.
 * Distributed under the MIT License.
 */

#include "ci_queue.h"
#include "log.h"

#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>

struct ci_queue_entry {
	char *url;
	char *rev;
	STAILQ_ENTRY(ci_queue_entry) entries;
};

int ci_queue_entry_create(struct ci_queue_entry **_entry, const char *_url, const char *_rev)
{
	struct ci_queue_entry *entry = malloc(sizeof(struct ci_queue_entry));
	if (!entry) {
		log_errno("malloc");
		goto fail;
	}

	char *url = strdup(_url);
	if (!url) {
		log_errno("strdup");
		goto free_entry;
	}

	char *rev = strdup(_rev);
	if (!rev) {
		log_errno("strdup");
		goto free_url;
	}

	entry->url = url;
	entry->rev = rev;

	*_entry = entry;
	return 0;

free_url:
	free(url);

free_entry:
	free(entry);

fail:
	return -1;
}

void ci_queue_entry_destroy(struct ci_queue_entry *entry)
{
	free(entry->rev);
	free(entry->url);
	free(entry);
}

const char *ci_queue_entry_get_url(const struct ci_queue_entry *entry)
{
	return entry->url;
}

const char *ci_queue_entry_get_rev(const struct ci_queue_entry *entry)
{
	return entry->rev;
}

void ci_queue_create(struct ci_queue *queue)
{
	STAILQ_INIT(queue);
}

void ci_queue_destroy(struct ci_queue *queue)
{
	struct ci_queue_entry *entry1 = STAILQ_FIRST(queue);
	while (entry1) {
		struct ci_queue_entry *entry2 = STAILQ_NEXT(entry1, entries);
		ci_queue_entry_destroy(entry1);
		entry1 = entry2;
	}
	STAILQ_INIT(queue);
}

int ci_queue_is_empty(const struct ci_queue *queue)
{
	return STAILQ_EMPTY(queue);
}

void ci_queue_add_last(struct ci_queue *queue, struct ci_queue_entry *entry)
{
	STAILQ_INSERT_TAIL(queue, entry, entries);
}

void ci_queue_add_first(struct ci_queue *queue, struct ci_queue_entry *entry)
{
	STAILQ_INSERT_HEAD(queue, entry, entries);
}

struct ci_queue_entry *ci_queue_remove_first(struct ci_queue *queue)
{
	struct ci_queue_entry *entry = STAILQ_FIRST(queue);
	STAILQ_REMOVE_HEAD(queue, entries);
	return entry;
}