/*
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include "at_slist.h"
struct linked_list *e_slist_find_custom(struct linked_list *list,
void *data,
int (*cmpfunc) (const void *a,
const void *b))
{
while (list) {
if (!cmpfunc(list->data, data))
return list;
list = list->next;
}
return NULL;
}
void
e_slist_foreach(struct linked_list *list,
void (*func) (void *a, void *b), void *udata)
{
struct linked_list *next;
while (list) {
next = list->next;
(*func) (list->data, udata);
list = next;
}
}
struct linked_list *e_slist_append(struct linked_list *list, void *data)
{
struct linked_list *new_list, *last;
new_list = malloc(sizeof(struct linked_list));
if (!new_list)
return NULL;
new_list->data = data;
new_list->next = NULL;
if (list) {
last = e_slist_last(list);
last->next = new_list;
return list;
} else
return new_list;
}
struct linked_list *e_slist_last(struct linked_list *list)
{
if (list) {
while (list->next)
list = list->next;
}
return list;
}
struct linked_list *e_slist_free_custom(struct linked_list *list,
void (*func) (void *a))
{
struct linked_list *tmp;
while (list) {
tmp = list;
func(list->data);
list = list->next;
free(tmp);
}
return NULL;
}
struct linked_list *e_slist_delete_link(struct linked_list *list,
struct linked_list *link)
{
struct linked_list *tmp = list;
struct linked_list *prev = NULL;
while (tmp) {
if (tmp == link) {
if (prev)
prev->next = tmp->next;
if (tmp == list)
list = tmp->next;
tmp->next = NULL;
free(link);
return list;
}
prev = tmp;
tmp = tmp->next;
}
free(link);
return list;
}
struct linked_list *e_slist_find(struct linked_list *list, const void *p)
{
while (list) {
if (list->data == p)
return list;
list = list->next;
}
return list;
}
struct linked_list *e_slist_reverse(struct linked_list *list)
{
struct linked_list *prev = NULL, *next;
while (list) {
next = list->next;
list->next = prev;
prev = list;
list = next;
}
return prev;
}