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"
44 #include <fcgi_stdio.h>
46 size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */
52 dst_len = strlen (dst);
53 src_len = strlen (src);
54 retval = dst_len + src_len;
56 if ((dst_len + 1) >= size)
63 /* Result will be truncated. */
67 memcpy (dst, src, src_len);
71 } /* }}} size_t c_strlcat */
73 int ds_list_from_rrd_file (char *file, /* {{{ */
74 size_t *ret_dses_num, char ***ret_dses)
76 char *rrd_argv[] = { "info", file, NULL };
77 int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
85 info = rrd_info (rrd_argc, rrd_argv);
88 printf ("%s: rrd_info (%s) failed.\n", __func__, file);
92 for (ptr = info; ptr != NULL; ptr = ptr->next)
99 if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
102 keylen = strlen (ptr->key);
103 if (keylen < strlen ("ds[?].index"))
106 dslen = keylen - strlen ("ds[].index");
109 if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
112 ds = malloc (dslen + 1);
116 memcpy (ds, ptr->key + strlen ("ds["), dslen);
119 tmp = realloc (dses, sizeof (*dses) * (dses_num + 1));
131 rrd_info_free (info);
135 assert (dses == NULL);
139 *ret_dses_num = dses_num;
143 } /* }}} int ds_list_from_rrd_file */
145 static int hsv_to_rgb (double *hsv, double *rgb) /* {{{ */
147 double c = hsv[2] * hsv[1];
148 double h = hsv[0] / 60.0;
149 double x = c * (1.0 - fabs (fmod (h, 2.0) - 1));
150 double m = hsv[2] - c;
156 if ((0.0 <= h) && (h < 1.0)) { rgb[0] = 1.0; rgb[1] = x; rgb[2] = 0.0; }
157 else if ((1.0 <= h) && (h < 2.0)) { rgb[0] = x; rgb[1] = 1.0; rgb[2] = 0.0; }
158 else if ((2.0 <= h) && (h < 3.0)) { rgb[0] = 0.0; rgb[1] = 1.0; rgb[2] = x; }
159 else if ((3.0 <= h) && (h < 4.0)) { rgb[0] = 0.0; rgb[1] = x; rgb[2] = 1.0; }
160 else if ((4.0 <= h) && (h < 5.0)) { rgb[0] = x; rgb[1] = 0.0; rgb[2] = 1.0; }
161 else if ((5.0 <= h) && (h < 6.0)) { rgb[0] = 1.0; rgb[1] = 0.0; rgb[2] = x; }
168 } /* }}} int hsv_to_rgb */
170 static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */
176 r = (uint8_t) (255.0 * rgb[0]);
177 g = (uint8_t) (255.0 * rgb[1]);
178 b = (uint8_t) (255.0 * rgb[2]);
180 return ((((uint32_t) r) << 16)
181 | (((uint32_t) g) << 8)
183 } /* }}} uint32_t rgb_to_uint32 */
185 static int uint32_to_rgb (uint32_t color, double *rgb)
191 r = (uint8_t) ((color >> 16) & 0x00ff);
192 g = (uint8_t) ((color >> 8) & 0x00ff);
193 b = (uint8_t) ((color >> 0) & 0x00ff);
195 rgb[0] = ((double) r) / 255.0;
196 rgb[1] = ((double) g) / 255.0;
197 rgb[2] = ((double) b) / 255.0;
200 } /* }}} int uint32_to_rgb */
202 uint32_t get_random_color (void) /* {{{ */
204 double hsv[3] = { 0.0, 1.0, 1.0 };
205 double rgb[3] = { 0.0, 0.0, 0.0 };
207 hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0);
209 hsv_to_rgb (hsv, rgb);
211 return (rgb_to_uint32 (rgb));
212 } /* }}} uint32_t get_random_color */
214 uint32_t fade_color (uint32_t color) /* {{{ */
218 uint32_to_rgb (color, rgb);
219 rgb[0] = 1.0 - ((1.0 - rgb[0]) * 0.1);
220 rgb[1] = 1.0 - ((1.0 - rgb[1]) * 0.1);
221 rgb[2] = 1.0 - ((1.0 - rgb[2]) * 0.1);
223 return (rgb_to_uint32 (rgb));
224 } /* }}} uint32_t fade_color */
226 int print_debug (const char *format, ...) /* {{{ */
228 static _Bool have_header = 0;
235 printf ("Content-Type: text/plain\n\n");
239 va_start (ap, format);
240 status = vprintf (format, ap);
244 } /* }}} int print_debug */
246 char *strtolower (char *str) /* {{{ */
253 for (i = 0; str[i] != 0; i++)
254 str[i] = (char) tolower ((int) str[i]);
257 } /* }}} char *strtolower */
259 char *strtolower_copy (const char *str)
264 return (strtolower (strdup (str)));
267 /* vim: set sw=2 sts=2 et fdm=marker : */