Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- #include <process.h>
- struct menuitem {
- char *name;
- char *exe;
- struct menuitem *next;
- };
- void fill(char far *video, int start, int end, int ch, int color) {
- int i;
- for(i=start;i<end;i+=2) {
- *(video+i) = ch;
- *(video+i+1) = color;
- }
- }
- void fillscreen(char far *video) {
- fill(video, 0, 159, 0, 0);
- fill(video, 160, 3999, 177, 0x0B);
- }
- void rectangle(char far *video, int x1, int y1, int x2, int y2, int ch, int color) {
- int i;
- for(i=y1*160;i<y2*160;i+=160) {
- fill(video, x1*2+i, x2*2+i, ch, color);
- }
- }
- void bar(char far *video, int x1, int y1, int x2, int y2, int ch, int color) {
- rectangle(video, x1, y1, x2, y2, ch, color);
- rectangle(video, x2, y1+1, x2+2, y2, 177, 0x08);
- rectangle(video, x1+2, y2, x2+2, y2+1, 177, 0x08);
- }
- void frame(char far *video, int x1, int y1, int x2, int y2, int color) {
- int topleft, topright, botleft, botright;
- topleft = x1*2+y1*160;
- *(video+topleft) = 218;
- *(video+topleft+1) = color;
- rectangle(video, x1+1,y1, x2-1, y1+1, 196, color);
- topright = (x2-1)*2+y1*160;
- *(video+topright) = 191;
- *(video+topright+1) = color;
- botleft = x1*2+(y2-1)*160;
- *(video+botleft) = 192;
- *(video+botleft+1) = color;
- rectangle(video, x1+1, y2-1, x2-1, y2, 196, color);
- botright = (x2-1)*2+(y2-1)*160;
- *(video+botright) = 217;
- *(video+botright+1) = color;
- rectangle(video, x1, y1+1, x1+1, y2-1, 179, color);
- rectangle(video, x2-1, y1+1, x2, y2-1, 179, color);
- }
- void barframe(char far *video, int x1, int y1, int x2, int y2, int color) {
- bar(video, x1, y1, x2, y2, 32, 0x70);
- frame(video, x1, y1, x2, y2, color);
- rectangle(video, x1+1, y1+1, x2-1, y2-1, 32, 0x1E);
- }
- void echo(char far *video, int x, int y, char *text) {
- int i;
- for(i=0; i<256; i++) {
- if(text[i]==0) break;
- *(video+x*2+y*160+i*2) = text[i];
- }
- }
- void printmenu(char far *video, char ** menulist, int items, int offset) {
- int i, j, k, end;
- end = items - offset;
- if(end>12) end = 12;
- for(i=0; i<end; i++) {
- for(j=0; i<40; j++) {
- if(menulist[offset+i][j]==0) break;
- *(video+22*2+(6+i)*160+j*2) = menulist[offset+i][j];
- }
- for(k=j; k<40; k++) *(video+22*2+(6+i)*160+k*2) = ' ';
- }
- }
- void highlight(char far *video, int y, int color) {
- int i;
- for(i=41; i<122; i+=2) *(video+i+(y+6)*160) = color;
- }
- int strquote(char *dest, char *src, int maxlen) {
- int count = 1;
- if(!dest) return 0;
- while((*src != 0) && maxlen >0) {
- switch(*src) {
- case '\\':
- case '\"':
- case '\'':
- *dest++ = '\\';
- count ++;
- default:
- *dest++ = *src;
- count ++;
- }
- ++src;
- --maxlen;
- }
- if(dest) *dest = 0;
- return count;
- }
- int main(void) {
- char far *video;
- FILE *fp;
- char line[255];
- struct menuitem *headitem = NULL;
- struct menuitem *curitem = NULL;
- char **menulist;
- char **exelist;
- int items=0;
- int offset = 0;
- int hl_item = 0;
- int sel = 0;
- int ch1 = 0;
- int ch2 = 0;
- int i = 0;
- char errst[3];
- _setcursortype(_NOCURSOR);
- video = (char far *)0xB8000000;
- fillscreen(video);
- barframe(video, 14, 0, 66, 3, 0x79);
- barframe(video, 17, 5, 64, 19, 0x7E);
- bar(video, 15, 21, 67, 23, 32, 0x70);
- echo(video, 26, 1, "ГЛАВНОЕ М Е Н Ю ПОЛЬЗОВАТЕЛЯ");
- echo(video, 31, 21, "Выбор нужного раздела:");
- echo(video, 22, 22, "клавишами: ( \x18 ), ( \x19 ) и нажмите Enter");
- fp = fopen("menu.txt", "r");
- if (fp == NULL) {
- perror("Error while opening menu.txt\n");
- exit(EXIT_FAILURE);
- }
- while( fgets(line, 254, fp) != NULL ) {
- char *k, *v;
- char tmpline[255];
- int klen, vlen;
- struct menuitem *tmp;
- tmp = malloc(sizeof(struct menuitem));
- tmp->next = NULL;
- if(!tmp) { printf("malloc error"); exit(1); }
- if(curitem==NULL) { curitem = tmp; headitem = tmp; }
- else { curitem->next = tmp; curitem = tmp; }
- k = strtok(line, "=");
- v = strtok(NULL, "=");
- klen=strlen(k)+1;
- if(klen>40) { klen=40; }
- curitem->name = malloc(klen*sizeof(char));
- if (!curitem->name) { printf("malloc error"); exit(1); }
- strncpy(curitem->name, k, 40);
- if(klen==40) curitem->name[39] = 0;
- vlen=strquote(tmpline, v, 214);
- if(vlen>214) { vlen=214; }
- curitem->exe=malloc(vlen*sizeof(char));
- if (!curitem->exe) { printf("malloc error"); exit(1); }
- strncpy(curitem->exe, tmpline, 214);
- if(vlen==214) curitem->exe[213] = 0;
- items++;
- }
- fclose(fp);
- menulist = malloc(items*sizeof(char *));
- exelist = malloc(items*sizeof(char *));
- curitem = headitem;
- for(i=0;i<items;i++) {
- menulist[i] = curitem->name;
- exelist[i] = curitem->exe;
- curitem = curitem->next;
- }
- printmenu(video, menulist, items, offset);
- highlight(video, sel, 0x4F);
- while (ch1 != 27) {
- ch1 = getch();
- ch2 = 0;
- switch(ch1) {
- case 0xE0:
- case 0:
- ch2 = getch();
- highlight(video, hl_item, 0x1E);
- switch(ch2) {
- case 72: if(sel != 0) {
- sel--;
- if(hl_item != 0) hl_item--;
- else {
- offset--;
- printmenu(video, menulist, items, offset);
- }
- } else {
- sel=items-1;
- if(items<12) { offset=0; hl_item=sel; }
- else {
- offset=items-12; hl_item=11;
- printmenu(video, menulist, items, offset);
- }
- }
- break;
- case 80: if(sel < items-1) {
- sel++;
- if(hl_item < 11) hl_item++;
- else {
- offset++;
- printmenu(video, menulist, items, offset);
- }
- } else {
- sel=0; hl_item=0; offset=0;
- printmenu(video, menulist, items, offset);
- }
- break;
- default:
- break;
- }
- highlight(video, hl_item, 0x4F);
- break;
- case 13:
- case 32:
- _setcursortype(_NORMALCURSOR);
- execlp(exelist[sel], exelist[sel], NULL);
- _setcursortype(_NOCURSOR);
- itoa(errno, errst,10);
- echo(video, 32,24, "ERROR NUMBER: ");
- echo(video, 47,24, errst);
- getch();
- fill(video, 3840, 3999, 177, 0x0B);
- break;
- default:
- //printf("%u", ch1);
- break;
- }
- }
- curitem=headitem;
- while(curitem!=NULL) {
- struct menuitem *tmp;
- tmp = curitem;
- curitem = curitem->next;
- free(tmp->name);
- free(tmp->exe);
- free(tmp);
- }
- free(menulist);
- free(exelist);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement