/*****************************************************************************1*2* Elmer, A Finite Element Software for Multiphysical Problems3*4* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland5*6* This library is free software; you can redistribute it and/or7* modify it under the terms of the GNU Lesser General Public8* License as published by the Free Software Foundation; either9* version 2.1 of the License, or (at your option) any later version.10*11* This library is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14* Lesser General Public License for more details.15*16* You should have received a copy of the GNU Lesser General Public17* License along with this library (in file ../LGPL-2.1); if not, write18* to the Free Software Foundation, Inc., 51 Franklin Street,19* Fifth Floor, Boston, MA 02110-1301 USA20*21*****************************************************************************/2223/*******************************************************************************24*25* List handling utilities.26*27*******************************************************************************28*29* Author: Juha Ruokolainen30*31* Address: CSC - IT Center for Science Ltd.32* Keilaranta 14, P.O. BOX 40533* 02101 Espoo, Finland34* Tel. +358 0 457 272335* Telefax: +358 0 457 230236* EMail: [email protected]37*38* Date: 30 May 199639*40* Modified by:41*42* Date of modification:43*44******************************************************************************/45/***********************************************************************46|47| LISTS.C - Last Edited 7. 8. 198848|49| Handling of global lists. Starts of global lists are hold in a50| global array named listheaders and type of LIST. For each routine51| managing one of lists you provide an index to this array. There52| are definitions in MATC.H to make these indexes more abstract53| entities.54|55| List structure is currently as follows56|57| struct list58| {59| struct list *next;60| char *name;61| };62|63| This is typedef'ed to LIST.64|65***********************************************************************/6667/*======================================================================68|Syntax of the manual pages:69|70|FUNCTION NAME(...) params ...71|72$ usage of the function and type of the parameters73? explain the effects of the function74= return value and the type of value if not of type int75@ globals effected directly by this routine76! current known bugs or limitations77& functions called by this function78~ these functions may interest you as an alternative function or79| because they control this function somehow80^=====================================================================*/818283/*84* $Id: lists.c,v 1.1.1.1 2005/04/14 13:29:14 vierinen Exp $85*86* $Log: lists.c,v $87* Revision 1.1.1.1 2005/04/14 13:29:14 vierinen88* initial matc automake package89*90* Revision 1.2 1998/08/01 12:34:44 jpr91*92* Added Id, started Log.93*94*95*/9697#include "elmer/matc.h"9899void lst_addtail(int list, LIST *item)100/*======================================================================101? Add specified item to end of a list given.102^=====================================================================*/103{104LIST *lst;105106/*107* check if the list exists, if not just make this item first in list108*/109if (listheaders[list].next == (LIST *)NULL)110listheaders[list].next = item;111112/*113* else look for current last item in list114*/115else116{117for(lst = listheaders[list].next; NEXT(lst); lst = NEXT(lst));118119/*120* and make the link121*/122NEXT(lst) = item;123}124}125126void lst_addhead(int list, LIST *item)127/*======================================================================128? add specified item to start of list given.129^=====================================================================*/130{131/*132* make the link.133*/134NEXT(item) = listheaders[list].next;135listheaders[list].next = item;136}137138void lst_add(int list, LIST *item)139/*======================================================================140? add item to lexically right place in the list.141|142& strcmp()143^=====================================================================*/144{145LIST *lst, *lstn;146147/*148* if the list is empty make this item first and return.149*/150if ((lst = listheaders[list].next) == (LIST *)NULL)151{152lst_addhead(list, item); return;153}154155/*156* if the name of the new item is lexically157* smaller than first item in the list, add it158* to beginning of the list.159*/160if (strcmp(NAME(lst), NAME(item)) > 0)161{162lst_addhead(list, item); return;163}164165/*166* look for right place to add.167*/168for(; NEXT(lst); lst = NEXT(lst))169if (strcmp(NAME(NEXT(lst)), NAME(item)) > 0)170{171lstn = NEXT(lst);172NEXT(lst) = item;173NEXT(item) = lstn;174return;175}176177/*178* fell of the loop. item should be added to the tail of the list.179*/180NEXT(lst) = item;181}182183void lst_unlink(int list, LIST *item)184/*======================================================================185? unlink specified item from a list given.186^=====================================================================*/187{188LIST *lst;189190/*191* if the list is empty return192*/193if ((lst = listheaders[list].next) == (LIST *)NULL) return;194195/*196* it's not the header, look if it is in list at all197*/198if (lst != item)199{200201for(; NEXT(lst); lst = NEXT(lst))202{203if (NEXT(lst) == item) break;204}205206/*207* item was not found from the list. do nothing.208*/209if (NEXT(lst) == (LIST *)NULL) return;210211/*212* found, unlink213*/214NEXT(lst) = NEXT(item);215}216217/*218* item was the header, unlink it219*/220else221listheaders[list].next = NEXT(item);222}223224void lst_free(int list, LIST *item)225/*======================================================================226? Unlink item from list and free memory used by it.227|228& lst_unlink(), FREEMEM229^=====================================================================*/230{231lst_unlink(list, item);232233FREEMEM(NAME(item));234FREEMEM((char *)item);235}236237LIST *lst_find(int list, char *name)238/*======================================================================239? Look for a named item from given list.240|241& strcmp()242^=====================================================================*/243{244LIST *lst;245246/*247* look for item248*/249for( lst = listheaders[list].next; lst; lst = NEXT(lst) )250{251if ( NAME(lst) && strcmp(name, NAME(lst)) == 0 ) break;252}253254return lst;255}256257void lst_purge(int list)258/*======================================================================259? Delete list and free memory allocated to it.260|261& FREEMEM262^=====================================================================*/263{264LIST *lst, *lstn;265266/*267* free memory allocated for this list268*/269for(lst = listheaders[list].next; lst;)270{271lstn = NEXT(lst);272FREEMEM(NAME(lst));273FREEMEM((char *)lst);274lst = lstn;275}276277listheaders[list].next = (LIST *)NULL; /* security */278}279280VARIABLE *lst_print(int list)281/*======================================================================282? Print list name and item names from given list283|284! Output looks real ugly, should do something to that. The command285| to get here is "help" but it really is not very helpful.286|287& fprintf(), strlen()288^=====================================================================*/289{290LIST *lst;291292int i, spc, len;293294/*295* if empty list return.296*/297if ( listheaders[list].next == (LIST *)NULL )298return (VARIABLE *)NULL;299300/*301* name of the list302*/303PrintOut( "\n%s\n\n", listheaders[list].name );304305/*306* and finally list all item names one by one.307* try printing as many names as possible to308* each output line.309*/310for(len = 0,lst = listheaders[list].next; lst; lst = NEXT(lst))311{312if ( NAME(lst) )313{314if (len >= 80)315{316PrintOut("\n");317len = 0;318} else len += 20;319PrintOut("%-20s\t", NAME(lst));320if ( strlen(NAME(lst)) >= 20 ) { PrintOut("%-20%s\t", " "); len+= 20; }321/*322spc = 20 - spc;323for(i=0; i<spc && len<80; i++)324{325PrintOut(" "); len++;326}327*/328}329}330PrintOut("\n");331332return (VARIABLE *)NULL;333}334335336