|
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 //printf("str1 = %d, %d\n", str1.len, strlen(str1.str)); |
|
47 //printf("str2 = %d, %d\n", str2.len, strlen(str2.str)); |
|
48 |
|
49 #ifdef FPCRTL_DEBUG |
|
50 if(str1.len + (int)(str2.len) > 255){ |
|
51 printf("String overflow\n"); |
|
52 printf("str1(%d): %s\nstr2(%d): %s\n", str1.len, str1.str, str2.len, str2.str); |
|
53 printf("String will be truncated.\n"); |
|
54 |
|
55 strbuf[0] = 0; |
|
56 strcpy(strbuf, str1.str); |
|
57 strcat(strbuf, str2.str); |
|
58 memcpy(str1.str, strbuf, 255); |
|
59 str1.str[254] = 0; |
|
60 |
|
61 return str1; |
|
62 } |
|
63 #endif |
|
64 |
|
65 memcpy(&(str1.str[str1.len]), str2.str, str2.len); |
|
66 str1.str[str1.len + str2.len] = 0; |
|
67 str1.len += str2.len; |
|
68 |
|
69 return str1; |
|
70 } |
|
71 |
|
72 string255 fpcrtl_strappend(string255 s, char c) |
|
73 { |
|
74 s.str[s.len] = c; |
|
75 s.str[s.len + 1] = 0; |
|
76 s.len ++; |
|
77 |
|
78 return s; |
|
79 } |
|
80 |
|
81 string255 fpcrtl_strprepend(char c, string255 s) |
|
82 { |
|
83 FIX_STRING(s); |
|
84 |
|
85 memmove(s.str + 1, s.str, s.len + 1); // also move '/0' |
|
86 s.str[0] = c; |
|
87 s.len++; |
|
88 |
|
89 return s; |
|
90 } |
|
91 |
|
92 string255 fpcrtl_chrconcat(char a, char b) |
|
93 { |
|
94 string255 result; |
|
95 |
|
96 result.len = 2; |
|
97 result.str[0] = a; |
|
98 result.str[1] = b; |
|
99 result.str[2] = 0; |
|
100 |
|
101 return result; |
|
102 } |
|
103 |
|
104 bool fpcrtl_strcompare(string255 str1, string255 str2) |
|
105 { |
|
106 //printf("str1 = %d, %d\n", str1.len, strlen(str1.str)); |
|
107 //printf("str2 = %d, %d\n", str2.len, strlen(str2.str)); |
|
108 FIX_STRING(str1); |
|
109 FIX_STRING(str2); |
|
110 |
|
111 if(strcmp(str1.str, str2.str) == 0){ |
|
112 return true; |
|
113 } |
|
114 |
|
115 return false; |
|
116 } |
|
117 |
|
118 bool fpcrtl_strcomparec(string255 a, char b) |
|
119 { |
|
120 FIX_STRING(a); |
|
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 |
|
134 //char* fpcrtl_pchar(string255 s) |
|
135 //{ |
|
136 // return s.str; |
|
137 //} |
|
138 |
|
139 string255 fpcrtl_pchar2str(char *s) |
|
140 { |
|
141 string255 result; |
|
142 int t = strlen(s); |
|
143 |
|
144 if(t > 255){ |
|
145 printf("pchar2str, length > 255\n"); |
|
146 assert(0); |
|
147 } |
|
148 |
|
149 result.len = t; |
|
150 memcpy(result.str, s, t); |
|
151 result.str[t] = 0; |
|
152 |
|
153 return result; |
|
154 } |
|
155 |
|
156 string255 fpcrtl_make_string(const char* s) { |
|
157 string255 result; |
|
158 strcpy(result.str, s); |
|
159 result.len = strlen(s); |
|
160 return result; |
|
161 } |
|
162 |
|
163 #ifdef EMSCRIPTEN |
|
164 GLenum glewInit() |
|
165 { |
|
166 return GLEW_OK; |
|
167 } |
|
168 #endif |