-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpl.value.c
176 lines (136 loc) · 3.42 KB
/
xpl.value.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
#include "xpl.h"
/* Value Objects */
xpl_value* xpl_value_create( void )
{
return (xpl_value*)xpl_malloc( (char*)NULL, sizeof( xpl_value ) );
}
xpl_value* xpl_value_create_integer( int i )
{
xpl_value* val;
val = xpl_value_create();
xpl_value_set_integer( val, i );
return val;
}
xpl_value* xpl_value_create_float( float f )
{
xpl_value* val;
val = xpl_value_create();
xpl_value_set_float( val, f );
return val;
}
xpl_value* xpl_value_create_string( char* s, short duplicate )
{
xpl_value* val;
val = xpl_value_create();
xpl_value_set_string( val, duplicate ? xpl_strdup( s ) : s );
return val;
}
void xpl_value_free( xpl_value* val )
{
if( !val )
return;
xpl_value_reset( val );
free( val );
}
void xpl_value_reset( xpl_value* val )
{
if( val->type == XPL_STRINGVAL && val->value.s )
free( val->value.s );
val->strval = xpl_free( val->strval );
memset( val, 0, sizeof( xpl_value ) );
val->type = XPL_NULLVAL;
}
xpl_value* xpl_value_dup( xpl_value* val )
{
xpl_value* dup;
dup = xpl_value_create();
if( !val )
return dup;
memcpy( dup, val, sizeof( xpl_value ) );
dup->strval = (char*)NULL;
if( dup->type == XPL_STRINGVAL )
dup->value.s = xpl_strdup( dup->value.s );
return dup;
}
void xpl_value_set_integer( xpl_value* val, int i )
{
xpl_value_reset( val );
val->type = XPL_INTEGERVAL;
val->value.i = i;
}
void xpl_value_set_float( xpl_value* val, float f )
{
xpl_value_reset( val );
val->type = XPL_FLOATVAL;
val->value.f = f;
}
void xpl_value_set_string( xpl_value* val, char* s )
{
xpl_value_reset( val );
val->type = XPL_STRINGVAL;
val->value.s = s;
}
int xpl_value_get_integer( xpl_value* val )
{
switch( val->type )
{
case XPL_INTEGERVAL:
return val->value.i;
case XPL_FLOATVAL:
return (int)val->value.f;
case XPL_STRINGVAL:
return atoi( val->value.s );
default:
break;
}
return 0;
}
float xpl_value_get_float( xpl_value* val )
{
switch( val->type )
{
case XPL_INTEGERVAL:
return (float)val->value.i;
case XPL_FLOATVAL:
return val->value.f;
case XPL_STRINGVAL:
return (float)atof( val->value.s );
default:
break;
}
return 0.0;
}
char* xpl_value_get_string( xpl_value* val )
{
char buf [ 128 + 1 ];
char* p;
val->strval = xpl_free( val->strval );
switch( val->type )
{
case XPL_INTEGERVAL:
sprintf( buf, "%d", val->value.i );
val->strval = xpl_strdup( buf );
return val->strval;
case XPL_FLOATVAL:
sprintf( buf, "%f", val->value.f );
/* Remove trailing zeros to make values look nicer */
for( p = buf + strlen( buf ) - 1; p > buf; p-- )
{
if( *p == '.' )
{
*p = '\0';
break;
}
else if( *p != '0' )
break;
*p = '\0';
}
val->strval = xpl_strdup( buf );
return val->strval;
case XPL_STRINGVAL:
return val->value.s;
default:
break;
}
return "";
}