31 #define StringSize 4096
51 // switch file buffering off
59 void my_random_init() {
65 int my_random_int(int n) {
71 r = ((int)floor(my_random_double()*((double)n)));
79 double my_random_double() {
83 r = ((double)rand()) / (((double)RAND_MAX) + 1.0);
84 ASSERT(r>=0.0&&r<1.0);
91 sint64 my_atoll(const char string[]) {
95 sscanf(string,S64_FORMAT,&n);
102 int my_round(double x) {
104 return ((int)floor(x+0.5));
109 void * my_malloc(size_t size) {
115 address = malloc(size);
116 if (address == NULL) my_fatal("my_malloc(): malloc(): %s\n",strerror(errno));
123 void * my_realloc(void * address, size_t size) {
125 ASSERT(address!=NULL);
128 address = realloc(address,size);
129 if (address == NULL) my_fatal("my_realloc(): realloc(): %s\n",strerror(errno));
136 void my_free(void * address) {
138 ASSERT(address!=NULL);
145 void my_log_open(const char file_name[]) {
147 ASSERT(file_name!=NULL);
149 LogFile = fopen(file_name,"a");
151 //line buffering doesn't work too well in MSVC and/or windows
152 if (LogFile != NULL) setvbuf(LogFile,NULL,_IOLBF,0); // line buffering
155 my_log("POLYGLOT *** LOGFILE OPENED ***\n");
162 void my_log_close() {
164 if (LogFile != NULL) fclose(LogFile);
170 void my_log(const char format[], ...) {
172 char string[FormatBufferSize];
174 ASSERT(format!=NULL);
178 CONSTRUCT_ARG_STRING(format,string);
181 if (LogFile != NULL) {
182 fprintf(LogFile,"%.3f %s",now_real(),string);
191 void my_fatal(const char format[], ...) {
193 char string[FormatBufferSize];
195 ASSERT(format!=NULL);
199 CONSTRUCT_ARG_STRING(format,string);
201 my_log("POLYGLOT %s",string);
202 // This should be gui_send but this does not work.
205 printf("tellusererror POLYGLOT: %s",string);
207 if (Error) { // recursive error
208 my_log("POLYGLOT *** RECURSIVE ERROR ***\n");
217 // my_file_read_line()
219 bool my_file_read_line(FILE * file, char string[], int size) {
225 ASSERT(string!=NULL);
228 if (fgets(string,size,file) == NULL) {
232 my_fatal("my_file_read_line(): fgets(): %s\n",strerror(errno));
236 // remove CRs and LFs
241 while ((c=string[src++]) != '\0') {
242 if (c != '\r' && c != '\n') string[dst++] = c;
252 void my_path_join(char *join_path, const char *path, const char *file){
259 snprintf(join_path,StringSize,"%s%c%s",path,separator,file);
260 join_path[StringSize-1]='\0';
265 int my_mkdir(const char *path){
270 ret=mkdir(path,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
278 bool my_string_empty(const char string[]) {
280 return string == NULL || string[0] == '\0';
283 // my_string_whitespace()
285 bool my_string_whitespace(const char string[]){
287 while(string[pos]!='\0'){
288 if(string[pos]!=' ' && string[pos]!='\t'){
298 bool my_string_equal(const char string_1[], const char string_2[]) {
300 ASSERT(string_1!=NULL);
301 ASSERT(string_2!=NULL);
303 return strcmp(string_1,string_2) == 0;
306 // my_string_case_equal()
308 bool my_string_case_equal(const char string_1[], const char string_2[]) {
312 ASSERT(string_1!=NULL);
313 ASSERT(string_2!=NULL);
320 if (tolower(c1) != tolower(c2)) return FALSE;
321 if (c1 == '\0') return TRUE;
329 void my_string_tolower(char *dst, const char *src){
340 // my_string_case_contains()
342 const char* my_string_case_contains(const char string_1[], const char string_2[]){
344 char tmp1[StringSize];
345 char tmp2[StringSize];
349 ASSERT(string_1!=NULL);
350 ASSERT(string_2!=NULL);
352 my_string_tolower(tmp1,string_1);
353 my_string_tolower(tmp2,string_2);
355 where=strstr(tmp1,tmp2);
357 return string_1+(where-tmp1);
367 char * my_strdup(const char string[]) {
371 ASSERT(string!=NULL);
373 // strdup() is not ANSI C
375 address = (char *) my_malloc(strlen(string)+1);
376 strcpy(address,string);
383 void my_string_clear(const char * * variable) {
385 ASSERT(variable!=NULL);
387 if (*variable != NULL) {
388 my_free((void*)(*variable));
395 void my_string_set(const char * * variable, const char string[]) {
397 ASSERT(variable!=NULL);
398 ASSERT(string!=NULL);
400 if (*variable != NULL) my_free((void*)(*variable));
401 *variable = my_strdup(string);
408 struct timeval tv[1];
409 struct timezone tz[1];
411 tz->tz_minuteswest = 0;
412 tz->tz_dsttime = 0; // DST_NONE not declared in linux
414 if (gettimeofday(tv,tz) == -1) {
415 my_fatal("now_real(): gettimeofday(): %s\n",strerror(errno));
418 return tv->tv_sec + tv->tv_usec * 1E-6;
420 struct _timeb timeptr;
422 return(timeptr.time+((double)timeptr.millitm)/1000.0);
423 // return (double) GetTickCount() / 1000.0; // we can do better here:-)
430 void my_timer_reset(my_timer_t * timer) {
434 timer->start_real = 0.0;
435 timer->elapsed_real = 0.0;
436 timer->running = FALSE;
441 void my_timer_start(my_timer_t * timer) {
442 // timer->start_real = 0.0;
443 timer->elapsed_real = 0.0;
444 // timer->running = FALSE;
447 timer->running = TRUE;
448 timer->start_real = now_real();
453 void my_timer_stop(my_timer_t * timer) {
457 ASSERT(timer->running);
459 timer->elapsed_real += now_real() - timer->start_real;
460 timer->start_real = 0.0;
461 timer->running = FALSE;
464 // my_timer_elapsed_real()
466 double my_timer_elapsed_real(const my_timer_t * timer) {
472 elapsed = timer->elapsed_real;
473 if (timer->running) elapsed += now_real() - timer->start_real;
475 if (elapsed < 0.0) elapsed = 0.0;
481 void my_dequote(char *out, const char *in, const char *special){
488 if(c=='\\' && strchr(special,*p)){
497 void my_quote(char *out, const char *in, const char *special){
503 while(q-out< StringSize-2 && (c=*(p++))){
505 if(*p!=0 && strchr(special,*p)){
508 }else if(strchr(special,c)){
517 void my_sleep(int msec){
521 tm.tv_nsec=1000000*(msec%1000);