aboutsummaryrefslogtreecommitdiff
path: root/tgeb/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'tgeb/common.c')
-rw-r--r--tgeb/common.c102
1 files changed, 63 insertions, 39 deletions
diff --git a/tgeb/common.c b/tgeb/common.c
index 22625c8..baf8a8a 100644
--- a/tgeb/common.c
+++ b/tgeb/common.c
@@ -21,14 +21,13 @@
#include <string.h>
#include "tgebdat.h"
-struct sort {
- int id;
- float fee;
-};
+typedef struct tosort {
+ int id;
+ float fee;
+} TOSORT;
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);
+static TA_D *selectdt(TA_D * d, int time);
void
_tgeb_sread(char **data, FILE * fd)
@@ -36,60 +35,85 @@ _tgeb_sread(char **data, FILE * fd)
unsigned short len;
fread(&len, sizeof(len), 1, fd);
- if (!(*data = (char *) calloc(len + 1, sizeof(**data))))
+ if (!(*data = 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,
+SORTED *
+tgeb_select(TA * ta, 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))))
+ int i;
+ int q = 0;
+ TOSORT *s;
+ SORTED *sd;
+
+ if (!(s = calloc(an->e_q, sizeof(TOSORT))))
+ err(1, "malloc");
+
+ 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(TOSORT), compare);
+
+ if (!(sd = malloc(sizeof(SORTED))))
err(1, "malloc");
- if (!(sd.data = calloc(q, sizeof(sd.data))))
+ bzero(sd, sizeof(SORTED));
+
+ 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);
- }
+ 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;
+ free(s);
+ return sd;
}
int
compare(const void *val1, const void *val2)
{
- struct sort *s1 = (struct sort *) val1;
- struct sort *s2 = (struct sort *) val2;
+ TOSORT *s1 = (TOSORT *) val1;
+ TOSORT *s2 = (TOSORT *) val2;
return ((s1->fee > s2->fee) ? 1 : (s1->fee < s2->fee) ? -1 : 0);
}
-struct ta_d *
-selectdt(struct ta_d * d, int time)
+TA_D *
+selectdt(TA_D * d, int time)
{
if (time >= d->time)
return (selectdt(d->next, time));
return d;
}
+
+void
+tgeb_free_mem(AN * an, AU * au, TA * ta)
+{
+ tgeb_free_ta(ta);
+ tgeb_free_au(au);
+ tgeb_free_an(an);
+ return;
+}
+
+void
+tgeb_free_sd(SORTED * sd)
+{
+ free(sd->id);
+ free(sd->data);
+ free(sd);
+}