在Unix下用C編寫curses程式的一些常用模組(轉)

post0發表於2007-08-11
在Unix下用C編寫curses程式的一些常用模組(轉)[@more@]

總有人問這個問題,下面是整理的模組程式碼,可以作為程式設計的參考demo。

幾個部分可以連線起來編譯程可執行程式執行。

初始化資源

[code:1:b2615b2355]

void initial() /* 自定開啟 curses 函式 */

{

initscr();

cbreak(); nonl(); noecho();

intrflush(stdscr,FALSE);

keypad(stdscr,TRUE);

refresh();

}

[/code:1:b2615b2355]

/* 按鍵等待函式 */

[code:1:b2615b2355]

void keycont()

{

fprintf(stderr, "按鍵繼續..."); getchar();

}

[/code:1:b2615b2355]

/* 執行可執行程式函式 */

[code:1:b2615b2355]

void execprog()

{

system("clear");

fprintf(stderr, "%s: ", scrpos->item);

system(scrpos->prog);

keycont(); initial();

touchwin(boxwin); touchwin(curw); keypad(curw, TRUE);

wrefresh(boxwin); wrefresh(curw);

}

[/code:1:b2615b2355]

/* 清除視窗函式 */

[code:1:b2615b2355]

void clearwin()

{

wmove(boxwin, 0, 0);

wclrtobot(boxwin); wrefresh(boxwin); delwin(curw); delwin(boxwin);

}

[/code:1:b2615b2355]

/* 主函式 */

[code:1:b2615b2355]

main()

{

initial();

getmenuconf(0); /* 取第0號選單引數 */

/* 建立主視窗 */

menuwin=newwin(m_conf.m_lengh, m_conf.m_wight, m_conf.m_bx+1, m_conf.m_by+1);

curw=menuwin; lastw[wno]=menuwin;

getitem(); /* 取當前選單各項內容 */

domenu(head, 0);

endwin();

}

[/code:1:b2615b2355]

/* 取選單各項引數函式 */

[code:1:b2615b2355]

void getitem()

{

FILE *fp;

char buff[0x100];

/* 建邊框視窗 */

boxwin=newwin(m_conf.m_lengh+2,m_conf.m_wight+2,m_conf.m_bx,m_conf.m_by);

keypad(curw, TRUE);

if (m_conf.bord_flag==1) {

box(boxwin, 0,0 );

wrefresh(boxwin);

}

head=NULL;

if ((fp = fopen("./menu.def","r")) == NULL) {

fprintf(stderr, " 不能開啟選單定義檔案 ");

return;

}

while( fgets(buff, 0x100, fp)!=NULL) {

get_m_item(buff);

if (m_item.menu_code != menu_no)

continue;

new=(struct menu*)malloc(sizeof(struct menu));

if (head == NULL) {

last = head; head = new;

}

else {

this->next = new; last = this;

}

this = new;

this->menu_code=m_item.menu_code;

this->item_order=m_item.item_order;

strcpy(this->item,m_item.item);

strcpy(this->prog,m_item.prog);

this->submenu_code=m_item.submenu_code;

this->next=NULL;

this->prev = last;

}

fclose(fp);

}

[/code:1:b2615b2355]

/* 選單處理函式 */

[code:1:b2615b2355]

void domenu(curscrp, curp)

struct menu *curscrp;

int curp;

