2 * collection4 - common.c
3 * Copyright (C) 2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 * Florian octo Forster <ff at octo.it>
31 #include <sys/types.h>
41 #include "graph_list.h"
42 #include "utils_cgi.h"
45 #include <fcgi_stdio.h>
47 size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */
53 dst_len = strlen (dst);
54 src_len = strlen (src);
55 retval = dst_len + src_len;
57 if ((dst_len + 1) >= size)
64 /* Result will be truncated. */
68 memcpy (dst, src, src_len);
72 } /* }}} size_t c_strlcat */
74 int ds_list_from_rrd_file (char *file, /* {{{ */
75 size_t *ret_dses_num, char ***ret_dses)
77 char *rrd_argv[] = { "info", file, NULL };
78 int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
86 info = rrd_info (rrd_argc, rrd_argv);
89 printf ("%s: rrd_info (%s) failed.\n", __func__, file);
93 for (ptr = info; ptr != NULL; ptr = ptr->next)
100 if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
103 keylen = strlen (ptr->key);
104 if (keylen < strlen ("ds[?].index"))
107 dslen = keylen - strlen ("ds[].index");
110 if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
113 ds = malloc (dslen + 1);
117 memcpy (ds, ptr->key + strlen ("ds["), dslen);
120 tmp = realloc (dses, sizeof (*dses) * (dses_num + 1));
132 rrd_info_free (info);
136 assert (dses == NULL);
140 *ret_dses_num = dses_num;
144 } /* }}} int ds_list_from_rrd_file */
146 static int hsv_to_rgb (double *hsv, double *rgb) /* {{{ */
148 double c = hsv[2] * hsv[1];
149 double h = hsv[0] / 60.0;
150 double x = c * (1.0 - fabs (fmod (h, 2.0) - 1));
151 double m = hsv[2] - c;
157 if ((0.0 <= h) && (h < 1.0)) { rgb[0] = 1.0; rgb[1] = x; rgb[2] = 0.0; }
158 else if ((1.0 <= h) && (h < 2.0)) { rgb[0] = x; rgb[1] = 1.0; rgb[2] = 0.0; }
159 else if ((2.0 <= h) && (h < 3.0)) { rgb[0] = 0.0; rgb[1] = 1.0; rgb[2] = x; }
160 else if ((3.0 <= h) && (h < 4.0)) { rgb[0] = 0.0; rgb[1] = x; rgb[2] = 1.0; }
161 else if ((4.0 <= h) && (h < 5.0)) { rgb[0] = x; rgb[1] = 0.0; rgb[2] = 1.0; }
162 else if ((5.0 <= h) && (h < 6.0)) { rgb[0] = 1.0; rgb[1] = 0.0; rgb[2] = x; }
169 } /* }}} int hsv_to_rgb */
171 static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */
177 r = (uint8_t) (255.0 * rgb[0]);
178 g = (uint8_t) (255.0 * rgb[1]);
179 b = (uint8_t) (255.0 * rgb[2]);
181 return ((((uint32_t) r) << 16)
182 | (((uint32_t) g) << 8)
184 } /* }}} uint32_t rgb_to_uint32 */
186 static int uint32_to_rgb (uint32_t color, double *rgb) /* {{{ */
192 r = (uint8_t) ((color >> 16) & 0x00ff);
193 g = (uint8_t) ((color >> 8) & 0x00ff);
194 b = (uint8_t) ((color >> 0) & 0x00ff);
196 rgb[0] = ((double) r) / 255.0;
197 rgb[1] = ((double) g) / 255.0;
198 rgb[2] = ((double) b) / 255.0;
201 } /* }}} int uint32_to_rgb */
203 uint32_t get_random_color (void) /* {{{ */
205 double hsv[3] = { 0.0, 1.0, 1.0 };
206 double rgb[3] = { 0.0, 0.0, 0.0 };
208 hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0);
210 hsv_to_rgb (hsv, rgb);
212 return (rgb_to_uint32 (rgb));
213 } /* }}} uint32_t get_random_color */
215 uint32_t fade_color (uint32_t color) /* {{{ */
219 uint32_to_rgb (color, rgb);
220 rgb[0] = 1.0 - ((1.0 - rgb[0]) * 0.1);
221 rgb[1] = 1.0 - ((1.0 - rgb[1]) * 0.1);
222 rgb[2] = 1.0 - ((1.0 - rgb[2]) * 0.1);
224 return (rgb_to_uint32 (rgb));
225 } /* }}} uint32_t fade_color */
227 int print_debug (const char *format, ...) /* {{{ */
229 static _Bool have_header = 0;
236 printf ("Content-Type: text/plain\n\n");
240 va_start (ap, format);
241 status = vprintf (format, ap);
245 } /* }}} int print_debug */
247 char *strtolower (char *str) /* {{{ */
254 for (i = 0; str[i] != 0; i++)
255 str[i] = (char) tolower ((int) str[i]);
258 } /* }}} char *strtolower */
260 char *strtolower_copy (const char *str) /* {{{ */
265 return (strtolower (strdup (str)));
266 } /* }}} char *strtolower_copy */
268 int get_time_args (long *ret_begin, long *ret_end, /* {{{ */
271 const char *begin_str;
279 if ((ret_begin == NULL) || (ret_end == NULL))
282 begin_str = param ("begin");
283 end_str = param ("end");
285 now = (long) time (NULL);
288 *ret_begin = now - 86400;
291 if (begin_str != NULL)
295 tmp = strtol (begin_str, &endptr, /* base = */ 0);
296 if ((endptr == begin_str) || (errno != 0))
303 else /* if (begin_str == NULL) */
312 tmp = strtol (end_str, &endptr, /* base = */ 0);
313 if ((endptr == end_str) || (errno != 0))
321 else /* if (end_str == NULL) */
340 } /* }}} int get_time_args */
342 /* vim: set sw=2 sts=2 et fdm=marker : */