#include #include #include #include #include #include #include #include "ml.h" #include "kbd_keys.h" #define MAX_SCR 16 struct text_info __old_sett[MAX_SCR]; int __scr=0; void error(char *msg) { extern void exit(int); printf("%s.\nProgram aborted.\n",msg); exit(1); } FILE *Fopen(char *name, char *mode) { FILE *F; char str[100]; extern int errno; if((F=fopen(name,mode)) == NULL) { sprintf(str,"can't open file %s:%s",name,strerror(errno)); error(str); } return F; } void check_argc(int argc, int need, char *msg) { if(argc != need) error(msg); } void beep(void) { /* sound(800); delay(200); nosound(); */ putch(7); } char *substr(char *str, int len) { static char res[80], *p; for(p=res; len && *str ; len--) *(p++)=*(str++); *p='\0'; return res; } void print_arr(char far *p0, unsigned q) { int k; unsigned far *p=(unsigned far *)p0; printf("Pointer =%Fp\n",p); for(k=0; q ; k++,q--) printf("w[%3.3d]=0x%Np; >%c:%c.\n",k,*p++, *(char*)p,*(((char*)p)+1) ); printf("--- Press any key -----\n"); getch(); } char *repeatstr(int q, char *str) { static char buf[255],*strp,*bufp; for(bufp=buf; q ; q--) for(strp=str; *strp; )*bufp++=*strp++; *bufp='\0'; return buf; } double fmin(double a, double b) { return a < b ? a : b; } double sqr(double a) { return a*a; } double fmax(double x, double y) { return x > y ? x : y; } char *fillchar(char *s,int len, char ch) { char *p=s; while(len--) *p++=ch; *p='\0'; return s; } int eqstr(char *s1, char *s2) /* 1 если s1 префикс s2 */ { for( ; *s1 ; ) if(*s1++ != *s2++) return 0; return 1; } void addspaces(char *buf, int n) { for( ; *buf && n; n--, buf++); for( ; n ; n--) *buf++=' '; *buf='\0'; } void getdig(char *p, char *p1, int shift) { if(*p >= '0' && *p <= '9') *p1 = *p1 | ((*p-'0') << shift); else if(*p >= 'A' && *p <= 'F') *p1 = *p1 | ((*p-'A'+10) << shift); else error("invalid character"); } void gethex(char *s, char *r, int lmax) { char *p,*p1; strupr(s); for(p=s, p1=r; *p && lmax; p1++,lmax--){ *p1=0x00; getdig(p,p1,4); p++; if(*p) { getdig(p,p1,0); p++; } } *p1='\0'; } int in_str(unsigned ch, char *p) { int i=1; while(*p) if(*p++ == ch) return i; else i++; return 0; } void store_scr_state(void) { if(__scr >= MAX_SCR) error("error calling set_scr_state"); gettextinfo(__old_sett+__scr++); } void set_scr_state(struct text_info *ti) { textattr(ti->attribute); gotoxy(ti->curx,ti->cury); window(ti->winleft,ti->wintop,ti->winright,ti->winbottom); gotoxy(ti->curx,ti->cury); } void restore_scr_state(void) { struct text_info *ti; if(__scr <= 0) error("error calling restore_scr_state"); ti=(__old_sett+ --__scr); set_scr_state(ti); } void store_scr(char **scr_mem) { if((*scr_mem=malloc(4000))==NULL) return; movmem(MK_FP(0xB800,0x0000),*scr_mem,4000); } void restore_scr(char **scr_mem) { movmem(*scr_mem,MK_FP(0xB800,0x0000),4000); free(*scr_mem); *scr_mem=NULL; } void *Malloc(size_t size, char *msg) { void *p; if(!(p=malloc(size))) if(msg==NULL) error("No memory"); else error(msg); return p; } int int2color(int i) { static int colors[]= { 8,7,3,2,1,5,6,4,9,10,13,12,11,14,15,0,0,0,0 }; return colors[i]; } static clock_t my_time; void init_my_timer(void) /* Устанавливает начало отсчета времени */ { my_time=clock(); } double get_timer(void) { return (clock()-my_time)/CLK_TCK; }