aboutsummaryrefslogtreecommitdiff
path: root/lib/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/common.c')
-rw-r--r--lib/common.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/lib/common.c b/lib/common.c
new file mode 100644
index 0000000..56f7fd1
--- /dev/null
+++ b/lib/common.c
@@ -0,0 +1,119 @@
+/* $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 <tgeb.h>
+
+typedef struct tosort {
+ int id;
+ float fee;
+} TOSORT;
+
+static int compare(const void *val1, const void *val2);
+static TA_D *selectdt(TA_D * d, int time);
+
+void
+_tgeb_sread(char **data, FILE * fd)
+{
+ unsigned short len;
+
+ fread(&len, sizeof(len), 1, fd);
+ if (!(*data = calloc(len + 1, sizeof(**data))))
+ err(1, "malloc");
+ fread(*data, sizeof(**data), len, fd);
+ return;
+}
+
+SORTED *
+tgeb_select(TA * ta, AN * an,
+ int reg, int flags, int time)
+{
+ 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");
+ 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);
+ }
+
+ free(s);
+ return sd;
+}
+
+int
+compare(const void *val1, const void *val2)
+{
+ TOSORT *s1 = (TOSORT *) val1;
+ TOSORT *s2 = (TOSORT *) val2;
+
+ return ((s1->fee > s2->fee) ? 1 : (s1->fee < s2->fee) ? -1 : 0);
+}
+
+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);
+}