This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar-2022/src/plugin_manager/plugin_manager_util.cpp

97 lines
2.1 KiB
C++
Raw Normal View History

2022-07-27 18:32:22 +08:00
#include <string.h>
#include "plugin_manager_util.h"
/******************************************************************************
* Malloc
******************************************************************************/
2022-07-27 18:32:22 +08:00
char *safe_dup(const char *str)
{
if (str == NULL)
{
return NULL;
}
char *dup = safe_alloc(char, strlen(str) + 1);
memcpy(dup, str, strlen(str));
return dup;
}
/******************************************************************************
* Session State
******************************************************************************/
struct session_state_map
2022-07-27 18:32:22 +08:00
{
const char *state_str;
enum session_state state_int;
};
2022-07-27 18:32:22 +08:00
static struct session_state_map state_map[] = {
{"SESSION_STATE_INVALID", SESSION_STATE_INVALID},
{"SESSION_STATE_OPENING", SESSION_STATE_OPENING},
{"SESSION_STATE_ACTIVE", SESSION_STATE_ACTIVE},
{"SESSION_STATE_CLOSING", SESSION_STATE_CLOSING},
{"SESSION_STATE_ALL", SESSION_STATE_ALL},
};
enum session_state session_state_str2int(const char *state_str)
{
enum session_state state_int = SESSION_STATE_INVALID;
int num = sizeof(state_map) / sizeof(state_map[0]);
2022-07-27 18:32:22 +08:00
char *buffer = safe_dup(state_str);
char *token = strtok(buffer, "|");
while (token)
2022-07-27 18:32:22 +08:00
{
for (int i = 0; i < num; i++)
{
if (strcmp(token, state_map[i].state_str) == 0)
{
state_int = (enum session_state)(state_int | state_map[i].state_int);
}
}
token = strtok(NULL, "|");
2022-07-27 18:32:22 +08:00
}
safe_free(buffer);
2022-07-27 18:32:22 +08:00
return state_int;
}
void session_state_int2str(enum session_state state_int, char *buffer, int size)
{
int used = 0;
int num = sizeof(state_map) / sizeof(state_map[0]);
if (state_int == SESSION_STATE_INVALID)
2022-07-27 18:32:22 +08:00
{
snprintf(buffer, size, "%s", "SESSION_STATE_INVALID");
return;
2022-07-27 18:32:22 +08:00
}
if (state_int == SESSION_STATE_ALL)
{
snprintf(buffer, size, "%s", "SESSION_STATE_ALL");
return;
}
for (int i = 0; i < num; i++)
{
if (state_map[i].state_int & state_int)
{
if (state_map[i].state_int == SESSION_STATE_ALL && state_int != SESSION_STATE_ALL)
{
continue;
}
used += snprintf(buffer + used, size - used, "%s|", state_map[i].state_str);
}
}
if (used)
{
buffer[used - 1] = '\0';
}
}