summaryrefslogtreecommitdiff
path: root/legacy/eet/src/lib/eet_utils.c
diff options
context:
space:
mode:
authordoursse <doursse>2007-09-25 18:26:49 +0000
committerdoursse <doursse@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>2007-09-25 18:26:49 +0000
commite769f25d10b4ad9af3544604be6039d9632ee1f5 (patch)
tree74aea2af07ff143f77afb42cc7968303e37176fe /legacy/eet/src/lib/eet_utils.c
parentcba48d38b5a614ee3b1c26854dcecbb369805276 (diff)
* Add conversion functions to replace the use of snprintf with %a
* remove useless defines * minor cleanups fixes bugs #181 and #182 SVN revision: 31805
Diffstat (limited to 'legacy/eet/src/lib/eet_utils.c')
-rw-r--r--legacy/eet/src/lib/eet_utils.c164
1 files changed, 164 insertions, 0 deletions
diff --git a/legacy/eet/src/lib/eet_utils.c b/legacy/eet/src/lib/eet_utils.c
index 898d87fa1f..d4bac8a13c 100644
--- a/legacy/eet/src/lib/eet_utils.c
+++ b/legacy/eet/src/lib/eet_utils.c
@@ -1,3 +1,6 @@
1#include <stdio.h>
2#include <math.h>
3
1int 4int
2_eet_hash_gen(const char *key, int hash_size) 5_eet_hash_gen(const char *key, int hash_size)
3{ 6{
@@ -33,3 +36,164 @@ _eet_hash_gen(const char *key, int hash_size)
33 return hash_num; 36 return hash_num;
34} 37}
35 38
39/* On Windows (using MinGW or VC++), printf-like functions */
40/* rely on MSVCRT, which does not fully support the C99 */
41/* specifications. In particular, they do not support the */
42/* modifier character %a. */
43
44
45/* That function converts a string created by a valid %a */
46/* modifier to a double. */
47/* */
48/* The string must have the following format: */
49/* */
50/* [-]0xh.hhhhhp[+-]e */
51/* */
52/* where e is a decimal number. */
53/* If n is the number of cyphers after the point, the */
54/* returned mantisse and exponents are */
55/* */
56/* mantisse: [-]hhhhhh */
57/* exponent: 2^([+-]e - 4 * n) */
58int
59_eet_string_to_double_convert(const char *src, long long *m, long *e)
60{
61 const char *str;
62 double val;
63 long long mantisse;
64 long exponent;
65 int nbr_decimals;
66 char sign;
67
68 str = src;
69 sign = +1;
70
71 if (*str == '-')
72 {
73 sign = -1;
74 str++;
75 }
76 else if (*str == '0')
77 {
78 str++;
79 if (*str == 'x')
80 str++;
81 else
82 {
83 fprintf(stderr, "[Eet] Error during conversion\n");
84 return 0;
85 }
86 }
87 else
88 {
89 fprintf(stderr, "[Eet] Error during conversion\n");
90 return 0;
91 }
92
93 nbr_decimals = 0;
94 mantisse = (*str >= 'a') ? *str - 'a' + 10 : *str - '0';
95 str++;
96 if (*str == '.')
97 {
98 str++;
99 while (*str != 'p')
100 {
101 mantisse <<= 4;
102 mantisse += (*str >= 'a') ? *str - 'a' + 10 : *str - '0';
103 str++;
104 nbr_decimals++;
105 }
106 }
107 if (sign < 0)
108 mantisse = -mantisse;
109 if (*str != 'p')
110 {
111 fprintf(stderr, "[Eet] Error during conversion\n");
112 return 0;
113 }
114 sign = +1;
115 str++;
116 if (*str == '-')
117 {
118 sign = -1;
119 str++;
120 }
121 else if (*str == '+') str++;
122
123 exponent = 0;
124 while (*str != '\0')
125 {
126 exponent *= 10;
127 exponent += *str - '0';
128 str++;
129 }
130
131 if (sign < 0)
132 exponent = -exponent;
133
134 *m = mantisse;
135 *e = exponent - (nbr_decimals << 2);
136
137 return 1;
138}
139
140/* That function converts a double to a string that as the */
141/* following format: */
142/* */
143/* [-]0xh.hhhhhp[+-]e */
144/* */
145/* where h is a hexadecimal number and e a decimal number. */
146void
147_eet_double_to_string_convert(char *des, double d)
148{
149 static const char look_up_table[] = {'0', '1', '2', '3', '4',
150 '5', '6', '7', '8', '9',
151 'a', 'b', 'c', 'd', 'e', 'f'};
152 int p;
153 int i;
154
155 char *str = des;
156
157 if (d < 0.0)
158 {
159 *(des++) = '-';
160 d = -d;
161 }
162
163 d = frexp(d, &p);
164
165 if (p)
166 {
167 d *= 2;
168 p -= 1;
169 }
170
171 *(des++) = '0';
172 *(des++) = 'x';
173 *(des++) = look_up_table[(size_t)d];
174 *(des++) = '.';
175
176 for (i = 0; i < 16; i++)
177 {
178 d -= floor(d);
179 d *= 16;
180 *(des++) = look_up_table[(size_t)d];
181 }
182
183 while (*(des - 1) == '0')
184 des--;
185
186 if (*(des - 1) == '.')
187 des--;
188
189 *(des++) = 'p';
190 if (p < 0)
191 {
192 *(des++) = '-';
193 p = -p;
194 }
195 else
196 *(des++) = '+';
197
198 sprintf(des, "%d", p);
199}