swindow: fix draw_context_update for north and south orientation
[awesome.git] / common / util.h
blob9e84e197ca4644d32a1c1ba787b3ec2286ffa914
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 #define _GNU_SOURCE
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdbool.h>
31 #include <stdarg.h>
32 #include <assert.h>
33 #include <stdio.h>
35 #if !(defined (__FreeBSD__) || defined(__OpenBSD__))
36 #include <alloca.h>
37 #endif
39 typedef enum
41 East,
42 South,
43 North,
44 } orientation_t;
46 /** A list of possible position, not sex related */
47 typedef enum
49 Top,
50 Bottom,
51 Right,
52 Left,
53 Floating
54 } position_t;
56 /** Link a name to a function */
57 typedef struct
59 const char *name;
60 size_t len;
61 void *func;
62 } name_func_link_t;
64 /** \brief replace \c NULL strings with emtpy strings */
65 #define NONULL(x) (x ? x : "")
67 #define DO_NOTHING(...)
69 #undef MAX
70 #undef MIN
71 #define MAX(a,b) ((a) < (b) ? (b) : (a))
72 #define MIN(a,b) ((a) < (b) ? (a) : (b))
74 #define unsigned_subtract(a, b) \
75 do { \
76 if (b > a) \
77 a = 0; \
78 else \
79 a -= b; \
80 } while (0)
82 #define ssizeof(foo) (ssize_t)sizeof(foo)
83 #define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
85 #define p_alloca(type, count) \
86 ((type *)memset(alloca(sizeof(type) * (count)), \
87 0, sizeof(type) * (count)))
89 #define p_alloc_nr(x) (((x) + 16) * 3 / 2)
90 #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
91 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
92 #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
93 #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
94 #define p_grow(pp, goalnb, allocnb) \
95 do { \
96 if ((goalnb) > *(allocnb)) { \
97 if (p_alloc_nr(*(allocnb)) < (goalnb)) { \
98 *(allocnb) = (goalnb); \
99 } else { \
100 *(allocnb) = p_alloc_nr(*(allocnb)); \
102 p_realloc(pp, *(allocnb)); \
104 } while (0)
107 #ifdef __GNUC__
109 #define p_delete(mem_pp) \
110 do { \
111 typeof(**(mem_pp)) **__ptr = (mem_pp); \
112 free(*__ptr); \
113 *__ptr = NULL; \
114 } while(0)
116 #define likely(expr) __builtin_expect(!!(expr), 1)
117 #define unlikely(expr) __builtin_expect((expr), 0)
119 #else
121 #define p_delete(mem_p) \
122 do { \
123 void *__ptr = (mem_p); \
124 free(*__ptr); \
125 *(void **)__ptr = NULL; \
126 } while (0)
128 #define likely(expr) expr
129 #define unlikely(expr) expr
131 #endif
133 static inline void * __attribute__ ((malloc)) xmalloc(ssize_t size)
135 void *ptr;
137 if(size <= 0)
138 return NULL;
140 ptr = calloc(1, size);
142 if(!ptr)
143 abort();
145 return ptr;
148 static inline void
149 xrealloc(void **ptr, ssize_t newsize)
151 if(newsize <= 0)
152 p_delete(ptr);
153 else
155 *ptr = realloc(*ptr, newsize);
156 if(!*ptr)
157 abort();
161 /** Duplicate a memory zone.
162 * \param src The source.
163 * \param size The source size.
164 * \return The memory address of the copy.
166 static inline void *xmemdup(const void *src, ssize_t size)
168 return memcpy(xmalloc(size), src, size);
171 /** \brief \c NULL resistant strlen.
173 * Unlike it's libc sibling, a_strlen returns a ssize_t, and supports its
174 * argument being NULL.
176 * \param[in] s the string.
177 * \return the string length (or 0 if \c s is \c NULL).
179 static inline ssize_t a_strlen(const char *s)
181 return s ? strlen(s) : 0;
184 /** \brief \c NULL resistant strnlen.
186 * Unlike it's GNU libc sibling, a_strnlen returns a ssize_t, and supports
187 * its argument being NULL.
189 * The a_strnlen() function returns the number of characters in the string
190 * pointed to by \c s, not including the terminating \c \\0 character, but at
191 * most \c n. In doing this, a_strnlen() looks only at the first \c n
192 * characters at \c s and never beyond \c s+n.
194 * \param[in] s the string.
195 * \param[in] n the maximum length to return.
196 * \return \c a_strlen(s) if less than \c n, else \c n.
198 static inline ssize_t a_strnlen(const char *s, ssize_t n)
200 if (s)
202 const char *p = memchr(s, '\0', n);
203 return p ? p - s : n;
205 return 0;
208 /** \brief \c NULL resistant strdup.
210 * the a_strdup() function returns a pointer to a new string, which is a
211 * duplicate of \c s. Memory should be freed using p_delete().
213 * \warning when s is \c "", it returns NULL !
215 * \param[in] s the string to duplicate.
216 * \return a pointer to the duplicated string.
218 static inline
219 char *a_strdup(const char *s)
221 ssize_t len = a_strlen(s);
222 return len ? p_dup(s, len + 1) : NULL;
225 /** \brief safe limited strdup.
227 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into a newly
228 * allocated buffer, always adding a final \c \\0, and returns that buffer.
230 * \warning when s is \c "" or l is 0, it returns NULL !
232 * \param[in] s source string.
233 * \param[in] l maximum number of chars to copy.
234 * \return a newly allocated buffer containing the first \c l chars of \c src.
236 static inline
237 char * a_strndup(const char *s, ssize_t l)
239 ssize_t len = MIN(a_strlen(s), l);
240 if(len)
242 char *p = p_dup(s, len + 1);
243 p[len] = '\0';
244 return p;
246 return NULL;
249 /** \brief \c NULL resistant strcmp.
250 * \param[in] a the first string.
251 * \param[in] b the second string.
252 * \return <tt>strcmp(a, b)</tt>, and treats \c NULL strings like \c ""
253 * ones.
255 static inline int a_strcmp(const char *a, const char *b)
257 return strcmp(NONULL(a), NONULL(b));
260 /** \brief \c NULL resistant strcasecmp.
261 * \param[in] a the first string.
262 * \param[in] b the second string.
263 * \return <tt>strcasecmp(a, b)</tt>, and treats \c NULL strings like \c ""
264 * ones.
266 static inline int a_strcasecmp(const char *a, const char *b)
268 return strcasecmp(NONULL(a), NONULL(b));
271 /** \brief \c NULL resistant strncmp.
272 * \param[in] a the first string.
273 * \param[in] b the second string.
274 * \param[in] n the number of maximum chars to compare.
275 * \return <tt>strncmp(a, b, n)</tt>, and treats \c NULL strings like \c ""
276 * ones.
278 static inline int a_strncmp(const char *a, const char *b, ssize_t n)
280 return strncmp(NONULL(a), NONULL(b), n);
283 ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1)));
284 ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1)));
286 /** \brief safe strcat.
288 * The a_strcat() function appends the string \c src at the end of the buffer
289 * \c dst if space is available.
291 * \param[in] dst destination buffer.
292 * \param[in] n size of the buffer, Negative sizes are allowed.
293 * \param[in] src the string to append.
294 * \return <tt>a_strlen(dst) + a_strlen(src)</tt>
296 static inline ssize_t a_strcat(char *dst, ssize_t n, const char *src)
298 ssize_t dlen = a_strnlen(dst, n - 1);
299 return dlen + a_strcpy(dst + dlen, n - dlen, src);
302 /** \brief safe strncat.
304 * The a_strncat() function appends at most \c n chars from the string \c src
305 * at the end of the buffer \c dst if space is available.
307 * \param[in] dst destination buffer.
308 * \param[in] n size of the buffer, Negative sizes are allowed.
309 * \param[in] src the string to append.
310 * \param[in] l maximum number of chars of src to consider.
311 * \return the smallest value between <tt>a_strlen(dst) + a_strlen(src)</tt>
312 * and <tt>a_strlen(dst) + l</tt>
314 static inline ssize_t
315 a_strncat(char *dst, ssize_t n, const char *src, ssize_t l)
317 ssize_t dlen = a_strnlen(dst, n - 1);
318 return dlen + a_strncpy(dst + dlen, n - dlen, src, l);
321 #define fatal(string, ...) _fatal(__LINE__, \
322 __FUNCTION__, \
323 string, ## __VA_ARGS__)
324 void _fatal(int, const char *, const char *, ...)
325 __attribute__ ((noreturn)) __attribute__ ((format(printf, 3, 4)));
327 #define warn(string, ...) _warn(__LINE__, \
328 __FUNCTION__, \
329 string, ## __VA_ARGS__)
330 void _warn(int, const char *, const char *, ...)
331 __attribute__ ((format(printf, 3, 4)));
333 position_t position_fromstr(const char *, ssize_t);
334 const char * position_tostr(position_t);
335 orientation_t orientation_fromstr(const char *, ssize_t);
336 const char * orientation_tostr(orientation_t);
337 void *name_func_lookup(const char *, size_t, const name_func_link_t *);
338 const char * name_func_rlookup(void *, const name_func_link_t *);
339 void a_exec(const char *);
341 #endif
342 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80