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 char * my_strdup(const char string[]) {
\r
325 ASSERT(string!=NULL);
\r
327 // strdup() is not ANSI C
\r
329 address = (char *) my_malloc(strlen(string)+1);
\r
330 strcpy(address,string);
\r
335 // my_string_clear()
\r
337 void my_string_clear(const char * * variable) {
\r
339 ASSERT(variable!=NULL);
\r
341 if (*variable != NULL) {
\r
342 my_free((void*)(*variable));
\r
349 void my_string_set(const char * * variable, const char string[]) {
\r
351 ASSERT(variable!=NULL);
\r
352 ASSERT(string!=NULL);
\r
354 if (*variable != NULL) my_free((void*)(*variable));
\r
355 *variable = my_strdup(string);
\r
358 double now_real() {
\r
360 struct timeval tv[1];
\r
361 struct timezone tz[1];
\r
363 tz->tz_minuteswest = 0;
\r
364 tz->tz_dsttime = 0; // DST_NONE not declared in linux
\r
366 if (gettimeofday(tv,tz) == -1) {
\r
367 my_fatal("now_real(): gettimeofday(): %s\n",strerror(errno));
\r
370 return tv->tv_sec + tv->tv_usec * 1E-6;
\r
372 struct _timeb timeptr;
\r
374 return(timeptr.time+((double)timeptr.millitm)/1000.0);
\r
375 // return (double) GetTickCount() / 1000.0; // we can do better here:-)
\r
380 // my_timer_reset()
\r
382 void my_timer_reset(my_timer_t * timer) {
\r
384 ASSERT(timer!=NULL);
\r
386 timer->start_real = 0.0;
\r
387 timer->elapsed_real = 0.0;
\r
388 timer->running = FALSE;
\r
391 // my_timer_start()
\r
393 void my_timer_start(my_timer_t * timer) {
\r
394 // timer->start_real = 0.0;
\r
395 timer->elapsed_real = 0.0;
\r
396 // timer->running = FALSE;
\r
397 ASSERT(timer!=NULL);
\r
399 timer->running = TRUE;
\r
400 timer->start_real = now_real();
\r
405 void my_timer_stop(my_timer_t * timer) {
\r
407 ASSERT(timer!=NULL);
\r
409 ASSERT(timer->running);
\r
411 timer->elapsed_real += now_real() - timer->start_real;
\r
412 timer->start_real = 0.0;
\r
413 timer->running = FALSE;
\r
416 // my_timer_elapsed_real()
\r
418 double my_timer_elapsed_real(const my_timer_t * timer) {
\r
422 ASSERT(timer!=NULL);
\r
424 elapsed = timer->elapsed_real;
\r
425 if (timer->running) elapsed += now_real() - timer->start_real;
\r
427 if (elapsed < 0.0) elapsed = 0.0;
\r