Debian Codegraph @

project_files/hwc/rtl/system.c

raw
1 #include "system.h"
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <wchar.h>
6 #include "pmath.h"
7
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
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
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
70 memcpy(result.s + 1, s.s + index, count);
71
72 result.len = count;
73
74 return result;
75 }
76
77 void __attribute__((overloadable)) fpcrtl_insert__vars(string255 *src, string255 *dst, SizeInt index) {
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) {
93 num_preshift = 255;
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
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
159 void __attribute__((overloadable)) fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) {
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
181 memmove(s->str + index - 1, s->str + index - 1 + count, num_move);
182 s->str[new_length] = 0;
183
184 s->len = new_length;
185
186 }
187
188 void __attribute__((overloadable)) fpcrtl_delete__vars(astring *s, SizeInt index, SizeInt count) {
189 // number of chars to be move
190 int num_move;
191 int new_length;
192
193 if (index < 1) {
194 // in fpc, if index < 1, the string won't be modified
195 return;
196 }
197
198 if(index > s->len){
199 return;
200 }
201
202 if (count > s->len - index + 1) {
203 s->len = index - 1;
204 return;
205 }
206
207 num_move = s->len - index + 1 - count;
208 new_length = s->len - count;
209
210 memmove(s->s + index, s->s + index + count, num_move);
211
212 s->len = new_length;
213
214 }
215
216 string255 fpcrtl_floatToStr(double n) {
217 string255 t;
218 sprintf(t.str, "%f", n);
219 t.len = strlen(t.str);
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) {
229
230 unsigned char* p;
231
232 if (str.len == 0) {
233 return 0;
234 }
235
236 FIX_STRING(str);
237
238 p = strchr(str.str, c);
239
240 if (p == NULL) {
241 return 0;
242 }
243
244 return p - (unsigned char*)&str.s;
245 }
246
247 Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, string255 str) {
248
249 unsigned char* p;
250
251 if (str.len == 0) {
252 return 0;
253 }
254
255 if (substr.len == 0) {
256 return 0;
257 }
258
259 FIX_STRING(substr);
260 FIX_STRING(str);
261
262 p = strstr(str.str, substr.str);
263
264 if (p == NULL) {
265 return 0;
266 }
267
268 return p - (unsigned char*)&str.s;
269 }
270
271 Integer __attribute__((overloadable)) fpcrtl_pos(Char c, astring str) {
272 unsigned char* p;
273
274 if (str.len == 0) {
275 return 0;
276 }
277
278 FIX_STRINGA(str);
279
280 p = strchr(str.str, c);
281
282 if (p == NULL) {
283 return 0;
284 }
285
286 return p - (unsigned char*)&str.s;
287
288 }
289
290 Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, astring str) {
291
292 unsigned char* p;
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);
303 FIX_STRINGA(str);
304
305 p = strstr(str.str, substr.str);
306
307 if (p == NULL) {
308 return 0;
309 }
310
311 return p - (unsigned char *)&str.s;
312 }
313
314 Integer fpcrtl_length(string255 s) {
315 return s.len;
316 }
317
318 Integer fpcrtl_lengthA(astring s)
319 {
320 return s.len;
321 }
322
323
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
344 Int64 fpcrtl_trunc(extended n) {
345 return (Int64) n;
346 }
347
348 Integer fpcrtl_ceil(extended n) {
349 return (Integer) (ceil(n));
350 }
351
352 LongInt str_to_int(char *src)
353 {
354 int i;
355 int len = strlen(src);
356 char *end;
357 for(i = 0; i < len; i++)
358 {
359 if(src[i] == '$'){
360 // hex
361 return strtol(src + i + 1, &end, 16);
362 }
363 }
364
365 // decimal
366 return atoi(src);
367 }
368
369 void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, LongInt *a)
370 {
371 FIX_STRING(s);
372 *a = str_to_int(s.str);
373 }
374
375 void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, Byte *a)
376 {
377 FIX_STRING(s);
378 *a = str_to_int(s.str);
379 }
380
381 void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, LongWord *a)
382 {
383 FIX_STRING(s);
384 *a = str_to_int(s.str);
385 }
386
387 LongInt fpcrtl_random(LongInt l) {
388 // random(0) is undefined in docs but effectively returns 0 in free pascal
389 if (l == 0) {
390 printf("WARNING: random(0) called!\n");
391 return 0;
392 }
393 return (LongInt) (rand() / (double) RAND_MAX * l);
394 }
395
396 void __attribute__((overloadable)) fpcrtl_str__vars(float x, string255 *s) {
397 sprintf(s->str, "%f", x);
398 s->len = strlen(s->str);
399 }
400 void __attribute__((overloadable)) fpcrtl_str__vars(double x, string255 *s) {
401 sprintf(s->str, "%f", x);
402 s->len = strlen(s->str);
403 }
404 void __attribute__((overloadable)) fpcrtl_str__vars(uint8_t x, string255 *s) {
405 sprintf(s->str, "%u", x);
406 s->len = strlen(s->str);
407 }
408 void __attribute__((overloadable)) fpcrtl_str__vars(int8_t x, string255 *s) {
409 sprintf(s->str, "%d", x);
410 s->len = strlen(s->str);
411 }
412 void __attribute__((overloadable)) fpcrtl_str__vars(uint16_t x, string255 *s) {
413 sprintf(s->str, "%u", x);
414 s->len = strlen(s->str);
415 }
416 void __attribute__((overloadable)) fpcrtl_str__vars(int16_t x, string255 *s) {
417 sprintf(s->str, "%d", x);
418 s->len = strlen(s->str);
419 }
420 void __attribute__((overloadable)) fpcrtl_str__vars(uint32_t x, string255 *s) {
421 sprintf(s->str, "%u", x);
422 s->len = strlen(s->str);
423 }
424 void __attribute__((overloadable)) fpcrtl_str__vars(int32_t x, string255 *s) {
425 sprintf(s->str, "%d", x);
426 s->len = strlen(s->str);
427 }
428 void __attribute__((overloadable)) fpcrtl_str__vars(uint64_t x, string255 *s) {
429 sprintf(s->str, "%llu", x);
430 s->len = strlen(s->str);
431 }
432 void __attribute__((overloadable)) fpcrtl_str__vars(int64_t x, string255 *s) {
433 sprintf(s->str, "%lld", x);
434 s->len = strlen(s->str);
435 }
436
437 /*
438 * XXX No protection currently!
439 */
440 void fpcrtl_interlockedIncrement__vars(int *i) {
441 (*i)++;
442 }
443
444 void fpcrtl_interlockedDecrement__vars(int *i) {
445 (*i)--;
446 }
447
448 /*
449 * This function should be called when entering main
450 */
451 void fpcrtl_init(int argc, char** argv) {
452 int i;
453 paramCount = argc;
454
455 printf("ARGC = %d\n", paramCount);
456
457 for (i = 0; i < argc; i++) {
458 if (strlen(argv[i]) > 255) {
459 assert(0);
460 }
461 strcpy(params[i].str, argv[i]);
462 params[i].len = strlen(params[i].str);
463 }
464
465 }
466
467 int fpcrtl_paramCount() {
468 return paramCount - 1; // ignore the first one
469 }
470
471 string255 fpcrtl_paramStr(int i) {
472 return params[i];
473 }
474
475 int fpcrtl_UTF8ToUnicode(PWideChar dest, PChar src, SizeInt maxLen) {
476 //return swprintf(dest, maxLen, L"%hs", "src"); //doesn't work in emscripten
477 return 0;
478 }
479
480 uint32_t __attribute__((overloadable)) fpcrtl_lo(uint64_t i) {
481 return (i & 0xFFFFFFFF);
482 }
483