aboutsummaryrefslogtreecommitdiff
path: root/tgeb/tarife.c
diff options
context:
space:
mode:
Diffstat (limited to 'tgeb/tarife.c')
-rw-r--r--tgeb/tarife.c161
1 files changed, 161 insertions, 0 deletions
diff --git a/tgeb/tarife.c b/tgeb/tarife.c
new file mode 100644
index 0000000..f73a7b5
--- /dev/null
+++ b/tgeb/tarife.c
@@ -0,0 +1,161 @@
+/* $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 "tgebdat.h"
+
+static void rhdr_ta(struct ta *, FILE *); /* read header */
+static void ridx_ta(struct ta *, FILE *); /* read index */
+static void rdat_ta(struct ta *, FILE *); /* read data */
+static void rdat2_ta(struct ta_d *, FILE *); /* read chain */
+static void maxtoq(struct au *, struct ta *);
+
+struct ta *
+tgeb_read_ta(char *file, struct au * au)
+{
+ struct ta *ta;
+ FILE *fd;
+
+ if (!(fd = fopen(file, "r")))
+ err(1, "fopen %s", file);
+
+ if (!(ta = (struct ta *) malloc(sizeof(struct ta))))
+ err(1, "malloc");
+
+ maxtoq(au, ta);
+ rhdr_ta(ta, fd);
+ ridx_ta(ta, fd);
+ rdat_ta(ta, fd);
+
+ fclose(fd);
+ return ta;
+}
+
+void
+rhdr_ta(struct ta * ta, FILE * fd)
+{
+ int i;
+
+ fread(&ta->q, sizeof(ta->q), 1, fd);
+ if (!(ta->h = (struct ta_hdr *) calloc(ta->q, sizeof(struct ta_hdr))))
+ err(1, "malloc");
+
+ fseek(fd, 2, SEEK_SET);
+ for (i = 0; i < ta->q; i++) {
+ fread(&ta->h[i].off_in, sizeof(ta->h[i].off_in), 1, fd);
+ ta->h[i].off_in ? ta->h[i].off_in-- : ta->h[i].off_in;
+ fread(&ta->h[i].off_aus, sizeof(ta->h[i].off_aus), 1, fd);
+ ta->h[i].off_aus ? ta->h[i].off_aus-- : ta->h[i].off_aus;
+ }
+ return;
+}
+
+void
+ridx_ta(struct ta * ta, FILE * fd)
+{
+ int i, j;
+
+ for (i = 0; i < ta->q; i++) {
+ /* read inland offsets */
+ if (!(ta->h[i].in = (struct ta_idx *)
+ calloc(Q_IN, sizeof(struct ta_idx))))
+ err(1, "malloc");
+ fseek(fd, ta->h[i].off_in, SEEK_SET);
+ for (j = 0; j < Q_IN; j++) {
+ fread(&ta->h[i].in[j].off,
+ sizeof(ta->h[i].in[j].off), 1, fd);
+ if (ta->h[i].in[j].off)
+ ta->h[i].in[j].off--;
+ }
+ /* read ausland offsets */
+ if (!(ta->h[i].aus = (struct ta_idx *)
+ calloc(ta->q_aus[i], sizeof(struct ta_idx))))
+ err(1, "malloc");
+ fseek(fd, ta->h[i].off_aus, SEEK_SET);
+ for (j = 0; j < ta->q_aus[i]; j++) {
+ fread(&ta->h[i].aus[j].off,
+ sizeof(ta->h[i].aus[j].off), 1, fd);
+ if (ta->h[i].aus[j].off)
+ ta->h[i].aus[j].off--;
+ }
+ }
+ return;
+}
+
+void
+rdat_ta(struct ta * ta, FILE * fd)
+{
+ int i, j;
+
+ for (i = 0; i < ta->q; i++) {
+ /* read inland data */
+ for (j = 0; j < Q_IN; j++) {
+ if (ta->h[i].in[j].off) {
+ fseek(fd, ta->h[i].in[j].off, SEEK_SET);
+
+ if (!(ta->h[i].in[j].data = (struct ta_d *)
+ malloc(sizeof(struct ta_d))))
+ err(1, "malloc");
+ rdat2_ta(ta->h[i].in[j].data, fd);
+ }
+ }
+ /* read ausland data */
+ for (j = 0; j < ta->q_aus[i]; j++) {
+ if (ta->h[i].aus[j].off) {
+ fseek(fd, ta->h[i].aus[j].off, SEEK_SET);
+
+ if (!(ta->h[i].aus[j].data = (struct ta_d *)
+ malloc(sizeof(struct ta_d))))
+ err(1, "malloc");
+ rdat2_ta(ta->h[i].aus[j].data, fd);
+ }
+ }
+ }
+ return;
+}
+
+void
+rdat2_ta(struct ta_d * data, FILE * fd)
+{
+ fread(&data->time, sizeof(data->time), 1, fd);
+ fread(&data->dfee, sizeof(data->dfee), 1, fd);
+ fread(&data->fee, sizeof(data->fee), 1, fd);
+ fread(&data->t1, sizeof(data->t1), 1, fd);
+ fread(&data->t2, sizeof(data->t2), 1, fd);
+ if (data->time != 24) {
+ if (!(data->next = malloc(sizeof(struct ta_d))))
+ err(1, "malloc");
+ data->next->prev = data;
+ rdat2_ta(data->next, fd);
+ }
+ return;
+}
+
+void
+maxtoq(struct au * au, struct ta * ta)
+{
+ int i;
+
+ if (!(ta->q_aus = (unsigned short *)
+ calloc(au->q, sizeof(unsigned short))))
+ err(1, "malloc");
+ for (i = 0; i < au->q; i++)
+ ta->q_aus[i] = au->max[i] * 2;
+ return;
+}