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 uint32_t get_random_color (void) /* {{{ */
164 double hsv[3] = { 0.0, 1.0, 1.0 };
165 double rgb[3] = { 0.0, 0.0, 0.0 };
167 hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0);
169 hsv_to_rgb (hsv, rgb);
171 return (rgb_to_uint32 (rgb));
172 } /* }}} uint32_t get_random_color */
174 int print_debug (const char *format, ...) /* {{{ */
176 static _Bool have_header = 0;
183 printf ("Content-Type: text/plain\n\n");
187 va_start (ap, format);
188 status = vprintf (format, ap);
192 } /* }}} int print_debug */
194 char *strtolower (char *str) /* {{{ */
201 for (i = 0; str[i] != 0; i++)
202 str[i] = (char) tolower ((int) str[i]);
205 } /* }}} char *strtolower */
207 char *strtolower_copy (const char *str)
212 return (strdup (str));
215 /* vim: set sw=2 sts=2 et fdm=marker : */