aboutsummaryrefslogtreecommitdiff
path: root/tgeb/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'tgeb/common.c')
-rw-r--r--tgeb/common.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/tgeb/common.c b/tgeb/common.c
new file mode 100644
index 0000000..22625c8
--- /dev/null
+++ b/tgeb/common.c
@@ -0,0 +1,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;
+}