author | unc0rr |
Sat, 15 Jun 2013 23:44:37 +0400 | |
branch | webgl |
changeset 9164 | d923ba9d1145 |
parent 8850 | ae8a957c69fd |
child 10128 | 0f6878b5395a |
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 |
||
43 |
result.str[count] = 0; |
|
44 |
result.len = count; |
|
45 |
||
46 |
return result; |
|
47 |
} |
|
48 |
||
49 |
void fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) { |
|
50 |
// number of chars to be move |
|
51 |
int num_move; |
|
52 |
int new_length; |
|
53 |
||
54 |
string255 temp = *s; |
|
55 |
||
56 |
if (index < 1) { |
|
57 |
// in fpc, if index < 1, the string won't be modified |
|
58 |
return; |
|
59 |
} |
|
60 |
||
61 |
if(index > s->len){ |
|
62 |
return; |
|
63 |
} |
|
64 |
||
65 |
if (count > s->len - index + 1) { |
|
66 |
s->str[index - 1] = 0; |
|
67 |
s->len = index - 1; |
|
68 |
return; |
|
69 |
} |
|
70 |
||
71 |
num_move = s->len - index + 1 - count; |
|
72 |
new_length = s->len - count; |
|
73 |
||
74 |
memmove(s->str + index - 1, temp.str + index - 1 + count, num_move); |
|
75 |
s->str[new_length] = 0; |
|
76 |
||
77 |
s->len = new_length; |
|
78 |
||
79 |
} |
|
80 |
||
81 |
string255 fpcrtl_floatToStr(double n) { |
|
82 |
string255 t; |
|
83 |
sprintf(t.str, "%f", n); |
|
84 |
t.len = strlen(t.str); |
|
85 |
||
86 |
return t; |
|
87 |
} |
|
88 |
||
89 |
void fpcrtl_move__vars(void *src, void *dst, SizeInt count) { |
|
90 |
memmove(dst, src, count); |
|
91 |
} |
|
92 |
||
93 |
Integer __attribute__((overloadable)) fpcrtl_pos(Char c, string255 str) { |
|
94 |
string255 t; |
|
95 |
t.len = 1; |
|
96 |
t.str[0] = c; |
|
97 |
t.str[1] = 0; |
|
98 |
return fpcrtl_pos(t, str); |
|
99 |
} |
|
100 |
||
101 |
Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, string255 str) { |
|
102 |
||
103 |
char* p; |
|
104 |
||
105 |
FIX_STRING(substr); |
|
106 |
FIX_STRING(str); |
|
107 |
||
108 |
if (str.len == 0) { |
|
109 |
return 0; |
|
110 |
} |
|
111 |
||
112 |
if (substr.len == 0) { |
|
113 |
return 0; |
|
114 |
} |
|
115 |
||
116 |
str.str[str.len] = 0; |
|
117 |
substr.str[substr.len] = 0; |
|
118 |
||
119 |
p = strstr(str.str, substr.str); |
|
120 |
||
121 |
if (p == NULL) { |
|
122 |
return 0; |
|
123 |
} |
|
124 |
||
125 |
return strlen(str.str) - strlen(p) + 1; |
|
126 |
} |
|
127 |
||
128 |
Integer fpcrtl_length(string255 s) { |
|
129 |
return s.len; |
|
130 |
} |
|
131 |
||
132 |
string255 fpcrtl_lowerCase(string255 s) { |
|
133 |
int i; |
|
134 |
||
135 |
for (i = 0; i < s.len; i++) { |
|
136 |
if (s.str[i] >= 'A' && s.str[i] <= 'Z') { |
|
137 |
s.str[i] += 'a' - 'A'; |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
return s; |
|
142 |
} |
|
143 |
||
144 |
void fpcrtl_fillChar__vars(void *x, SizeInt count, Byte value) { |
|
145 |
memset(x, value, count); |
|
146 |
} |
|
147 |
||
148 |
void fpcrtl_new__vars(void **p, int size) { |
|
149 |
*p = malloc(size); |
|
150 |
} |
|
151 |
||
152 |
Integer fpcrtl_trunc(extended n) { |
|
153 |
return (int) n; |
|
154 |
} |
|
155 |
||
156 |
LongInt str_to_int(char *src) |
|
157 |
{ |
|
158 |
int i; |
|
159 |
int len = strlen(src); |
|
160 |
char *end; |
|
161 |
for(i = 0; i < len; i++) |
|
162 |
{ |
|
163 |
if(src[i] == '$'){ |
|
164 |
// hex |
|
165 |
return strtol(src + i + 1, &end, 16); |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
// decimal |
|
170 |
return atoi(src); |
|
171 |
} |
|
8850
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
172 |
|
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
173 |
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
|
174 |
{ |
7983 | 175 |
FIX_STRING(s); |
176 |
*a = str_to_int(s.str); |
|
177 |
} |
|
178 |
||
8850
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
179 |
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
|
180 |
{ |
7983 | 181 |
FIX_STRING(s); |
182 |
*a = str_to_int(s.str); |
|
183 |
} |
|
184 |
||
8850
ae8a957c69fd
engine to c now compiles with some manual intervention (as of bug 596)
koda
parents:
8053
diff
changeset
|
185 |
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
|
186 |
{ |
7983 | 187 |
FIX_STRING(s); |
188 |
*a = str_to_int(s.str); |
|
189 |
} |
|
190 |
||
191 |
LongInt fpcrtl_random(LongInt l) { |
|
192 |
return (LongInt) (rand() / (double) RAND_MAX * l); |
|
193 |
} |
|
194 |
||
195 |
void __attribute__((overloadable)) fpcrtl_str__vars(float x, string255 *s) { |
|
196 |
sprintf(s->str, "%f", x); |
|
197 |
s->len = strlen(s->str); |
|
198 |
} |
|
199 |
void __attribute__((overloadable)) fpcrtl_str__vars(double x, string255 *s) { |
|
200 |
sprintf(s->str, "%f", x); |
|
201 |
s->len = strlen(s->str); |
|
202 |
} |
|
203 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint8_t x, string255 *s) { |
|
204 |
sprintf(s->str, "%u", x); |
|
205 |
s->len = strlen(s->str); |
|
206 |
} |
|
207 |
void __attribute__((overloadable)) fpcrtl_str__vars(int8_t x, string255 *s) { |
|
208 |
sprintf(s->str, "%d", x); |
|
209 |
s->len = strlen(s->str); |
|
210 |
} |
|
211 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint16_t x, string255 *s) { |
|
212 |
sprintf(s->str, "%u", x); |
|
213 |
s->len = strlen(s->str); |
|
214 |
} |
|
215 |
void __attribute__((overloadable)) fpcrtl_str__vars(int16_t x, string255 *s) { |
|
216 |
sprintf(s->str, "%d", x); |
|
217 |
s->len = strlen(s->str); |
|
218 |
} |
|
219 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint32_t x, string255 *s) { |
|
220 |
sprintf(s->str, "%u", x); |
|
221 |
s->len = strlen(s->str); |
|
222 |
} |
|
223 |
void __attribute__((overloadable)) fpcrtl_str__vars(int32_t x, string255 *s) { |
|
224 |
sprintf(s->str, "%d", x); |
|
225 |
s->len = strlen(s->str); |
|
226 |
} |
|
227 |
void __attribute__((overloadable)) fpcrtl_str__vars(uint64_t x, string255 *s) { |
|
228 |
sprintf(s->str, "%llu", x); |
|
229 |
s->len = strlen(s->str); |
|
230 |
} |
|
231 |
void __attribute__((overloadable)) fpcrtl_str__vars(int64_t x, string255 *s) { |
|
232 |
sprintf(s->str, "%lld", x); |
|
233 |
s->len = strlen(s->str); |
|
234 |
} |
|
235 |
||
236 |
/* |
|
237 |
* XXX No protection currently! |
|
238 |
*/ |
|
239 |
void fpcrtl_interlockedIncrement__vars(int *i) { |
|
240 |
(*i)++; |
|
241 |
} |
|
242 |
||
243 |
void fpcrtl_interlockedDecrement__vars(int *i) { |
|
244 |
(*i)--; |
|
245 |
} |
|
246 |
||
247 |
/* |
|
248 |
* This function should be called when entering main |
|
249 |
*/ |
|
250 |
void fpcrtl_init(int argc, char** argv) { |
|
251 |
int i; |
|
252 |
paramCount = argc; |
|
253 |
||
254 |
printf("ARGC = %d\n", paramCount); |
|
255 |
||
256 |
for (i = 0; i < argc; i++) { |
|
257 |
if (strlen(argv[i]) > 255) { |
|
258 |
assert(0); |
|
259 |
} |
|
260 |
strcpy(params[i].str, argv[i]); |
|
261 |
params[i].len = strlen(params[i].str); |
|
262 |
} |
|
263 |
||
264 |
} |
|
265 |
||
266 |
int fpcrtl_paramCount() { |
|
267 |
return paramCount - 1; // ignore the first one |
|
268 |
} |
|
269 |
||
270 |
string255 fpcrtl_paramStr(int i) { |
|
271 |
return params[i]; |
|
272 |
} |
|
273 |
||
274 |
int fpcrtl_UTF8ToUnicode(PWideChar dest, PChar src, SizeInt maxLen) { |
|
275 |
//return swprintf(dest, maxLen, L"%hs", "src"); //doesn't work in emscripten |
|
276 |
return 0; |
|
277 |
} |
|
278 |
||
279 |
uint32_t __attribute__((overloadable)) fpcrtl_lo(uint64_t i) { |
|
280 |
return (i & 0xFFFFFFFF); |
|
281 |
} |
|
282 |