File: setdiff.c

package info (click to toggle)
libavl 0.3.5-4.1
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 152 kB
  • sloc: ansic: 671; makefile: 39
file content (90 lines) | stat: -rw-r--r-- 2,186 bytes parent folder | download | duplicates (5)
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
/*****************************************************************************

    setdiff.c - Example program for the AVL-tree library.

    Copyright (C) 1998  Michael H. Buselli <cosine@cosine.org>
    Copyright (C) 2000-2002  Wessel Dankers <wsl@nl.linux.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

*****************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "avl.h"

int depth(avl_node_t *n) {
	int d;
	for(d = 0; n; n = n->parent)
		d++;
	return d;
}

void readinto(avl_tree_t *t, char *fname) {
	FILE *f;
	char *s, *e;
	int len;

	f = fopen(fname, "r");
	if(!f) {
		perror(fname);
		exit(1);
	}

	while((e = s = malloc(65536)) && fgets(s, 65536, f)) {
		len = strlen(s);
		if(s[len-1] == '\n')
			s[len-1] = '\0';
		else
			len++;
		e = s = realloc(s, len);
		if(!s)
			break;
		avl_insert(t, s);
	}
	if(!e) {
		perror("malloc()");
		exit(2);
	}
	fclose(f);
}

int main(int argc, char **argv) {
	avl_tree_t t, u;
	avl_node_t *c;

	if(argc != 3) {
		fprintf(stderr, "Requires exactly 2 arguments.\n");
		exit(2);
	}

	avl_init_tree(&t, (avl_compare_t)strcmp, NULL);
	avl_init_tree(&u, (avl_compare_t)strcmp, NULL);

	readinto(&t, argv[1]);
	readinto(&u, argv[2]);

	for(c = t.head; c; c = c->next)
		if(!avl_search(&u, c->item))
			printf("-%s\n", c->item);

	for(c = u.head; c; c = c->next)
		if(!avl_search(&t, c->item))
			printf("+%s\n", c->item);

	return 0;
}