author | unc0rr |
Fri, 25 Apr 2014 14:22:59 +0400 | |
changeset 10227 | 0b848d595f22 |
parent 10137 | a4537aab4117 |
child 10241 | 2dc9ff47c5b9 |
permissions | -rw-r--r-- |
10015 | 1 |
#include "misc.h" |
2 |
#include <stdio.h> |
|
3 |
#include <stdarg.h> |
|
4 |
#include <string.h> |
|
5 |
#include <assert.h> |
|
6 |
||
7 |
char strbuf[512]; |
|
8 |
||
9 |
void fpcrtl_assert(int i) |
|
10 |
{ |
|
11 |
if(!i){ |
|
12 |
assert(0); |
|
13 |
} |
|
14 |
} |
|
15 |
||
16 |
// EFFECTS: return the nearest integer of the given number |
|
17 |
int fpcrtl_round(double number) |
|
18 |
{ |
|
19 |
return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5); |
|
20 |
} |
|
21 |
||
22 |
void fpcrtl_printf(const char* format, ...) |
|
23 |
{ |
|
24 |
#ifdef FPCRTL_DEBUG |
|
25 |
va_list args; |
|
26 |
va_start (args, format); |
|
27 |
vprintf (format, args); |
|
28 |
va_end (args); |
|
29 |
#endif |
|
30 |
} |
|
31 |
||
32 |
// |
|
33 |
//void fpcrtl_check_string(string255 str) |
|
34 |
//{ |
|
35 |
//#ifdef FPCRTL_DEBUG |
|
36 |
// int len = strlen(str.str); |
|
37 |
// if(len != str.len){ |
|
38 |
// printf("String %s internal inconsistency error. Length should be %d but actually is %d.\n", str.str, len, str.len); |
|
39 |
// } |
|
40 |
// //assert(len == str.len); |
|
41 |
//#endif |
|
42 |
//} |
|
43 |
||
44 |
string255 fpcrtl_strconcat(string255 str1, string255 str2) |
|
45 |
{ |
|
10128 | 46 |
int newlen = str1.len + str2.len; |
47 |
if(newlen > 255) newlen = 255; |
|
10015 | 48 |
|
10128 | 49 |
memcpy(&(str1.str[str1.len]), str2.str, newlen - str1.len); |
50 |
str1.len = newlen; |
|
10015 | 51 |
|
10128 | 52 |
return str1; |
53 |
} |
|
10015 | 54 |
|
10128 | 55 |
astring fpcrtl_strconcatA(astring str1, astring str2) |
56 |
{ |
|
57 |
int newlen = str1.len + str2.len; |
|
58 |
if(newlen > MAX_ANSISTRING_LENGTH) newlen = MAX_ANSISTRING_LENGTH; |
|
10015 | 59 |
|
10129
cd2a64a1f4aa
- Pas2C: make use of 'external' function decorator
unc0rr
parents:
10128
diff
changeset
|
60 |
memcpy(&(str1.s[str1.len + 1]), &str2.s[1], newlen - str1.len); |
10128 | 61 |
str1.len = newlen; |
10015 | 62 |
|
63 |
return str1; |
|
64 |
} |
|
65 |
||
66 |
string255 fpcrtl_strappend(string255 s, char c) |
|
67 |
{ |
|
10128 | 68 |
if(s.len < 255) |
69 |
{ |
|
10129
cd2a64a1f4aa
- Pas2C: make use of 'external' function decorator
unc0rr
parents:
10128
diff
changeset
|
70 |
++s.len; |
10128 | 71 |
s.s[s.len] = c; |
72 |
} |
|
73 |
||
74 |
return s; |
|
75 |
} |
|
76 |
||
77 |
astring fpcrtl_strappendA(astring s, char c) |
|
78 |
{ |
|
79 |
if(s.len < MAX_ANSISTRING_LENGTH) |
|
80 |
{ |
|
81 |
s.s[s.len] = c; |
|
82 |
++s.len; |
|
83 |
} |
|
10015 | 84 |
|
85 |
return s; |
|
86 |
} |
|
87 |
||
88 |
string255 fpcrtl_strprepend(char c, string255 s) |
|
89 |
{ |
|
10128 | 90 |
uint8_t newlen = s.len < 255 ? s.len + 1 : 255; |
91 |
memmove(s.str + 1, s.str, newlen); // also move '/0' |
|
10015 | 92 |
s.str[0] = c; |
10128 | 93 |
s.len = newlen; |
10015 | 94 |
|
95 |
return s; |
|
96 |
} |
|
97 |
||
98 |
string255 fpcrtl_chrconcat(char a, char b) |
|
99 |
{ |
|
100 |
string255 result; |
|
101 |
||
102 |
result.len = 2; |
|
103 |
result.str[0] = a; |
|
104 |
result.str[1] = b; |
|
105 |
||
106 |
return result; |
|
107 |
} |
|
108 |
||
109 |
bool fpcrtl_strcompare(string255 str1, string255 str2) |
|
110 |
{ |
|
10132 | 111 |
FIX_STRING(str1); |
112 |
FIX_STRING(str2); |
|
10128 | 113 |
if(strncmp(str1.str, str2.str, 256) == 0){ |
10015 | 114 |
return true; |
115 |
} |
|
116 |
||
117 |
return false; |
|
118 |
} |
|
119 |
||
120 |
bool fpcrtl_strcomparec(string255 a, char b) |
|
121 |
{ |
|
122 |
if(a.len == 1 && a.str[0] == b){ |
|
123 |
return true; |
|
124 |
} |
|
125 |
||
126 |
return false; |
|
127 |
} |
|
128 |
||
129 |
bool fpcrtl_strncompare(string255 a, string255 b) |
|
130 |
{ |
|
131 |
return !fpcrtl_strcompare(a, b); |
|
132 |
} |
|
133 |
||
10128 | 134 |
bool fpcrtl_strncompareA(astring a, astring b) |
135 |
{ |
|
136 |
return (a.len == b.len) && (strncmp(a.s, b.s, MAX_ANSISTRING_LENGTH) == 0); |
|
137 |
} |
|
10015 | 138 |
|
10128 | 139 |
|
140 |
string255 fpcrtl_pchar2str(const char *s) |
|
10015 | 141 |
{ |
142 |
string255 result; |
|
10137 | 143 |
|
144 |
if(!s) |
|
145 |
{ |
|
146 |
result.len = 0; |
|
147 |
} else |
|
148 |
{ |
|
149 |
int rlen = strlen(s); |
|
10015 | 150 |
|
10137 | 151 |
if(rlen > 255){ |
152 |
rlen = 255; |
|
153 |
} |
|
154 |
||
155 |
result.len = rlen; |
|
156 |
memcpy(result.str, s, rlen); |
|
10015 | 157 |
} |
158 |
||
10128 | 159 |
return result; |
160 |
} |
|
161 |
||
162 |
||
163 |
string255 fpcrtl_make_string(const char* s) { |
|
164 |
return fpcrtl_pchar2str(s); |
|
165 |
} |
|
166 |
||
167 |
||
168 |
astring fpcrtl_pchar2astr(const char *s) |
|
169 |
{ |
|
170 |
astring result; |
|
171 |
int rlen = strlen(s); |
|
172 |
||
173 |
if(rlen > MAX_ANSISTRING_LENGTH){ |
|
174 |
rlen = MAX_ANSISTRING_LENGTH; |
|
175 |
} |
|
176 |
||
177 |
result.len = rlen; |
|
178 |
memcpy(result.s + 1, s, rlen); |
|
10015 | 179 |
|
180 |
return result; |
|
181 |
} |
|
182 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10130
diff
changeset
|
183 |
astring fpcrtl_str2astr(const string255 s) |
10128 | 184 |
{ |
185 |
astring result; |
|
186 |
||
187 |
result.str255 = s; |
|
188 |
result.len = s.len; |
|
189 |
||
190 |
return result; |
|
191 |
} |
|
192 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10130
diff
changeset
|
193 |
string255 fpcrtl_astr2str(const astring s) |
10128 | 194 |
{ |
10015 | 195 |
string255 result; |
10128 | 196 |
|
197 |
result = s.str255; |
|
198 |
result.len = s.len > 255 ? 255 : s.len; |
|
199 |
||
10015 | 200 |
return result; |
201 |
} |
|
202 |
||
10128 | 203 |
char __pcharBuf[256]; |
204 |
||
10131
4b4a043111f4
- pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10130
diff
changeset
|
205 |
char* fpcrtl__pchar__vars(const string255 * s) |
10128 | 206 |
{ |
10129
cd2a64a1f4aa
- Pas2C: make use of 'external' function decorator
unc0rr
parents:
10128
diff
changeset
|
207 |
memcpy(__pcharBuf, &s->s[1], s->len); |
cd2a64a1f4aa
- Pas2C: make use of 'external' function decorator
unc0rr
parents:
10128
diff
changeset
|
208 |
__pcharBuf[s->len] = 0; |
cd2a64a1f4aa
- Pas2C: make use of 'external' function decorator
unc0rr
parents:
10128
diff
changeset
|
209 |
return __pcharBuf; |
10128 | 210 |
} |
211 |
||
212 |
char* fpcrtl__pcharA__vars(astring * s) |
|
213 |
{ |
|
214 |
if(s->len == MAX_ANSISTRING_LENGTH) |
|
215 |
--s->len; |
|
216 |
||
10130
a9d509848390
Small fix which makes pas2c engine successfully replay demos
unc0rr
parents:
10129
diff
changeset
|
217 |
s->s[s->len + 1] = 0; |
10128 | 218 |
return &s->s[1]; |
219 |
} |
|
220 |
||
10015 | 221 |
#ifdef EMSCRIPTEN |
222 |
GLenum glewInit() |
|
223 |
{ |
|
224 |
return GLEW_OK; |
|
225 |
} |
|
226 |
#endif |