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
154 my_log("POLYGLOT *** LOGFILE OPENED ***\n");
\r
161 void my_log_close() {
\r
163 if (LogFile != NULL) fclose(LogFile);
\r
169 void my_log(const char format[], ...) {
\r
171 char string[FormatBufferSize];
\r
173 ASSERT(format!=NULL);
\r
177 CONSTRUCT_ARG_STRING(format,string);
\r
180 if (LogFile != NULL) {
\r
181 fprintf(LogFile,"%.3f %s",now_real(),string);
\r
190 void my_fatal(const char format[], ...) {
\r
192 char string[FormatBufferSize];
\r
194 ASSERT(format!=NULL);
\r
198 CONSTRUCT_ARG_STRING(format,string);
\r
200 fprintf(stderr,"%s",string);
\r
201 my_log("POLYGLOT %s",string);
\r
203 if (Error) { // recursive error
\r
204 my_log("POLYGLOT *** RECURSIVE ERROR ***\n");
\r
205 exit(EXIT_FAILURE);
\r
213 // my_file_read_line()
\r
215 bool my_file_read_line(FILE * file, char string[], int size) {
\r
220 ASSERT(file!=NULL);
\r
221 ASSERT(string!=NULL);
\r
224 if (fgets(string,size,file) == NULL) {
\r
228 my_fatal("my_file_read_line(): fgets(): %s\n",strerror(errno));
\r
232 // remove CRs and LFs
\r
237 while ((c=string[src++]) != '\0') {
\r
238 if (c != '\r' && c != '\n') string[dst++] = c;
\r
241 string[dst] = '\0';
\r
248 void my_path_join(char *join_path, const char *path, const char *file){
\r
255 snprintf(join_path,StringSize,"%s%c%s",path,separator,file);
\r
256 join_path[StringSize-1]='\0';
\r
261 int my_mkdir(const char *path){
\r
266 ret=mkdir(path,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
\r
272 // my_string_empty()
\r
274 bool my_string_empty(const char string[]) {
\r
276 return string == NULL || string[0] == '\0';
\r
279 // my_string_whitespace()
\r
281 bool my_string_whitespace(const char string[]){
\r
283 while(string[pos]!='\0'){
\r
284 if(string[pos]!=' ' && string[pos]!='\t'){
\r
292 // my_string_equal()
\r
294 bool my_string_equal(const char string_1[], const char string_2[]) {
\r
296 ASSERT(string_1!=NULL);
\r
297 ASSERT(string_2!=NULL);
\r
299 return strcmp(string_1,string_2) == 0;
\r
302 // my_string_case_equal()
\r
304 bool my_string_case_equal(const char string_1[], const char string_2[]) {
\r
308 ASSERT(string_1!=NULL);
\r
309 ASSERT(string_2!=NULL);
\r
316 if (tolower(c1) != tolower(c2)) return FALSE;
\r
317 if (c1 == '\0') return TRUE;
\r
325 void my_string_tolower(char *dst, const char *src){
\r
329 while((c=*(src++))){
\r
336 // my_string_case_contains()
\r
338 const char* my_string_case_contains(const char string_1[], const char string_2[]){
\r
340 char tmp1[StringSize];
\r
341 char tmp2[StringSize];
\r
345 ASSERT(string_1!=NULL);
\r
346 ASSERT(string_2!=NULL);
\r
348 my_string_tolower(tmp1,string_1);
\r
349 my_string_tolower(tmp2,string_2);
\r
351 where=strstr(tmp1,tmp2);
\r
353 return string_1+(where-tmp1);
\r
363 char * my_strdup(const char string[]) {
\r
367 ASSERT(string!=NULL);
\r
369 // strdup() is not ANSI C
\r
371 address = (char *) my_malloc(strlen(string)+1);
\r
372 strcpy(address,string);
\r
377 // my_string_clear()
\r
379 void my_string_clear(const char * * variable) {
\r
381 ASSERT(variable!=NULL);
\r
383 if (*variable != NULL) {
\r
384 my_free((void*)(*variable));
\r
391 void my_string_set(const char * * variable, const char string[]) {
\r
393 ASSERT(variable!=NULL);
\r
394 ASSERT(string!=NULL);
\r
396 if (*variable != NULL) my_free((void*)(*variable));
\r
397 *variable = my_strdup(string);
\r
402 double now_real() {
\r
404 struct timeval tv[1];
\r
405 struct timezone tz[1];
\r
407 tz->tz_minuteswest = 0;
\r
408 tz->tz_dsttime = 0; // DST_NONE not declared in linux
\r
410 if (gettimeofday(tv,tz) == -1) {
\r
411 my_fatal("now_real(): gettimeofday(): %s\n",strerror(errno));
\r
414 return tv->tv_sec + tv->tv_usec * 1E-6;
\r
416 struct _timeb timeptr;
\r
418 return(timeptr.time+((double)timeptr.millitm)/1000.0);
\r
419 // return (double) GetTickCount() / 1000.0; // we can do better here:-)
\r
424 // my_timer_reset()
\r
426 void my_timer_reset(my_timer_t * timer) {
\r
428 ASSERT(timer!=NULL);
\r
430 timer->start_real = 0.0;
\r
431 timer->elapsed_real = 0.0;
\r
432 timer->running = FALSE;
\r
435 // my_timer_start()
\r
437 void my_timer_start(my_timer_t * timer) {
\r
438 // timer->start_real = 0.0;
\r
439 timer->elapsed_real = 0.0;
\r
440 // timer->running = FALSE;
\r
441 ASSERT(timer!=NULL);
\r
443 timer->running = TRUE;
\r
444 timer->start_real = now_real();
\r
449 void my_timer_stop(my_timer_t * timer) {
\r
451 ASSERT(timer!=NULL);
\r
453 ASSERT(timer->running);
\r
455 timer->elapsed_real += now_real() - timer->start_real;
\r
456 timer->start_real = 0.0;
\r
457 timer->running = FALSE;
\r
460 // my_timer_elapsed_real()
\r
462 double my_timer_elapsed_real(const my_timer_t * timer) {
\r
466 ASSERT(timer!=NULL);
\r
468 elapsed = timer->elapsed_real;
\r
469 if (timer->running) elapsed += now_real() - timer->start_real;
\r
471 if (elapsed < 0.0) elapsed = 0.0;
\r