author | unC0Rr |
Tue, 27 Aug 2024 17:01:13 +0200 | |
changeset 16047 | 692903667ece |
parent 14270 | 06f2dc4deab2 |
permissions | -rw-r--r-- |
7983 | 1 |
#include "system.h" |
2 |
#include <string.h> |
|
3 |
#include <stdio.h> |
|
4 |
#include <stdlib.h> |
|
5 |
#include <wchar.h> |
|
11655 | 6 |
#include "pmath.h" |
7983 | 7 |
|
8053 | 8 |
#ifndef M_PI |
9 |
// some math.h do not have M_PI macros |
|
10 |
# define M_PI 3.14159265358979323846 /* pi */ |
|
11 |
# define M_PI_2 1.57079632679489661923 /* pi/2 */ |
|
12 |
# define M_PI_4 0.78539816339744830962 /* pi/4 */ |
|
13 |
# define M_PIl 3.1415926535897932384626433832795029L /* pi */ |
|
14 |
# define M_PI_2l 1.5707963267948966192313216916397514L /* pi/2 */ |
|
15 |
# define M_PI_4l 0.7853981633974483096156608458198757L /* pi/4 */ |
|
16 |
#endif |
|
17 |
||
18 |
double pi = M_PI; |
|
19 |
||
7983 | 20 |
int paramCount; |
21 |
string255 params[MAX_PARAMS]; |
|
22 |
||
23 |
string255 fpcrtl_copy(string255 s, Integer index, Integer count) { |
|
24 |
string255 result = STRINIT(""); |
|
25 |
||
26 |
if (count < 1) { |
|
27 |
return result; |
|
28 |
} |
|
29 |
||
30 |
if (index < 1) { |
|
31 |
index = 1; |
|
32 |
} |
|
33 |
||
34 |
if (index > s.len) { |
|
35 |
return result; |
|
36 |
} |
|
37 |
||
38 |
if (index + count > s.len + 1) { |
|
39 |
count = s.len + 1 - index; |
|
40 |
} |
|
41 |
||
42 |
memcpy(result.str, s.str + index - 1, count); |
|
43 |
||
10128 | 44 |
result.len = count; |
45 |
||
46 |
return result; |
|
47 |
} |
|
48 |
||
49 |
astring fpcrtl_copyA(astring s, Integer index, Integer count) { |
|
50 |
astring result; |
|
51 |
||
52 |
result.len = 0; |
|
53 |
||
54 |
if (count < 1) { |
|
55 |
return result; |
|
56 |
} |
|
57 |
||
58 |
if (index < 1) { |
|
59 |
index = 1; |
|
60 |
} |
|
61 |
||
62 |
if (index > s.len) { |
|
63 |
return result; |
|
64 |
} |
|
65 |
||
66 |
if (index + count > s.len + 1) { |
|
67 |
count = s.len + 1 - index; |
|
68 |
} |
|
69 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
70 |
memcpy(result.s + 1, s.s + index, count); |
10128 | 71 |
|
7983 | 72 |
result.len = count; |
73 |
||
74 |
return result; |
|
75 |
} |
|
76 |
||
13941
7173b702e8db
tag both as overloadable - seems to satisfy clang 3.4
nemo
parents:
13903
diff
changeset
|
77 |
void __attribute__((overloadable)) fpcrtl_insert__vars(string255 *src, string255 *dst, SizeInt index) { |
10838 | 78 |
int num_insert; |
79 |
int num_shift; |
|
80 |
int num_preshift; |
|
81 |
||
82 |
// nothing to do if empty string is inserted or index invalid |
|
83 |
if ((src->len == 0) || (index < 1) || (index > 255)) { |
|
84 |
return; |
|
85 |
} |
|
86 |
||
87 |
num_insert = src->len; |
|
88 |
// number of chars from start of destination string to end of insertion |
|
89 |
num_preshift = index - 1 + num_insert; |
|
90 |
||
91 |
// don't overflow on insert |
|
92 |
if (num_preshift > 255) { |
|
13903 | 93 |
num_preshift = 255; |
10838 | 94 |
num_insert = 255 - (index - 1); |
95 |
num_shift = 0; |
|
96 |
} |
|
97 |
// shift trailing chars |
|
98 |
else { |
|
99 |
// number of bytes to be shifted |
|
100 |
num_shift = dst->len - (index - 1); |
|
101 |
||
102 |
if (num_shift > 0) { |
|
103 |
// don't overflow when shifting |
|
104 |
if (num_shift + num_preshift > 255) |
|
105 |
num_shift = 255 - num_preshift; |
|
106 |
||
107 |
// time to move some bytes! |
|
108 |
memmove(dst->str + num_preshift, dst->str + index - 1, num_shift); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
// actual byte insertion |
|
113 |
memmove(dst->str + index - 1, src->str, num_insert); |
|
114 |
// store new length |
|
115 |
dst->len = num_shift + num_preshift; |
|
116 |
} |
|
117 |
||
13903 | 118 |
void __attribute__((overloadable)) fpcrtl_insert__vars(astring *src, astring *dst, SizeInt index) { |
119 |
int num_insert; |
|
120 |
int num_shift; |
|
121 |
int num_preshift; |
|
122 |
||
123 |
// nothing to do if empty string is inserted or index invalid |
|
124 |
if ((src->len == 0) || (index < 1) || (index > MAX_ANSISTRING_LENGTH)) { |
|
125 |
return; |
|
126 |
} |
|
127 |
||
128 |
num_insert = src->len; |
|
129 |
// number of chars from start of destination string to end of insertion |
|
130 |
num_preshift = index - 1 + num_insert; |
|
131 |
||
132 |
// don't overflow on insert |
|
133 |
if (num_preshift > MAX_ANSISTRING_LENGTH) { |
|
134 |
num_preshift = MAX_ANSISTRING_LENGTH; |
|
135 |
num_insert = MAX_ANSISTRING_LENGTH - (index - 1); |
|
136 |
num_shift = 0; |
|
137 |
} |
|
138 |
// shift trailing chars |
|
139 |
else { |
|
140 |
// number of bytes to be shifted |
|
141 |
num_shift = dst->len - (index - 1); |
|
142 |
||
143 |
if (num_shift > 0) { |
|
144 |
// don't overflow when shifting |
|
145 |
if (num_shift + num_preshift > MAX_ANSISTRING_LENGTH) |
|
146 |
num_shift = MAX_ANSISTRING_LENGTH - num_preshift; |
|
147 |
||
148 |
// time to move some bytes! |
|
149 |
memmove(dst->str + num_preshift, dst->str + index - 1, num_shift); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
// actual byte insertion |
|
154 |
memmove(dst->str + index - 1, src->str, num_insert); |
|
155 |
// store new length |
|
156 |
dst->len = num_shift + num_preshift; |
|
157 |
} |
|
158 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
159 |
void __attribute__((overloadable)) fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) { |
7983 | 160 |
// number of chars to be move |
161 |
int num_move; |
|
162 |
int new_length; |
|
163 |
||
164 |
if (index < 1) { |
|
165 |
// in fpc, if index < 1, the string won't be modified |
|
166 |
return; |
|
167 |
} |
|
168 |
||
169 |
if(index > s->len){ |
|
170 |
return; |
|
171 |
} |
|
172 |
||
173 |
if (count > s->len - index + 1) { |
|
174 |
s->len = index - 1; |
|
175 |
return; |
|
176 |
} |
|
177 |
||
178 |
num_move = s->len - index + 1 - count; |
|
179 |
new_length = s->len - count; |
|
180 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
181 |
memmove(s->str + index - 1, s->str + index - 1 + count, num_move); |
7983 | 182 |
s->str[new_length] = 0; |
183 |
||
184 |
s->len = new_length; |
|
185 |
||
186 |
} |
|
187 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
188 |
void __attribute__((overloadable)) fpcrtl_delete__vars(astring *s, SizeInt index, SizeInt count) { |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
189 |
// number of chars to be move |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
190 |
int num_move; |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
191 |
int new_length; |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
192 |
|
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
193 |
if (index < 1) { |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
194 |
// in fpc, if index < 1, the string won't be modified |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
195 |
return; |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
196 |
} |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
197 |
|
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
198 |
if(index > s->len){ |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
199 |
return; |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
200 |
} |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
201 |
|
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
202 |
if (count > s->len - index + 1) { |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
203 |
s->len = index - 1; |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
204 |
return; |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
205 |
} |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
206 |
|
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
207 |
num_move = s->len - index + 1 - count; |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
208 |
new_length = s->len - count; |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
209 |
|
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
210 |
memmove(s->s + index, s->s + index + count, num_move); |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
211 |
|
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
212 |
s->len = new_length; |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
213 |
|
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
214 |
} |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
215 |
|
7983 | 216 |
string255 fpcrtl_floatToStr(double n) { |
217 |
string255 t; |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
218 |
|
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
219 |
t.len = sprintf(t.str, "%f", n); |
7983 | 220 |
|
221 |
return t; |
|
222 |
} |
|
223 |
||
224 |
void fpcrtl_move__vars(void *src, void *dst, SizeInt count) { |
|
225 |
memmove(dst, src, count); |
|
226 |
} |
|
227 |
||
228 |
Integer __attribute__((overloadable)) fpcrtl_pos(Char c, string255 str) { |
|
10134
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
229 |
|
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
230 |
unsigned char* p; |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
231 |
|
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
232 |
if (str.len == 0) { |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
233 |
return 0; |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
234 |
} |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
235 |
|
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
236 |
FIX_STRING(str); |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
237 |
|
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
238 |
p = strchr(str.str, c); |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
239 |
|
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
240 |
if (p == NULL) { |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
241 |
return 0; |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
242 |
} |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
243 |
|
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
244 |
return p - (unsigned char*)&str.s; |
7983 | 245 |
} |
246 |
||
247 |
Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, string255 str) { |
|
248 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
249 |
unsigned char* p; |
7983 | 250 |
|
251 |
if (str.len == 0) { |
|
252 |
return 0; |
|
253 |
} |
|
254 |
||
255 |
if (substr.len == 0) { |
|
256 |
return 0; |
|
257 |
} |
|
258 |
||
10128 | 259 |
FIX_STRING(substr); |
260 |
FIX_STRING(str); |
|
7983 | 261 |
|
262 |
p = strstr(str.str, substr.str); |
|
263 |
||
264 |
if (p == NULL) { |
|
265 |
return 0; |
|
266 |
} |
|
267 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
268 |
return p - (unsigned char*)&str.s; |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
269 |
} |
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
270 |
|
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
271 |
Integer __attribute__((overloadable)) fpcrtl_pos(Char c, astring str) { |
10134
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
272 |
unsigned char* p; |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
273 |
|
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
274 |
if (str.len == 0) { |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
275 |
return 0; |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
276 |
} |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
277 |
|
14180 | 278 |
FIX_STRINGA(str); |
279 |
||
14270
06f2dc4deab2
Remove erroneous code duplicating FIX_STRING functionality, adjust ansistring array size
unc0rr
parents:
14211
diff
changeset
|
280 |
p = strchr(str.str, c); |
10134
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
281 |
|
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
282 |
if (p == NULL) { |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
283 |
return 0; |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
284 |
} |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
285 |
|
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
286 |
return p - (unsigned char*)&str.s; |
81d733899e06
Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents:
10131
diff
changeset
|
287 |
|
7983 | 288 |
} |
289 |
||
10128 | 290 |
Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, astring str) { |
291 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
292 |
unsigned char* p; |
10128 | 293 |
|
294 |
if (str.len == 0) { |
|
295 |
return 0; |
|
296 |
} |
|
297 |
||
298 |
if (substr.len == 0) { |
|
299 |
return 0; |
|
300 |
} |
|
301 |
||
302 |
FIX_STRING(substr); |
|
14180 | 303 |
FIX_STRINGA(str); |
304 |
||
14270
06f2dc4deab2
Remove erroneous code duplicating FIX_STRING functionality, adjust ansistring array size
unc0rr
parents:
14211
diff
changeset
|
305 |
p = strstr(str.str, substr.str); |
10128 | 306 |
|
307 |
if (p == NULL) { |
|
308 |
return 0; |
|
309 |
} |
|
310 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10128
diff
changeset
|
311 |
return p - (unsigned char *)&str.s; |
10128 | 312 |
} |
313 |
||
7983 | 314 |
Integer fpcrtl_length(string255 s) { |
315 |
return s.len; |
|
316 |
} |
|
317 |
||
10128 | 318 |
Integer fpcrtl_lengthA(astring s) |
319 |
{ |
|
320 |
return s.len; |
|
321 |
} |
|
322 |
||
323 |
||
7983 | 324 |
string255 fpcrtl_lowerCase(string255 s) { |
325 |
int i; |
|
326 |
||
327 |
for (i = 0; i < s.len; i++) { |
|
328 |
if (s.str[i] >= 'A' && s.str[i] <= 'Z') { |
|
329 |
s.str[i] += 'a' - 'A'; |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
return s; |
|
334 |
} |
|
335 |
||
336 |
void fpcrtl_fillChar__vars(void *x, SizeInt count, Byte value) { |
|
337 |
memset(x, value, count); |
|
338 |
} |
|
339 |
||
340 |
void fpcrtl_new__vars(void **p, int size) { |
|
341 |
*p = malloc(size); |
|
342 |
} |
|
343 |
||
14189
5c6f947c342c
Pas2C: Fix data types of Trunc and Ceil
Wuzzy <Wuzzy2@mail.ru>
parents:
14180
diff
changeset
|
344 |
Int64 fpcrtl_trunc(extended n) { |
5c6f947c342c
Pas2C: Fix data types of Trunc and Ceil
Wuzzy <Wuzzy2@mail.ru>
parents:
14180
diff
changeset
|
345 |
return (Int64) n; |
7983 | 346 |
} |
347 |
||
10910 | 348 |
Integer fpcrtl_ceil(extended n) { |
14189
5c6f947c342c
Pas2C: Fix data types of Trunc and Ceil
Wuzzy <Wuzzy2@mail.ru>
parents:
14180
diff
changeset
|
349 |
return (Integer) (ceil(n)); |
10910 | 350 |
} |
351 |
||
7983 | 352 |
LongInt str_to_int(char *src) |
353 |
{ |
|
354 |
int i; |
|
355 |
char *end; |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
356 |
for(i = 0; src[i]; i++) |
7983 | 357 |
{ |
14211 | 358 |
if(src[i] == '$'){ |
7983 | 359 |
// hex |
360 |
return strtol(src + i + 1, &end, 16); |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
// decimal |
|
365 |
return atoi(src); |
|
366 |
} |
|
8850
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
367 |
|
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
368 |
void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, LongInt *a) |
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
369 |
{ |
7983 | 370 |
FIX_STRING(s); |
371 |
*a = str_to_int(s.str); |
|
372 |
} |
|
373 |
||
8850
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
374 |
void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, Byte *a) |
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
375 |
{ |
7983 | 376 |
FIX_STRING(s); |
377 |
*a = str_to_int(s.str); |
|
378 |
} |
|
379 |
||
8850
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
380 |
void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, LongWord *a) |
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
381 |
{ |
7983 | 382 |
FIX_STRING(s); |
383 |
*a = str_to_int(s.str); |
|
384 |
} |
|
385 |
||
386 |
LongInt fpcrtl_random(LongInt l) { |
|
10625
125e120165aa
flake FrameTicks value of 0 now indicades that the frame should not be changed
sheepluva
parents:
10134
diff
changeset
|
387 |
// random(0) is undefined in docs but effectively returns 0 in free pascal |
125e120165aa
flake FrameTicks value of 0 now indicades that the frame should not be changed
sheepluva
parents:
10134
diff
changeset
|
388 |
if (l == 0) { |
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
389 |
//printf("WARNING: random(0) called!\n"); |
10625
125e120165aa
flake FrameTicks value of 0 now indicades that the frame should not be changed
sheepluva
parents:
10134
diff
changeset
|
390 |
return 0; |
125e120165aa
flake FrameTicks value of 0 now indicades that the frame should not be changed
sheepluva
parents:
10134
diff
changeset
|
391 |
} |
7983 | 392 |
return (LongInt) (rand() / (double) RAND_MAX * l); |
393 |
} |
|
394 |
||
395 |
void __attribute__((overloadable)) fpcrtl_str__vars(float x, string255 *s) { |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
396 |
s->len = sprintf(s->str, "%f", x); |
7983 | 397 |
} |
398 |
void __attribute__((overloadable)) fpcrtl_str__vars(double x, string255 *s) { |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
399 |
s->len = sprintf(s->str, "%f", x); |
7983 | 400 |
} |
401 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint8_t x, string255 *s) { |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
402 |
s->len = sprintf(s->str, "%u", x); |
7983 | 403 |
} |
404 |
void __attribute__((overloadable)) fpcrtl_str__vars(int8_t x, string255 *s) { |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
405 |
s->len = sprintf(s->str, "%d", x); |
7983 | 406 |
} |
407 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint16_t x, string255 *s) { |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
408 |
s->len = sprintf(s->str, "%u", x); |
7983 | 409 |
} |
410 |
void __attribute__((overloadable)) fpcrtl_str__vars(int16_t x, string255 *s) { |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
411 |
s->len = sprintf(s->str, "%d", x); |
7983 | 412 |
} |
413 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint32_t x, string255 *s) { |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
414 |
s->len = sprintf(s->str, "%u", x); |
7983 | 415 |
} |
416 |
void __attribute__((overloadable)) fpcrtl_str__vars(int32_t x, string255 *s) { |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
417 |
s->len = sprintf(s->str, "%d", x); |
7983 | 418 |
} |
419 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint64_t x, string255 *s) { |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
420 |
s->len = sprintf(s->str, "%llu", x); |
7983 | 421 |
} |
422 |
void __attribute__((overloadable)) fpcrtl_str__vars(int64_t x, string255 *s) { |
|
16047
692903667ece
Improve pas2c rtl performance by avoiding double iteration on strings
unC0Rr
parents:
14270
diff
changeset
|
423 |
s->len = sprintf(s->str, "%lld", x); |
7983 | 424 |
} |
425 |
||
426 |
/* |
|
427 |
* XXX No protection currently! |
|
428 |
*/ |
|
429 |
void fpcrtl_interlockedIncrement__vars(int *i) { |
|
430 |
(*i)++; |
|
431 |
} |
|
432 |
||
433 |
void fpcrtl_interlockedDecrement__vars(int *i) { |
|
434 |
(*i)--; |
|
435 |
} |
|
436 |
||
437 |
/* |
|
438 |
* This function should be called when entering main |
|
439 |
*/ |
|
440 |
void fpcrtl_init(int argc, char** argv) { |
|
441 |
int i; |
|
442 |
paramCount = argc; |
|
443 |
||
444 |
printf("ARGC = %d\n", paramCount); |
|
445 |
||
446 |
for (i = 0; i < argc; i++) { |
|
447 |
if (strlen(argv[i]) > 255) { |
|
448 |
assert(0); |
|
449 |
} |
|
450 |
strcpy(params[i].str, argv[i]); |
|
451 |
params[i].len = strlen(params[i].str); |
|
452 |
} |
|
453 |
||
454 |
} |
|
455 |
||
456 |
int fpcrtl_paramCount() { |
|
457 |
return paramCount - 1; // ignore the first one |
|
458 |
} |
|
459 |
||
460 |
string255 fpcrtl_paramStr(int i) { |
|
461 |
return params[i]; |
|
462 |
} |
|
463 |
||
464 |
int fpcrtl_UTF8ToUnicode(PWideChar dest, PChar src, SizeInt maxLen) { |
|
465 |
//return swprintf(dest, maxLen, L"%hs", "src"); //doesn't work in emscripten |
|
466 |
return 0; |
|
467 |
} |
|
468 |
||
469 |
uint32_t __attribute__((overloadable)) fpcrtl_lo(uint64_t i) { |
|
470 |
return (i & 0xFFFFFFFF); |
|
471 |
} |
|
472 |