-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
58 lines (52 loc) · 1.1 KB
/
parse.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
#include "parse.h"
#include "file.h"
#include "symbol.h"
#include "helpers.h"
#include "dlb_types.h"
unsigned int parse_uint(char *buf)
{
unsigned long value = strtoul(buf, 0, 10);
return (unsigned int)value;
}
int parse_int_binary(char *buf)
{
DLB_ASSERT(!"[PARSE_ERROR] Binary integers not yet supported.");
return 0;
}
int parse_int(char *buf)
{
long value;
if (buf[0] == '0' && buf[1] == 'b') {
value = parse_int_binary(buf);
} else {
value = strtol(buf, 0, 10);
}
return (int)value;
}
float parse_float_hex(char *buf)
{
float value;
unsigned long l = strtoul(buf, 0, 16);
value = *(float *)&l;
return value;
}
float parse_float(char *buf)
{
float value;
if (buf[0] == '0' && buf[1] == 'x') {
value = parse_float_hex(buf);
} else {
value = strtof(buf, 0);
}
return value;
}
void parse_tests()
{
float a = 123.0f;
float b = parse_float("123.0f");
float c = parse_float("0x42f60000");
float d = parse_float("0x42f60000(123)");
DLB_ASSERT(b == a);
DLB_ASSERT(c == a);
DLB_ASSERT(d == a);
}