21 #include <sys/time.h>
\r
30 FILE * LogFile=NULL;
\r
44 // switch file buffering off
\r
47 setbuf(stdout,NULL);
\r
52 void my_random_init() {
\r
58 int my_random_int(int n) {
\r
64 r = ((int)floor(my_random_double()*((double)n)));
\r
70 // my_random_double()
\r
72 double my_random_double() {
\r
76 r = ((double)rand()) / (((double)RAND_MAX) + 1.0);
\r
77 ASSERT(r>=0.0&&r<1.0);
\r
84 sint64 my_atoll(const char string[]) {
\r
88 sscanf(string,S64_FORMAT,&n);
\r
95 int my_round(double x) {
\r
97 return ((int)floor(x+0.5));
\r
102 void * my_malloc(size_t size) {
\r
108 address = malloc(size);
\r
109 if (address == NULL) my_fatal("my_malloc(): malloc(): %s\n",strerror(errno));
\r
116 void * my_realloc(void * address, size_t size) {
\r
118 ASSERT(address!=NULL);
\r
121 address = realloc(address,size);
\r
122 if (address == NULL) my_fatal("my_realloc(): realloc(): %s\n",strerror(errno));
\r
129 void my_free(void * address) {
\r
131 ASSERT(address!=NULL);
\r
138 void my_log_open(const char file_name[]) {
\r
140 ASSERT(file_name!=NULL);
\r
142 LogFile = fopen(file_name,"a");
\r
144 //line buffering doesn't work too well in MSVC and/or windows
\r
145 if (LogFile != NULL) setvbuf(LogFile,NULL,_IOLBF,0); // line buffering
\r
151 void my_log_close() {
\r
153 if (LogFile != NULL) fclose(LogFile);
\r
159 void my_log(const char format[], ...) {
\r
161 char string[FormatBufferSize];
\r
163 ASSERT(format!=NULL);
\r
167 CONSTRUCT_ARG_STRING(format,string);
\r
170 if (LogFile != NULL) {
\r
171 fprintf(LogFile,"%.3f %s",now_real(),string);
\r
180 void my_fatal(const char format[], ...) {
\r
182 char string[FormatBufferSize];
\r
184 ASSERT(format!=NULL);
\r
188 CONSTRUCT_ARG_STRING(format,string);
\r
190 fprintf(stderr,format,string);
\r
191 if (LogFile != NULL) fprintf(LogFile,format,&string);
\r
193 if (Error) { // recursive error
\r
194 my_log("POLYGLOT *** RECURSIVE ERROR ***\n");
\r
195 exit(EXIT_FAILURE);
\r
203 // my_file_read_line()
\r
205 bool my_file_read_line(FILE * file, char string[], int size) {
\r
210 ASSERT(file!=NULL);
\r
211 ASSERT(string!=NULL);
\r
214 if (fgets(string,size,file) == NULL) {
\r
218 my_fatal("my_file_read_line(): fgets(): %s\n",strerror(errno));
\r
222 // remove CRs and LFs
\r
227 while ((c=string[src++]) != '\0') {
\r
228 if (c != '\r' && c != '\n') string[dst++] = c;
\r
231 string[dst] = '\0';
\r
236 // my_string_empty()
\r
238 bool my_string_empty(const char string[]) {
\r
240 return string == NULL || string[0] == '\0';
\r
243 // my_string_whitespace()
\r
245 bool my_string_whitespace(const char string[]){
\r
247 while(string[pos]!='\0'){
\r
248 if(string[pos]!=' ' && string[pos]!='\t'){
\r
256 // my_string_equal()
\r
258 bool my_string_equal(const char string_1[], const char string_2[]) {
\r
260 ASSERT(string_1!=NULL);
\r
261 ASSERT(string_2!=NULL);
\r
263 return strcmp(string_1,string_2) == 0;
\r
266 // my_string_case_equal()
\r
268 bool my_string_case_equal(const char string_1[], const char string_2[]) {
\r
272 ASSERT(string_1!=NULL);
\r
273 ASSERT(string_2!=NULL);
\r
280 if (tolower(c1) != tolower(c2)) return FALSE;
\r
281 if (c1 == '\0') return TRUE;
\r
289 char * my_strdup(const char string[]) {
\r
293 ASSERT(string!=NULL);
\r
295 // strdup() is not ANSI C
\r
297 address = (char *) my_malloc(strlen(string)+1);
\r
298 strcpy(address,string);
\r
303 // my_string_clear()
\r
305 void my_string_clear(const char * * variable) {
\r
307 ASSERT(variable!=NULL);
\r
309 if (*variable != NULL) {
\r
310 my_free((void*)(*variable));
\r
317 void my_string_set(const char * * variable, const char string[]) {
\r
319 ASSERT(variable!=NULL);
\r
320 ASSERT(string!=NULL);
\r
322 if (*variable != NULL) my_free((void*)(*variable));
\r
323 *variable = my_strdup(string);
\r
326 double now_real() {
\r
328 struct timeval tv[1];
\r
329 struct timezone tz[1];
\r
331 tz->tz_minuteswest = 0;
\r
332 tz->tz_dsttime = 0; // DST_NONE not declared in linux
\r
334 if (gettimeofday(tv,tz) == -1) {
\r
335 my_fatal("now_real(): gettimeofday(): %s\n",strerror(errno));
\r
338 return tv->tv_sec + tv->tv_usec * 1E-6;
\r
340 return (double) GetTickCount() / 1000.0; // we can do better here:-)
\r
345 // my_timer_reset()
\r
347 void my_timer_reset(my_timer_t * timer) {
\r
349 ASSERT(timer!=NULL);
\r
351 timer->start_real = 0.0;
\r
352 timer->elapsed_real = 0.0;
\r
353 timer->running = FALSE;
\r
356 // my_timer_start()
\r
358 void my_timer_start(my_timer_t * timer) {
\r
359 // timer->start_real = 0.0;
\r
360 timer->elapsed_real = 0.0;
\r
361 // timer->running = FALSE;
\r
362 ASSERT(timer!=NULL);
\r
364 timer->running = TRUE;
\r
365 timer->start_real = now_real();
\r
370 void my_timer_stop(my_timer_t * timer) {
\r
372 ASSERT(timer!=NULL);
\r
374 ASSERT(timer->running);
\r
376 timer->elapsed_real += now_real() - timer->start_real;
\r
377 timer->start_real = 0.0;
\r
378 timer->running = FALSE;
\r
381 // my_timer_elapsed_real()
\r
383 double my_timer_elapsed_real(const my_timer_t * timer) {
\r
387 ASSERT(timer!=NULL);
\r
389 elapsed = timer->elapsed_real;
\r
390 if (timer->running) elapsed += now_real() - timer->start_real;
\r
392 if (elapsed < 0.0) elapsed = 0.0;
\r