GUI-FreeRDP/node_settings.c

213 lines
4.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* node_settings.c
*
* Created on: 6 июл. 2022 г.
* Author: alexander
*/
#include <stdlib.h>
#include <string.h>
#include "node_settings.h"
#include "concat.h"
HeadNode settings = {0, 0, NULL};
static Node* settingsCreateNode()
{
Node *node = (Node*) malloc(sizeof(Node));
node->parameter = PARAMETER_NULL;
node->next = NULL;
node->data = (SettingsData*)malloc(sizeof(SettingsData));
node->data->key = NULL;
node->data->value = NULL;
node->data->fullArgument = NULL;
return node;
}
/*
* Если передан параметр PARAMETER_NULL, то возвращается последний действующий узел.
* Если возврат NULL, то ни одного узла не существует.
* Если передан параметр отличный от PARAMETER_NULL, то ищется существующий узел с этим параметром.
* Если возврат NULL, то узла с таким параметром не найдено.
*/
Node *settingsGetNode(SettingsParameters parameter)
{
if (!settings.countAll)
{
return NULL;
}
Node *head = settings.next;
if (parameter == PARAMETER_NULL)
{
while (head->next)
{
head = head->next;
}
return head;
}
for (; head; head = head->next)
{
if (head->parameter == parameter)
{
return head;
}
}
return NULL;
}
void settingsAddKey(SettingsParameters parameter, char *key, bool isValue, bool enable)
{
Node *newNode = settingsCreateNode();
newNode->parameter = parameter;
newNode->enable = enable;
SettingsData *data = newNode->data;
data->isValue = isValue;
size_t len = strlen(key);
data->key = (char *) malloc(sizeof(char) * (len + 1));
memcpy(data->key, key, len);
Node *lastNode = settingsGetNode(PARAMETER_NULL);
if (lastNode)
{
lastNode->next = newNode;
}
else
{
settings.next = newNode;
}
if (enable)
{
++settings.countEnable;
}
++settings.countAll;
}
void settingsSetValue(SettingsParameters parameter, char *value)
{
Node *currentNode = settingsGetNode(parameter);
if (!currentNode || !currentNode->data->isValue)
{
return;
}
if (!strlen(value))
{
settingsSetEnable(parameter, false);
return;
}
else
{
settingsSetEnable(parameter, true);
}
SettingsData *data = currentNode->data;
if (data->value)
{
free(data->value);
}
if (data->fullArgument)
{
free(data->fullArgument);
}
size_t len = strlen(value);
data->value = (char *) malloc(sizeof(char) * (len + 1));
memcpy(data->value, value, len);
data->fullArgument = concat(data->key, value);
}
void settingsSetEnable(SettingsParameters parameter, bool enable)
{
Node *currentNode = settingsGetNode(parameter);
if (!currentNode)
{
return;
}
if (currentNode->enable && !enable)
{
--settings.countEnable;
}
else if (!currentNode->enable && enable)
{
++settings.countEnable;
}
currentNode->enable = enable;
}
void settingsToggleEnable(SettingsParameters parameter)
{
Node *currentNode = settingsGetNode(parameter);
if (!currentNode)
{
return;
}
if (currentNode->enable)
{
--settings.countEnable;
}
else
{
++settings.countEnable;
}
currentNode->enable = !currentNode->enable;
}
bool settingsGetEnable(SettingsParameters parameter)
{
Node *currentNode = settingsGetNode(parameter);
if (!currentNode)
{
return false;
}
return currentNode->enable;
}
static void settingsFreeData(SettingsData *data)
{
if (data->key)
{
free(data->key);
}
if (data->value)
{
free(data->value);
}
if (data->fullArgument)
{
free(data->fullArgument);
}
free(data);
}
void settingsFree()
{
if (!settings.countAll)
{
return;
}
Node *head = settings.next;
Node *current = NULL;
while (head->next)
{
current = head->next;
settingsFreeData(head->data);
free(head);
head = current;
}
settingsFreeData(head->data);
free(head);
settings.next = NULL;
}