main() { head = CreateLinkTable(); AddLinkTableNode(head,(tLinkTableNode *)&data[0]); AddLinkTableNode(head,(tLinkTableNode *)&data[1]); /* cmd line begins */ while(1) { char cmd[CMD_MAX_LEN]; printf("Input a cmd number > "); scanf("%s", cmd); tDataNode *p = FindCmd(head, cmd); if( p == NULL) { printf("This is a wrong cmd!\n "); continue; } printf("%s - %s\n", p->cmd, p->desc); if(p->handler != NULL) { p->handler(); } } }
/* * run add cmd */ int Add(tLinkTable *pLinkTable) { char AddCmd[CMD_MAX_LEN]; char AddDesc[DESC_LEN]; if(pLinkTable == NULL) { return FAILURE; } tDataNode * pAdd = (tDataNode *)malloc(sizeof(tDataNode)); tDataNode * pTempNode; printf("Plase input AddCmd name > "); scanf("%s", AddCmd); pTempNode = FindCmd(pLinkTable, AddCmd); if(pTempNode != NULL) { printf("Cmd is existed!\n"); return FAILURE; } strcpy(pAdd->cmd, AddCmd); printf("Plase input cmd desc >"); getchar(); scanf("%[^\n]", AddDesc); strcpy(pAdd->desc, AddDesc); pAdd->pNext = NULL; pAdd->hander = NULL; AddLinkTableNode(pLinkTable,(tLinkTableNode *)pAdd); return SUCCESS; }
/* add commands into the head */ int AddCmd(tLinkTable *head, const char * cmd, const char * desc,int (*handler)(tLinkTable * handler), int * enable) { if(head != NULL && cmd != NULL) { if(FindCmd(head, (char*)cmd)==NULL) { tDataNode * pNode = (tDataNode*)malloc(sizeof(tDataNode)); pNode->pNext=NULL; pNode->cmd=cmd; pNode->desc=desc; pNode->handler=handler; pNode->enable=ENABLE; AddLinkTableNode(head,(tLinkTableNode *)pNode); return SUCCESS; } else { printf("command is exist!\n"); return FAILURE; } } else { return FAILURE; } }
int AddMenuData(tLinkTable ** head, char s1[], char s2[], int (* handler)()) { tDataNode * pNode = (tDataNode *)malloc(sizeof(tDataNode)); pNode->cmd = s1; pNode->desc = s2; pNode->handler = handler; AddLinkTableNode(* head, (tLinkTableNode *)pNode); return 0; }
tLinkTable * InitCmd() { tLinkTable * pLinkTable = CreateLinkTable(); int i; for(i = 0; i < CMD_NUM; i++) { AddLinkTableNode(pLinkTable,(tLinkTableNode *)&data[i]); } return pLinkTable; }
/* Add MenuItem to Menu */ int AddMenuItem(tLinkTable* head, char* cmdname, char* cmddesc, int (*function)()) { tMenuItem* ptMenuItem = (tMenuItem*) malloc(sizeof(tMenuItem)); ptMenuItem->cmd = (char*)malloc(sizeof(char)*CMD_LENGTH); ptMenuItem->desc = (char*)malloc(sizeof(char)*DESCRIPTION_LENGTH); strcpy(ptMenuItem->cmd,cmdname); strcpy(ptMenuItem->desc,cmddesc); ptMenuItem->handler = function; AddLinkTableNode(head,(tLinkTableNode *)ptMenuItem); return 0; }
int InitMenuData(tLinkTable ** ppLinktable) { *ppLinktable = CreateLinkTable(); tDataNode* pNode = (tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd = "help"; pNode->desc = "Menu List:"; pNode->handler = Help; AddLinkTableNode(*ppLinktable,(tLinkTableNode *)pNode); pNode = (tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd = "version"; pNode->desc = "Menu Program V1.0"; pNode->handler = NULL; AddLinkTableNode(*ppLinktable,(tLinkTableNode *)pNode); pNode = (tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd = "quit"; pNode->desc = "Quit from Menu Program V1.0"; pNode->handler = Quit; AddLinkTableNode(*ppLinktable,(tLinkTableNode *)pNode); return 0; }
/* initialize the Command */ int InitCmd(tLinkTable* head,tDataNode data[]) { int i; i = 0; for(; i<CMD_MAX_LEN; i++) { if(data[i].cmd != NULL) { AddLinkTableNode(head,(tLinkTableNode *)&data[i]); } else { break; } } return 0; }
int main() { int i; tLinkTable * pLinkTable = CreateLinkTable(); for(i = 0; i < 10; i++) { tNode* pNode = (tNode*)malloc(sizeof(tNode)); pNode->data = i; debug("AddLinkTableNode\n"); AddLinkTableNode(pLinkTable,(tLinkTableNode *)pNode); } /* search by callback */ debug("SearchLinkTableNode\n"); tNode* pTempNode = (tNode*)SearchLinkTableNode(pLinkTable,SearchConditon); printf("%d\n",pTempNode->data); /* search one by one */ pTempNode = Search(pLinkTable); printf("%d\n",pTempNode->data); debug("DelLinkTableNode\n"); DelLinkTableNode(pLinkTable,(tLinkTableNode *)pTempNode); free(pTempNode); DeleteLinkTable(pLinkTable); }
/* Add cmd to linklist */ int AddCmd(char *cmd, char *desc, int handler(char *)) { /* Check arguments */ if(head == NULL) { head = CreateLinkTable(); } if(head == NULL || cmd == NULL || desc == NULL || handler == NULL) { return FAILURE; } /* Check if the cmd is existence */ if(CheckCmdExist(cmd) == SUCCESS) { return FAILURE; } tDataNode* pNode = (tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd = cmd; pNode->desc = desc; pNode->handler = handler; return AddLinkTableNode(head,(tLinkTableNode *)pNode); }