Mercurial > hg > wm
comparison Meerwald-dir/coord.c @ 24:9f20bce6184e v0.7
move directories, support netpbm 11
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Fri, 20 Dec 2024 13:08:59 +0100 |
parents | Meerwald/coord.c@f83ef905a63d |
children |
comparison
equal
deleted
inserted
replaced
23:71dd4b96221b | 24:9f20bce6184e |
---|---|
1 #include <stdlib.h> | |
2 #include <stdio.h> | |
3 #include "coord.h" | |
4 | |
5 struct coords *alloc_coords(int n) { | |
6 struct coords *c; | |
7 | |
8 if ((c = malloc(sizeof(struct coords))) != NULL) | |
9 init_coords(c, n); | |
10 #ifdef DEBUG | |
11 else | |
12 fprintf(stderr, "alloc_coords(): malloc failed\n"); | |
13 #endif | |
14 | |
15 return c; | |
16 } | |
17 | |
18 void free_coords(struct coords *c) { | |
19 | |
20 #ifdef DEBUG | |
21 if (!c) | |
22 fprintf(stderr, "free_coords(): got NULL pointer\n"); | |
23 #endif | |
24 | |
25 free(c->values); | |
26 free(c); | |
27 } | |
28 | |
29 int init_coords(struct coords *c, int n) { | |
30 | |
31 #ifdef DEBUG | |
32 if (!c) | |
33 fprintf(stderr, "init_coords(): got NULL poiner\n"); | |
34 | |
35 if (n <= 0) | |
36 fprintf(stderr, "init_coords(): n out of range\n"); | |
37 #endif | |
38 | |
39 c->count = 0; | |
40 c->max = n; | |
41 | |
42 if ((c->values = malloc(n * sizeof(struct coord))) != NULL) | |
43 return 0; | |
44 else | |
45 return -1; | |
46 } | |
47 | |
48 int add_coord(struct coords *c, int x, int y) { | |
49 struct coord *v; | |
50 int n; | |
51 | |
52 #ifdef DEBUG | |
53 if (!c) | |
54 fprintf(stderr, "add_coord(): got NULL pointer\n"); | |
55 | |
56 if (c->count >= c->max) | |
57 fprintf(stderr, "add_coord(): maximum reached\n"); | |
58 #endif | |
59 | |
60 v = c->values; | |
61 | |
62 for (n = 0; n < c->count; v++, n++) | |
63 if (v->x == x && v->y == y) break; | |
64 | |
65 if (n == c->count) { | |
66 v->x = x; | |
67 v->y = y; | |
68 c->count++; | |
69 return 0; | |
70 } | |
71 else | |
72 return -1; | |
73 } | |
74 | |
75 |