-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_cast.c
297 lines (250 loc) · 6.72 KB
/
types_cast.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/********************************************
cast.c
copyright 1991,2014-2016 Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 3, 2007.
If you import elements of this code into another product,
you agree to not name that product mawk.
********************************************/
/* cast.c */
#include "mawk.h"
#include "field.h"
#include "types_string.h"
#include "scan.h"
#include "repl.h"
#include "types_int.h"
/* modern strtod accepts "inf" "nan" and hex numbers
awk should not (gawk and mawk agree on this)
maybe if --posix, but stubbed out for now]
*/
#define AWK_USE_POSIX_strtod 0
static double
awk_strtod( const STRING * sval )
{
double ret = 0.0;
const char * s = sval->str;
char * stop;
/* eat space ourselves because it makes it easy to eliminate "inf"/"nan" */
while ( scan_code_get(*(unsigned char *)s) == SC_SPACE )
s++;
switch ( scan_code_get(*(unsigned char *)s) ) {
case SC_DIGIT:
case SC_PLUS:
case SC_MINUS:
case SC_DOT:
errno = 0;
ret = strtod( s, &stop );
#if FPE_TRAPS_ON
if ( errno && ret != 0.0 ) {
rt_error( "overflow converting \"%s\" to double", s );
}
#endif
/* check for hex number */
while ( s < stop ) {
if ( *s == 'x' || *s == 'X' )
return 0.0;
s++;
}
}
return ret;
}
static double
posix_strtod( const STRING * sval )
{
double ret;
errno = 0;
ret = strtod( sval->str, 0 );
#if FPE_TRAPS_ON
if ( errno && ret != 0 ) {
rt_error( "overflow converting \"%s\" to double", sval->str );
}
#endif
return ret;
}
void
cast1_to_d( CELL * cp )
{
switch ( cp->type ) {
case C_NOINIT:
cp->dval = 0.0;
break;
case C_DOUBLE:
return;
case C_MBSTRN:
case C_STRING: {
STRING * sval = (STRING *)cp->ptr;
#if AWK_USE_POSIX_strtod
cp->dval = posix_strtod( sval );
#else
cp->dval = awk_strtod( sval );
#endif
free_STRING( sval );
} break;
case C_STRNUM:
/* don't need to convert, but do need to free the STRING part */
free_STRING( string( cp ) );
break;
default:
bozo( "cast on bad type" );
}
cp->type = C_DOUBLE;
}
static STRING *
slow_convfmt( const char * conv, double d, size_t need )
{
/* don't expect to get here for a reasonable program */
STRING * ret;
char * buffer = (char *)emalloc( need + 1 );
buffer[need] = 0;
sprintf( buffer, conv, d );
ret = new_STRING( buffer );
free( buffer );
return ret;
}
void
cast1_to_s( CELL * cp )
{
switch ( cp->type ) {
case C_NOINIT:
cp->ptr = STRING_dup( the_empty_str );
break;
case C_DOUBLE: {
char buffer[1024];
const double d = cp->dval;
if ( is_int_double( d ) ) {
#if LONG64
sprintf( buffer, "%ld", (int64_t)d );
#else
sprintf( buffer, "%lld", (int64_t)d );
#endif
cp->ptr = new_STRING( buffer );
}
else {
const char * conv = string( CONVFMT )->str;
unsigned used;
used = snprintf( buffer, 1024, conv, d );
if ( (int)used < 0 ) {
rt_error( "snprintf bozo (%d)", errno );
}
if ( used > 1024 ) {
cp->ptr = slow_convfmt( conv, d, used );
}
else {
cp->ptr = new_STRING2( buffer, used );
}
}
break;
}
case C_STRING:
return;
case C_MBSTRN:
case C_STRNUM:
break;
default:
bozo( "bad type on cast" );
}
cp->type = C_STRING;
}
void
cast_to_RE( CELL * cp )
{
register void *p;
if ( cp->type < C_STRING )
cast1_to_s( cp );
p = re_compile( string( cp ) );
free_STRING( string( cp ) );
cp->type = C_RE;
cp->ptr = p;
}
void
cast_for_split( CELL * cp )
{
static char meta[] = "^$.*+?|[]()";
static char xbuff[] = "\\X";
int c;
size_t len;
if ( cp->type < C_STRING )
cast1_to_s( cp );
if ( ( len = string( cp )->len ) == 1 ) {
if ( ( c = string( cp )->str[0] ) == ' ' ) {
free_STRING( string( cp ) );
cp->type = C_SPACE;
return;
}
else if ( c != 0 && strchr( meta, c ) ) {
xbuff[1] = c;
free_STRING( string( cp ) );
cp->ptr = (void *)new_STRING( xbuff );
}
}
else if ( len == 0 ) {
free_STRING( string( cp ) );
cp->type = C_SNULL;
return;
}
cast_to_RE( cp );
}
/* input: cp-> a CELL of type C_MBSTRN (maybe strnum)
test it -- casting it to the appropriate type
which is C_STRING or C_STRNUM
eliminate some values strtod likes 0x 0X inf nan
*/
void
check_strnum( CELL * cp )
{
unsigned char * test;
char ** tp = (char **)&test;
unsigned char * s;
unsigned char * q;
cp->type = C_STRING; /* assume not C_STRNUM */
s = (unsigned char *)string( cp )->str;
q = s + string( cp )->len;
while ( scan_code_get(*s) == SC_SPACE )
s++;
switch ( scan_code_get(*s) ) {
case SC_DIGIT:
case SC_PLUS:
case SC_MINUS:
case SC_DOT:
errno = 0;
cp->dval = strtod( (char *)s, tp );
/* make overflow and underflow pure string */
if ( errno || test == s ) {
errno = 0;
return;
}
/* we have a number, but must be all of it .
we allow space at back */
while ( q > test && scan_code_get(q[-1]) == SC_SPACE )
q--;
if ( q != test )
return;
/* and finally eliminate hex strings */
while ( s < q ) {
if ( *s == 'x' || *s == 'X' )
return;
s++;
}
cp->type = C_STRNUM;
return;
default:
/* not strnum */
return;
}
}
/* cast a CELL to a replacement cell */
void
cast_to_REPL( CELL * cp )
{
STRING * sval;
if ( cp->type < C_STRING ) {
cast1_to_s( cp );
}
sval = (STRING *)cp->ptr;
/* cp no longer ownes sval */
cp->type = C_NOINIT;
replacement_scan( sval, cp );
free_STRING( sval );
}