You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libsxmp/sxmp/stream_generic_listops.c

165 lines
4.0 KiB
C

/*
* Secure X Message Passing Library v2 implementation.
* (sxmplv2) it superseed all versions before due to the:
* - memory consumption
* - new features such as pulse emitting
* - performance optimization
*
* (c) Askele Group 2013-2015 <http://askele.com>
* (c) Alexander Vdolainen 2013-2015,2016 <avdolainen@zoho.com>
*
* libsxmp is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsxmp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <tdata/usrtc.h>
#include <tdata/list.h>
#include <sexpr/sexp.h>
#include <sxmp/sxmp.h>
list_head_t *sxstream_generic_slist_alloc(void)
{
list_head_t *list = malloc(sizeof(list_head_t));
if(list) list_init_head(list);
return list;
}
void sxstream_generic_slist_free(list_head_t *list)
{
struct _nn_stream_entry_node *entry;
list_node_t *iter, *siter;
list_for_each_safe(list, iter, siter) {
entry = list_entry(iter, struct _nn_stream_entry_node, node);
list_del(iter);
free(entry->value);
free(entry);
}
free(list);
return;
}
int sxstream_generic_slist_additem(list_head_t *list, const char *value)
{
struct _nn_stream_entry_node *entry = malloc(sizeof(struct _nn_stream_entry_node));
size_t value_len = 0;
if(!entry) return -1;
if(!value) {
__enomem:
free(entry);
return -1;
} else value_len = strlen(value) + 3*sizeof(char); /* additional nil and two underscopes */
if(!(entry->value = malloc(value_len)))
goto __enomem;
else snprintf(entry->value, value_len, "\"%s\"", value);
list_init_node(&entry->node);
list_add2tail(list, &entry->node);
return 0;
}
int sxstream_generic_nmlist_additem(list_head_t *list, const char *name, int order)
{
struct _stream_named_order_node *entry = malloc(sizeof(struct _stream_named_order_node));
size_t name_len = 0;
if(!entry || !list) return -1;
if(!name) {
__enomem:
free(entry);
return -1;
}
/* check up */
if(strchr(name, '"')) goto __enomem;
else name_len = strlen(name) + 2*sizeof(char);
if(!(entry->name = malloc(name_len))) goto __enomem;
else snprintf(entry->name, name_len, ":%s", name);
entry->order = order;
/* add to list */
list_init_node(&entry->node);
list_add2tail(list, &entry->node);
return 0;
}
void sxstream_generic_named_free(list_head_t *list)
{
struct _stream_named_order_node *entry;
list_node_t *iter, *siter;
if(!list) return;
list_for_each_safe(list, iter, siter) {
entry = list_entry(iter, struct _stream_named_order_node, node);
list_del(iter);
free(entry->name);
free(entry);
}
free(list);
return;
}
const char *sxstream_generic_named_lookupname(list_head_t *list, int order)
{
struct _stream_named_order_node *entry;
list_node_t *iter, *siter;
if(!list) return NULL;
list_for_each_safe(list, iter, siter) {
entry = list_entry(iter, struct _stream_named_order_node, node);
if(entry->order == order) return entry->name + sizeof(char);
}
return NULL;
}
int sxstream_generic_named_lookuporder(list_head_t *list, const char *name)
{
struct _stream_named_order_node *entry;
list_node_t *iter, *siter;
if(!list || !name) return -1;
list_for_each_safe(list, iter, siter) {
entry = list_entry(iter, struct _stream_named_order_node, node);
if(!strcmp(entry->name + sizeof(char), name)) return entry->order;
}
return -1;
}