summaryrefslogtreecommitdiff
path: root/lib/rpmps.c
blob: 50b7d29c4c63771fd87f825fec8f78fae3f28838 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
 * \file lib/rpmps.c
 */

#include "system.h"

#include <inttypes.h>
#include <stdlib.h>

#include <rpm/rpmstring.h>
#include <rpm/rpmps.h>

#include "debug.h"

struct rpmps_s {
    int numProblems;		/*!< Current probs array size. */
    int numProblemsAlloced;	/*!< Allocated probs array size. */
    rpmProblem *probs;		/*!< Array of pointers to specific problems. */
    int nrefs;			/*!< Reference count. */
};

struct rpmpsi_s {
    int ix;
    rpmps ps;
};


static rpmps rpmpsUnlink(rpmps ps)
{
    if (ps) {
	ps->nrefs--;
    }
    return NULL;
}

rpmps rpmpsLink(rpmps ps)
{
    if (ps) {
	ps->nrefs++;
    }
    return ps;
}

int rpmpsNumProblems(rpmps ps)
{
    int numProblems = 0;
    if (ps && ps->probs)
	numProblems = ps->numProblems;
    return numProblems;
}

rpmpsi rpmpsInitIterator(rpmps ps)
{
    rpmpsi psi = NULL;
    if (ps != NULL && ps->numProblems > 0) {
	psi = xcalloc(1, sizeof(*psi));
	psi->ps = rpmpsLink(ps);
	psi->ix = -1;
    }
    return psi;
}

rpmpsi rpmpsFreeIterator(rpmpsi psi)
{
    if (psi != NULL) {
	rpmpsUnlink(psi->ps);
	free(psi);
    }
    return NULL;
}

rpmProblem rpmpsiNext(rpmpsi psi)
{
    rpmProblem p = NULL;
    if (psi != NULL && psi->ps != NULL && ++psi->ix >= 0) {
	rpmps ps = psi->ps;
	if (psi->ix < ps->numProblems) {
	    p = ps->probs[psi->ix];
	} else {
	    psi->ix = -1;
	}
    }
    return p;
}

int rpmpsNextIterator(rpmpsi psi)
{
    return (rpmpsiNext(psi) != NULL) ? psi->ix : -1;
}

rpmProblem rpmpsGetProblem(rpmpsi psi)
{
    rpmProblem p = NULL;
    if (psi != NULL && psi->ix >= 0 && psi->ix < rpmpsNumProblems(psi->ps)) {
	p = psi->ps->probs[psi->ix];
    } 
    return p;
}

rpmps rpmpsCreate(void)
{
    rpmps ps = xcalloc(1, sizeof(*ps));
    return rpmpsLink(ps);
}

rpmps rpmpsFree(rpmps ps)
{
    if (ps == NULL) return NULL;
    if (ps->nrefs > 1) {
	return rpmpsUnlink(ps);
    }
	
    if (ps->probs) {
	rpmpsi psi = rpmpsInitIterator(ps);
	while (rpmpsNextIterator(psi) >= 0) {
	    rpmProblemFree(rpmpsGetProblem(psi));	
	}
	rpmpsFreeIterator(psi);
	ps->probs = _free(ps->probs);
    }
    ps = _free(ps);
    return NULL;
}

void rpmpsAppendProblem(rpmps ps, rpmProblem prob)
{
    if (ps == NULL || prob == NULL) return;

    if (ps->numProblems == ps->numProblemsAlloced) {
	if (ps->numProblemsAlloced)
	    ps->numProblemsAlloced *= 2;
	else
	    ps->numProblemsAlloced = 2;
	ps->probs = xrealloc(ps->probs,
			ps->numProblemsAlloced * sizeof(*ps->probs));
    }

    ps->probs[ps->numProblems] = rpmProblemLink(prob);
    ps->numProblems++;
}

/*
 * TODO: filter out duplicates while merging. Also horribly inefficient... */
int rpmpsMerge(rpmps dest, rpmps src)
{
    int rc = 0;
    if (dest != NULL) {
	rpmProblem p;
	rpmpsi spi = rpmpsInitIterator(src);
	while ((p = rpmpsiNext(spi)) != NULL) {
	    rpmpsAppendProblem(dest, p);
	    rc++;
	}
	rpmpsFreeIterator(spi);
    }
    return rc;
}

void rpmpsPrint(FILE *fp, rpmps ps)
{
    rpmProblem p;
    rpmpsi psi = rpmpsInitIterator(ps);
    FILE *f = (fp != NULL) ? fp : stderr;

    while ((p = rpmpsiNext(psi))) {
	char *msg = rpmProblemString(p);
	fprintf(f, "\t%s\n", msg);
	free(msg);
    }
    rpmpsFreeIterator(psi);
}