/* Copyright 1996 Acorn Computers Ltd
 *
 * 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 "slist.h"
#include "mem.h"

void *slist_add_new(void **_head,int size,char *tag)
{

   SList *newl,**head = (SList **)_head;

   (void) tag;
   newl = mem_allocate(size,tag);
   if (newl) {
      newl->next = *head;
      *head = newl;
   }
   return newl;
}

void slist_delete_member(void **_head,void *member)
{

   SList *prev=NULL,*list = (SList *) (*_head),*tst = (SList *)member;

   while(list) {
       if (list == tst) {
          if (prev) prev->next = list->next;
          else *_head = (void *) (list->next);
          mem_free(list,"Freeing list member");
          return;

       }

       prev =list;
       list = list->next;
   }
}

void *slist_find_member(void **_head,int field, int value)
{

   SList *list = (SList *) (*_head);

   while(list) {
       if (list->members[field] == value) break;

       list = list->next;
   }
   return list;
}

void *slist_find_matched_member(void **_head,int (*func)(void *list,int value), int value)
{

   SList *list = (SList *) (*_head);

   while(list) {
       if (func(list, value)) break;

       list = list->next;
   }
   return list;
}

void slist_delete_and_call(void **_head,void (*func)(void * d))
{

   SList *next,*list = (SList *) (*_head);

   while(list) {
      func(list);
      next = list->next;
      mem_free(list,"Freeing list member");
      list = next;
   }
   *_head = NULL;
}