forked from drahnr/oregano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoregano-utils.c
105 lines (96 loc) · 2.2 KB
/
oregano-utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* oregano-utils.c
*
*
* Author:
* Richard Hult <rhult@hem.passagen.se>
* Ricardo Markiewicz <rmarkie@fi.uba.ar>
* Andres de Barbara <adebarbara@fi.uba.ar>
* Marc Lorber <lorber.marc@wanadoo.fr>
* Guido Trentalancia <guido@trentalancia.com>
*
* Web page: https://ahoi.io/project/oregano
*
* Copyright (C) 1999-2001 Richard Hult
* Copyright (C) 2003,2006 Ricardo Markiewicz
* Copyright (C) 2009-2012 Marc Lorber
* Copyright (C) 2017 Guido Trentalancia
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <gtk/gtk.h>
#include "debug.h"
#include "oregano-utils.h"
gdouble oregano_strtod (const gchar *str, const gchar *unit)
{
gboolean unit_does_not_match = FALSE;
gdouble ret;
gchar *endptr, *c;
size_t unit_length = 0;
if (unit)
unit_length = strlen (unit);
if (!str)
return 0.0;
ret = g_ascii_strtod (str, &endptr);
for (c = endptr; *c; c++) {
switch (*c) {
case 'T':
ret *= 1e12;
break;
case 'G':
ret *= 1e9;
break;
case 'M':
ret *= 1e6;
break;
case 'k':
ret *= 1e3;
break;
case 'm':
ret *= 1e-3;
break;
case 'u':
ret *= 1e-6;
break;
case 'n':
ret *= 1e-9;
break;
case 'p':
ret *= 1e-12;
break;
case 'f':
ret *= 1e-15;
break;
case ' ':
break;
default:
if (c) {
if (!g_ascii_strncasecmp (c, unit, unit_length)) {
return ret;
} else {
unit_does_not_match = TRUE;
break;
}
}
break;
}
}
if (unit_does_not_match)
g_printf ("Unexpected unit of measurement\n");
return ret;
}