/* utilities.c Things that really should be in a standard library somewhere */ #include "utilities.h" #include #ifdef BSD void randomise(void) { srandomdev(); } int get_random(void) { return random(); } void dos_pause(void) { } #endif #ifdef WATCOM void randomise(void) { srand(random_seed_from_clock()); } int get_random(void) { int r=rand()*rand(); if (r<0) r=-r; return r; } void dos_pause(void) { press_enter_to_continue(); } #endif int random_in_range(int a, int b) { return a+get_random()%(b+1-a); } int random_seed_from_clock(void) { time_t now; struct tm *tm; now=time(NULL); tm=localtime(&now); return ((((tm->tm_year*366+tm->tm_yday)*24+tm->tm_hour)*60+tm->tm_hour)*60+tm->tm_sec); } float difference(float a, float b) { float d=a-b; if (d<0) return -d; return d; } int run_away=0; void press_enter_to_continue(void) { char s[100]; if (run_away) return; printf("Press enter to continue (or R=run, X=exit): "); fgets(s,100,stdin); if (s[0]=='r' || s[0]=='R') run_away=1; else if (s[0]=='x' || s[0]=='X') exit(1); } char *new_string(char *s) { char *copy=(char *)malloc(strlen(s)+1); strcpy(copy,s); return copy; } char *trim(char *s) { int n=strlen(s)-1; while (n>0 && s[n]<=' ') { s[n]=0; n-=1; } n=0; while (s[n]>0 && s[n]<=' ') n+=1; return s+n; } char capitalise_char(char c) { if (c>='a' && c<='z') return (c-'a'+'A'); return c; } char *read_line(void) { static char line[100]; fgets(line,100,stdin); return line; } int read_integer(void) { char *line; int x; line=trim(read_line()); if (line[0]!=0) sscanf(line,"%d",&x); return x; } int read_integer_with_default(int dflt) { char *line; int x, r=0; line=trim(read_line()); if (line[0]!=0) r=sscanf(line,"%d",&x); if (r!=1) x=dflt; return x; }