MT#61856 control_ng: add func for command lookup

Instead of the previous approach using the strhash,
introduce if-based lookup from `NG_COMMANDS`.

The usage of strhash is deprecated because it:
- forses string literals in switch cases, hence gives
  no possibility to use variables
- makes then shortcuts using macro not possible to be used
- it makes the part with the command find and
  actual commands definition duplicate, because
  one has to maintain both of them when adding/removing commands
- switch is redundant and feels heavy, meanwhile the same
  thing can be generated using marco based on `NG_COMMANDS`
  introduced before, which are the main source of the truth

Newer approach:
- avoid usage of for/while cycles (because CPU consuming)
- sorts the lookup based on the cmd name (string) length,
  what makes the command find efficient, because exlcludes
  unnecessary comparisons
- uses the `memcmp()` at the very last
- for example, for the "offer" command it may run the `memcmp()`
  only two times, because there are only two commands with
  the length 5 our of supported 35 commands in list now

Additionally:
include `string.h` for `memcmp()` operations.

Change-Id: Iae70de87b88cd444ae162ba2b7c6aa7690731ee3
mr26.1
Donat Zenichev 2 weeks ago
parent 551ba3d82d
commit cc5576476a

@ -16,6 +16,7 @@
#include "statistics.h"
#include "streambuf.h"
#include "homer.h"
#include <string.h>
mutex_t rtpe_cngs_lock;
mutex_t tcp_connections_lock;
@ -55,6 +56,26 @@ static const struct ng_command_def ng_command_defs[OP_COUNT] = {
#undef X
};
static const struct ng_command_def *ng_command_find(const str *cmd) {
if (!cmd || !cmd->s)
return NULL;
#define X(op, name, esc, short_name, handler) \
if (cmd->len == sizeof(name) - 1 && !memcmp(cmd->s, name, sizeof(name) - 1)) \
return &ng_command_defs[op];
#define XA(op, name, esc, short_name, handler) \
if (cmd->len == sizeof(name) - 1 && !memcmp(cmd->s, name, sizeof(name) - 1)) \
return &ng_command_defs[op];
NG_COMMANDS(X, XA)
#undef XA
#undef X
return NULL;
}
const char *ng_command_strings[OP_COUNT] = {
#define X(op, name, esc, short_name, handler) [op] = name,
#define XA(op, name, esc, short_name, handler) [op] = name,

Loading…
Cancel
Save