fix bug with configure request windows
[awesome.git] / util.h
blob88b0a4028fa3894cf26e95496be56d4139e21df4
1 /*
2 * util.c - useful functions header
4 * Copyright © 2007 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_MEM_H
24 #define AWESOME_MEM_H
26 #include <string.h>
27 #include <stdlib.h>
28 #include "common.h"
30 /** Link a name to a function */
31 typedef struct
33 const char *name;
34 void *func;
35 } NameFuncLink;
37 /** \brief replace \c NULL strings with emtpy strings */
38 #define NONULL(x) (x ? x : "")
40 #undef MAX
41 #undef MIN
42 #define MAX(a,b) ((a) < (b) ? (b) : (a))
43 #define MIN(a,b) ((a) < (b) ? (a) : (b))
45 #define ssizeof(foo) (ssize_t)sizeof(foo)
46 #define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
48 #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
49 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
50 #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
51 #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
53 #ifdef __GNUC__
55 #define p_delete(mem_pp) \
56 do { \
57 typeof(**(mem_pp)) **__ptr = (mem_pp); \
58 free(*__ptr); \
59 *__ptr = NULL; \
60 } while(0)
62 #else
64 #define p_delete(mem_p) \
65 do { \
66 void *__ptr = (mem_p); \
67 free(*__ptr); \
68 *(void **)__ptr = NULL; \
69 } while (0)
71 #endif
73 static inline void * __attribute__ ((malloc)) xmalloc(ssize_t size)
75 void *ptr;
77 if(size <= 0)
78 return NULL;
80 ptr = calloc(1, size);
82 if(!ptr)
83 abort();
85 return ptr;
88 static inline void
89 xrealloc(void **ptr, ssize_t newsize)
91 if(newsize <= 0)
92 p_delete(ptr);
93 else
95 *ptr = realloc(*ptr, newsize);
96 if(!*ptr)
97 abort();
101 static inline void *xmemdup(const void *src, ssize_t size)
103 return memcpy(xmalloc(size), src, size);
106 /** \brief \c NULL resistant strlen.
108 * Unlinke it's libc sibling, a_strlen returns a ssize_t, and supports its
109 * argument beeing NULL.
111 * \param[in] s the string.
112 * \return the string length (or 0 if \c s is \c NULL).
114 static inline ssize_t a_strlen(const char *s)
116 return s ? strlen(s) : 0;
119 /** \brief \c NULL resistant strnlen.
121 * Unlinke it's GNU libc sibling, a_strnlen returns a ssize_t, and supports
122 * its argument beeing NULL.
124 * The a_strnlen() function returns the number of characters in the string
125 * pointed to by \c s, not including the terminating \c \\0 character, but at
126 * most \c n. In doing this, a_strnlen() looks only at the first \c n
127 * characters at \c s and never beyond \c s+n.
129 * \param[in] s the string.
130 * \param[in] n the maximum length to return.
131 * \return \c a_strlen(s) if less than \c n, else \c n.
133 static inline ssize_t a_strnlen(const char *s, ssize_t n)
135 if (s)
137 const char *p = memchr(s, '\0', n);
138 return p ? p - s : n;
140 return 0;
143 /** \brief \c NULL resistant strdup.
145 * the a_strdup() function returns a pointer to a new string, which is a
146 * duplicate of \c s. Memory should be freed using p_delete().
148 * \warning when s is \c "", it returns NULL !
150 * \param[in] s the string to duplicate.
151 * \return a pointer to the duplicated string.
153 static inline char *a_strdup(const char *s)
155 ssize_t len = a_strlen(s);
156 return len ? p_dup(s, len + 1) : NULL;
159 /** \brief \c NULL resistant strcmp.
160 * \param[in] a the first string.
161 * \param[in] b the second string.
162 * \return <tt>strcmp(a, b)</tt>, and treats \c NULL strings like \c ""
163 * ones.
165 static inline int a_strcmp(const char *a, const char *b)
167 return strcmp(NONULL(a), NONULL(b));
170 /** \brief \c NULL resistant strncmp.
171 * \param[in] a the first string.
172 * \param[in] b the second string.
173 * \param[in] n the number of maximum chars to compare.
174 * \return <tt>strncmp(a, b, n)</tt>, and treats \c NULL strings like \c ""
175 * ones.
177 static inline int a_strncmp(const char *a, const char *b, ssize_t n)
179 return strncmp(NONULL(a), NONULL(b), n);
182 ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1)));
183 ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1)));
185 /** \brief safe strcat.
187 * The a_strcat() function appends the string \c src at the end of the buffer
188 * \c dst if space is available.
190 * \param[in] dst destination buffer.
191 * \param[in] n size of the buffer, Negative sizes are allowed.
192 * \param[in] src the string to append.
193 * \return <tt>a_strlen(dst) + a_strlen(src)</tt>
195 static inline ssize_t a_strcat(char *dst, ssize_t n, const char *src)
197 ssize_t dlen = a_strnlen(dst, n - 1);
198 return dlen + a_strcpy(dst + dlen, n - dlen, src);
201 void die(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));
202 void eprint(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));
203 Bool xgettextprop(Display *, Window, Atom, char *, ssize_t);
204 double compute_new_value_from_arg(const char *, double);
205 void *name_func_lookup(const char *, const NameFuncLink *);
207 UICB_PROTO(uicb_spawn);
208 UICB_PROTO(uicb_exec);
209 #endif
210 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99