初始化版本
This commit is contained in:
50
src/core/menu_stack.c
Normal file
50
src/core/menu_stack.c
Normal file
@ -0,0 +1,50 @@
|
||||
#include "menu_stack.h"
|
||||
#include <string.h>
|
||||
|
||||
MenuErrCode menu_stack_init(MenuStack* stack) {
|
||||
if (!stack) return MENU_ERR_INVALID_PARAM;
|
||||
memset(stack, 0, sizeof(MenuStack));
|
||||
stack->top = 0;
|
||||
return MENU_ERR_OK;
|
||||
}
|
||||
|
||||
MenuErrCode menu_stack_push(MenuStack* stack, MenuNodeId id) {
|
||||
if (!stack) return MENU_ERR_INVALID_PARAM;
|
||||
if (stack->top >= MENU_CONFIG_STACK_DEPTH) {
|
||||
return MENU_ERR_STACK_OVERFLOW;
|
||||
}
|
||||
stack->path[stack->top++] = id;
|
||||
return MENU_ERR_OK;
|
||||
}
|
||||
|
||||
MenuErrCode menu_stack_pop(MenuStack* stack, MenuNodeId* id) {
|
||||
if (!stack) return MENU_ERR_INVALID_PARAM;
|
||||
if (stack->top == 0) {
|
||||
return MENU_ERR_STACK_UNDERFLOW;
|
||||
}
|
||||
if (id) {
|
||||
*id = stack->path[--stack->top];
|
||||
} else {
|
||||
stack->top--;
|
||||
}
|
||||
return MENU_ERR_OK;
|
||||
}
|
||||
|
||||
MenuErrCode menu_stack_peek(const MenuStack* stack, MenuNodeId* id) {
|
||||
if (!stack || !id) return MENU_ERR_INVALID_PARAM;
|
||||
if (stack->top == 0) {
|
||||
return MENU_ERR_STACK_UNDERFLOW;
|
||||
}
|
||||
*id = stack->path[stack->top - 1];
|
||||
return MENU_ERR_OK;
|
||||
}
|
||||
|
||||
bool menu_stack_is_empty(const MenuStack* stack) {
|
||||
return (stack == NULL) || (stack->top == 0);
|
||||
}
|
||||
|
||||
void menu_stack_clear(MenuStack* stack) {
|
||||
if (stack) {
|
||||
stack->top = 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user