Code

src/common.c: Add missing folding markers.
[collection4.git] / src / common.c
1 /**
2  * collection4 - common.c
3  * Copyright (C) 2010  Florian octo Forster
4  * 
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.
9  * 
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.
14  * 
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
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <inttypes.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <dirent.h>
35 #include <assert.h>
36 #include <math.h>
38 #include <rrd.h>
40 #include "common.h"
41 #include "graph_list.h"
43 #include <fcgiapp.h>
44 #include <fcgi_stdio.h>
46 size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */
47 {
48   size_t retval;
49   size_t dst_len;
50   size_t src_len;
52   dst_len = strlen (dst);
53   src_len = strlen (src);
54   retval = dst_len + src_len;
56   if ((dst_len + 1) >= size)
57     return (retval);
59   dst  += dst_len;
60   size -= dst_len;
61   assert (size >= 2);
63   /* Result will be truncated. */
64   if (src_len >= size)
65     src_len = size - 1;
67   memcpy (dst, src, src_len);
68   dst[src_len] = 0;
70   return (retval);
71 } /* }}} size_t c_strlcat */
73 int ds_list_from_rrd_file (char *file, /* {{{ */
74     size_t *ret_dses_num, char ***ret_dses)
75 {
76   char *rrd_argv[] = { "info", file, NULL };
77   int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
79   rrd_info_t *info;
80   rrd_info_t *ptr;
82   char **dses = NULL;
83   size_t dses_num = 0;
85   info = rrd_info (rrd_argc, rrd_argv);
86   if (info == NULL)
87   {
88     printf ("%s: rrd_info (%s) failed.\n", __func__, file);
89     return (-1);
90   }
92   for (ptr = info; ptr != NULL; ptr = ptr->next)
93   {
94     size_t keylen;
95     size_t dslen;
96     char *ds;
97     char **tmp;
99     if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
100       continue;
102     keylen = strlen (ptr->key);
103     if (keylen < strlen ("ds[?].index"))
104       continue;
106     dslen = keylen - strlen ("ds[].index");
107     assert (dslen >= 1);
109     if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
110       continue;
112     ds = malloc (dslen + 1);
113     if (ds == NULL)
114       continue;
116     memcpy (ds, ptr->key + strlen ("ds["), dslen);
117     ds[dslen] = 0;
119     tmp = realloc (dses, sizeof (*dses) * (dses_num + 1));
120     if (tmp == NULL)
121     {
122       free (ds);
123       continue;
124     }
125     dses = tmp;
127     dses[dses_num] = ds;
128     dses_num++;
129   }
131   rrd_info_free (info);
133   if (dses_num < 1)
134   {
135     assert (dses == NULL);
136     return (ENOENT);
137   }
139   *ret_dses_num = dses_num;
140   *ret_dses = dses;
142   return (0);
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;
152   rgb[0] = 0.0;
153   rgb[1] = 0.0;
154   rgb[2] = 0.0;
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; }
163   rgb[0] += m;
164   rgb[1] += m;
165   rgb[2] += m;
167   return (0);
168 } /* }}} int hsv_to_rgb */
170 static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */
172   uint8_t r;
173   uint8_t g;
174   uint8_t b;
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)
182       | ((uint32_t) b));
183 } /* }}} uint32_t rgb_to_uint32 */
185 static int uint32_to_rgb (uint32_t color, double *rgb) /* {{{ */
187   uint8_t r;
188   uint8_t g;
189   uint8_t b;
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;
199   return (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) /* {{{ */
216   double rgb[3];
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;
230   va_list ap;
231   int status;
233   if (!have_header)
234   {
235     printf ("Content-Type: text/plain\n\n");
236     have_header = 1;
237   }
239   va_start (ap, format);
240   status = vprintf (format, ap);
241   va_end (ap);
243   return (status);
244 } /* }}} int print_debug */
246 char *strtolower (char *str) /* {{{ */
248   unsigned int i;
250   if (str == NULL)
251     return (NULL);
253   for (i = 0; str[i] != 0; i++)
254     str[i] = (char) tolower ((int) str[i]);
256   return (str);
257 } /* }}} char *strtolower */
259 char *strtolower_copy (const char *str) /* {{{ */
261   if (str == NULL)
262     return (NULL);
264   return (strtolower (strdup (str)));
265 } /* }}} char *strtolower_copy */
267 /* vim: set sw=2 sts=2 et fdm=marker : */