Add Auto value to Position
[awesome.git] / common / util.c
blob4b90201f90bb26e3c81d2103dc89bd152e26fea2
1 /*
2 * util.c - useful functions
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 #include <stdarg.h>
24 #include <stdio.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <limits.h>
29 #include "util.h"
31 /** Print error and exit with EXIT_FAILURE code
33 void
34 _eprint(int line, const char *fct, const char *fmt, ...)
36 va_list ap;
38 va_start(ap, fmt);
39 fprintf(stderr, "E: awesome: %s:%d: ", fct, line);
40 vfprintf(stderr, fmt, ap);
41 va_end(ap);
42 exit(EXIT_FAILURE);
45 /** Print error message on stderr
47 void
48 _warn(int line, const char *fct, const char *fmt, ...)
50 va_list ap;
51 va_start(ap, fmt);
52 fprintf(stderr, "W: awesome: %s:%d: ", fct, line);
53 vfprintf(stderr, fmt, ap);
54 va_end(ap);
57 /** Compute a value from a string containing
58 * an absolute or a relative number. If relative,
59 * add it to current_value.
60 * \param arg the string with the number
61 * \param current_value value to add the number if it's relative
62 * \return new value
64 double
65 compute_new_value_from_arg(const char *arg, double current_value)
67 double delta;
69 if(arg && sscanf(arg, "%lf", &delta) == 1)
71 if(arg[0] == '+' || arg[0] == '-')
72 current_value += delta;
73 else
74 current_value = delta;
77 return current_value;
80 /** Lookup for a function pointer from its name
81 * in the given name_func_link_t list
82 * \param funcname Function name
83 * \param list Function and name link list
84 * \return function pointer
86 void *
87 name_func_lookup(const char *funcname, const name_func_link_t *list)
89 int i;
91 if(funcname && list)
92 for(i = 0; list[i].name; i++)
93 if(!a_strcmp(funcname, list[i].name))
94 return list[i].func;
96 return NULL;
99 Position
100 position_get_from_str(const char *pos)
102 if(!a_strncmp(pos, "top", 3))
103 return Top;
104 else if(!a_strncmp(pos, "bottom", 6))
105 return Bottom;
106 else if(!a_strncmp(pos, "right", 5))
107 return Right;
108 else if(!a_strncmp(pos, "left", 4))
109 return Left;
110 else if(!a_strncmp(pos, "auto", 4))
111 return Auto;
112 return Off;
115 /** \brief safe limited strcpy.
117 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
118 * always adding a final \c \\0 in \c dst.
120 * \param[in] dst destination buffer.
121 * \param[in] n size of the buffer. Negative sizes are allowed.
122 * \param[in] src source string.
123 * \param[in] l maximum number of chars to copy.
125 * \return minimum of \c src \e length and \c l.
127 ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
129 ssize_t len = MIN(a_strlen(src), l);
131 if (n > 0)
133 ssize_t dlen = MIN(n - 1, len);
134 memcpy(dst, src, dlen);
135 dst[dlen] = '\0';
138 return len;
141 /** \brief safe strcpy.
143 * Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
144 * adding a final \c \\0 in \c dst.
146 * \param[in] dst destination buffer.
147 * \param[in] n size of the buffer. Negative sizes are allowed.
148 * \param[in] src source string.
149 * \return \c src \e length. If this value is \>= \c n then the copy was
150 * truncated.
152 ssize_t a_strcpy(char *dst, ssize_t n, const char *src)
154 ssize_t len = a_strlen(src);
156 if (n > 0)
158 ssize_t dlen = MIN(n - 1, len);
159 memcpy(dst, src, dlen);
160 dst[dlen] = '\0';
163 return len;
165 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80