-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimitives.c
126 lines (107 loc) · 3.5 KB
/
primitives.c
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
125
126
#include "primitives.h"
//* String functions
char *strclone(const char *s) {
int size = strlen(s) + 1;
char *ret = (char *)malloc(size);
SYSEXPECT(ret != NULL);
memcpy(ret, s, size);
return ret;
}
//* point_t
char *point_str(point_t point, int channel) {
static char buf[POINT_CHANNEL_COUNT][POINT_CHANNEL_SIZE];
assert(channel >= 0 && channel < POINT_CHANNEL_COUNT);
snprintf(buf[channel], POINT_CHANNEL_SIZE, "<%.3lf, %.3lf>", point.x, point.y);
return buf[channel];
}
//* bar_t
bar_t *bar_init() {
bar_t *bar = (bar_t *)malloc(sizeof(bar_t));
SYSEXPECT(bar != NULL);
memset(bar, 0x00, sizeof(bar_t));
return bar;
}
bar_t *bar_init_name(char *name) {
bar_t *bar = bar_init();
if(name != NULL) {
bar->name = strclone(name);
}
return bar;
}
void bar_free(bar_t *bar) {
if(bar->name != NULL) {
free(bar->name);
}
free(bar);
return;
}
void bar_set_error(const char *target, const char *existing1, const char *existing2) {
error_exit("Could not set %s when %s and %s are both set\n", target, existing1, existing2);
}
void bar_set_tl(bar_t *bar, point_t tl) {
// Check if tr or bl is set
if((bar->pos_mask & BAR_POS_MASK_TR) && (bar->pos_mask & BAR_POS_MASK_WIDTH)) {
bar_set_error("top-left", "top-right", "width");
} else if((bar->pos_mask & BAR_POS_MASK_BL) && (bar->pos_mask & BAR_POS_MASK_HEIGHT)) {
bar_set_error("top-left", "bottom-left", "height");
}
bar->tl = tl;
bar->pos_mask |= BAR_POS_MASK_TL;
return;
}
void bar_set_tr(bar_t *bar, point_t tr) {
// Check if tl or br is set
if((bar->pos_mask & BAR_POS_MASK_TL) && (bar->pos_mask & BAR_POS_MASK_WIDTH)) {
bar_set_error("top-right", "top-left", "width");
} else if((bar->pos_mask & BAR_POS_MASK_BR) && (bar->pos_mask & BAR_POS_MASK_HEIGHT)) {
bar_set_error("top-right", "bottom-right", "height");
}
bar->tr = tr;
bar->pos_mask |= BAR_POS_MASK_TR;
return;
}
void bar_set_bl(bar_t *bar, point_t bl) {
// Check if tl or br is set
if((bar->pos_mask & BAR_POS_MASK_TL) && (bar->pos_mask & BAR_POS_MASK_HEIGHT)) {
bar_set_error("bottom-left", "top-left", "height");
} else if((bar->pos_mask & BAR_POS_MASK_BR) && (bar->pos_mask & BAR_POS_MASK_WIDTH)) {
bar_set_error("bottom-left", "bottom-right", "width");
}
bar->bl = bl;
bar->pos_mask |= BAR_POS_MASK_BL;
return;
}
void bar_set_br(bar_t *bar, point_t br) {
// Check if tr or bl is set
if((bar->pos_mask & BAR_POS_MASK_TR) && (bar->pos_mask & BAR_POS_MASK_HEIGHT)) {
bar_set_error("bottom-right", "top-right", "height");
} else if((bar->pos_mask & BAR_POS_MASK_BL) && (bar->pos_mask & BAR_POS_MASK_WIDTH)) {
bar_set_error("bottom-right", "bottom-left", "width");
}
bar->br = br;
bar->pos_mask |= BAR_POS_MASK_BR;
return;
}
void bar_set_width(bar_t *bar, double width) {
bar->width = width;
bar->pos_mask |= BAR_POS_MASK_WIDTH;
return;
}
void bar_set_height(bar_t *bar, double height) {
bar->height = height;
bar->pos_mask |= BAR_POS_MASK_HEIGHT;
return;
}
//void bar_validate(bar_t *bar) {
//}
void bar_print(bar_t *bar) {
printf("bar %s pos", (bar->name == NULL) ? "<NULL>" : bar->name);
printf(" tl %s tr %s bl %s br %s",
point_str(bar->tl, 0), point_str(bar->tr, 1), point_str(bar->bl, 2), point_str(bar->br, 3));
printf(" width %.3lf height %.3lf", bar->width, bar->height);
printf(" pos_mask 0x%X", bar->pos_mask);
printf(" fill color 0x%06X edge color 0x%06X edge width %.3lf hatch \'%c'",
bar->fill_color, bar->edge_color, bar->edge_width, bar->hatch);
putchar('\n');
return;
}