mirror of
https://github.com/owntone/owntone-server.git
synced 2025-02-09 12:48:09 -05:00
Add key/value functions & data types
This commit is contained in:
parent
aa57931879
commit
33d51f0652
133
src/misc.c
133
src/misc.c
@ -256,6 +256,139 @@ safe_hextou64(const char *str, uint64_t *val)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Key/value functions */
|
||||||
|
int
|
||||||
|
keyval_add_size(struct keyval *kv, const char *name, const char *value, size_t size)
|
||||||
|
{
|
||||||
|
struct onekeyval *okv;
|
||||||
|
const char *val;
|
||||||
|
|
||||||
|
/* Check for duplicate key names */
|
||||||
|
val = keyval_get(kv, name);
|
||||||
|
if (val)
|
||||||
|
{
|
||||||
|
/* Same value, fine */
|
||||||
|
if (strcmp(val, value) == 0)
|
||||||
|
return 0;
|
||||||
|
else /* Different value, bad */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
okv = (struct onekeyval *)malloc(sizeof(struct onekeyval));
|
||||||
|
if (!okv)
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_MISC, "Out of memory for new keyval\n");
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
okv->name = strdup(name);
|
||||||
|
if (!okv->name)
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_MISC, "Out of memory for new keyval name\n");
|
||||||
|
|
||||||
|
free(okv);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
okv->value = (char *)malloc(size + 1);
|
||||||
|
if (!okv->value)
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_MISC, "Out of memory for new keyval value\n");
|
||||||
|
|
||||||
|
free(okv->name);
|
||||||
|
free(okv);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(okv->value, value, size);
|
||||||
|
okv->value[size] = '\0';
|
||||||
|
|
||||||
|
okv->next = NULL;
|
||||||
|
|
||||||
|
if (!kv->head)
|
||||||
|
kv->head = okv;
|
||||||
|
|
||||||
|
if (kv->tail)
|
||||||
|
kv->tail->next = okv;
|
||||||
|
|
||||||
|
kv->tail = okv;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
keyval_add(struct keyval *kv, const char *name, const char *value)
|
||||||
|
{
|
||||||
|
return keyval_add_size(kv, name, value, strlen(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
keyval_remove(struct keyval *kv, const char *name)
|
||||||
|
{
|
||||||
|
struct onekeyval *okv;
|
||||||
|
struct onekeyval *pokv;
|
||||||
|
|
||||||
|
for (pokv = NULL, okv = kv->head; okv; pokv = okv, okv = okv->next)
|
||||||
|
{
|
||||||
|
if (strcasecmp(okv->name, name) == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!okv)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (okv == kv->head)
|
||||||
|
kv->head = okv->next;
|
||||||
|
|
||||||
|
if (okv == kv->tail)
|
||||||
|
kv->tail = pokv;
|
||||||
|
|
||||||
|
if (pokv)
|
||||||
|
pokv->next = okv->next;
|
||||||
|
|
||||||
|
free(okv->name);
|
||||||
|
free(okv->value);
|
||||||
|
free(okv);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *
|
||||||
|
keyval_get(struct keyval *kv, const char *name)
|
||||||
|
{
|
||||||
|
struct onekeyval *okv;
|
||||||
|
|
||||||
|
for (okv = kv->head; okv; okv = okv->next)
|
||||||
|
{
|
||||||
|
if (strcasecmp(okv->name, name) == 0)
|
||||||
|
return okv->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
keyval_clear(struct keyval *kv)
|
||||||
|
{
|
||||||
|
struct onekeyval *hokv;
|
||||||
|
struct onekeyval *okv;
|
||||||
|
|
||||||
|
hokv = kv->head;
|
||||||
|
|
||||||
|
for (okv = hokv; hokv; okv = hokv)
|
||||||
|
{
|
||||||
|
hokv = okv->next;
|
||||||
|
|
||||||
|
free(okv->name);
|
||||||
|
free(okv->value);
|
||||||
|
free(okv);
|
||||||
|
}
|
||||||
|
|
||||||
|
kv->head = NULL;
|
||||||
|
kv->tail = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
char *
|
char *
|
||||||
m_realpath(const char *pathname)
|
m_realpath(const char *pathname)
|
||||||
{
|
{
|
||||||
|
32
src/misc.h
32
src/misc.h
@ -4,6 +4,20 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
struct onekeyval {
|
||||||
|
char *name;
|
||||||
|
char *value;
|
||||||
|
|
||||||
|
struct onekeyval *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct keyval {
|
||||||
|
struct onekeyval *head;
|
||||||
|
struct onekeyval *tail;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
safe_atoi32(const char *str, int32_t *val);
|
safe_atoi32(const char *str, int32_t *val);
|
||||||
|
|
||||||
@ -22,6 +36,24 @@ safe_atou64(const char *str, uint64_t *val);
|
|||||||
int
|
int
|
||||||
safe_hextou64(const char *str, uint64_t *val);
|
safe_hextou64(const char *str, uint64_t *val);
|
||||||
|
|
||||||
|
|
||||||
|
/* Key/value functions */
|
||||||
|
int
|
||||||
|
keyval_add(struct keyval *kv, const char *name, const char *value);
|
||||||
|
|
||||||
|
int
|
||||||
|
keyval_add_size(struct keyval *kv, const char *name, const char *value, size_t size);
|
||||||
|
|
||||||
|
void
|
||||||
|
keyval_remove(struct keyval *kv, const char *name);
|
||||||
|
|
||||||
|
const char *
|
||||||
|
keyval_get(struct keyval *kv, const char *name);
|
||||||
|
|
||||||
|
void
|
||||||
|
keyval_clear(struct keyval *kv);
|
||||||
|
|
||||||
|
|
||||||
char *
|
char *
|
||||||
m_realpath(const char *pathname);
|
m_realpath(const char *pathname);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user