aboutsummaryrefslogtreecommitdiff
path: root/tgeb/anbieter.c
blob: 575ed03a5c6327c6f4e5e1de192603482d784e75 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* $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_an(struct an *, FILE *);	/* read header */
static void rent_an(struct an *, FILE *);	/* read entries */
static void resort_an(struct an *);

struct an *
tgeb_read_an(char *file)
{
	struct an *an;
	FILE   *fd;

	if (!(fd = fopen(file, "r")))
		err(1, "fopen %s", file);

	if (!(an = (struct an *) malloc(sizeof(struct an))))
		err(1, "malloc");

	rhdr_an(an, fd);
	rent_an(an, fd);

	fclose(fd);
	return an;
}

void
rhdr_an(struct an * an, FILE * fd)
{
	int     i;

	fread(&an->q, sizeof(an->q), 1, fd);
	if (!(an->h = (struct an_hdr *) calloc(an->q, sizeof(struct an_hdr))))
		err(1, "malloc");

	fseek(fd, 2, SEEK_SET);
	for (i = 0; i < an->q; i++) {
		_tgeb_sread(&an->h[i].name, fd);
		_tgeb_sread(&an->h[i].mail, fd);
		_tgeb_sread(&an->h[i].url, fd);
		fread(&an->h[i].off, sizeof(an->h[i].off), 1, fd);
		an->h[i].off--;
	}
	return;
}

void
rent_an(struct an * an, FILE * fd)
{
	int     i, j;

	an->e_q = 0;
	for (i = 0; i < an->q; i++) {
		fseek(fd, an->h[i].off, SEEK_SET);
		fread(&an->h[i].q, sizeof(an->h[i].q), 1, fd);
		if (!(an->h[i].d = (struct an_d *)
		      calloc(an->h[i].q, sizeof(struct an_d))))
			err(1, "malloc");
		for (j = 0; j < an->h[i].q; j++) {
			an->h[i].d[j].h = &an->h[i];
			_tgeb_sread(&an->h[i].d[j].serv, fd);
			_tgeb_sread(&an->h[i].d[j].pref, fd);
			fread(&an->h[i].d[j].id,
			      sizeof(an->h[i].d[j].id), 1, fd);
			if (an->h[i].d[j].id > an->e_q)
				an->e_q = an->h[i].d[j].id;
			_tgeb_sread(&an->h[i].d[j].valf, fd);
			_tgeb_sread(&an->h[i].d[j].valt, fd);
			fread(&an->h[i].d[j].type,
			      sizeof(an->h[i].d[j].type), 1, fd);
			fread(&an->h[i].d[j].mvol,
			      sizeof(an->h[i].d[j].mvol), 1, fd);
			fread(&an->h[i].d[j].bfee,
			      sizeof(an->h[i].d[j].bfee), 1, fd);
			fread(&an->h[i].d[j].afee,
			      sizeof(an->h[i].d[j].afee), 1, fd);
			_tgeb_sread(&an->h[i].d[j].url, fd);
			_tgeb_sread(&an->h[i].d[j].url2, fd);
			_tgeb_sread(&an->h[i].d[j].phone, fd);
			_tgeb_sread(&an->h[i].d[j].mail, fd);
			_tgeb_sread(&an->h[i].d[j].x, fd);
			fread(&an->h[i].d[j].reg,
			      sizeof(an->h[i].d[j].reg), 1, fd);
			_tgeb_sread(&an->h[i].d[j].rurl, fd);
			_tgeb_sread(&an->h[i].d[j].rfax, fd);
		}
	}
	resort_an(an);
	return;
}

void
resort_an(struct an * an)
{
	int     i, j;

	if (!(an->e = (struct an_d *) calloc(an->e_q, sizeof(struct an_d))))
		err(1, "malloc");
	for (i = 0; i < an->e_q; i++) {
		for (j = 0; j < an->h[i].q; j++) {
			an->e[an->h[i].d[j].id - 1] = an->h[i].d[j];
		}
	}
	return;
}