22 #include <sys/time.h>
\r
30 #define StringSize 4096
\r
36 FILE * LogFile=NULL;
\r
50 // switch file buffering off
\r
53 setbuf(stdout,NULL);
\r
58 void my_random_init() {
\r
64 int my_random_int(int n) {
\r
70 r = ((int)floor(my_random_double()*((double)n)));
\r
76 // my_random_double()
\r
78 double my_random_double() {
\r
82 r = ((double)rand()) / (((double)RAND_MAX) + 1.0);
\r
83 ASSERT(r>=0.0&&r<1.0);
\r
90 sint64 my_atoll(const char string[]) {
\r
94 sscanf(string,S64_FORMAT,&n);
\r
101 int my_round(double x) {
\r
103 return ((int)floor(x+0.5));
\r
108 void * my_malloc(size_t size) {
\r
114 address = malloc(size);
\r
115 if (address == NULL) my_fatal("my_malloc(): malloc(): %s\n",strerror(errno));
\r
122 void * my_realloc(void * address, size_t size) {
\r
124 ASSERT(address!=NULL);
\r
127 address = realloc(address,size);
\r
128 if (address == NULL) my_fatal("my_realloc(): realloc(): %s\n",strerror(errno));
\r
135 void my_free(void * address) {
\r
137 ASSERT(address!=NULL);
\r
144 void my_log_open(const char file_name[]) {
\r
146 ASSERT(file_name!=NULL);
\r
148 LogFile = fopen(file_name,"a");
\r
150 //line buffering doesn't work too well in MSVC and/or windows
\r
151 if (LogFile != NULL) setvbuf(LogFile,NULL,_IOLBF,0); // line buffering
\r
157 void my_log_close() {
\r
159 if (LogFile != NULL) fclose(LogFile);
\r
165 void my_log(const char format[], ...) {
\r
167 char string[FormatBufferSize];
\r
169 ASSERT(format!=NULL);
\r
173 CONSTRUCT_ARG_STRING(format,string);
\r
176 if (LogFile != NULL) {
\r
177 fprintf(LogFile,"%.3f %s",now_real(),string);
\r
186 void my_fatal(const char format[], ...) {
\r
188 char string[FormatBufferSize];
\r
190 ASSERT(format!=NULL);
\r
194 CONSTRUCT_ARG_STRING(format,string);
\r
196 fprintf(stderr,"%s",string);
\r
197 my_log("POLYGLOT %s",string);
\r
199 if (Error) { // recursive error
\r
200 my_log("POLYGLOT *** RECURSIVE ERROR ***\n");
\r
201 exit(EXIT_FAILURE);
\r
209 // my_file_read_line()
\r
211 bool my_file_read_line(FILE * file, char string[], int size) {
\r
216 ASSERT(file!=NULL);
\r
217 ASSERT(string!=NULL);
\r
220 if (fgets(string,size,file) == NULL) {
\r
224 my_fatal("my_file_read_line(): fgets(): %s\n",strerror(errno));
\r
228 // remove CRs and LFs
\r
233 while ((c=string[src++]) != '\0') {
\r
234 if (c != '\r' && c != '\n') string[dst++] = c;
\r
237 string[dst] = '\0';
\r
244 void my_path_join(char *join_path, const char *path, const char *file){
\r
251 snprintf(join_path,StringSize,"%s%c%s",path,separator,file);
\r
252 join_path[StringSize-1]='\0';
\r
257 int my_mkdir(const char *path){
\r
262 ret=mkdir(path,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
\r
268 // my_string_empty()
\r
270 bool my_string_empty(const char string[]) {
\r
272 return string == NULL || string[0] == '\0';
\r
275 // my_string_whitespace()
\r
277 bool my_string_whitespace(const char string[]){
\r
279 while(string[pos]!='\0'){
\r
280 if(string[pos]!=' ' && string[pos]!='\t'){
\r
288 // my_string_equal()
\r
290 bool my_string_equal(const char string_1[], const char string_2[]) {
\r
292 ASSERT(string_1!=NULL);
\r
293 ASSERT(string_2!=NULL);
\r
295 return strcmp(string_1,string_2) == 0;
\r
298 // my_string_case_equal()
\r
300 bool my_string_case_equal(const char string_1[], const char string_2[]) {
\r
304 ASSERT(string_1!=NULL);
\r
305 ASSERT(string_2!=NULL);
\r
312 if (tolower(c1) != tolower(c2)) return FALSE;
\r
313 if (c1 == '\0') return TRUE;
\r
321 void my_string_tolower(char *dst, const char *src){
\r
325 while((c=*(src++))){
\r
332 // my_string_case_contains()
\r
334 const char* my_string_case_contains(const char string_1[], const char string_2[]){
\r
336 char tmp1[StringSize];
\r
337 char tmp2[StringSize];
\r
341 ASSERT(string_1!=NULL);
\r
342 ASSERT(string_2!=NULL);
\r
344 my_string_tolower(tmp1,string_1);
\r
345 my_string_tolower(tmp2,string_2);
\r
347 where=strstr(tmp1,tmp2);
\r
349 return string_1+(where-tmp1);
\r
359 char * my_strdup(const char string[]) {
\r
363 ASSERT(string!=NULL);
\r
365 // strdup() is not ANSI C
\r
367 address = (char *) my_malloc(strlen(string)+1);
\r
368 strcpy(address,string);
\r
373 // my_string_clear()
\r
375 void my_string_clear(const char * * variable) {
\r
377 ASSERT(variable!=NULL);
\r
379 if (*variable != NULL) {
\r
380 my_free((void*)(*variable));
\r
387 void my_string_set(const char * * variable, const char string[]) {
\r
389 ASSERT(variable!=NULL);
\r
390 ASSERT(string!=NULL);
\r
392 if (*variable != NULL) my_free((void*)(*variable));
\r
393 *variable = my_strdup(string);
\r
398 double now_real() {
\r
400 struct timeval tv[1];
\r
401 struct timezone tz[1];
\r
403 tz->tz_minuteswest = 0;
\r
404 tz->tz_dsttime = 0; // DST_NONE not declared in linux
\r
406 if (gettimeofday(tv,tz) == -1) {
\r
407 my_fatal("now_real(): gettimeofday(): %s\n",strerror(errno));
\r
410 return tv->tv_sec + tv->tv_usec * 1E-6;
\r
412 struct _timeb timeptr;
\r
414 return(timeptr.time+((double)timeptr.millitm)/1000.0);
\r
415 // return (double) GetTickCount() / 1000.0; // we can do better here:-)
\r
420 // my_timer_reset()
\r
422 void my_timer_reset(my_timer_t * timer) {
\r
424 ASSERT(timer!=NULL);
\r
426 timer->start_real = 0.0;
\r
427 timer->elapsed_real = 0.0;
\r
428 timer->running = FALSE;
\r
431 // my_timer_start()
\r
433 void my_timer_start(my_timer_t * timer) {
\r
434 // timer->start_real = 0.0;
\r
435 timer->elapsed_real = 0.0;
\r
436 // timer->running = FALSE;
\r
437 ASSERT(timer!=NULL);
\r
439 timer->running = TRUE;
\r
440 timer->start_real = now_real();
\r
445 void my_timer_stop(my_timer_t * timer) {
\r
447 ASSERT(timer!=NULL);
\r
449 ASSERT(timer->running);
\r
451 timer->elapsed_real += now_real() - timer->start_real;
\r
452 timer->start_real = 0.0;
\r
453 timer->running = FALSE;
\r
456 // my_timer_elapsed_real()
\r
458 double my_timer_elapsed_real(const my_timer_t * timer) {
\r
462 ASSERT(timer!=NULL);
\r
464 elapsed = timer->elapsed_real;
\r
465 if (timer->running) elapsed += now_real() - timer->start_real;
\r
467 if (elapsed < 0.0) elapsed = 0.0;
\r