author | unc0rr |
Mon, 10 Feb 2014 23:02:49 +0400 | |
changeset 10128 | 0f6878b5395a |
parent 8850 | ae8a957c69fd |
child 10131 | 4b4a043111f4 |
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> |
|
6 |
||
8053 | 7 |
#ifndef M_PI |
8 |
// some math.h do not have M_PI macros |
|
9 |
# define M_PI 3.14159265358979323846 /* pi */ |
|
10 |
# define M_PI_2 1.57079632679489661923 /* pi/2 */ |
|
11 |
# define M_PI_4 0.78539816339744830962 /* pi/4 */ |
|
12 |
# define M_PIl 3.1415926535897932384626433832795029L /* pi */ |
|
13 |
# define M_PI_2l 1.5707963267948966192313216916397514L /* pi/2 */ |
|
14 |
# define M_PI_4l 0.7853981633974483096156608458198757L /* pi/4 */ |
|
15 |
#endif |
|
16 |
||
17 |
double pi = M_PI; |
|
18 |
||
7983 | 19 |
int paramCount; |
20 |
string255 params[MAX_PARAMS]; |
|
21 |
||
22 |
string255 fpcrtl_copy(string255 s, Integer index, Integer count) { |
|
23 |
string255 result = STRINIT(""); |
|
24 |
||
25 |
if (count < 1) { |
|
26 |
return result; |
|
27 |
} |
|
28 |
||
29 |
if (index < 1) { |
|
30 |
index = 1; |
|
31 |
} |
|
32 |
||
33 |
if (index > s.len) { |
|
34 |
return result; |
|
35 |
} |
|
36 |
||
37 |
if (index + count > s.len + 1) { |
|
38 |
count = s.len + 1 - index; |
|
39 |
} |
|
40 |
||
41 |
memcpy(result.str, s.str + index - 1, count); |
|
42 |
||
10128 | 43 |
result.len = count; |
44 |
||
45 |
return result; |
|
46 |
} |
|
47 |
||
48 |
astring fpcrtl_copyA(astring s, Integer index, Integer count) { |
|
49 |
astring result; |
|
50 |
||
51 |
result.len = 0; |
|
52 |
||
53 |
if (count < 1) { |
|
54 |
return result; |
|
55 |
} |
|
56 |
||
57 |
if (index < 1) { |
|
58 |
index = 1; |
|
59 |
} |
|
60 |
||
61 |
if (index > s.len) { |
|
62 |
return result; |
|
63 |
} |
|
64 |
||
65 |
if (index + count > s.len + 1) { |
|
66 |
count = s.len + 1 - index; |
|
67 |
} |
|
68 |
||
69 |
memcpy(result.s + 1, s.s + index - 1, count); |
|
70 |
||
7983 | 71 |
result.len = count; |
72 |
||
73 |
return result; |
|
74 |
} |
|
75 |
||
76 |
void fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) { |
|
77 |
// number of chars to be move |
|
78 |
int num_move; |
|
79 |
int new_length; |
|
80 |
||
81 |
string255 temp = *s; |
|
82 |
||
83 |
if (index < 1) { |
|
84 |
// in fpc, if index < 1, the string won't be modified |
|
85 |
return; |
|
86 |
} |
|
87 |
||
88 |
if(index > s->len){ |
|
89 |
return; |
|
90 |
} |
|
91 |
||
92 |
if (count > s->len - index + 1) { |
|
93 |
s->str[index - 1] = 0; |
|
94 |
s->len = index - 1; |
|
95 |
return; |
|
96 |
} |
|
97 |
||
98 |
num_move = s->len - index + 1 - count; |
|
99 |
new_length = s->len - count; |
|
100 |
||
101 |
memmove(s->str + index - 1, temp.str + index - 1 + count, num_move); |
|
102 |
s->str[new_length] = 0; |
|
103 |
||
104 |
s->len = new_length; |
|
105 |
||
106 |
} |
|
107 |
||
108 |
string255 fpcrtl_floatToStr(double n) { |
|
109 |
string255 t; |
|
110 |
sprintf(t.str, "%f", n); |
|
111 |
t.len = strlen(t.str); |
|
112 |
||
113 |
return t; |
|
114 |
} |
|
115 |
||
116 |
void fpcrtl_move__vars(void *src, void *dst, SizeInt count) { |
|
117 |
memmove(dst, src, count); |
|
118 |
} |
|
119 |
||
120 |
Integer __attribute__((overloadable)) fpcrtl_pos(Char c, string255 str) { |
|
121 |
string255 t; |
|
122 |
t.len = 1; |
|
123 |
t.str[0] = c; |
|
124 |
t.str[1] = 0; |
|
125 |
return fpcrtl_pos(t, str); |
|
126 |
} |
|
127 |
||
128 |
Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, string255 str) { |
|
129 |
||
130 |
char* p; |
|
131 |
||
132 |
if (str.len == 0) { |
|
133 |
return 0; |
|
134 |
} |
|
135 |
||
136 |
if (substr.len == 0) { |
|
137 |
return 0; |
|
138 |
} |
|
139 |
||
10128 | 140 |
FIX_STRING(substr); |
141 |
FIX_STRING(str); |
|
7983 | 142 |
|
143 |
p = strstr(str.str, substr.str); |
|
144 |
||
145 |
if (p == NULL) { |
|
146 |
return 0; |
|
147 |
} |
|
148 |
||
149 |
return strlen(str.str) - strlen(p) + 1; |
|
150 |
} |
|
151 |
||
10128 | 152 |
Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, astring str) { |
153 |
||
154 |
char* p; |
|
155 |
||
156 |
if (str.len == 0) { |
|
157 |
return 0; |
|
158 |
} |
|
159 |
||
160 |
if (substr.len == 0) { |
|
161 |
return 0; |
|
162 |
} |
|
163 |
||
164 |
FIX_STRING(substr); |
|
165 |
str.s[str.len] = 0; |
|
166 |
||
167 |
p = strstr(str.s + 1, substr.str); |
|
168 |
||
169 |
if (p == NULL) { |
|
170 |
return 0; |
|
171 |
} |
|
172 |
||
173 |
return str.len - strlen(p) + 1; |
|
174 |
} |
|
175 |
||
7983 | 176 |
Integer fpcrtl_length(string255 s) { |
177 |
return s.len; |
|
178 |
} |
|
179 |
||
10128 | 180 |
Integer fpcrtl_lengthA(astring s) |
181 |
{ |
|
182 |
return s.len; |
|
183 |
} |
|
184 |
||
185 |
||
7983 | 186 |
string255 fpcrtl_lowerCase(string255 s) { |
187 |
int i; |
|
188 |
||
189 |
for (i = 0; i < s.len; i++) { |
|
190 |
if (s.str[i] >= 'A' && s.str[i] <= 'Z') { |
|
191 |
s.str[i] += 'a' - 'A'; |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
return s; |
|
196 |
} |
|
197 |
||
198 |
void fpcrtl_fillChar__vars(void *x, SizeInt count, Byte value) { |
|
199 |
memset(x, value, count); |
|
200 |
} |
|
201 |
||
202 |
void fpcrtl_new__vars(void **p, int size) { |
|
203 |
*p = malloc(size); |
|
204 |
} |
|
205 |
||
206 |
Integer fpcrtl_trunc(extended n) { |
|
207 |
return (int) n; |
|
208 |
} |
|
209 |
||
210 |
LongInt str_to_int(char *src) |
|
211 |
{ |
|
212 |
int i; |
|
213 |
int len = strlen(src); |
|
214 |
char *end; |
|
215 |
for(i = 0; i < len; i++) |
|
216 |
{ |
|
217 |
if(src[i] == '$'){ |
|
218 |
// hex |
|
219 |
return strtol(src + i + 1, &end, 16); |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
// decimal |
|
224 |
return atoi(src); |
|
225 |
} |
|
8850
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
226 |
|
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
227 |
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
|
228 |
{ |
7983 | 229 |
FIX_STRING(s); |
230 |
*a = str_to_int(s.str); |
|
231 |
} |
|
232 |
||
8850
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
233 |
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
|
234 |
{ |
7983 | 235 |
FIX_STRING(s); |
236 |
*a = str_to_int(s.str); |
|
237 |
} |
|
238 |
||
8850
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
239 |
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
|
240 |
{ |
7983 | 241 |
FIX_STRING(s); |
242 |
*a = str_to_int(s.str); |
|
243 |
} |
|
244 |
||
245 |
LongInt fpcrtl_random(LongInt l) { |
|
246 |
return (LongInt) (rand() / (double) RAND_MAX * l); |
|
247 |
} |
|
248 |
||
249 |
void __attribute__((overloadable)) fpcrtl_str__vars(float x, string255 *s) { |
|
250 |
sprintf(s->str, "%f", x); |
|
251 |
s->len = strlen(s->str); |
|
252 |
} |
|
253 |
void __attribute__((overloadable)) fpcrtl_str__vars(double x, string255 *s) { |
|
254 |
sprintf(s->str, "%f", x); |
|
255 |
s->len = strlen(s->str); |
|
256 |
} |
|
257 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint8_t x, string255 *s) { |
|
258 |
sprintf(s->str, "%u", x); |
|
259 |
s->len = strlen(s->str); |
|
260 |
} |
|
261 |
void __attribute__((overloadable)) fpcrtl_str__vars(int8_t x, string255 *s) { |
|
262 |
sprintf(s->str, "%d", x); |
|
263 |
s->len = strlen(s->str); |
|
264 |
} |
|
265 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint16_t x, string255 *s) { |
|
266 |
sprintf(s->str, "%u", x); |
|
267 |
s->len = strlen(s->str); |
|
268 |
} |
|
269 |
void __attribute__((overloadable)) fpcrtl_str__vars(int16_t x, string255 *s) { |
|
270 |
sprintf(s->str, "%d", x); |
|
271 |
s->len = strlen(s->str); |
|
272 |
} |
|
273 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint32_t x, string255 *s) { |
|
274 |
sprintf(s->str, "%u", x); |
|
275 |
s->len = strlen(s->str); |
|
276 |
} |
|
277 |
void __attribute__((overloadable)) fpcrtl_str__vars(int32_t x, string255 *s) { |
|
278 |
sprintf(s->str, "%d", x); |
|
279 |
s->len = strlen(s->str); |
|
280 |
} |
|
281 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint64_t x, string255 *s) { |
|
282 |
sprintf(s->str, "%llu", x); |
|
283 |
s->len = strlen(s->str); |
|
284 |
} |
|
285 |
void __attribute__((overloadable)) fpcrtl_str__vars(int64_t x, string255 *s) { |
|
286 |
sprintf(s->str, "%lld", x); |
|
287 |
s->len = strlen(s->str); |
|
288 |
} |
|
289 |
||
290 |
/* |
|
291 |
* XXX No protection currently! |
|
292 |
*/ |
|
293 |
void fpcrtl_interlockedIncrement__vars(int *i) { |
|
294 |
(*i)++; |
|
295 |
} |
|
296 |
||
297 |
void fpcrtl_interlockedDecrement__vars(int *i) { |
|
298 |
(*i)--; |
|
299 |
} |
|
300 |
||
301 |
/* |
|
302 |
* This function should be called when entering main |
|
303 |
*/ |
|
304 |
void fpcrtl_init(int argc, char** argv) { |
|
305 |
int i; |
|
306 |
paramCount = argc; |
|
307 |
||
308 |
printf("ARGC = %d\n", paramCount); |
|
309 |
||
310 |
for (i = 0; i < argc; i++) { |
|
311 |
if (strlen(argv[i]) > 255) { |
|
312 |
assert(0); |
|
313 |
} |
|
314 |
strcpy(params[i].str, argv[i]); |
|
315 |
params[i].len = strlen(params[i].str); |
|
316 |
} |
|
317 |
||
318 |
} |
|
319 |
||
320 |
int fpcrtl_paramCount() { |
|
321 |
return paramCount - 1; // ignore the first one |
|
322 |
} |
|
323 |
||
324 |
string255 fpcrtl_paramStr(int i) { |
|
325 |
return params[i]; |
|
326 |
} |
|
327 |
||
328 |
int fpcrtl_UTF8ToUnicode(PWideChar dest, PChar src, SizeInt maxLen) { |
|
329 |
//return swprintf(dest, maxLen, L"%hs", "src"); //doesn't work in emscripten |
|
330 |
return 0; |
|
331 |
} |
|
332 |
||
333 |
uint32_t __attribute__((overloadable)) fpcrtl_lo(uint64_t i) { |
|
334 |
return (i & 0xFFFFFFFF); |
|
335 |
} |
|
336 |