| 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 |
{ |
| 46 |
int newlen = str1.len + str2.len; |
| 47 |
if(newlen > 255) newlen = 255; |
| 48 |
|
| 49 |
memcpy(&(str1.str[str1.len]), str2.str, newlen - str1.len); |
| 50 |
str1.len = newlen; |
| 51 |
|
| 52 |
return str1; |
| 53 |
} |
| 54 |
|
| 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; |
| 59 |
|
| 60 |
memcpy(&(str1.s[str1.len + 1]), &str2.s[1], newlen - str1.len); |
| 61 |
str1.len = newlen; |
| 62 |
|
| 63 |
return str1; |
| 64 |
} |
| 65 |
|
| 66 |
string255 fpcrtl_strappend(string255 s, char c) |
| 67 |
{ |
| 68 |
if(s.len < 255) |
| 69 |
{ |
| 70 |
++s.len; |
| 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.len; |
| 82 |
s.s[s.len] = c; |
| 83 |
} |
| 84 |
|
| 85 |
return s; |
| 86 |
} |
| 87 |
|
| 88 |
string255 fpcrtl_strprepend(char c, string255 s) |
| 89 |
{ |
| 90 |
uint8_t newlen = s.len < 255 ? s.len + 1 : 255; |
| 91 |
memmove(s.str + 1, s.str, newlen); // also move '/0' |
| 92 |
s.str[0] = c; |
| 93 |
s.len = newlen; |
| 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 |
{ |
| 111 |
return memcmp(str1.s, str2.s, str1.len + 1) == 0; |
| 112 |
} |
| 113 |
|
| 114 |
bool fpcrtl_strcomparec(string255 a, char b) |
| 115 |
{ |
| 116 |
if(a.len == 1 && a.str[0] == b){ |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
bool fpcrtl_strncompare(string255 a, string255 b) |
| 124 |
{ |
| 125 |
return !fpcrtl_strcompare(a, b); |
| 126 |
} |
| 127 |
|
| 128 |
bool fpcrtl_strncompareA(astring a, astring b) |
| 129 |
{ |
| 130 |
return (a.len != b.len) || (memcmp(a.str, b.str, a.len) != 0); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
string255 fpcrtl_pchar2str(const char *s) |
| 135 |
{ |
| 136 |
string255 result; |
| 137 |
|
| 138 |
if(!s) |
| 139 |
{ |
| 140 |
result.len = 0; |
| 141 |
} else |
| 142 |
{ |
| 143 |
int rlen = strlen(s); |
| 144 |
|
| 145 |
if(rlen > 255){ |
| 146 |
rlen = 255; |
| 147 |
} |
| 148 |
|
| 149 |
result.len = rlen; |
| 150 |
memcpy(result.str, s, rlen); |
| 151 |
} |
| 152 |
|
| 153 |
return result; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
string255 fpcrtl_make_string(const char* s) { |
| 158 |
return fpcrtl_pchar2str(s); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
astring fpcrtl_pchar2astr(const char *s) |
| 163 |
{ |
| 164 |
astring result; |
| 165 |
|
| 166 |
if(!s) |
| 167 |
{ |
| 168 |
result.len = 0; |
| 169 |
} else |
| 170 |
{ |
| 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.str, s, rlen); |
| 179 |
} |
| 180 |
|
| 181 |
return result; |
| 182 |
} |
| 183 |
|
| 184 |
astring fpcrtl_str2astr(const string255 s) |
| 185 |
{ |
| 186 |
astring result; |
| 187 |
|
| 188 |
result.str255 = s; |
| 189 |
result.len = s.len; |
| 190 |
|
| 191 |
return result; |
| 192 |
} |
| 193 |
|
| 194 |
string255 fpcrtl_astr2str(const astring s) |
| 195 |
{ |
| 196 |
string255 result; |
| 197 |
|
| 198 |
result = s.str255; |
| 199 |
result.len = s.len > 255 ? 255 : s.len; |
| 200 |
|
| 201 |
return result; |
| 202 |
} |
| 203 |
|
| 204 |
char __pcharBuf[256]; |
| 205 |
|
| 206 |
char* fpcrtl__pchar__vars(const string255 * s) |
| 207 |
{ |
| 208 |
memcpy(__pcharBuf, &s->s[1], s->len); |
| 209 |
__pcharBuf[s->len] = 0; |
| 210 |
return __pcharBuf; |
| 211 |
} |
| 212 |
|
| 213 |
char* fpcrtl__pcharA__vars(astring * s) |
| 214 |
{ |
| 215 |
if(s->len == MAX_ANSISTRING_LENGTH) |
| 216 |
--s->len; |
| 217 |
|
| 218 |
s->s[s->len + 1] = 0; |
| 219 |
return &s->s[1]; |
| 220 |
} |
| 221 |
|
| 222 |
void fpcrtl_glShaderSource(GLuint shader, GLsizei count,/* const dropped for pas2c compat */ GLchar **string, const GLint *length) { |
| 223 |
glShaderSource(shader, count, (const char * const *)string, length); |
| 224 |
} |