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