17 #include "graph_list.h"
20 #include <fcgi_stdio.h>
22 size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */
28 dst_len = strlen (dst);
29 src_len = strlen (src);
30 retval = dst_len + src_len;
32 if ((dst_len + 1) >= size)
39 /* Result will be truncated. */
43 memcpy (dst, src, src_len);
47 } /* }}} size_t c_strlcat */
49 int ds_list_from_rrd_file (char *file, /* {{{ */
50 size_t *ret_dses_num, char ***ret_dses)
52 char *rrd_argv[] = { "info", file, NULL };
53 int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
61 info = rrd_info (rrd_argc, rrd_argv);
64 printf ("%s: rrd_info (%s) failed.\n", __func__, file);
68 for (ptr = info; ptr != NULL; ptr = ptr->next)
75 if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
78 keylen = strlen (ptr->key);
79 if (keylen < strlen ("ds[?].index"))
82 dslen = keylen - strlen ("ds[].index");
85 if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
88 ds = malloc (dslen + 1);
92 memcpy (ds, ptr->key + strlen ("ds["), dslen);
95 tmp = realloc (dses, sizeof (*dses) * (dses_num + 1));
107 rrd_info_free (info);
111 assert (dses == NULL);
115 *ret_dses_num = dses_num;
119 } /* }}} int ds_list_from_rrd_file */
121 static int hsv_to_rgb (double *hsv, double *rgb) /* {{{ */
123 double c = hsv[2] * hsv[1];
124 double h = hsv[0] / 60.0;
125 double x = c * (1.0 - fabs (fmod (h, 2.0) - 1));
126 double m = hsv[2] - c;
132 if ((0.0 <= h) && (h < 1.0)) { rgb[0] = 1.0; rgb[1] = x; rgb[2] = 0.0; }
133 else if ((1.0 <= h) && (h < 2.0)) { rgb[0] = x; rgb[1] = 1.0; rgb[2] = 0.0; }
134 else if ((2.0 <= h) && (h < 3.0)) { rgb[0] = 0.0; rgb[1] = 1.0; rgb[2] = x; }
135 else if ((3.0 <= h) && (h < 4.0)) { rgb[0] = 0.0; rgb[1] = x; rgb[2] = 1.0; }
136 else if ((4.0 <= h) && (h < 5.0)) { rgb[0] = x; rgb[1] = 0.0; rgb[2] = 1.0; }
137 else if ((5.0 <= h) && (h < 6.0)) { rgb[0] = 1.0; rgb[1] = 0.0; rgb[2] = x; }
144 } /* }}} int hsv_to_rgb */
146 static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */
152 r = (uint8_t) (255.0 * rgb[0]);
153 g = (uint8_t) (255.0 * rgb[1]);
154 b = (uint8_t) (255.0 * rgb[2]);
156 return ((((uint32_t) r) << 16)
157 | (((uint32_t) g) << 8)
159 } /* }}} uint32_t rgb_to_uint32 */
161 uint32_t get_random_color (void) /* {{{ */
163 double hsv[3] = { 0.0, 1.0, 1.0 };
164 double rgb[3] = { 0.0, 0.0, 0.0 };
166 hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0);
168 hsv_to_rgb (hsv, rgb);
170 return (rgb_to_uint32 (rgb));
171 } /* }}} uint32_t get_random_color */
173 int print_debug (const char *format, ...) /* {{{ */
175 static _Bool have_header = 0;
182 printf ("Content-Type: text/plain\n\n");
186 va_start (ap, format);
187 status = vprintf (format, ap);
191 } /* }}} int print_debug */
193 /* vim: set sw=2 sts=2 et fdm=marker : */