use switch instead of if/elseif/else
[awesome.git] / util.h
blobd806edc08624dc46312b17a1385766303e767c1a
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>
29 /** Link a name to a function */
30 typedef struct
32 const char *name;
33 void *func;
34 } NameFuncLink;
36 /** \brief replace \c NULL strings with emtpy strings */
37 #define NONULL(x) (x ? x : "")
39 #undef MAX
40 #undef MIN
41 #define MAX(a,b) ((a) < (b) ? (b) : (a))
42 #define MIN(a,b) ((a) < (b) ? (a) : (b))
44 #define ssizeof(foo) (ssize_t)sizeof(foo)
45 #define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
47 #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
48 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
49 #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
50 #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
52 #ifdef __GNUC__
54 #define p_delete(mem_pp) \
55 do { \
56 typeof(**(mem_pp)) **__ptr = (mem_pp); \
57 free(*__ptr); \
58 *__ptr = NULL; \
59 } while(0)
61 #else
63 #define p_delete(mem_p) \
64 do { \
65 void *__ptr = (mem_p); \
66 free(*__ptr); \
67 *(void **)__ptr = NULL; \
68 } while (0)
70 #endif
72 static inline void * __attribute__ ((malloc)) xmalloc(ssize_t size)
74 void *ptr;
76 if(size <= 0)
77 return NULL;
79 ptr = calloc(1, size);
81 if(!ptr)
82 abort();
84 return ptr;
87 static inline void
88 xrealloc(void **ptr, ssize_t newsize)
90 if(newsize <= 0)
91 p_delete(ptr);
92 else
94 *ptr = realloc(*ptr, newsize);
95 if(!*ptr)
96 abort();
100 static inline void *xmemdup(const void *src, ssize_t size)
102 return memcpy(xmalloc(size), src, size);
105 /** \brief \c NULL resistant strlen.
107 * Unlike it's libc sibling, a_strlen returns a ssize_t, and supports its
108 * argument being NULL.
110 * \param[in] s the string.
111 * \return the string length (or 0 if \c s is \c NULL).
113 static inline ssize_t a_strlen(const char *s)
115 return s ? strlen(s) : 0;
118 /** \brief \c NULL resistant strnlen.
120 * Unlike it's GNU libc sibling, a_strnlen returns a ssize_t, and supports
121 * its argument being NULL.
123 * The a_strnlen() function returns the number of characters in the string
124 * pointed to by \c s, not including the terminating \c \\0 character, but at
125 * most \c n. In doing this, a_strnlen() looks only at the first \c n
126 * characters at \c s and never beyond \c s+n.
128 * \param[in] s the string.
129 * \param[in] n the maximum length to return.
130 * \return \c a_strlen(s) if less than \c n, else \c n.
132 static inline ssize_t a_strnlen(const char *s, ssize_t n)
134 if (s)
136 const char *p = memchr(s, '\0', n);
137 return p ? p - s : n;
139 return 0;
142 /** \brief \c NULL resistant strdup.
144 * the a_strdup() function returns a pointer to a new string, which is a
145 * duplicate of \c s. Memory should be freed using p_delete().
147 * \warning when s is \c "", it returns NULL !
149 * \param[in] s the string to duplicate.
150 * \return a pointer to the duplicated string.
152 static inline char *a_strdup(const char *s)
154 ssize_t len = a_strlen(s);
155 return len ? p_dup(s, len + 1) : NULL;
158 /** \brief \c NULL resistant strcmp.
159 * \param[in] a the first string.
160 * \param[in] b the second string.
161 * \return <tt>strcmp(a, b)</tt>, and treats \c NULL strings like \c ""
162 * ones.
164 static inline int a_strcmp(const char *a, const char *b)
166 return strcmp(NONULL(a), NONULL(b));
169 /** \brief \c NULL resistant strncmp.
170 * \param[in] a the first string.
171 * \param[in] b the second string.
172 * \param[in] n the number of maximum chars to compare.
173 * \return <tt>strncmp(a, b, n)</tt>, and treats \c NULL strings like \c ""
174 * ones.
176 static inline int a_strncmp(const char *a, const char *b, ssize_t n)
178 return strncmp(NONULL(a), NONULL(b), n);
181 ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1)));
182 ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1)));
184 /** \brief safe strcat.
186 * The a_strcat() function appends the string \c src at the end of the buffer
187 * \c dst if space is available.
189 * \param[in] dst destination buffer.
190 * \param[in] n size of the buffer, Negative sizes are allowed.
191 * \param[in] src the string to append.
192 * \return <tt>a_strlen(dst) + a_strlen(src)</tt>
194 static inline ssize_t a_strcat(char *dst, ssize_t n, const char *src)
196 ssize_t dlen = a_strnlen(dst, n - 1);
197 return dlen + a_strcpy(dst + dlen, n - dlen, src);
200 void die(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));
201 void eprint(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));
202 double compute_new_value_from_arg(const char *, double);
203 void warn(const char *, ...) __attribute__ ((format(printf, 1, 2)));
204 void *name_func_lookup(const char *, const NameFuncLink *);
206 #endif
207 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80