Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
112 | pmbaty | 1 | /* |
2 | Protector -- a UCI chess engine |
||
3 | |||
4 | Copyright (C) 2009-2010 Raimund Heid (Raimund_Heid@yahoo.com) |
||
5 | |||
6 | This program is free software: you can redistribute it and/or modify |
||
7 | it under the terms of the GNU General Public License as published by |
||
8 | the Free Software Foundation, either version 3 of the License, or |
||
9 | (at your option) any later version. |
||
10 | |||
11 | This program is distributed in the hope that it will be useful, |
||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | GNU General Public License for more details. |
||
15 | |||
16 | You should have received a copy of the GNU General Public License |
||
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
18 | |||
19 | */ |
||
20 | |||
21 | #include <stdlib.h> |
||
22 | #include <string.h> |
||
23 | #include <stdarg.h> |
||
24 | #include <stdio.h> |
||
25 | #include <ctype.h> |
||
26 | #include <assert.h> |
||
27 | #include <math.h> |
||
28 | #include <time.h> |
||
29 | #include <sys/timeb.h> |
||
30 | #include "tools.h" |
||
31 | |||
32 | unsigned long getTimestamp() |
||
33 | { |
||
34 | struct timeb t_current; |
||
35 | |||
36 | ftime(&t_current); |
||
37 | |||
38 | return 1000 * (long) t_current.time + (long) t_current.millitm; |
||
39 | } |
||
40 | |||
41 | long getProcessTimestamp() |
||
42 | { |
||
43 | clock_t ts = clock(); |
||
44 | |||
45 | while (ts < 0) |
||
46 | { |
||
47 | ts = clock(); |
||
48 | } |
||
49 | |||
50 | return ts / (CLOCKS_PER_SEC / 1000); |
||
51 | } |
||
52 | |||
53 | String getEmptyString() |
||
54 | { |
||
55 | String s; |
||
56 | |||
57 | s.bufferSize = 256; |
||
58 | s.buffer = s.tail = (char *) malloc(s.bufferSize); |
||
59 | *s.tail = '\0'; |
||
60 | |||
61 | return s; |
||
62 | } |
||
63 | |||
64 | String getString(const char *buffer, const char *lastChar) |
||
65 | { |
||
66 | String s; |
||
67 | |||
68 | s.bufferSize = (unsigned int) (lastChar - buffer + 2); |
||
69 | s.buffer = (char *) malloc(s.bufferSize); |
||
70 | s.tail = s.buffer + s.bufferSize - 1; |
||
71 | strncpy(s.buffer, buffer, s.bufferSize - 1); |
||
72 | *s.tail = '\0'; |
||
73 | |||
74 | return s; |
||
75 | } |
||
76 | |||
77 | static String *appendBufferToString(String * string, const char *buffer) |
||
78 | { |
||
79 | size_t appendedLength = strlen(buffer); |
||
80 | size_t newLength = string->tail - string->buffer + appendedLength + 1; |
||
81 | |||
82 | if (newLength > string->bufferSize) |
||
83 | { |
||
84 | size_t delta = string->tail - string->buffer; |
||
85 | |||
86 | string->bufferSize = (unsigned int) (2 * newLength); |
||
87 | string->buffer = (char *) realloc(string->buffer, string->bufferSize); |
||
88 | string->tail = string->buffer + delta; |
||
89 | } |
||
90 | |||
91 | strcat(string->tail, buffer); |
||
92 | string->tail += appendedLength; |
||
93 | |||
94 | return string; |
||
95 | } |
||
96 | |||
97 | #define BUFFER_SIZE 8192 |
||
98 | |||
99 | String *appendToString(String * string, const char *fmt, ...) |
||
100 | { |
||
101 | va_list args; |
||
102 | |||
103 | /* int numBytesWritten; */ |
||
104 | char buffer[BUFFER_SIZE]; |
||
105 | |||
106 | va_start(args, fmt); |
||
107 | /*numBytesWritten = */ |
||
108 | vsprintf(buffer, fmt, args); |
||
109 | va_end(args); |
||
110 | |||
111 | /* assert(numBytesWritten < BUFFER_SIZE); */ |
||
112 | |||
113 | return (appendBufferToString(string, buffer)); |
||
114 | } |
||
115 | |||
116 | void breakLines(char *buffer, unsigned int maxLineLength) |
||
117 | { |
||
118 | char *lastChar; |
||
119 | |||
120 | while (strlen(buffer) > maxLineLength) |
||
121 | { |
||
122 | lastChar = buffer + maxLineLength; |
||
123 | |||
124 | while (isspace((int) *lastChar) == 0 && lastChar > buffer) |
||
125 | { |
||
126 | lastChar--; |
||
127 | } |
||
128 | |||
129 | if (isspace((int) *lastChar)) |
||
130 | { |
||
131 | *lastChar = '\n'; |
||
132 | buffer = lastChar + 1; |
||
133 | } |
||
134 | else |
||
135 | { |
||
136 | while (*buffer != '\0') |
||
137 | { |
||
138 | if (isspace((int) *buffer)) |
||
139 | { |
||
140 | *buffer++ = '\n'; |
||
141 | |||
142 | break; |
||
143 | } |
||
144 | else |
||
145 | { |
||
146 | buffer++; |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | void trim(char *buffer) |
||
154 | { |
||
155 | char *p = buffer; |
||
156 | size_t length = strlen(buffer); |
||
157 | |||
158 | if (length == 0) |
||
159 | { |
||
160 | return; |
||
161 | } |
||
162 | |||
163 | while (isspace((int) *p)) |
||
164 | { |
||
165 | p++; |
||
166 | } |
||
167 | |||
168 | if (p > buffer) |
||
169 | { |
||
170 | length -= p - buffer; |
||
171 | memmove(buffer, p, length + 1); |
||
172 | } |
||
173 | |||
174 | p = buffer + length - 1; |
||
175 | |||
176 | while (p >= buffer && isspace((int) *p)) |
||
177 | { |
||
178 | *(p--) = '\0'; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | char *getToken(const char *token, const char *tokenDelimiters) |
||
183 | { |
||
184 | unsigned int i = 0; |
||
185 | char *buffer; |
||
186 | |||
187 | while (token[i] != '\0' && strchr(tokenDelimiters, token[i]) == NULL) |
||
188 | { |
||
189 | i++; |
||
190 | } |
||
191 | |||
192 | buffer = (char *) malloc(i + 1); |
||
193 | strncpy(buffer, token, i); |
||
194 | buffer[i] = '\0'; |
||
195 | |||
196 | assert(strlen(buffer) == i); |
||
197 | |||
198 | return buffer; |
||
199 | } |
||
200 | |||
201 | int isPrime(unsigned long candidate) |
||
202 | { |
||
203 | long limit, i; |
||
204 | |||
205 | if (candidate == 2 || candidate == 3) |
||
206 | { |
||
207 | return 1; |
||
208 | } |
||
209 | |||
210 | if (candidate < 2 || candidate % 2 == 0) |
||
211 | { |
||
212 | return 0; |
||
213 | } |
||
214 | |||
215 | limit = (unsigned long) sqrt((double) candidate) + 1; |
||
216 | |||
217 | for (i = 3; i <= limit; i += 2) |
||
218 | { |
||
219 | if (candidate % i == 0) |
||
220 | { |
||
221 | return 0; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | return 1; |
||
226 | } |
||
227 | |||
228 | int logIntValue(const double zeroPoint, const int maxPoint, |
||
229 | const double maxValue, const int x) |
||
230 | { |
||
231 | const double xScaleFactor = 1.0 / (zeroPoint); |
||
232 | const double yScaleFactor = maxValue / |
||
233 | log(((double) maxPoint) * xScaleFactor); |
||
234 | const double y = log(((double) x) * xScaleFactor) * yScaleFactor; |
||
235 | |||
236 | return (int) (floor(y + 0.5)); |
||
237 | } |
||
238 | |||
239 | int applyWeight(double value, double weight) |
||
240 | { |
||
241 | double weightedValue = (value * weight) / 256.0; |
||
242 | |||
243 | return (int) (floor(weightedValue + 0.5)); |
||
244 | } |
||
245 | |||
246 | int getLimitedValue(const int minValue, const int maxValue, const int value) |
||
247 | { |
||
248 | return min(maxValue, max(minValue, value)); |
||
249 | } |
||
250 | |||
251 | unsigned long long getUnsignedLongLongFromHexString(const char *str) |
||
252 | { |
||
253 | unsigned long long number = strtoull(str, 0, 16); |
||
254 | |||
255 | return number; |
||
256 | } |
||
257 | |||
258 | void getHexStringFromUnsignedLongLong(char *buffer, unsigned long long value) |
||
259 | { |
||
260 | char *fmt = "%I64x"; |
||
261 | |||
262 | sprintf(buffer, fmt, value); |
||
263 | } |
||
264 | |||
265 | int initializeModuleTools() |
||
266 | { |
||
267 | return 0; |
||
268 | } |
||
269 | |||
270 | static int testStringOperations() |
||
271 | { |
||
272 | const char *testString = "Pascal"; |
||
273 | String string = getEmptyString(); |
||
274 | String string2; |
||
275 | |||
276 | appendToString(&string, "test"); |
||
277 | assert(strcmp(string.buffer, "test") == 0); |
||
278 | assert(string.tail - string.buffer == 4); |
||
279 | |||
280 | appendToString(&string, " %d", 123); |
||
281 | assert(strcmp(string.buffer, "test 123") == 0); |
||
282 | assert(string.tail - string.buffer == 8); |
||
283 | |||
284 | string2 = getString(testString, strchr(testString, 'c')); |
||
285 | assert(strcmp(string2.buffer, "Pasc") == 0); |
||
286 | assert(string2.bufferSize == 5); |
||
287 | |||
288 | return (string2.bufferSize == 5 ? 0 : -1); |
||
289 | } |
||
290 | |||
291 | static int testLineBreaking() |
||
292 | { |
||
293 | char ts1[] = "abcd efgh", ts2[] = "abcdefgh"; |
||
294 | char buffer[1024]; |
||
295 | |||
296 | strcpy(buffer, ts1); |
||
297 | breakLines(buffer, 3); |
||
298 | assert(strcmp(buffer, "abcd\nefgh") == 0); |
||
299 | |||
300 | strcpy(buffer, ts1); |
||
301 | breakLines(buffer, 4); |
||
302 | assert(strcmp(buffer, "abcd\nefgh") == 0); |
||
303 | |||
304 | strcpy(buffer, ts1); |
||
305 | breakLines(buffer, 5); |
||
306 | assert(strcmp(buffer, "abcd\nefgh") == 0); |
||
307 | |||
308 | strcpy(buffer, ts2); |
||
309 | breakLines(buffer, 3); |
||
310 | assert(strcmp(buffer, "abcdefgh") == 0); |
||
311 | |||
312 | return 0; |
||
313 | } |
||
314 | |||
315 | static int testTrimming() |
||
316 | { |
||
317 | char buffer1[] = " abcd efgh\n ", buffer2[] = " "; |
||
318 | |||
319 | trim(buffer1); |
||
320 | assert(strcmp(buffer1, "abcd efgh") == 0); |
||
321 | |||
322 | trim(buffer2); |
||
323 | assert(strcmp(buffer2, "") == 0); |
||
324 | |||
325 | return 0; |
||
326 | } |
||
327 | |||
328 | static int testTokenizer() |
||
329 | { |
||
330 | char *result; |
||
331 | |||
332 | result = getToken("123456", "6"); |
||
333 | assert(strcmp(result, "12345") == 0); |
||
334 | free(result); |
||
335 | |||
336 | result = getToken("123456", " "); |
||
337 | assert(strcmp(result, "123456") == 0); |
||
338 | free(result); |
||
339 | |||
340 | return 0; |
||
341 | } |
||
342 | |||
343 | static int testPrimechecker() |
||
344 | { |
||
345 | assert(isPrime(159) == 0); |
||
346 | assert(isPrime(221) == 0); |
||
347 | assert(isPrime(3337) == 0); |
||
348 | |||
349 | assert(isPrime(101) == 1); |
||
350 | assert(isPrime(257) == 1); |
||
351 | assert(isPrime(997) == 1); |
||
352 | |||
353 | return 0; |
||
354 | } |
||
355 | |||
356 | static int testMiscFunctions() |
||
357 | { |
||
358 | unsigned long long testValue = 18446744073709551564llu; |
||
359 | char buffer[256]; |
||
360 | |||
361 | assert(getLimitedValue(100, 200, 50) == 100); |
||
362 | assert(getLimitedValue(100, 200, 150) == 150); |
||
363 | assert(getLimitedValue(100, 200, 250) == 200); |
||
364 | |||
365 | assert(getUnsignedLongLongFromHexString("ffffffffffffffce") == |
||
366 | 18446744073709551566llu); |
||
367 | assert(getUnsignedLongLongFromHexString("FFFFFFFFFFFFFFCD") == |
||
368 | 18446744073709551565llu); |
||
369 | getHexStringFromUnsignedLongLong(buffer, testValue); |
||
370 | assert(getUnsignedLongLongFromHexString(buffer) == testValue); |
||
371 | |||
372 | return 0; |
||
373 | } |
||
374 | |||
375 | int testModuleTools() |
||
376 | { |
||
377 | int result; |
||
378 | |||
379 | if ((result = testStringOperations()) != 0) |
||
380 | { |
||
381 | return result; |
||
382 | } |
||
383 | |||
384 | if ((result = testLineBreaking()) != 0) |
||
385 | { |
||
386 | return result; |
||
387 | } |
||
388 | |||
389 | if ((result = testTrimming()) != 0) |
||
390 | { |
||
391 | return result; |
||
392 | } |
||
393 | |||
394 | if ((result = testTokenizer()) != 0) |
||
395 | { |
||
396 | return result; |
||
397 | } |
||
398 | |||
399 | if ((result = testPrimechecker()) != 0) |
||
400 | { |
||
401 | return result; |
||
402 | } |
||
403 | |||
404 | if ((result = testMiscFunctions()) != 0) |
||
405 | { |
||
406 | return result; |
||
407 | } |
||
408 | |||
409 | return 0; |
||
410 | } |