22 #include <sys/time.h>
\r
32 FILE * LogFile=NULL;
\r
46 // switch file buffering off
\r
49 setbuf(stdout,NULL);
\r
54 void my_random_init() {
\r
60 int my_random_int(int n) {
\r
66 r = ((int)floor(my_random_double()*((double)n)));
\r
72 // my_random_double()
\r
74 double my_random_double() {
\r
78 r = ((double)rand()) / (((double)RAND_MAX) + 1.0);
\r
79 ASSERT(r>=0.0&&r<1.0);
\r
86 sint64 my_atoll(const char string[]) {
\r
90 sscanf(string,S64_FORMAT,&n);
\r
97 int my_round(double x) {
\r
99 return ((int)floor(x+0.5));
\r
104 void * my_malloc(size_t size) {
\r
110 address = malloc(size);
\r
111 if (address == NULL) my_fatal("my_malloc(): malloc(): %s\n",strerror(errno));
\r
118 void * my_realloc(void * address, size_t size) {
\r
120 ASSERT(address!=NULL);
\r
123 address = realloc(address,size);
\r
124 if (address == NULL) my_fatal("my_realloc(): realloc(): %s\n",strerror(errno));
\r
131 void my_free(void * address) {
\r
133 ASSERT(address!=NULL);
\r
140 void my_log_open(const char file_name[]) {
\r
142 ASSERT(file_name!=NULL);
\r
144 LogFile = fopen(file_name,"a");
\r
146 //line buffering doesn't work too well in MSVC and/or windows
\r
147 if (LogFile != NULL) setvbuf(LogFile,NULL,_IOLBF,0); // line buffering
\r
153 void my_log_close() {
\r
155 if (LogFile != NULL) fclose(LogFile);
\r
161 void my_log(const char format[], ...) {
\r
163 char string[FormatBufferSize];
\r
165 ASSERT(format!=NULL);
\r
169 CONSTRUCT_ARG_STRING(format,string);
\r
172 if (LogFile != NULL) {
\r
173 fprintf(LogFile,"%.3f %s",now_real(),string);
\r
182 void my_fatal(const char format[], ...) {
\r
184 char string[FormatBufferSize];
\r
186 ASSERT(format!=NULL);
\r
190 CONSTRUCT_ARG_STRING(format,string);
\r
192 fprintf(stderr,"%s",string);
\r
193 my_log("POLYGLOT %s",string);
\r
195 if (Error) { // recursive error
\r
196 my_log("POLYGLOT *** RECURSIVE ERROR ***\n");
\r
197 exit(EXIT_FAILURE);
\r
205 // my_file_read_line()
\r
207 bool my_file_read_line(FILE * file, char string[], int size) {
\r
212 ASSERT(file!=NULL);
\r
213 ASSERT(string!=NULL);
\r
216 if (fgets(string,size,file) == NULL) {
\r
220 my_fatal("my_file_read_line(): fgets(): %s\n",strerror(errno));
\r
224 // remove CRs and LFs
\r
229 while ((c=string[src++]) != '\0') {
\r
230 if (c != '\r' && c != '\n') string[dst++] = c;
\r
233 string[dst] = '\0';
\r
238 // my_string_empty()
\r
240 bool my_string_empty(const char string[]) {
\r
242 return string == NULL || string[0] == '\0';
\r
245 // my_string_whitespace()
\r
247 bool my_string_whitespace(const char string[]){
\r
249 while(string[pos]!='\0'){
\r
250 if(string[pos]!=' ' && string[pos]!='\t'){
\r
258 // my_string_equal()
\r
260 bool my_string_equal(const char string_1[], const char string_2[]) {
\r
262 ASSERT(string_1!=NULL);
\r
263 ASSERT(string_2!=NULL);
\r
265 return strcmp(string_1,string_2) == 0;
\r
268 // my_string_case_equal()
\r
270 bool my_string_case_equal(const char string_1[], const char string_2[]) {
\r
274 ASSERT(string_1!=NULL);
\r
275 ASSERT(string_2!=NULL);
\r
282 if (tolower(c1) != tolower(c2)) return FALSE;
\r
283 if (c1 == '\0') return TRUE;
\r
291 char * my_strdup(const char string[]) {
\r
295 ASSERT(string!=NULL);
\r
297 // strdup() is not ANSI C
\r
299 address = (char *) my_malloc(strlen(string)+1);
\r
300 strcpy(address,string);
\r
305 // my_string_clear()
\r
307 void my_string_clear(const char * * variable) {
\r
309 ASSERT(variable!=NULL);
\r
311 if (*variable != NULL) {
\r
312 my_free((void*)(*variable));
\r
319 void my_string_set(const char * * variable, const char string[]) {
\r
321 ASSERT(variable!=NULL);
\r
322 ASSERT(string!=NULL);
\r
324 if (*variable != NULL) my_free((void*)(*variable));
\r
325 *variable = my_strdup(string);
\r
328 double now_real() {
\r
330 struct timeval tv[1];
\r
331 struct timezone tz[1];
\r
333 tz->tz_minuteswest = 0;
\r
334 tz->tz_dsttime = 0; // DST_NONE not declared in linux
\r
336 if (gettimeofday(tv,tz) == -1) {
\r
337 my_fatal("now_real(): gettimeofday(): %s\n",strerror(errno));
\r
340 return tv->tv_sec + tv->tv_usec * 1E-6;
\r
342 struct _timeb timeptr;
\r
344 return(timeptr.time+((double)timeptr.millitm)/1000.0);
\r
345 // return (double) GetTickCount() / 1000.0; // we can do better here:-)
\r
350 // my_timer_reset()
\r
352 void my_timer_reset(my_timer_t * timer) {
\r
354 ASSERT(timer!=NULL);
\r
356 timer->start_real = 0.0;
\r
357 timer->elapsed_real = 0.0;
\r
358 timer->running = FALSE;
\r
361 // my_timer_start()
\r
363 void my_timer_start(my_timer_t * timer) {
\r
364 // timer->start_real = 0.0;
\r
365 timer->elapsed_real = 0.0;
\r
366 // timer->running = FALSE;
\r
367 ASSERT(timer!=NULL);
\r
369 timer->running = TRUE;
\r
370 timer->start_real = now_real();
\r
375 void my_timer_stop(my_timer_t * timer) {
\r
377 ASSERT(timer!=NULL);
\r
379 ASSERT(timer->running);
\r
381 timer->elapsed_real += now_real() - timer->start_real;
\r
382 timer->start_real = 0.0;
\r
383 timer->running = FALSE;
\r
386 // my_timer_elapsed_real()
\r
388 double my_timer_elapsed_real(const my_timer_t * timer) {
\r
392 ASSERT(timer!=NULL);
\r
394 elapsed = timer->elapsed_real;
\r
395 if (timer->running) elapsed += now_real() - timer->start_real;
\r
397 if (elapsed < 0.0) elapsed = 0.0;
\r