-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhelpers.c
141 lines (115 loc) · 2.39 KB
/
helpers.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Copyright (c) 2016-2018 Tudor Ioan Roman. All rights reserved. */
/* Licensed under the ISC License. See the LICENSE file in the project root for full license information. */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "helpers.h"
int
asprintf(char **buf, const char *fmt, ...)
{
int size = 0;
va_list args;
va_start(args, fmt);
size = vasprintf(buf, fmt, args);
va_end(args);
return size;
}
int
vasprintf(char **buf, const char *fmt, va_list args)
{
va_list tmp;
va_copy(tmp, args);
int size = vsnprintf(NULL, 0, fmt, tmp);
va_end(tmp);
if (size < 0) {
return -1;
}
*buf = malloc(size + 1);
if (*buf == NULL) {
return -1;
}
size = vsprintf(*buf, fmt, args);
return size;
}
/*
* Move list item to the beginning (head) of the list.
*/
void
list_move_to_head(struct list_item **list, struct list_item *item)
{
if (list == NULL || *list == NULL || *list == item || item == NULL)
return;
/* fill the hole */
if (item->prev != NULL)
item->prev->next = item->next;
if (item->next != NULL)
item->next->prev = item->prev;
/* we are at the head, nothing is behind us now */
item->prev = NULL;
item->next = *list;
item->next->prev = item;
*list = item;
}
/*
* Allocate item at the head of the list.
*/
struct list_item*
list_add_item(struct list_item **list)
{
struct list_item *item;
item = malloc(sizeof(struct list_item));
if (item == NULL)
return NULL;
if (*list == NULL) {
item->prev = item->next = NULL;
} else {
item->prev = NULL;
item->next = *list;
item->next->prev = item;
}
*list = item;
return item;
}
/*
* Delete item from the list.
* Data must be freed manually.
*/
void
list_delete_item(struct list_item **list, struct list_item *item)
{
if (list == NULL || *list == NULL || item == NULL)
return;
if (*list == item) {
*list = item->next;
if (*list != NULL) {
(*list)->prev = NULL;
}
} else {
item->prev->next = item->next;
if (item->next != NULL)
item->next->prev = item->prev;
}
free(item);
}
/*
* Deletes a whole list.
*/
void
list_delete_all_items(struct list_item **list, bool can_free_data)
{
struct list_item *item, *next;
for (item = *list; item->next != NULL; item = next) {
next = item->next;
if (can_free_data)
free(item->data);
list_delete_item(list, item);
}
}
uint32_t
get_color_pixel(uint32_t color)
{
if (color >> 24 == 0x00) {
color |= 0xff000000;
}
return color;
}