{

int i, x, y;

struct menu *mpos;

this = head;

disponepage(this);

curpos = curp; scrpos = curscrp;

lastcurpos = lastscrcurpos = 0;

revcurpos();

for(;;) {

switch (wgetch(curw)) {

case ENT:

/* 有下一級選單 */

if ((!strcmp(scrpos->prog, "0")) && (scrpos->submenu_code != 0)) {

lastbegin = begin->next;

getmenuconf(scrpos->submenu_code);

menu_no = scrpos->submenu_code;

wno++;

lastmenucur[wno]=curpos;

lastscr[wno] = scrpos;

lastw[wno]=curw;

workwin=newwin(m_conf.m_lengh,m_conf.m_wight,m_conf.m_bx+1,m_conf.m_by+1);

curw=workwin;

getitem();

domenu(head, 0);

}

/* 是內部函式 */

/* 是外部可執行程式 */

else {

endwin();

execprog();

}

break;

case ESC:

case 'q':

case 'Q':

case '0':

/* 無上級選單 */

if (m_conf.last_code == -1) {

clearwin(); endwin(); exit(0);

}

/* 有上級選單 */

else {

menu_no = m_conf.last_code;

clearwin();

getmenuconf(menu_no);

getitem();

touchwin(lastw[wno]);

curw=lastw[wno];

curpos = lastmenucur[wno];

scrpos = lastscr[wno];

wno--;

wrefresh(curw);

}

break;

case 'r':

case 'R':

case REFRESH: /* 重顯螢幕 */

wrefresh(curscr);

break;

case KEY_RIGHT: /* 右游標鍵 */

if ( scrpos->next != NULL ) {

lastcurpos = curpos; lastscrpos = scrpos;

scrpos=scrpos->next;

getyx(curw, x, y);

if((x==m_conf.m_lengh-1)&&(curpos%m_conf.m_col==m_conf.m_col-1)){

curpos-=(m_conf.m_col-1); lastcurpos = curpos - 1;

/* 實現向上卷屏 */

wmove(curw, 0, 0); wdeleteln(curw); dispnextline("R");

}

else

curpos++;

if ((curpos%m_conf.m_col == 0) && (m_conf.m_lengh == 1)) {

revcurpos(); break;

}

else {

nomlastpos(); revcurpos();

}

}

break;

case KEY_LEFT: /* 左游標鍵 */

if ( scrpos->prev != NULL ) {

lastcurpos = curpos; lastscrpos = scrpos;

scrpos=scrpos->prev;

getyx(curw, x, y);

if ((x==0) && (curpos%m_conf.m_col ==0)) {

curpos+=m_conf.m_col-1; lastcurpos = curpos + 1;

/* 實現向下卷屏 */

winsertln(curw); dispprevline("L");

}

else

curpos--;

if ((curpos%m_conf.m_col==m_conf.m_col-1)&&(m_conf.m_lengh==1)) {

revcurpos(); break;

}

else {

nomlastpos(); revcurpos();

}

}

break;

case KEY_UP: /* 上游標鍵 */

lastcurpos = curpos; lastscrpos = scrpos;

mpos = scrpos;

for(i=0; i

if ( mpos->prev != NULL ) mpos=mpos->prev;

else break;

}

if ( i==m_conf.m_col ) {

getyx(curw, x, y);

if (x==0) {

lastcurpos += m_conf.m_col;

/* 實現向下卷屏 */

winsertln(curw); dispprevline("U");

}

else {

curpos-=m_conf.m_col;

}

scrpos = mpos;

if ( m_conf.m_lengh!=1)

nomlastpos();

revcurpos();

}

break;

case KEY_DOWN: /* 下游標鍵 */

lastcurpos = curpos; lastscrpos = scrpos;

mpos = scrpos;

for(i=0;i

if ( mpos->next != NULL )

mpos=mpos->next;

else

break;

}

if ( i==m_conf.m_col ) {

getyx(curw, x, y);

if (x==m_conf.m_lengh-1) {

lastcurpos -= m_conf.m_col;

/* 實現向上卷屏 */

wmove(curw, 0, 0); wdeleteln(curw); dispnextline("D");

}

else

curpos+=m_conf.m_col;

scrpos = mpos;

if ( m_conf.m_lengh!=1)

nomlastpos();

revcurpos();

}

break;

default:

beep();

break;

}

}

}

[/code:1:b2615b2355]

/* 反顯當前項函式 */

[code:1:b2615b2355]

void revcurpos()

{

wattrset(curw, A_STANDOUT);

wmove(curw, curpos/m_conf.m_col,

(curpos%m_conf.m_col)*m_conf.m_wight/m_conf.m_col+m_conf.m_col);

wprintw(curw, "%s", scrpos->item);

wattrset(curw, A_NORMAL);

wrefresh(boxwin);

}

[/code:1:b2615b2355]

/* 正常顯示上一項函式 */

[code:1:b2615b2355]

