aboutsummaryrefslogtreecommitdiff
path: root/tgeb/common.c
blob: 22625c8826bb24169f9ee97fa0c8af803faac936 (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
/* $Id$ */
/*
 * Copyright (c) 2004 demon <demon@vhost.dyndns.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <string.h>
#include "tgebdat.h"

struct sort {
	int	id;
	float	fee;
};

static int compare(const void *val1, const void *val2);
static struct ta_d * selectdt(struct ta_d * d, int time);
static void prdt(struct an_d * an_d, struct ta_d * ta_d);

void
_tgeb_sread(char **data, FILE * fd)
{
	unsigned short len;

	fread(&len, sizeof(len), 1, fd);
	if (!(*data = (char *) calloc(len + 1, sizeof(**data))))
		err(1, "malloc");
	fread(*data, sizeof(**data), len, fd);
	return;
}

struct sorted
tgeb_select(struct ta * ta, struct an * an,
	    int reg, int flags, int time)
{
        int     i;
        int     q = 0;
        struct sort *s = (struct sort *) calloc(an->e_q, sizeof(struct sort));
	struct sorted sd;

        for (i = 0; i < an->e_q; i++) {
                if (!(flags & an->e[i].type))
                        continue;
                if (!((flags & T_0190) || strncmp("0190", an->e[i].pref, 4)))
                        continue;
                if (ta->h[i].in[reg].off) {
                        s[q].id = i;
                        s[q].fee = (selectdt(ta->h[i].in[reg].data, time))->fee;
                        q++;
                }
        }
        qsort(s, q, sizeof(*s), compare);
	sd.q = q;
	if (!(sd.id = calloc(q, sizeof(sd.id))))
		err(1, "malloc");
	if (!(sd.data = calloc(q, sizeof(sd.data))))
		err(1, "malloc");
        for (i = 0; i < q; i++) {
		sd.id[i] = s[i].id;
		sd.data[i] = selectdt(ta->h[s[i].id].in[reg].data, time);
        }

        free(s);
        return sd;
}

int
compare(const void *val1, const void *val2)
{
	struct sort *s1 = (struct sort *) val1;
	struct sort *s2 = (struct sort *) val2;

	return ((s1->fee > s2->fee) ? 1 : (s1->fee < s2->fee) ? -1 : 0);
}

struct ta_d *
selectdt(struct ta_d * d, int time)
{
	if (time >= d->time)
		return (selectdt(d->next, time));
	return d;
}