18 #include "graph_list.h"
21 #include <fcgi_stdio.h>
23 size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */
29 dst_len = strlen (dst);
30 src_len = strlen (src);
31 retval = dst_len + src_len;
33 if ((dst_len + 1) >= size)
40 /* Result will be truncated. */
44 memcpy (dst, src, src_len);
48 } /* }}} size_t c_strlcat */
50 int ds_list_from_rrd_file (char *file, /* {{{ */
51 size_t *ret_dses_num, char ***ret_dses)
53 char *rrd_argv[] = { "info", file, NULL };
54 int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
62 info = rrd_info (rrd_argc, rrd_argv);
65 printf ("%s: rrd_info (%s) failed.\n", __func__, file);
69 for (ptr = info; ptr != NULL; ptr = ptr->next)
76 if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
79 keylen = strlen (ptr->key);
80 if (keylen < strlen ("ds[?].index"))
83 dslen = keylen - strlen ("ds[].index");
86 if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
89 ds = malloc (dslen + 1);
93 memcpy (ds, ptr->key + strlen ("ds["), dslen);
96 tmp = realloc (dses, sizeof (*dses) * (dses_num + 1));
108 rrd_info_free (info);
112 assert (dses == NULL);
116 *ret_dses_num = dses_num;
120 } /* }}} int ds_list_from_rrd_file */
122 static int hsv_to_rgb (double *hsv, double *rgb) /* {{{ */
124 double c = hsv[2] * hsv[1];
125 double h = hsv[0] / 60.0;
126 double x = c * (1.0 - fabs (fmod (h, 2.0) - 1));
127 double m = hsv[2] - c;
133 if ((0.0 <= h) && (h < 1.0)) { rgb[0] = 1.0; rgb[1] = x; rgb[2] = 0.0; }
134 else if ((1.0 <= h) && (h < 2.0)) { rgb[0] = x; rgb[1] = 1.0; rgb[2] = 0.0; }
135 else if ((2.0 <= h) && (h < 3.0)) { rgb[0] = 0.0; rgb[1] = 1.0; rgb[2] = x; }
136 else if ((3.0 <= h) && (h < 4.0)) { rgb[0] = 0.0; rgb[1] = x; rgb[2] = 1.0; }
137 else if ((4.0 <= h) && (h < 5.0)) { rgb[0] = x; rgb[1] = 0.0; rgb[2] = 1.0; }
138 else if ((5.0 <= h) && (h < 6.0)) { rgb[0] = 1.0; rgb[1] = 0.0; rgb[2] = x; }
145 } /* }}} int hsv_to_rgb */
147 static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */
153 r = (uint8_t) (255.0 * rgb[0]);
154 g = (uint8_t) (255.0 * rgb[1]);
155 b = (uint8_t) (255.0 * rgb[2]);
157 return ((((uint32_t) r) << 16)
158 | (((uint32_t) g) << 8)
160 } /* }}} uint32_t rgb_to_uint32 */
162 static int uint32_to_rgb (uint32_t color, double *rgb)
168 r = (uint8_t) ((color >> 16) & 0x00ff);
169 g = (uint8_t) ((color >> 8) & 0x00ff);
170 b = (uint8_t) ((color >> 0) & 0x00ff);
172 rgb[0] = ((double) r) / 255.0;
173 rgb[1] = ((double) g) / 255.0;
174 rgb[2] = ((double) b) / 255.0;
177 } /* }}} int uint32_to_rgb */
179 uint32_t get_random_color (void) /* {{{ */
181 double hsv[3] = { 0.0, 1.0, 1.0 };
182 double rgb[3] = { 0.0, 0.0, 0.0 };
184 hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0);
186 hsv_to_rgb (hsv, rgb);
188 return (rgb_to_uint32 (rgb));
189 } /* }}} uint32_t get_random_color */
191 uint32_t fade_color (uint32_t color) /* {{{ */
195 uint32_to_rgb (color, rgb);
196 rgb[0] = 1.0 - ((1.0 - rgb[0]) * 0.1);
197 rgb[1] = 1.0 - ((1.0 - rgb[1]) * 0.1);
198 rgb[2] = 1.0 - ((1.0 - rgb[2]) * 0.1);
200 return (rgb_to_uint32 (rgb));
201 } /* }}} uint32_t fade_color */
203 int print_debug (const char *format, ...) /* {{{ */
205 static _Bool have_header = 0;
212 printf ("Content-Type: text/plain\n\n");
216 va_start (ap, format);
217 status = vprintf (format, ap);
221 } /* }}} int print_debug */
223 char *strtolower (char *str) /* {{{ */
230 for (i = 0; str[i] != 0; i++)
231 str[i] = (char) tolower ((int) str[i]);
234 } /* }}} char *strtolower */
236 char *strtolower_copy (const char *str)
241 return (strtolower (strdup (str)));
244 /* vim: set sw=2 sts=2 et fdm=marker : */