void nomlastpos() {

wmove(curw, lastcurpos/m_conf.m_col, (lastcurpos%m_conf.m_col)

*m_conf.m_wight/m_conf.m_col+m_conf.m_col);

wprintw(curw, "%s", lastscrpos->item);

}

[/code:1:b2615b2355]

/* 顯示一頁函式 */

[code:1:b2615b2355]

void disponepage(first)

struct menu *first;

{

short col, row;

begin=first; /* begin 為本頁首指標 */

for(row=0;row

//這兩個地方都不是很清楚,我想這個函式是想列印選單的,所以照這個意思寫

for(col=0;col

/* m_conf.m_wight/m_col為每一選單項應占列數*/

wmove(curw,row,col*m_conf.m_wight/m_conf.m_col+m_conf.m_col);

wprintw(curw, "%s", first->item);

wrefresh(curw);

last = first;

first = first->next;

if (first == NULL) {

break;

}

}

}

}

[/code:1:b2615b2355]

/* 顯示上一行函式 */

[code:1:b2615b2355]

void dispprevline(flag)

char flag[2]; /* L-左游標引起 U-上游標引起 */

{

struct menu *tmppos;

int tmpcurpos;

tmpcurpos = curpos;

tmppos = scrpos;

if ( flag[0] == 'U') {

while ( tmpcurpos % m_conf.m_col != 0) {

tmppos = tmppos->prev;

tmpcurpos--;

}

tmppos = tmppos->prev;

}

for (tmpcurpos = m_conf.m_col-1; tmpcurpos >= 0; tmpcurpos--) {

wmove(curw, 0, (tmpcurpos%m_conf.m_col)

*m_conf.m_wight/m_conf.m_col+m_conf.m_col);

wprintw(curw, "%s", tmppos->item);

begin = tmppos; /*begin 為本頁首指標*/

last = tmppos;

tmppos = tmppos->prev;

if (tmppos == NULL)

break;

}

wrefresh(curw);

}

[/code:1:b2615b2355]

/* 顯示下一行函式 */

[code:1:b2615b2355]

void dispnextline(flag)

char flag[2];/* R-右游標引起 D-下游標引起 */

{

struct menu *tmppos;

int tmpcurpos;

tmpcurpos = curpos;

tmppos = scrpos;

if ( flag[0] == 'D') {

while ( tmpcurpos % m_conf.m_col != m_conf.m_col-1) {

tmppos = tmppos->next; tmpcurpos++;

}

tmppos = tmppos->next;

}

for (tmpcurpos = 0; tmpcurpos < m_conf.m_col; tmpcurpos++) {

wmove(curw, m_conf.m_lengh-1, (tmpcurpos%m_conf.m_col)

*m_conf.m_wight/m_conf.m_col+m_conf.m_col);

wprintw(curw, "%s", tmppos->item);

last=tmppos;/* last 為本頁最後一個結點指標 */

begin=tmppos; tmppos = tmppos->next;

if (tmppos == NULL)

break;

}

}

[/code:1:b2615b2355]

/* 取指定選單引數函式 */

[code:1:b2615b2355]

void getmenuconf(menu_code)

short menu_code;

{

FILE *fp;

char menu_buff[0x100];

if ((fp = fopen("menu.conf", "r"))==NULL) {

fprintf(stderr, "can not open menu config file");

return;

}

while( fgets(menu_buff, 0x100, fp)!=NULL ) {

get_m_conf(menu_buff);

if (m_conf.menu_code == menu_code)

break;

}

return ;

}

[/code:1:b2615b2355]

/* 取指定選單引數處理函式 */

[code:1:b2615b2355]

void get_m_conf(menu_conf)

char *menu_conf;

