xdbe replaced with more generic pixmap based buffering but there are 2 bugs:
[monky] / src / temphelper.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * temphelper.c:  aid in converting temperature units
5  *
6  * Copyright (C) 2008 Phil Sutter <Phil@nwl.cc>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
21  * USA.
22  *
23  */
24 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <sys/types.h>
30 #include "temphelper.h"
31 #include "conky.h"
32
33 /* default to output in celsius */
34 static enum TEMP_UNIT output_unit = TEMP_CELSIUS;
35
36 static double fahrenheit_to_celsius(double n)
37 {
38         return ((n - 32) * 5 / 9);
39 }
40
41 static double celsius_to_fahrenheit(double n)
42 {
43         return ((n * 9 / 5) + 32);
44 }
45
46 int set_temp_output_unit(const char *name)
47 {
48         long i;
49         int rc = 0;
50         char *buf;
51
52         if (!name)
53                 return 1;
54
55         buf = strdup(name);
56         for (i = 0; i < (long)strlen(name); i++)
57                 buf[i] = tolower(name[i]);
58
59         if (!strcmp(buf, "celsius"))
60                 output_unit = TEMP_CELSIUS;
61         else if (!strcmp(buf, "fahrenheit"))
62                 output_unit = TEMP_FAHRENHEIT;
63         else
64                 rc = 1;
65         free(buf);
66         return rc;
67 }
68
69 static double convert_temp_output(double n, enum TEMP_UNIT input_unit)
70 {
71         if (input_unit == output_unit)
72                 return n;
73
74         switch(output_unit) {
75                 case TEMP_CELSIUS:
76                         return fahrenheit_to_celsius(n);
77                 case TEMP_FAHRENHEIT:
78                         return celsius_to_fahrenheit(n);
79         }
80         /* NOT REACHED */
81         return 0.0;
82 }
83
84 int temp_print(char *p, size_t p_max_size, double n, enum TEMP_UNIT input_unit)
85 {
86         int out;
87         size_t plen;
88
89         out = round_to_int_temp(convert_temp_output(n, input_unit));
90         plen = spaced_print(p, p_max_size, "%d", 3, out);
91
92         return !(plen >= p_max_size);
93 }