Removed trailing whitespaces from .h and .c files using the
[obnox/wireshark/wip.git] / epan / ftypes / ftype-double.c
1 /*
2  * $Id: ftype-double.c,v 1.7 2002/08/28 20:41:00 jmayer Exp $
3  *
4  * Ethereal - Network traffic analyzer
5  * By Gerald Combs <gerald@ethereal.com>
6  * Copyright 2001 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program 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 program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <ftypes-int.h>
28 #include <stdlib.h>
29 #include <math.h>
30 #include <errno.h>
31
32 static void
33 double_fvalue_new(fvalue_t *fv)
34 {
35         fv->value.floating = 0.0;
36 }
37
38 static void
39 double_fvalue_set_floating(fvalue_t *fv, gdouble value)
40 {
41         fv->value.floating = value;
42 }
43
44 static double
45 value_get_floating(fvalue_t *fv)
46 {
47         return fv->value.floating;
48 }
49
50 static gboolean
51 val_from_string(fvalue_t *fv, char *s, LogFunc logfunc)
52 {
53         char    *endptr = NULL;
54
55         fv->value.floating = strtod(s, &endptr);
56
57         if (endptr == s || *endptr != '\0') {
58                 /* This isn't a valid number. */
59                 logfunc("\"%s\" is not a valid number.", s);
60                 return FALSE;
61         }
62         if (errno == ERANGE) {
63                 if (fv->value.floating == 0) {
64                         logfunc("\"%s\" causes floating-point underflow.", s);
65                 }
66                 else if (fv->value.floating == HUGE_VAL) {
67                         logfunc("\"%s\" causes floating-point overflow.", s);
68                 }
69                 else {
70                         logfunc("\"%s\" is not a valid floating-point number.",
71                             s);
72                 }
73                 return FALSE;
74         }
75
76         return TRUE;
77 }
78
79
80 static gboolean
81 cmp_eq(fvalue_t *a, fvalue_t *b)
82 {
83         return a->value.floating == b->value.floating;
84 }
85
86 static gboolean
87 cmp_ne(fvalue_t *a, fvalue_t *b)
88 {
89         return a->value.floating != b->value.floating;
90 }
91
92 static gboolean
93 cmp_gt(fvalue_t *a, fvalue_t *b)
94 {
95         return a->value.floating > b->value.floating;
96 }
97
98 static gboolean
99 cmp_ge(fvalue_t *a, fvalue_t *b)
100 {
101         return a->value.floating >= b->value.floating;
102 }
103
104 static gboolean
105 cmp_lt(fvalue_t *a, fvalue_t *b)
106 {
107         return a->value.floating < b->value.floating;
108 }
109
110 static gboolean
111 cmp_le(fvalue_t *a, fvalue_t *b)
112 {
113         return a->value.floating <= b->value.floating;
114 }
115
116 void
117 ftype_register_double(void)
118 {
119
120         static ftype_t float_type = {
121                 "FT_FLOAT",
122                 "floating point (single-precision)",
123                 0,
124                 double_fvalue_new,
125                 NULL,
126                 val_from_string,
127
128                 NULL,
129                 NULL,
130                 double_fvalue_set_floating,
131
132                 NULL,
133                 NULL,
134                 value_get_floating,
135
136                 cmp_eq,
137                 cmp_ne,
138                 cmp_gt,
139                 cmp_ge,
140                 cmp_lt,
141                 cmp_le,
142
143                 NULL,
144                 NULL,
145         };
146
147         static ftype_t double_type = {
148                 "FT_DOUBLE",
149                 "floating point (double-precision)",
150                 0,
151                 double_fvalue_new,
152                 NULL,
153                 val_from_string,
154
155                 NULL,
156                 NULL,
157                 double_fvalue_set_floating,
158
159                 NULL,
160                 NULL,
161                 value_get_floating,
162
163                 cmp_eq,
164                 cmp_ne,
165                 cmp_gt,
166                 cmp_ge,
167                 cmp_lt,
168                 cmp_le,
169
170                 NULL,
171                 NULL,
172         };
173
174         ftype_register(FT_FLOAT, &float_type);
175         ftype_register(FT_DOUBLE, &double_type);
176 }