{

register i, j, k;

char buff[20];

j = k = 0;

for (i = 0; i < strlen(menu_conf); i++) {

if ( menu_conf[i] == '!' ) {

j++;

if ( j == 1) {

k = i+1;

continue;

}

switch(j) {

case 2:

memcpy(buff, &menu_conf[k], i-k);

buff[i-k]=0;

m_conf.menu_code = atoi(buff);

k=i+1;

break;

case 3:

memcpy(buff, &menu_conf[k], i-k);

buff[i-k]=0;

m_conf.last_code = atoi(buff);

k=i+1;

break;

case 4:

memcpy(buff, &menu_conf[k], i-k);

buff[i-k]=0;

m_conf.bord_flag = atoi(buff);

k=i+1;

break;

case 5:

memcpy(buff, &menu_conf[k], i-k);

buff[i-k]=0;

m_conf.m_wight = atoi(buff);

k=i+1;

break;

case 6:

memcpy(buff, &menu_conf[k], i-k);

buff[i-k]=0;

m_conf.m_lengh = atoi(buff);

k=i+1;

break;

case 7:

memcpy(buff, &menu_conf[k], i-k);

buff[i-k]=0;

m_conf.m_col = atoi(buff);

k=i+1;

break;

case 8:

memcpy(buff, &menu_conf[k], i-k);

buff[i-k]=0;

m_conf.m_bx = atoi(buff);

k=i+1;

break;

case 9:

memcpy(buff, &menu_conf[k], i-k);

buff[i-k]=0;

m_conf.m_by = atoi(buff);

k=i+1;

break;

default:

break;

}

}

}

}

[/code:1:b2615b2355]

/* 取指定項引數處理函式 */

[code:1:b2615b2355]

void get_m_item(menu_item)

char *menu_item;

{

register i, j, k;

char buff[80];

j = k = 0;

for (i = 0; i < strlen(menu_item); i++) {

if ( menu_item[i] == '!' ) {

j++;

if ( j == 1) {

k = i+1;

continue;

}

switch(j) {

case 2:

memcpy(buff, &menu_item[k], i-k);

buff[i-k] = 0;

m_item.menu_code = atoi(buff);

k=i+1;

break;

case 3:

memcpy(buff, &menu_item[k], i-k);

buff[i-k] = 0;

m_item.item_order = atoi(buff);

k=i+1;

break;

case 4:

memcpy(buff, &menu_item[k], i-k);

buff[i-k] = 0;

strcpy(m_item.item,buff);

k=i+1;

break;

case 5:

memcpy(buff, &menu_item[k], i-k);

buff[i-k] = 0;

strcpy(m_item.prog,buff);

k=i+1;

break;

case 6:

memcpy(buff, &menu_item[k], i-k);

buff[i-k] = 0;

m_item.submenu_code = atoi(buff);

k=i+1;

break;

default:

break;

}

}

}

}

[/code:1:b2615b2355]

資料結構和標頭檔案等初始化資訊

[code:1:b2615b2355]

#include

#define ESC 27

#define ENT 13

#define REFRESH 12

#define MAX_M 10 /* 選單最大層數 */

void initial(),nomlastpos(),revcurpos(),disponepage(),dispprevline();

void dispnextline(),domenu(),getmenuconf(),keycont();

void getitem(), get_m_conf(), get_m_item(),clearwin(),execprog();

/* 標識每一選單項的結構 */

struct menu {

short menu_code; /* 所屬選單代號 */

short item_order; /* 項順序號 */

char item[20]; /* 選單項名稱 */

char prog[80]; /* 本項選單執行程式 */

short submenu_code; /* 下一級選單編號 */

struct menu *next; /* 指向上一項的指標 */

struct menu *prev; /* 指向下一項的指標 */

} m_item,*head,*this,*new,*last,*scrpos,*lastscrpos,*begin,*lastbegin,*lastscr[MAX_M];

/* 標識每一選單內容的結構 */

struct menuconf {

short menu_code; /* 選單代號 */

short last_code; /* 上一級選單代號 */

short bord_flag; /* 邊框標誌 0--無邊框 1--有邊框 **/

short m_wight; /* 選單顯示寬度 */

short m_lengh; /* 每一行項數 */

short m_col; /* 選單列數 */

short m_bx; /* 選單起始橫座標 */

short m_by; /* 選單起始縱座標 */

} m_conf;

WINDOW *menuwin, *boxwin, *curw, *lastw[MAX_M], *workwin;

long curpos, lastcurpos, lastscrcurpos, lastmenucur[MAX_M];

short menu_no = 0, wno = 0;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-944576/,如需轉載,請註明出處,否則將追究法律責任。

相關文章