X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=util.c;h=c2dec6b29e1b51a3b16bb04f8c31e541979d7993;hb=60900035e6d0309705f2326ee50edc52386305e9;hp=8551aca7f6c43eb03b6206431447f84723818541;hpb=e15efca6667b2673b4c1a5879a6917eab6800e58;p=polyglot.git diff --git a/util.c b/util.c index 8551aca..c2dec6b 100644 --- a/util.c +++ b/util.c @@ -18,10 +18,17 @@ #include #include #include +#ifndef _MSC_VER #include +#endif #include "main.h" #include "util.h" +#include "gui.h" + +// macros + +#define StringSize 4096 // variables @@ -29,7 +36,6 @@ static bool Error; FILE * LogFile=NULL; - // functions // util_init() @@ -145,6 +151,10 @@ void my_log_open(const char file_name[]) { //line buffering doesn't work too well in MSVC and/or windows if (LogFile != NULL) setvbuf(LogFile,NULL,_IOLBF,0); // line buffering #endif + if(LogFile!=NULL){ + my_log("POLYGLOT *** LOGFILE OPENED ***\n"); + } + } // my_log_close() @@ -158,45 +168,50 @@ void my_log_close() { // my_log() void my_log(const char format[], ...) { + + char string[FormatBufferSize]; + + ASSERT(format!=NULL); - va_list ap; - - ASSERT(format!=NULL); +// format - if (LogFile != NULL) { - fprintf(LogFile,"%.3f ",now_real()); - va_start(ap,format); + CONSTRUCT_ARG_STRING(format,string); + - vfprintf(LogFile,format,ap); - va_end(ap); + if (LogFile != NULL) { + fprintf(LogFile,"%.3f %s",now_real(),string); #ifdef _WIN32 - fflush(LogFile); + fflush(LogFile); #endif - } + } } // my_fatal() void my_fatal(const char format[], ...) { - va_list ap; + char string[FormatBufferSize]; + + ASSERT(format!=NULL); - ASSERT(format!=NULL); +// format - va_start(ap,format); + CONSTRUCT_ARG_STRING(format,string); + + my_log("POLYGLOT %s",string); + // This should be gui_send but this does not work. + // Why? - vfprintf(stderr,format,ap); - if (LogFile != NULL) vfprintf(LogFile,format,ap); + printf("tellusererror POLYGLOT: %s",string); - va_end(ap); - if (Error) { // recursive error - my_log("POLYGLOT *** RECURSIVE ERROR ***\n"); - exit(EXIT_FAILURE); - // abort(); - } else { + if (Error) { // recursive error + my_log("POLYGLOT *** RECURSIVE ERROR ***\n"); + exit(EXIT_FAILURE); + // abort(); + } else { Error = TRUE; quit(); - } + } } // my_file_read_line() @@ -232,6 +247,32 @@ bool my_file_read_line(FILE * file, char string[], int size) { return TRUE; } +// my_file_join() + +void my_path_join(char *join_path, const char *path, const char *file){ + char separator; +#ifdef _WIN32 + separator='\\'; +#else + separator='/'; +#endif + snprintf(join_path,StringSize,"%s%c%s",path,separator,file); + join_path[StringSize-1]='\0'; +} + +// my_mkdir() + +int my_mkdir(const char *path){ + int ret; +#ifdef _WIN32 + ret=_mkdir(path); +#else + ret=mkdir(path,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); +#endif + return ret; +} + + // my_string_empty() bool my_string_empty(const char string[]) { @@ -283,6 +324,44 @@ bool my_string_case_equal(const char string_1[], const char string_2[]) { return FALSE; } +// my_strtolower() + +void my_string_tolower(char *dst, const char *src){ + int c; + ASSERT(src!=NULL); + ASSERT(dst!=NULL); + while((c=*(src++))){ + *dst=tolower(c); + dst++; + } + *(dst++)='\0'; +} + +// my_string_case_contains() + +const char* my_string_case_contains(const char string_1[], const char string_2[]){ + + char tmp1[StringSize]; + char tmp2[StringSize]; + char *where; + + + ASSERT(string_1!=NULL); + ASSERT(string_2!=NULL); + + my_string_tolower(tmp1,string_1); + my_string_tolower(tmp2,string_2); + + where=strstr(tmp1,tmp2); + if(where){ + return string_1+(where-tmp1); + } + return NULL; + + +} + + // my_strdup() char * my_strdup(const char string[]) { @@ -322,6 +401,8 @@ void my_string_set(const char * * variable, const char string[]) { *variable = my_strdup(string); } +// now_real() + double now_real() { #ifndef _WIN32 struct timeval tv[1]; @@ -336,7 +417,10 @@ double now_real() { return tv->tv_sec + tv->tv_usec * 1E-6; #else - return (double) GetTickCount() / 1000.0; // we can do better here:-) + struct _timeb timeptr; + _ftime(&timeptr); + return(timeptr.time+((double)timeptr.millitm)/1000.0); +// return (double) GetTickCount() / 1000.0; // we can do better here:-) #endif } @@ -394,19 +478,49 @@ double my_timer_elapsed_real(const my_timer_t * timer) { } -char * my_getcwd(char *buf, size_t size){ -#ifdef _WIN32 - return _getcwd(buf,size); -#else - return getcwd(buf,size); -#endif +void my_dequote(char *out, const char *in, const char *special){ + const char *p; + char *q; + char c; + p=in; + q=out; + while((c=*(p++))){ + if(c=='\\' && strchr(special,*p)){ + *(q++)=*(p++); + }else{ + *(q++)=c; + } + } + *q='\0'; } -int my_chdir (const char *path){ - ASSERT(path!=NULL); -#ifdef _WIN32 - return _chdir(path); +void my_quote(char *out, const char *in, const char *special){ + const char *p; + char *q; + char c; + p=in; + q=out; + while(q-out< StringSize-2 && (c=*(p++))){ + if(c=='\\'){ + if(*p!=0 && strchr(special,*p)){ + *(q++)='\\'; + } + }else if(strchr(special,c)){ + *(q++)='\\'; + } + *(q++)=c; + } + *q='\0'; +} + + +void my_sleep(int msec){ +#ifndef _WIN32 + struct timespec tm; + tm.tv_sec=msec/1000; + tm.tv_nsec=1000000*(msec%1000); + nanosleep(&tm,NULL); #else - return chdir(path); + Sleep(msec); #endif }