-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutils.h
More file actions
107 lines (85 loc) · 3.68 KB
/
utils.h
File metadata and controls
107 lines (85 loc) · 3.68 KB
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
#ifndef UTILS_H
#define UTILS_H
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
/// Call malloc and die on error.
void * xmalloc (size_t size)
__attribute__ ((__malloc__, __warn_unused_result__));
/// Call realloc and die on error.
void * xrealloc (void * ptr, size_t size)
__attribute__ ((__warn_unused_result__));
/// Call calloc and die on error.
void * xcalloc (size_t size)
__attribute__ ((__malloc__, __warn_unused_result__));
/// Call free.
void xfree (const void * p);
/// Call strdup and die on error.
char * xstrdup (const char * s)
__attribute__ ((__malloc__, __warn_unused_result__));
/// Format a string into a malloc'd buffer.
char * xasprintf (const char * format, ...)
__attribute__ ((malloc, warn_unused_result, format (printf, 1, 2)));
/// Directory-aware string compare; this puts all paths in the same directory
/// together.
int compare_paths (const char * A, const char * B);
/// Binary search for the string @c needle in an @c array of @c count items of
/// @c size bytes each, with a string pointer at offset @c position.
void * find_string (const void * array, size_t count, size_t size,
size_t position, const char * needle);
/// Like @ref find_string but use strverscmp.
void * find_version_string (const void * array, size_t count, size_t size,
size_t position, const char * needle);
/// Does @c haystack start with @c needle?
static inline bool starts_with (const char * haystack, const char * needle)
{
return strncmp (haystack, needle, strlen (needle)) == 0;
}
static inline bool ends_with (const char * haystack, const char * needle)
{
size_t h_len = strlen (haystack);
size_t n_len = strlen (needle);
return h_len >= n_len
&& memcmp (haystack + h_len - n_len, needle, n_len) == 0;
}
/// Allocate an array with malloc().
#define ARRAY_ALLOC(T,N) ((T *) xmalloc (sizeof (T) * (N)))
/// Allocate an array with calloc().
#define ARRAY_CALLOC(T,N) ((T *) xcalloc (sizeof (T) * (N)))
/// Re-size an array with realloc().
#define ARRAY_REALLOC(P,N) ((__typeof__ (P)) xrealloc (P, sizeof *(P) * (N)))
/// Extend an array by one item. P_end should be the end pointer. We keep the
/// size of the array as 1 less than a power of 2.
#define ARRAY_EXTEND(P) do { \
size_t ITEMS = P##_end - P; \
if (ITEMS & (ITEMS + 1)) { \
++P##_end; \
break; \
} \
P = ARRAY_REALLOC (P, ITEMS * 2 + 1); \
P##_end = P + ITEMS + 1; \
} while (0)
/// Extend an array by one item. P_end should be the end pointer.
#define ARRAY_APPEND(P,I) do { \
size_t ITEMS = P##_end - P; \
if ((ITEMS & (ITEMS + 1)) == 0) { \
P = ARRAY_REALLOC (P, ITEMS * 2 + 1); \
P##_end = P + ITEMS; \
} \
*(P##_end)++ = I; \
} while (0)
/// Resize an array to it's exact size.
#define ARRAY_TRIM(P) do \
if (P != P##_end) { \
size_t ITEMS = P##_end - P; \
P = ARRAY_REALLOC (P, ITEMS); \
P##_end = P + ITEMS; \
} while (0)
/// Sort an array using qsort.
#define ARRAY_SORT(P, F) do \
if (P##_end != P) \
qsort (P, P##_end - P, sizeof *(P), F); \
while (0)
#endif