convert.anubis
4.41 KB
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
*Project* Anubis
*Title* conversion tools
*Copyright* Copyright (c) David René 2006-2007.
*Author* David René
*Created* December 2006
*Status* Beta
read tools/basis.anubis
read system/types.anubis
read system/string.anubis
//TODO manage the real endianness of the host
public define ByteArray reverse(ByteArray source).
public define ByteArray to_ByteArray(Bool value).
public define ByteArray to_ByteArray(Word8 value).
public define String to_String(Int32 value).
public define Bool
to_Bool
(
String value
)=
if insensitive_equal(value,"true") then
true
else
false.
public define String
to_String
(
Bool b
) =
if b then "true" else "false".
public define Int32
to_Int32
(
Bool b
) =
if b then 1 else 0.
public define ByteArray
to_ByteArray
(
Bool value
)=
with ba = constant_byte_array(1,0),
if value then
forget(put(ba,0,1));
ba
else
//already to 0, then return the ByteArray
ba.
public define ByteArray
to_ByteArray
(
Word8 value
)=
constant_byte_array(1,value).
public define ByteArray
host_to_lendian_Int16_ByteArray
(
Int16 value,
) =
//create the ByteArray which will receive the result
with ba = constant_byte_array(2, 0),
if host_endian is
{
little then
forget(put(ba,1,value.high));
forget(put(ba,0,value.low )),
big then
forget(put(ba,1,value.low));
forget(put(ba,0,value.high))
};
//return the result ByteArray
ba.
public define ByteArray
host_to_lendian_Int32_ByteArray
(
Int32 value,
) =
//create the ByteArray which will receive the result
with ba = constant_byte_array(4, 0),
if host_endian is
{
little then
forget(put(ba,3,truncate_to_word8( value>>24) ));
forget(put(ba,2,truncate_to_word8((value>>16) & 0xFF)));
forget(put(ba,1,truncate_to_word8((value>>8) & 0xFF)));
forget(put(ba,0,truncate_to_word8( value & 0xFF))),
big then
forget(put(ba,0,truncate_to_word8( value>>24) ));
forget(put(ba,1,truncate_to_word8((value>>16) & 0xFF)));
forget(put(ba,2,truncate_to_word8((value>>8) & 0xFF)));
forget(put(ba,3,truncate_to_word8( value & 0xFF)))
};
//return the result ByteArray
ba.
public define ByteArray
host_to_lendian_Int64_ByteArray
(
Int64 value,
) =
if host_endian is
{
little then
host_to_lendian_Int32_ByteArray(value.low) +
host_to_lendian_Int32_ByteArray(value.high),
big then
host_to_lendian_Int32_ByteArray(value.high) +
host_to_lendian_Int32_ByteArray(value.low)
}.
public define ByteArray
host_to_lendian_Float64_ByteArray
(
Float value
)=
with ba = serialize(value),
if host_endian is
{
little then ba,
big then reverse(ba)
}.
public define ByteArray
host_to_lendian_Float32_ByteArray
(
Float32 val
)=
host_to_lendian_Int32_ByteArray(val.value).
public define ByteArray
host_to_lendian_B_Point_ByteArray
(
B_Point value
) =
host_to_lendian_Float32_ByteArray(value.x) +
host_to_lendian_Float32_ByteArray(value.y).
public define ByteArray
host_to_lendian_B_Rect_ByteArray
(
B_Rect value
) =
if value is
{
rect(left_top, right_bottom) then
host_to_lendian_B_Point_ByteArray(left_top) +
host_to_lendian_B_Point_ByteArray(right_bottom),
rect(left, top, right, bottom) then
host_to_lendian_Float32_ByteArray(left) +
host_to_lendian_Float32_ByteArray(top) +
host_to_lendian_Float32_ByteArray(right) +
host_to_lendian_Float32_ByteArray(bottom)
}
.
define ByteArray
reverse
(
ByteArray source,
ByteArray dest,
Int32 src_idx,
Int32 dest_idx
) =
if src_idx < 0 then
dest
else
forget(put(dest, dest_idx, force_nth(src_idx, source)));
reverse(source, dest, src_idx -1, dest_idx +1)
.
/** Reverse the entire content of the given ByteArray
* if the content is [1|2|3] after reverse it will be [3|2|1]
*/
public define ByteArray
reverse
(
ByteArray source
)=
with len = length(source),
dest = constant_byte_array(len,0),
reverse(source, dest, len - 1, 0).
/************** to_String part *************/
public define String
to_String
(
Int32 v
)=
integer_to_string(v).