简化key和cmd注册链表

This commit is contained in:
hqm 2022-01-28 16:31:36 +08:00 committed by qmfr
parent e7f59c6a46
commit 6c1a6674cf
1 changed files with 13 additions and 57 deletions

View File

@ -28,16 +28,12 @@ struct
struct struct
{ {
uint16_t len;
xcmd_t *head; xcmd_t *head;
xcmd_t *tail;
}cmd_list; }cmd_list;
struct struct
{ {
uint16_t len;
xcmd_key_t *head; xcmd_key_t *head;
xcmd_key_t *tail;
}key_list; }key_list;
struct struct
@ -551,45 +547,30 @@ int xcmd_exec(char* str)
int xcmd_key_register(xcmd_key_t *keys, uint16_t number) int xcmd_key_register(xcmd_key_t *keys, uint16_t number)
{ {
uint16_t i=0; uint16_t i=0;
if(g_xcmder.key_list.len == 0) xcmd_key_t * temp;
{
g_xcmder.key_list.head = &keys[i++];
g_xcmder.key_list.head->next = NULL;
g_xcmder.key_list.tail = g_xcmder.key_list.head;
++g_xcmder.key_list.len;
}
while(i<number) while(i<number)
{ {
g_xcmder.key_list.tail->next = &keys[i]; temp = g_xcmder.key_list.head;
g_xcmder.key_list.tail = g_xcmder.key_list.tail->next; g_xcmder.key_list.head = &keys[i];
keys[i].next = NULL; keys[i].next = temp;
++g_xcmder.key_list.len;
++i; ++i;
} }
return g_xcmder.key_list.len; return 0;
} }
int xcmd_cmd_register(xcmd_t *cmds, uint16_t number) int xcmd_cmd_register(xcmd_t *cmds, uint16_t number)
{ {
uint16_t i=0; xcmd_t * temp;
if(g_xcmder.cmd_list.len == 0) uint16_t i = 0;
{
g_xcmder.cmd_list.head = &cmds[i++];
g_xcmder.cmd_list.head->next = NULL;
g_xcmder.cmd_list.tail = g_xcmder.cmd_list.head;
++g_xcmder.cmd_list.len;
}
while(i<number) while(i<number)
{ {
g_xcmder.cmd_list.tail->next = &cmds[i]; temp = g_xcmder.cmd_list.head;
g_xcmder.cmd_list.tail = g_xcmder.cmd_list.tail->next; g_xcmder.cmd_list.head = &cmds[i];
cmds[i].next = NULL; cmds[i].next = temp;
++g_xcmder.cmd_list.len;
++i; ++i;
} }
return g_xcmder.cmd_list.len; return 0;
} }
xcmd_key_t *xcmd_keylist_get(void) xcmd_key_t *xcmd_keylist_get(void)
@ -609,20 +590,8 @@ int xcmd_unregister_cmd(char *cmd)
while(p) while(p)
{ {
if(strcmp(cmd, p->name) == 0) if(strcmp(cmd, p->name) == 0)
{
if(g_xcmder.cmd_list.len == 1)
{
g_xcmder.cmd_list.head = g_xcmder.cmd_list.tail = NULL;
}
else
{ {
bk->next = p->next; bk->next = p->next;
if(p->next == NULL)
{
g_xcmder.cmd_list.tail = bk;
}
}
g_xcmder.cmd_list.len--;
return 0; return 0;
} }
bk = p; bk = p;
@ -638,20 +607,8 @@ int xcmd_unregister_key(char *key)
while(p) while(p)
{ {
if(strcmp(key, p->key) == 0) if(strcmp(key, p->key) == 0)
{
if(g_xcmder.key_list.len == 1)
{
g_xcmder.key_list.head = g_xcmder.key_list.tail = NULL;
}
else
{ {
bk->next = p->next; bk->next = p->next;
if(p->next == NULL)
{
g_xcmder.key_list.tail = bk;
}
}
g_xcmder.key_list.len--;
return 0; return 0;
} }
bk = p; bk = p;
@ -689,7 +646,6 @@ void xcmd_init( int (*get_c)(uint8_t*), int (*put_c)(uint8_t))
g_xcmder.parser.byte_num = 0; g_xcmder.parser.byte_num = 0;
g_xcmder.parser.cursor = 0; g_xcmder.parser.cursor = 0;
g_xcmder.parser.encode_case_stu = 0; g_xcmder.parser.encode_case_stu = 0;
g_xcmder.cmd_list.len = 0;
g_xcmder.cmd_list.head = NULL; g_xcmder.cmd_list.head = NULL;
if(g_xcmder._initOK == 0) if(g_xcmder._initOK == 0)