2007-10-03 17:26:14 +02:00
|
|
|
/*
|
2008-01-12 14:42:33 +01:00
|
|
|
* util.h - useful functions header
|
2007-10-03 17:26:14 +02:00
|
|
|
*
|
2008-01-12 14:42:33 +01:00
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
2007-09-12 14:29:51 +02:00
|
|
|
* Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
|
2007-10-03 17:26:14 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2007-09-12 14:29:51 +02:00
|
|
|
*/
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2008-01-26 18:00:47 +01:00
|
|
|
#ifndef AWESOME_COMMON_UTIL_H
|
|
|
|
#define AWESOME_COMMON_UTIL_H
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2007-09-11 13:35:58 +02:00
|
|
|
#include <string.h>
|
2007-09-11 14:00:49 +02:00
|
|
|
#include <stdlib.h>
|
2008-04-09 20:20:48 +02:00
|
|
|
#include <stdbool.h>
|
2008-06-22 14:57:29 +02:00
|
|
|
#include <stdarg.h>
|
2008-06-22 14:59:45 +02:00
|
|
|
#include <assert.h>
|
2008-06-23 16:31:40 +02:00
|
|
|
#include <alloca.h>
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2008-03-14 14:27:56 +01:00
|
|
|
/** A list of possible position, not sex related */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
Off = 0,
|
|
|
|
Top,
|
|
|
|
Bottom,
|
|
|
|
Right,
|
2008-03-14 17:18:48 +01:00
|
|
|
Left,
|
|
|
|
Auto
|
2008-04-11 11:19:23 +02:00
|
|
|
} position_t;
|
2008-03-14 14:27:56 +01:00
|
|
|
|
2007-10-12 17:10:36 +02:00
|
|
|
/** Link a name to a function */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
void *func;
|
2008-01-12 14:34:53 +01:00
|
|
|
} name_func_link_t;
|
2007-10-12 17:10:36 +02:00
|
|
|
|
2007-10-03 17:26:14 +02:00
|
|
|
/** \brief replace \c NULL strings with emtpy strings */
|
|
|
|
#define NONULL(x) (x ? x : "")
|
2007-09-21 13:09:23 +02:00
|
|
|
|
2008-06-22 22:09:48 +02:00
|
|
|
#define DO_NOTHING(...)
|
|
|
|
|
2007-09-24 16:42:04 +02:00
|
|
|
#undef MAX
|
|
|
|
#undef MIN
|
|
|
|
#define MAX(a,b) ((a) < (b) ? (b) : (a))
|
|
|
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
|
|
|
|
2007-09-05 20:15:00 +02:00
|
|
|
#define ssizeof(foo) (ssize_t)sizeof(foo)
|
|
|
|
#define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
|
|
|
|
|
2008-06-23 16:31:40 +02:00
|
|
|
#define p_alloca(type, count) \
|
|
|
|
((type *)memset(alloca(sizeof(type) * (count)), \
|
|
|
|
0, sizeof(type) * (count)))
|
|
|
|
|
2008-06-22 13:30:39 +02:00
|
|
|
#define p_alloc_nr(x) (((x) + 16) * 3 / 2)
|
2007-09-05 20:15:00 +02:00
|
|
|
#define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
|
|
|
|
#define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
|
2007-09-19 19:56:10 +02:00
|
|
|
#define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
|
2007-09-11 13:35:58 +02:00
|
|
|
#define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
|
2008-06-22 13:30:39 +02:00
|
|
|
#define p_grow(pp, goalnb, allocnb) \
|
|
|
|
do { \
|
|
|
|
if ((goalnb) > *(allocnb)) { \
|
|
|
|
if (p_alloc_nr(*(allocnb)) < (goalnb)) { \
|
|
|
|
*(allocnb) = (goalnb); \
|
|
|
|
} else { \
|
|
|
|
*(allocnb) = p_alloc_nr(*(allocnb)); \
|
|
|
|
} \
|
|
|
|
p_realloc(pp, *(allocnb)); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2007-09-05 20:15:00 +02:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
|
|
|
|
#define p_delete(mem_pp) \
|
|
|
|
do { \
|
|
|
|
typeof(**(mem_pp)) **__ptr = (mem_pp); \
|
|
|
|
free(*__ptr); \
|
|
|
|
*__ptr = NULL; \
|
|
|
|
} while(0)
|
|
|
|
|
2008-06-22 14:57:29 +02:00
|
|
|
#define likely(expr) __builtin_expect(!!(expr), 1)
|
|
|
|
#define unlikely(expr) __builtin_expect((expr), 0)
|
|
|
|
|
2007-09-05 20:15:00 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define p_delete(mem_p) \
|
|
|
|
do { \
|
|
|
|
void *__ptr = (mem_p); \
|
|
|
|
free(*__ptr); \
|
|
|
|
*(void **)__ptr = NULL; \
|
|
|
|
} while (0)
|
|
|
|
|
2008-06-22 14:57:29 +02:00
|
|
|
#define likely(expr) expr
|
|
|
|
#define unlikely(expr) expr
|
|
|
|
|
2007-09-05 20:15:00 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline void * __attribute__ ((malloc)) xmalloc(ssize_t size)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
if(size <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ptr = calloc(1, size);
|
|
|
|
|
|
|
|
if(!ptr)
|
|
|
|
abort();
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
xrealloc(void **ptr, ssize_t newsize)
|
|
|
|
{
|
|
|
|
if(newsize <= 0)
|
|
|
|
p_delete(ptr);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ptr = realloc(*ptr, newsize);
|
|
|
|
if(!*ptr)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-11 13:35:58 +02:00
|
|
|
static inline void *xmemdup(const void *src, ssize_t size)
|
|
|
|
{
|
|
|
|
return memcpy(xmalloc(size), src, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief \c NULL resistant strlen.
|
|
|
|
*
|
2007-12-14 08:47:21 +01:00
|
|
|
* Unlike it's libc sibling, a_strlen returns a ssize_t, and supports its
|
|
|
|
* argument being NULL.
|
2007-09-11 13:35:58 +02:00
|
|
|
*
|
|
|
|
* \param[in] s the string.
|
|
|
|
* \return the string length (or 0 if \c s is \c NULL).
|
|
|
|
*/
|
|
|
|
static inline ssize_t a_strlen(const char *s)
|
|
|
|
{
|
|
|
|
return s ? strlen(s) : 0;
|
|
|
|
}
|
|
|
|
|
2007-09-24 16:53:03 +02:00
|
|
|
/** \brief \c NULL resistant strnlen.
|
|
|
|
*
|
2007-12-14 08:47:21 +01:00
|
|
|
* Unlike it's GNU libc sibling, a_strnlen returns a ssize_t, and supports
|
|
|
|
* its argument being NULL.
|
2007-09-24 16:53:03 +02:00
|
|
|
*
|
|
|
|
* The a_strnlen() function returns the number of characters in the string
|
|
|
|
* pointed to by \c s, not including the terminating \c \\0 character, but at
|
|
|
|
* most \c n. In doing this, a_strnlen() looks only at the first \c n
|
|
|
|
* characters at \c s and never beyond \c s+n.
|
|
|
|
*
|
|
|
|
* \param[in] s the string.
|
|
|
|
* \param[in] n the maximum length to return.
|
|
|
|
* \return \c a_strlen(s) if less than \c n, else \c n.
|
|
|
|
*/
|
|
|
|
static inline ssize_t a_strnlen(const char *s, ssize_t n)
|
|
|
|
{
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
const char *p = memchr(s, '\0', n);
|
|
|
|
return p ? p - s : n;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-11 13:35:58 +02:00
|
|
|
/** \brief \c NULL resistant strdup.
|
|
|
|
*
|
|
|
|
* the a_strdup() function returns a pointer to a new string, which is a
|
|
|
|
* duplicate of \c s. Memory should be freed using p_delete().
|
|
|
|
*
|
|
|
|
* \warning when s is \c "", it returns NULL !
|
|
|
|
*
|
|
|
|
* \param[in] s the string to duplicate.
|
|
|
|
* \return a pointer to the duplicated string.
|
|
|
|
*/
|
2008-05-29 09:45:10 +02:00
|
|
|
static inline
|
|
|
|
char *a_strdup(const char *s)
|
2007-09-11 13:35:58 +02:00
|
|
|
{
|
|
|
|
ssize_t len = a_strlen(s);
|
|
|
|
return len ? p_dup(s, len + 1) : NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-29 09:45:10 +02:00
|
|
|
/** \brief safe limited strdup.
|
|
|
|
*
|
|
|
|
* Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into a newly
|
|
|
|
* allocated buffer, always adding a final \c \\0, and returns that buffer.
|
|
|
|
*
|
|
|
|
* \warning when s is \c "" or l is 0, it returns NULL !
|
|
|
|
*
|
|
|
|
* \param[in] s source string.
|
|
|
|
* \param[in] l maximum number of chars to copy.
|
|
|
|
* \return a newly allocated buffer containing the first \c l chars of \c src.
|
|
|
|
*/
|
|
|
|
static inline
|
|
|
|
char * a_strndup(const char *s, ssize_t l)
|
|
|
|
{
|
|
|
|
ssize_t len = MIN(a_strlen(s), l);
|
|
|
|
return len ? p_dup(s, len + 1) : NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-21 13:09:23 +02:00
|
|
|
/** \brief \c NULL resistant strcmp.
|
|
|
|
* \param[in] a the first string.
|
|
|
|
* \param[in] b the second string.
|
|
|
|
* \return <tt>strcmp(a, b)</tt>, and treats \c NULL strings like \c ""
|
|
|
|
* ones.
|
|
|
|
*/
|
|
|
|
static inline int a_strcmp(const char *a, const char *b)
|
|
|
|
{
|
2008-06-23 00:51:50 +02:00
|
|
|
return strcmp(NONULL(a), NONULL(b));
|
2007-09-21 13:09:23 +02:00
|
|
|
}
|
|
|
|
|
2008-03-19 17:33:54 +01:00
|
|
|
/** \brief \c NULL resistant strcasecmp.
|
|
|
|
* \param[in] a the first string.
|
|
|
|
* \param[in] b the second string.
|
|
|
|
* \return <tt>strcasecmp(a, b)</tt>, and treats \c NULL strings like \c ""
|
|
|
|
* ones.
|
|
|
|
*/
|
|
|
|
static inline int a_strcasecmp(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
return strcasecmp(NONULL(a), NONULL(b));
|
|
|
|
}
|
|
|
|
|
2007-09-24 17:50:42 +02:00
|
|
|
/** \brief \c NULL resistant strncmp.
|
|
|
|
* \param[in] a the first string.
|
|
|
|
* \param[in] b the second string.
|
|
|
|
* \param[in] n the number of maximum chars to compare.
|
|
|
|
* \return <tt>strncmp(a, b, n)</tt>, and treats \c NULL strings like \c ""
|
|
|
|
* ones.
|
|
|
|
*/
|
|
|
|
static inline int a_strncmp(const char *a, const char *b, ssize_t n)
|
|
|
|
{
|
|
|
|
return strncmp(NONULL(a), NONULL(b), n);
|
|
|
|
}
|
|
|
|
|
2007-09-24 16:42:04 +02:00
|
|
|
ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1)));
|
2007-09-24 16:44:36 +02:00
|
|
|
ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1)));
|
2007-09-24 16:42:04 +02:00
|
|
|
|
2007-09-24 16:53:03 +02:00
|
|
|
/** \brief safe strcat.
|
|
|
|
*
|
|
|
|
* The a_strcat() function appends the string \c src at the end of the buffer
|
|
|
|
* \c dst if space is available.
|
|
|
|
*
|
|
|
|
* \param[in] dst destination buffer.
|
|
|
|
* \param[in] n size of the buffer, Negative sizes are allowed.
|
|
|
|
* \param[in] src the string to append.
|
|
|
|
* \return <tt>a_strlen(dst) + a_strlen(src)</tt>
|
|
|
|
*/
|
|
|
|
static inline ssize_t a_strcat(char *dst, ssize_t n, const char *src)
|
|
|
|
{
|
|
|
|
ssize_t dlen = a_strnlen(dst, n - 1);
|
|
|
|
return dlen + a_strcpy(dst + dlen, n - dlen, src);
|
|
|
|
}
|
|
|
|
|
2008-01-07 15:48:04 +01:00
|
|
|
/** \brief safe strncat.
|
|
|
|
*
|
2008-03-19 20:31:21 +01:00
|
|
|
* The a_strncat() function appends at most \c n chars from the string \c src
|
2008-01-07 15:48:04 +01:00
|
|
|
* at the end of the buffer \c dst if space is available.
|
|
|
|
*
|
|
|
|
* \param[in] dst destination buffer.
|
|
|
|
* \param[in] n size of the buffer, Negative sizes are allowed.
|
|
|
|
* \param[in] src the string to append.
|
|
|
|
* \param[in] l maximum number of chars of src to consider.
|
2008-03-19 20:31:21 +01:00
|
|
|
* \return the smallest value between <tt>a_strlen(dst) + a_strlen(src)</tt>
|
|
|
|
* and <tt>a_strlen(dst) + l</tt>
|
2008-01-07 15:48:04 +01:00
|
|
|
*/
|
|
|
|
static inline ssize_t
|
|
|
|
a_strncat(char *dst, ssize_t n, const char *src, ssize_t l)
|
|
|
|
{
|
|
|
|
ssize_t dlen = a_strnlen(dst, n - 1);
|
|
|
|
return dlen + a_strncpy(dst + dlen, n - dlen, src, l);
|
|
|
|
}
|
|
|
|
|
2008-05-02 16:07:13 +02:00
|
|
|
/** \brief convert a string to a boolean value.
|
|
|
|
*
|
|
|
|
* The a_strtobool() function converts a string \c s into a boolean.
|
|
|
|
* It recognizes the strings "true", "on", "yes" and "1".
|
|
|
|
*
|
|
|
|
* \param[in] s the string to convert
|
|
|
|
* \return true if the string is recognized as possibly true, false otherwise.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
a_strtobool(const char *s)
|
|
|
|
{
|
|
|
|
if(!strcasecmp(s, "true")
|
|
|
|
|| !strcasecmp(s, "on")
|
|
|
|
|| !strcasecmp(s, "yes")
|
|
|
|
|| !strcasecmp(s, "1"))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-06-19 09:45:08 +02:00
|
|
|
#define fatal(string, ...) _fatal(__LINE__, \
|
|
|
|
__FUNCTION__, \
|
|
|
|
string, ## __VA_ARGS__)
|
|
|
|
void _fatal(int, const char *, const char *, ...)
|
2008-03-09 17:10:23 +01:00
|
|
|
__attribute__ ((noreturn)) __attribute__ ((format(printf, 3, 4)));
|
|
|
|
|
|
|
|
#define warn(string, ...) _warn(__LINE__, \
|
|
|
|
__FUNCTION__, \
|
|
|
|
string, ## __VA_ARGS__)
|
|
|
|
void _warn(int, const char *, const char *, ...)
|
|
|
|
__attribute__ ((format(printf, 3, 4)));
|
|
|
|
|
2008-06-23 14:01:33 +02:00
|
|
|
position_t position_fromstr(const char *, ssize_t);
|
|
|
|
char * position_tostr(position_t);
|
2008-01-12 14:34:53 +01:00
|
|
|
void *name_func_lookup(const char *, const name_func_link_t *);
|
2008-05-23 16:53:37 +02:00
|
|
|
const char * name_func_rlookup(void *, const name_func_link_t *);
|
2008-05-20 15:39:47 +02:00
|
|
|
void a_exec(const char *);
|
2007-09-11 13:35:58 +02:00
|
|
|
|
2007-09-05 20:15:00 +02:00
|
|
|
#endif
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|