aboutsummaryrefslogtreecommitdiffstats
path: root/lists.c
blob: aa427b1253bc12e594eb87bd7e0bd5eec0a7d8d8 (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
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "lists.h"
#include "packages.h"

void * list_filter(const char * name, const char * version,
	struct list_item_base *b, unsigned int prefix)
{
	struct list_item * t;

	/* This could be modified to do a sorted search */

	if (!b)
		return NULL;

	for (t = b->start; t; t = t->next) {
		if (prefix) {
			if (strncmp(t->name, name, strlen(t->name)) == 0) {
				t->count++;
				return (void *) t;
			}
		 } else {
			if (strcmp(t->name, name) == 0) {
				t->count++;
				return (void *) t;
			}
		}
	}
	return NULL;

}

struct list_item * insert_into_list(const char * name, const char * version,
	struct list_item_base *b)
{

	int ret;
	struct list_item * t;
	struct list_item * new = malloc(sizeof(*new));

	memset(new, 0, sizeof(*new));

	new->count = 0;
	strncpy(new->name, name, PACKAGE_NAME_LENGTH);
	strncpy(new->version, version, PACKAGE_VERSION_LENGTH);

	if (b->start == NULL) {
		b->start = new;
		goto done;
	}

	ret = strncmp(name, b->start->name, PACKAGE_NAME_LENGTH);
	if (ret < 0) {
		new->next = b->start;
		b->start = new;
		new->prev = b->start;
		new->next->prev = new;
		goto done;
	}

	for (t = b->start; t; t = t->next) {
		if (t->next == NULL) {
			t->next = new;
			new->prev = t;
			new->next = NULL;
			goto done;
		}
		ret = strncmp(name, t->name, PACKAGE_NAME_LENGTH);
		if (ret > 0) {
			/* it is after t, check next */
			ret = strncmp(name, t->next->name, PACKAGE_NAME_LENGTH);
			if (ret < 0) { /* stick it here */
				new->next = t->next;
				new->prev = t;
				t->next = new;
				t->next->prev = t;
				goto done;
			}
		}
		if (ret == 0) {
			/* it is the same */
			b->number_items--;
			free(new);
			new = t;
			goto done;
		}
	}
done:
	b->number_items++;
	return new;
}

struct list_item_base * list_setup_base(void)
{
	struct list_item_base * new_base =
		malloc(sizeof(*new_base));
	new_base->number_items = 0;
	new_base->start = NULL;
	return new_base;
}

struct list_item_base * list_load(struct list_item_base * old_base,
	const char * filename, int report_error)
{
	FILE * file;
	char buf[1024];
	unsigned int ret;
	char name[PACKAGE_NAME_LENGTH];
	char version[PACKAGE_VERSION_LENGTH];
	struct list_item_base * base;


	if (old_base)
		base = old_base;
	 else 
		base = list_setup_base();

	if ((file = fopen(filename, "r")) == NULL) {
		if (report_error) 
			printf("Opening list file %s: %s\n",
				filename, strerror(errno));
		return NULL;
	}

	while (fgets(buf, sizeof(buf), file)) {
		if (buf[0] == '#') /* skip comments */
			continue; 

		ret = sscanf(buf, "%s %s", name, version);
		switch (ret) {
		case 1: /* just a name */
			insert_into_list(name, "", base);
			break;
		case 2: /* we have a version number with it */
			insert_into_list(name, version, base);
			break;
		default: /* skip line */
			break;
		}
	}
	fclose(file);
	return base;
}