awful.widget: fix widget<->tag association
[awesome.git] / common / util.h
blob7869a96926e1d40134937e22948e81c4d40dd279
1 /*
2 * util.h - useful functions header
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef AWESOME_COMMON_UTIL_H
24 #define AWESOME_COMMON_UTIL_H
26 /* asprintf */
27 #define _GNU_SOURCE
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdbool.h>
32 #include <stdarg.h>
33 #include <assert.h>
34 #include <stdio.h>
36 #if !(defined (__FreeBSD__) || defined(__OpenBSD__))
37 #include <alloca.h>
38 #endif
40 typedef enum
42 East,
43 South,
44 North,
45 } orientation_t;
47 /** A list of possible position, not sex related */
48 typedef enum
50 Top,
51 Bottom,
52 Right,
53 Left,
54 Floating
55 } position_t;
57 /** Link a name to a function */
58 typedef struct
60 const char *name;
61 size_t len;
62 void *func;
63 } name_func_link_t;
65 /** \brief replace \c NULL strings with emtpy strings */
66 #define NONULL(x) (x ? x : "")
68 #define DO_NOTHING(...)
70 #undef MAX
71 #undef MIN
72 #define MAX(a,b) ((a) < (b) ? (b) : (a))
73 #define MIN(a,b) ((a) < (b) ? (a) : (b))
75 #define unsigned_subtract(a, b) \
76 do { \
77 if (b > a) \
78 a = 0; \
79 else \
80 a -= b; \
81 } while (0)
83 #define ssizeof(foo) (ssize_t)sizeof(foo)
84 #define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
86 #define p_alloca(type, count) \
87 ((type *)memset(alloca(sizeof(type) * (count)), \
88 0, sizeof(type) * (count)))
90 #define p_alloc_nr(x) (((x) + 16) * 3 / 2)
91 #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
92 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
93 #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
94 #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
95 #define p_grow(pp, goalnb, allocnb) \
96 do { \
97 if ((goalnb) > *(allocnb)) { \
98 if (p_alloc_nr(*(allocnb)) < (goalnb)) { \
99 *(allocnb) = (goalnb); \
100 } else { \
101 *(allocnb) = p_alloc_nr(*(allocnb)); \
103 p_realloc(pp, *(allocnb)); \
105 } while (0)
108 #ifdef __GNUC__
110 #define p_delete(mem_pp) \
111 do { \
112 typeof(**(mem_pp)) **__ptr = (mem_pp); \
113 free(*__ptr); \
114 *__ptr = NULL; \
115 } while(0)
117 #define likely(expr) __builtin_expect(!!(expr), 1)
118 #define unlikely(expr) __builtin_expect((expr), 0)
120 #else
122 #define p_delete(mem_p) \
123 do { \
124 void *__ptr = (mem_p); \
125 free(*__ptr); \
126 *(void **)__ptr = NULL; \
127 } while (0)
129 #define likely(expr) expr
130 #define unlikely(expr) expr
132 #endif
134 static inline void * __attribute__ ((malloc)) xmalloc(ssize_t size)
136 void *ptr;
138 if(size <= 0)
139 return NULL;
141 ptr = calloc(1, size);
143 if(!ptr)
144 abort();
146 return ptr;
149 static inline void
150 xrealloc(void **ptr, ssize_t newsize)
152 if(newsize <= 0)
153 p_delete(ptr);
154 else
156 *ptr = realloc(*ptr, newsize);
157 if(!*ptr)
158 abort();
162 /** Duplicate a memory zone.
163 * \param src The source.
164 * \param size The source size.
165 * \return The memory address of the copy.
167 static inline void *xmemdup(const void *src, ssize_t size)
169 return memcpy(xmalloc(size), src, size);
172 /** \brief \c NULL resistant strlen.
174 * Unlike it's libc sibling, a_strlen returns a ssize_t, and supports its
175 * argument being NULL.
177 * \param[in] s the string.
178 * \return the string length (or 0 if \c s is \c NULL).
180 static inline ssize_t a_strlen(const char *s)
182 return s ? strlen(s) : 0;
185 /** \brief \c NULL resistant strnlen.
187 * Unlike it's GNU libc sibling, a_strnlen returns a ssize_t, and supports
188 * its argument being NULL.
190 * The a_strnlen() function returns the number of characters in the string
191 * pointed to by \c s, not including the terminating \c \\0 character, but at
192 * most \c n. In doing this, a_strnlen() looks only at the first \c n
193 * characters at \c s and never beyond \c s+n.
195 * \param[in] s the string.
196 * \param[in] n the maximum length to return.
197 * \return \c a_strlen(s) if less than \c n, else \c n.
199 static inline ssize_t a_strnlen(const char *s, ssize_t n)
201 if (s)
203 const char *p = memchr(s, '\0', n);
204 return p ? p - s : n;
206 return 0;
209 /** \brief \c NULL resistant strdup.
211 * the a_strdup() function returns a pointer to a new string, which is a
212 * duplicate of \c s. Memory should be freed using p_delete().
214 * \warning when s is \c "", it returns NULL !
216 * \param[in] s the string to duplicate.
217 * \return a pointer to the duplicated string.
219 static inline
220 char *a_strdup(const char *s)
222 ssize_t len = a_strlen(s);
223 return len ? p_dup(s, len + 1) : NULL;
226 /** \brief safe limited strdup.
228 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into a newly
229 * allocated buffer, always adding a final \c \\0, and returns that buffer.
231 * \warning when s is \c "" or l is 0, it returns NULL !
233 * \param[in] s source string.
234 * \param[in] l maximum number of chars to copy.
235 * \return a newly allocated buffer containing the first \c l chars of \c src.
237 static inline
238 char * a_strndup(const char *s, ssize_t l)
240 ssize_t len = MIN(a_strlen(s), l);
241 if(len)
243 char *p = p_dup(s, len + 1);
244 p[len] = '\0';
245 return p;
247 return NULL;
250 /** \brief \c NULL resistant strcmp.
251 * \param[in] a the first string.
252 * \param[in] b the second string.
253 * \return <tt>strcmp(a, b)</tt>, and treats \c NULL strings like \c ""
254 * ones.
256 static inline int a_strcmp(const char *a, const char *b)
258 return strcmp(NONULL(a), NONULL(b));
261 /** \brief \c NULL resistant strcasecmp.
262 * \param[in] a the first string.
263 * \param[in] b the second string.
264 * \return <tt>strcasecmp(a, b)</tt>, and treats \c NULL strings like \c ""
265 * ones.
267 static inline int a_strcasecmp(const char *a, const char *b)
269 return strcasecmp(NONULL(a), NONULL(b));
272 /** \brief \c NULL resistant strncmp.
273 * \param[in] a the first string.
274 * \param[in] b the second string.
275 * \param[in] n the number of maximum chars to compare.
276 * \return <tt>strncmp(a, b, n)</tt>, and treats \c NULL strings like \c ""
277 * ones.
279 static inline int a_strncmp(const char *a, const char *b, ssize_t n)
281 return strncmp(NONULL(a), NONULL(b), n);
284 ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1)));
285 ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1)));
287 /** \brief safe strcat.
289 * The a_strcat() function appends the string \c src at the end of the buffer
290 * \c dst if space is available.
292 * \param[in] dst destination buffer.
293 * \param[in] n size of the buffer, Negative sizes are allowed.
294 * \param[in] src the string to append.
295 * \return <tt>a_strlen(dst) + a_strlen(src)</tt>
297 static inline ssize_t a_strcat(char *dst, ssize_t n, const char *src)
299 ssize_t dlen = a_strnlen(dst, n - 1);
300 return dlen + a_strcpy(dst + dlen, n - dlen, src);
303 /** \brief safe strncat.
305 * The a_strncat() function appends at most \c n chars from the string \c src
306 * at the end of the buffer \c dst if space is available.
308 * \param[in] dst destination buffer.
309 * \param[in] n size of the buffer, Negative sizes are allowed.
310 * \param[in] src the string to append.
311 * \param[in] l maximum number of chars of src to consider.
312 * \return the smallest value between <tt>a_strlen(dst) + a_strlen(src)</tt>
313 * and <tt>a_strlen(dst) + l</tt>
315 static inline ssize_t
316 a_strncat(char *dst, ssize_t n, const char *src, ssize_t l)
318 ssize_t dlen = a_strnlen(dst, n - 1);
319 return dlen + a_strncpy(dst + dlen, n - dlen, src, l);
322 #define fatal(string, ...) _fatal(__LINE__, \
323 __FUNCTION__, \
324 string, ## __VA_ARGS__)
325 void _fatal(int, const char *, const char *, ...)
326 __attribute__ ((noreturn)) __attribute__ ((format(printf, 3, 4)));
328 #define warn(string, ...) _warn(__LINE__, \
329 __FUNCTION__, \
330 string, ## __VA_ARGS__)
331 void _warn(int, const char *, const char *, ...)
332 __attribute__ ((format(printf, 3, 4)));
334 position_t position_fromstr(const char *, ssize_t);
335 const char * position_tostr(position_t);
336 orientation_t orientation_fromstr(const char *, ssize_t);
337 const char * orientation_tostr(orientation_t);
338 void *name_func_lookup(const char *, size_t, const name_func_link_t *);
339 const char * name_func_rlookup(void *, const name_func_link_t *);
340 void a_exec(const char *);
341 char ** a_strsplit(const char *, ssize_t, char);
343 #define a_asprintf(strp, fmt, ...) \
344 do { \
345 if(asprintf(strp, fmt, ## __VA_ARGS__) < 0) \
346 abort(); \
347 } while(0)
349 #endif
350 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80