menu (minor): add space around operators for readability

This commit is contained in:
King Kévin 2019-03-26 18:47:17 +01:00
parent f37a6ee4ef
commit a82666e997
1 changed files with 15 additions and 16 deletions

View File

@ -30,11 +30,11 @@
bool menu_handle_command(const char* line, const struct menu_command_t* command_list, size_t command_list_length)
{
// ensure line is not empty
if (!line || 0==strlen(line)) {
if (!line || 0 == strlen(line)) {
return false;
}
// ensure command are available
if (NULL==command_list || 0==command_list_length) {
if (NULL == command_list || 0 == command_list_length) {
return false;
}
@ -49,29 +49,29 @@ bool menu_handle_command(const char* line, const struct menu_command_t* command_
// find corresponding command
bool command_found = false; // remember if we found the corresponding command
for (size_t i=0; i<command_list_length; i++) { // go through available command list
for (size_t i = 0; i < command_list_length; i++) { // go through available command list
struct menu_command_t command = command_list[i]; // get current command
if ((1==strlen(word) && command.shortcut==word[0]) || 0==strcmp(word, command.name)) { // check if shortcut or name match
if ((1==strlen(word) && command.shortcut == word[0]) || 0 == strcmp(word, command.name)) { // check if shortcut or name match
command_found = true; // remember we found the command
if (command.command_handler) { // ensure there is an command handler
if (MENU_ARGUMENT_NONE==command.argument) { // check if argument can be passed
if (MENU_ARGUMENT_NONE == command.argument) { // check if argument can be passed
(*command.command_handler)(NULL); // call without argument
} else { // argument can be passed
const char* original_argument = line+strlen(word)+1; // remember the start of the argument in the original line
const char* original_argument = line + strlen(word)+1; // remember the start of the argument in the original line
word = strtok(NULL, delimiter); // get next word
if (!word) { // no argument provided
(*command.command_handler)(NULL); // call without argument
} else if (MENU_ARGUMENT_SIGNED==command.argument) { // next argument should be a signed integer
} else if (MENU_ARGUMENT_SIGNED == command.argument) { // next argument should be a signed integer
int32_t argument = atoi(word); // get signed integer
(*command.command_handler)(&argument); // call with argument
} else if (MENU_ARGUMENT_UNSIGNED==command.argument) { // next argument should be an unsigned integer
} else if (MENU_ARGUMENT_UNSIGNED == command.argument) { // next argument should be an unsigned integer
uint32_t argument = atoi(word); // get unsigned integer
(*command.command_handler)(&argument); // call with argument
} else if (MENU_ARGUMENT_FLOAT==command.argument) { // next argument should be a floating point number
} else if (MENU_ARGUMENT_FLOAT == command.argument) { // next argument should be a floating point number
double argument = atof(word); // get floating point number
(*command.command_handler)(&argument); // call with argument
} else if (MENU_ARGUMENT_STRING==command.argument) { // next argument should be a string
if (delimiter[0]==original_argument[strlen(word)]) { // if there is more than one word
} else if (MENU_ARGUMENT_STRING == command.argument) { // next argument should be a string
if (delimiter[0] == original_argument[strlen(word)]) { // if there is more than one word
word[strlen(word)] = delimiter[0]; // remove the end of string
}
(*command.command_handler)(word); // call with argument (remaining of the string)
@ -84,9 +84,9 @@ bool menu_handle_command(const char* line, const struct menu_command_t* command_
// find default command
if (!command_found) { // we didn't find the corresponding command
for (size_t i=0; i<command_list_length; i++) { // go through available command list
for (size_t i = 0; i < command_list_length; i++) { // go through available command list
struct menu_command_t command = command_list[i]; // get current command
if (0==command.shortcut && NULL==command.name) { // check if there is a default command
if (0 == command.shortcut && NULL == command.name) { // check if there is a default command
command_found = true; // remember we found the command
if (command.command_handler) { // ensure there is an command handler
(*command.command_handler)(word); // call with current word
@ -102,14 +102,14 @@ bool menu_handle_command(const char* line, const struct menu_command_t* command_
void menu_print_commands(const struct menu_command_t* command_list, size_t command_list_length)
{
for (size_t i=0; i<command_list_length; i++) {
for (size_t i = 0; i < command_list_length; i++) {
struct menu_command_t command = command_list[i];
if (command.name) {
if (command.shortcut) {
printf("%c|", command.shortcut);
}
printf("%s", command.name);
if (MENU_ARGUMENT_NONE!=command.argument && command.argument_description) {
if (MENU_ARGUMENT_NONE != command.argument && command.argument_description) {
printf(" %s", command.argument_description);
} else {
printf("\t");
@ -121,4 +121,3 @@ void menu_print_commands(const struct menu_command_t* command_list, size_t comma
}
}
}