annotate Meerwald/coord.c @ 7:2b350281f8b0

fix number of bits written
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Wed, 16 Apr 2008 15:55:52 +0200
parents be303a3f5ea8
children f83ef905a63d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include <stdlib.h>
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 #include <stdio.h>
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 #include "coord.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 struct coords *alloc_coords(int n) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 struct coords *c;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 if (c = malloc(sizeof(struct coords)))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 init_coords(c, n);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 fprintf(stderr, "alloc_coords(): malloc failed\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 return c;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 void free_coords(struct coords *c) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 if (!c)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 fprintf(stderr, "free_coords(): got NULL pointer\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 free(c->values);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 free(c);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 int init_coords(struct coords *c, int n) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 if (!c)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 fprintf(stderr, "init_coords(): got NULL poiner\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 if (n <= 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 fprintf(stderr, "init_coords(): n out of range\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 c->count = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 c->max = n;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 if (c->values = malloc(n * sizeof(struct coord)))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 return 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 int add_coord(struct coords *c, int x, int y) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 struct coord *v;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 int n;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 if (!c)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 fprintf(stderr, "add_coord(): got NULL pointer\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 if (c->count >= c->max)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 fprintf(stderr, "add_coord(): maximum reached\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 v = c->values;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 for (n = 0; n < c->count; v++, n++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 if (v->x == x && v->y == y) break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 if (n == c->count) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 v->x = x;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 v->y = y;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 c->count++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 return 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.