awful.titlebar: do not build args
[awesome.git] / common / util.h
blob2c3454894749e1b8ffb8a9a7bc0533f79805dbe0
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 #include <string.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <stdarg.h>
30 #include <assert.h>
32 #if !(defined (__FreeBSD__) || defined(__OpenBSD__))
33 #include <alloca.h>
34 #endif
36 #include "tokenize.h"
38 typedef enum
40 East,
41 South,
42 North,
43 } orientation_t;
45 /** A list of possible position, not sex related */
46 typedef enum
48 Top,
49 Bottom,
50 Right,
51 Left,
52 Floating
53 } position_t;
55 /** Link a name to a function */
56 typedef struct
58 const char *name;
59 void *func;
60 } name_func_link_t;
62 /** \brief replace \c NULL strings with emtpy strings */
63 #define NONULL(x) (x ? x : "")
65 #define DO_NOTHING(...)
67 #undef MAX
68 #undef MIN
69 #define MAX(a,b) ((a) < (b) ? (b) : (a))
70 #define MIN(a,b) ((a) < (b) ? (a) : (b))
72 #define ssizeof(foo) (ssize_t)sizeof(foo)
73 #define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
75 #define p_alloca(type, count) \
76 ((type *)memset(alloca(sizeof(type) * (count)), \
77 0, sizeof(type) * (count)))
79 #define p_alloc_nr(x) (((x) + 16) * 3 / 2)
80 #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
81 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
82 #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
83 #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
84 #define p_grow(pp, goalnb, allocnb) \
85 do { \
86 if ((goalnb) > *(allocnb)) { \
87 if (p_alloc_nr(*(allocnb)) < (goalnb)) { \
88 *(allocnb) = (goalnb); \
89 } else { \
90 *(allocnb) = p_alloc_nr(*(allocnb)); \
91 } \
92 p_realloc(pp, *(allocnb)); \
93 } \
94 } while (0)
97 #ifdef __GNUC__
99 #define p_delete(mem_pp) \
100 do { \
101 typeof(**(mem_pp)) **__ptr = (mem_pp); \
102 free(*__ptr); \
103 *__ptr = NULL; \
104 } while(0)
106 #define likely(expr) __builtin_expect(!!(expr), 1)
107 #define unlikely(expr) __builtin_expect((expr), 0)
109 #else
111 #define p_delete(mem_p) \
112 do { \
113 void *__ptr = (mem_p); \
114 free(*__ptr); \
115 *(void **)__ptr = NULL; \
116 } while (0)
118 #define likely(expr) expr
119 #define unlikely(expr) expr
121 #endif
123 static inline void * __attribute__ ((malloc)) xmalloc(ssize_t size)
125 void *ptr;
127 if(size <= 0)
128 return NULL;
130 ptr = calloc(1, size);
132 if(!ptr)
133 abort();
135 return ptr;
138 static inline void
139 xrealloc(void **ptr, ssize_t newsize)
141 if(newsize <= 0)
142 p_delete(ptr);
143 else
145 *ptr = realloc(*ptr, newsize);
146 if(!*ptr)
147 abort();
151 static inline void *xmemdup(const void *src, ssize_t size)
153 return memcpy(xmalloc(size), src, size);
156 /** \brief \c NULL resistant strlen.
158 * Unlike it's libc sibling, a_strlen returns a ssize_t, and supports its
159 * argument being NULL.
161 * \param[in] s the string.
162 * \return the string length (or 0 if \c s is \c NULL).
164 static inline ssize_t a_strlen(const char *s)
166 return s ? strlen(s) : 0;
169 /** \brief \c NULL resistant strnlen.
171 * Unlike it's GNU libc sibling, a_strnlen returns a ssize_t, and supports
172 * its argument being NULL.
174 * The a_strnlen() function returns the number of characters in the string
175 * pointed to by \c s, not including the terminating \c \\0 character, but at
176 * most \c n. In doing this, a_strnlen() looks only at the first \c n
177 * characters at \c s and never beyond \c s+n.
179 * \param[in] s the string.
180 * \param[in] n the maximum length to return.
181 * \return \c a_strlen(s) if less than \c n, else \c n.
183 static inline ssize_t a_strnlen(const char *s, ssize_t n)
185 if (s)
187 const char *p = memchr(s, '\0', n);
188 return p ? p - s : n;
190 return 0;
193 /** \brief \c NULL resistant strdup.
195 * the a_strdup() function returns a pointer to a new string, which is a
196 * duplicate of \c s. Memory should be freed using p_delete().
198 * \warning when s is \c "", it returns NULL !
200 * \param[in] s the string to duplicate.
201 * \return a pointer to the duplicated string.
203 static inline
204 char *a_strdup(const char *s)
206 ssize_t len = a_strlen(s);
207 return len ? p_dup(s, len + 1) : NULL;
210 /** \brief safe limited strdup.
212 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into a newly
213 * allocated buffer, always adding a final \c \\0, and returns that buffer.
215 * \warning when s is \c "" or l is 0, it returns NULL !
217 * \param[in] s source string.
218 * \param[in] l maximum number of chars to copy.
219 * \return a newly allocated buffer containing the first \c l chars of \c src.
221 static inline
222 char * a_strndup(const char *s, ssize_t l)
224 ssize_t len = MIN(a_strlen(s), l);
225 if(len)
227 char *p = p_dup(s, len + 1);
228 p[len] = '\0';
229 return p;
231 return NULL;
234 /** \brief \c NULL resistant strcmp.
235 * \param[in] a the first string.
236 * \param[in] b the second string.
237 * \return <tt>strcmp(a, b)</tt>, and treats \c NULL strings like \c ""
238 * ones.
240 static inline int a_strcmp(const char *a, const char *b)
242 return strcmp(NONULL(a), NONULL(b));
245 /** \brief \c NULL resistant strcasecmp.
246 * \param[in] a the first string.
247 * \param[in] b the second string.
248 * \return <tt>strcasecmp(a, b)</tt>, and treats \c NULL strings like \c ""
249 * ones.
251 static inline int a_strcasecmp(const char *a, const char *b)
253 return strcasecmp(NONULL(a), NONULL(b));
256 /** \brief \c NULL resistant strncmp.
257 * \param[in] a the first string.
258 * \param[in] b the second string.
259 * \param[in] n the number of maximum chars to compare.
260 * \return <tt>strncmp(a, b, n)</tt>, and treats \c NULL strings like \c ""
261 * ones.
263 static inline int a_strncmp(const char *a, const char *b, ssize_t n)
265 return strncmp(NONULL(a), NONULL(b), n);
268 ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1)));
269 ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1)));
271 /** \brief safe strcat.
273 * The a_strcat() function appends the string \c src at the end of the buffer
274 * \c dst if space is available.
276 * \param[in] dst destination buffer.
277 * \param[in] n size of the buffer, Negative sizes are allowed.
278 * \param[in] src the string to append.
279 * \return <tt>a_strlen(dst) + a_strlen(src)</tt>
281 static inline ssize_t a_strcat(char *dst, ssize_t n, const char *src)
283 ssize_t dlen = a_strnlen(dst, n - 1);
284 return dlen + a_strcpy(dst + dlen, n - dlen, src);
287 /** \brief safe strncat.
289 * The a_strncat() function appends at most \c n chars from the string \c src
290 * at the end of the buffer \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 * \param[in] l maximum number of chars of src to consider.
296 * \return the smallest value between <tt>a_strlen(dst) + a_strlen(src)</tt>
297 * and <tt>a_strlen(dst) + l</tt>
299 static inline ssize_t
300 a_strncat(char *dst, ssize_t n, const char *src, ssize_t l)
302 ssize_t dlen = a_strnlen(dst, n - 1);
303 return dlen + a_strncpy(dst + dlen, n - dlen, src, l);
306 /** \brief convert a string to a boolean value.
308 * The a_strtobool() function converts a string \c s into a boolean.
309 * It recognizes the strings "true", "on", "yes" and "1".
311 * \param[in] s the string to convert
312 * \return true if the string is recognized as possibly true, false otherwise.
314 static inline bool
315 a_strtobool(const char *s, ssize_t len)
317 switch(a_tokenize(s, len))
319 case A_TK_TRUE:
320 case A_TK_YES:
321 case A_TK_ON:
322 case A_TK_1:
323 return true;
324 default:
325 return false;
329 #define fatal(string, ...) _fatal(__LINE__, \
330 __FUNCTION__, \
331 string, ## __VA_ARGS__)
332 void _fatal(int, const char *, const char *, ...)
333 __attribute__ ((noreturn)) __attribute__ ((format(printf, 3, 4)));
335 #define warn(string, ...) _warn(__LINE__, \
336 __FUNCTION__, \
337 string, ## __VA_ARGS__)
338 void _warn(int, const char *, const char *, ...)
339 __attribute__ ((format(printf, 3, 4)));
341 position_t position_fromstr(const char *, ssize_t);
342 const char * position_tostr(position_t);
343 orientation_t orientation_fromstr(const char *, ssize_t);
344 const char * orientation_tostr(orientation_t);
345 void *name_func_lookup(const char *, const name_func_link_t *);
346 const char * name_func_rlookup(void *, const name_func_link_t *);
347 void a_exec(const char *);
348 char ** a_strsplit(const char *, ssize_t, char);
350 #endif
351 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80