21 #include <sys/time.h>
\r
30 FILE * LogFile=NULL;
\r
45 // switch file buffering off
\r
48 setbuf(stdout,NULL);
\r
53 void my_random_init() {
\r
59 int my_random_int(int n) {
\r
65 r = ((int)floor(my_random_double()*((double)n)));
\r
71 // my_random_double()
\r
73 double my_random_double() {
\r
77 r = ((double)rand()) / (((double)RAND_MAX) + 1.0);
\r
78 ASSERT(r>=0.0&&r<1.0);
\r
85 sint64 my_atoll(const char string[]) {
\r
89 sscanf(string,S64_FORMAT,&n);
\r
96 int my_round(double x) {
\r
98 return ((int)floor(x+0.5));
\r
103 void * my_malloc(size_t size) {
\r
109 address = malloc(size);
\r
110 if (address == NULL) my_fatal("my_malloc(): malloc(): %s\n",strerror(errno));
\r
117 void * my_realloc(void * address, size_t size) {
\r
119 ASSERT(address!=NULL);
\r
122 address = realloc(address,size);
\r
123 if (address == NULL) my_fatal("my_realloc(): realloc(): %s\n",strerror(errno));
\r
130 void my_free(void * address) {
\r
132 ASSERT(address!=NULL);
\r
139 void my_log_open(const char file_name[]) {
\r
141 ASSERT(file_name!=NULL);
\r
143 LogFile = fopen(file_name,"a");
\r
145 //line buffering doesn't work too well in MSVC and/or windows
\r
146 if (LogFile != NULL) setvbuf(LogFile,NULL,_IOLBF,0); // line buffering
\r
152 void my_log_close() {
\r
154 if (LogFile != NULL) fclose(LogFile);
\r
160 void my_log(const char format[], ...) {
\r
164 ASSERT(format!=NULL);
\r
166 if (LogFile != NULL) {
\r
167 fprintf(LogFile,"%.3f ",now_real());
\r
168 va_start(ap,format);
\r
170 vfprintf(LogFile,format,ap);
\r
180 void my_fatal(const char format[], ...) {
\r
184 ASSERT(format!=NULL);
\r
186 va_start(ap,format);
\r
188 vfprintf(stderr,format,ap);
\r
189 if (LogFile != NULL) vfprintf(LogFile,format,ap);
\r
192 if (Error) { // recursive error
\r
193 my_log("POLYGLOT *** RECURSIVE ERROR ***\n");
\r
194 exit(EXIT_FAILURE);
\r
202 // my_file_read_line()
\r
204 bool my_file_read_line(FILE * file, char string[], int size) {
\r
209 ASSERT(file!=NULL);
\r
210 ASSERT(string!=NULL);
\r
213 if (fgets(string,size,file) == NULL) {
\r
217 my_fatal("my_file_read_line(): fgets(): %s\n",strerror(errno));
\r
221 // remove CRs and LFs
\r
226 while ((c=string[src++]) != '\0') {
\r
227 if (c != '\r' && c != '\n') string[dst++] = c;
\r
230 string[dst] = '\0';
\r
235 // my_string_empty()
\r
237 bool my_string_empty(const char string[]) {
\r
239 return string == NULL || string[0] == '\0';
\r
242 // my_string_whitespace()
\r
244 bool my_string_whitespace(const char string[]){
\r
246 while(string[pos]!='\0'){
\r
247 if(string[pos]!=' ' && string[pos]!='\t'){
\r
255 // my_string_equal()
\r
257 bool my_string_equal(const char string_1[], const char string_2[]) {
\r
259 ASSERT(string_1!=NULL);
\r
260 ASSERT(string_2!=NULL);
\r
262 return strcmp(string_1,string_2) == 0;
\r
265 // my_string_case_equal()
\r
267 bool my_string_case_equal(const char string_1[], const char string_2[]) {
\r
271 ASSERT(string_1!=NULL);
\r
272 ASSERT(string_2!=NULL);
\r
279 if (tolower(c1) != tolower(c2)) return FALSE;
\r
280 if (c1 == '\0') return TRUE;
\r
288 char * my_strdup(const char string[]) {
\r
292 ASSERT(string!=NULL);
\r
294 // strdup() is not ANSI C
\r
296 address = (char *) my_malloc(strlen(string)+1);
\r
297 strcpy(address,string);
\r
302 // my_string_clear()
\r
304 void my_string_clear(const char * * variable) {
\r
306 ASSERT(variable!=NULL);
\r
308 if (*variable != NULL) {
\r
309 my_free((void*)(*variable));
\r
316 void my_string_set(const char * * variable, const char string[]) {
\r
318 ASSERT(variable!=NULL);
\r
319 ASSERT(string!=NULL);
\r
321 if (*variable != NULL) my_free((void*)(*variable));
\r
322 *variable = my_strdup(string);
\r
325 double now_real() {
\r
327 struct timeval tv[1];
\r
328 struct timezone tz[1];
\r
330 tz->tz_minuteswest = 0;
\r
331 tz->tz_dsttime = 0; // DST_NONE not declared in linux
\r
333 if (gettimeofday(tv,tz) == -1) {
\r
334 my_fatal("now_real(): gettimeofday(): %s\n",strerror(errno));
\r
337 return tv->tv_sec + tv->tv_usec * 1E-6;
\r
339 return (double) GetTickCount() / 1000.0; // we can do better here:-)
\r
344 // my_timer_reset()
\r
346 void my_timer_reset(my_timer_t * timer) {
\r
348 ASSERT(timer!=NULL);
\r
350 timer->start_real = 0.0;
\r
351 timer->elapsed_real = 0.0;
\r
352 timer->running = FALSE;
\r
355 // my_timer_start()
\r
357 void my_timer_start(my_timer_t * timer) {
\r
358 // timer->start_real = 0.0;
\r
359 timer->elapsed_real = 0.0;
\r
360 // timer->running = FALSE;
\r
361 ASSERT(timer!=NULL);
\r
363 timer->running = TRUE;
\r
364 timer->start_real = now_real();
\r
369 void my_timer_stop(my_timer_t * timer) {
\r
371 ASSERT(timer!=NULL);
\r
373 ASSERT(timer->running);
\r
375 timer->elapsed_real += now_real() - timer->start_real;
\r
376 timer->start_real = 0.0;
\r
377 timer->running = FALSE;
\r
380 // my_timer_elapsed_real()
\r
382 double my_timer_elapsed_real(const my_timer_t * timer) {
\r
386 ASSERT(timer!=NULL);
\r
388 elapsed = timer->elapsed_real;
\r
389 if (timer->running) elapsed += now_real() - timer->start_real;
\r
391 if (elapsed < 0.0) elapsed = 0.0;
\r
397 char * my_getcwd(char *buf, size_t size){
\r
399 return _getcwd(buf,size);
\r
401 return getcwd(buf,size);
\r
405 int my_chdir (const char *path){
\r
406 ASSERT(path!=NULL);
\r
408 return _chdir(path);
\r
410 return chdir(path);
\r