Main Page   Modules   Alphabetical List   Compound List   File List   Compound Members   File Members  

list.c

Go to the documentation of this file.
00001 /*  $Id: list.c,v 1.4 2000/11/30 07:37:40 dbryson Exp $
00002 **
00003 **  Copyright (c) 2000 Derry Bryson <derry@techass.com>
00004 **
00005 **  Description:
00006 **  
00007 **    Postscript Library
00008 **
00009 **
00010 **  License:
00011 **
00012 **    This library is free software; you can redistribute it and/or
00013 **    modify it under the terms of the GNU Lesser General Public
00014 **    License as published by the Free Software Foundation; either
00015 **    version 2.1 of the License, or (at your option) any later version.
00016 **
00017 **    This library is distributed in the hope that it will be useful,
00018 **    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 **    Lesser General Public License for more details.
00021 **
00022 **    You should have received a copy of the GNU Lesser General Public
00023 **    License along with this library; if not, write to the Free Software
00024 **    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 **
00026 **
00027 **  Contact:
00028 **
00029 **      Mail:
00030 **
00031 **        Technology Associates, Inc.
00032 **        LIBPS Project
00033 **        1455 Deming Way #11
00034 **        Sparks, NV  89431
00035 **        USA
00036 **
00037 **      Email:
00038 **
00039 **        libps@techass.com
00040 **
00041 **      See our website at:
00042 **
00043 **        libps.sourceforge.net
00044 **
00045 */
00046 
00054 #ifdef HAVE_CONFIG_H
00055 # include "psconfig.h"
00056 #endif
00057 
00058 #include <stdio.h>
00059 #include <stdlib.h>
00060 
00061 #include "ps/list.h"
00062 
00075 PSList *
00076 psListAlloc(PSListDestroyFunc destroyFunc)
00077 {
00078   PSList
00079     *tempList;
00080 
00081   if((tempList = calloc(1, sizeof(PSList))) != NULL)
00082     tempList->destroyFunc = destroyFunc;
00083 
00084   return(tempList);
00085 }
00086 
00097 void
00098 psListFree(PSList *list)
00099 {
00100   PSListNode
00101     *nextNode,
00102     *node;
00103 
00104   if(!list)
00105     return;
00106 
00107   for(node = list->first; node; node = nextNode)
00108   {
00109     nextNode = node->next;
00110     if(list->destroyFunc)
00111       list->destroyFunc(node->value);
00112     free(node);
00113   }
00114 
00115   free(list);
00116 }
00117 
00129 PSListNode *
00130 psListInsert(PSList *list, PSListNode *node, void *value)
00131 {
00132   PSListNode
00133     *newNode;
00134 
00135   if(!list)
00136     return(NULL);
00137 
00138   if((newNode = calloc(1, sizeof(PSListNode))) != NULL)
00139   {
00140     newNode->value = value;
00141 
00142     if(node)
00143     {
00144       if(node->prev)
00145       {
00146         newNode->next = node;
00147         newNode->prev = node->prev;
00148         node->prev = newNode;
00149         newNode->prev->next = newNode;
00150       }
00151       else
00152       {
00153         newNode->next = list->first;
00154         if(list->first)
00155           list->first->prev = newNode;
00156         list->first = newNode;
00157         if(!list->last)
00158           list->last = newNode;
00159       }
00160     }
00161     else
00162     {
00163       if(list->last)
00164       {
00165         newNode->prev = list->last;
00166         list->last->next = newNode;
00167       }
00168       list->last = newNode;
00169       if(!list->first)
00170         list->first = newNode;
00171     }
00172 
00173     list->numNodes++;
00174   }
00175 
00176   return(newNode);
00177 }
00178 
00190 void
00191 psListDelete(PSList *list, PSListNode *node)
00192 {
00193   if(!list || !node)
00194     return;
00195 
00196   if(node->prev)
00197     node->prev->next = node->next;
00198   else
00199     if((list->first = node->next) != NULL)
00200       list->first->prev = NULL;
00201 
00202   if(node->next)
00203     node->next->prev = node->prev;
00204   else
00205     if((list->last = node->prev) != NULL)
00206       list->last->next = NULL;
00207 
00208   if(list->destroyFunc)
00209     list->destroyFunc(node->value);
00210   list->numNodes--;
00211   free(node);
00212 }
00213 
00223 PSListNode *
00224 psListFirst(PSList *list)
00225 {
00226   if(list)
00227     return(list->first);
00228     
00229   return(NULL);
00230 }
00231 
00241 PSListNode *
00242 psListLast(PSList *list)
00243 {
00244   if(list)
00245     return(list->last);
00246     
00247   return(NULL);
00248 }
00249 
00260 PSListNode *
00261 psListNext(PSList *list, PSListNode *node)
00262 {
00263   if(list && node)
00264     return(node->next);
00265     
00266   return(NULL);
00267 }
00268 
00279 PSListNode *
00280 psListPrev(PSList *list, PSListNode *node)
00281 {
00282   if(list && node)
00283     return(node->prev);
00284     
00285   return(NULL);
00286 }
00287 
00296 long
00297 psListNumNodes(PSList *list)
00298 {
00299   if(list)
00300     return(list->numNodes);
00301     
00302   return(0);
00303 }
00304 
00314 void *
00315 psListGetValue(PSList *list, PSListNode *node)
00316 {
00317   if(list && node)
00318     return(node->value);
00319     
00320   return(NULL);
00321 }
00322 
00330 PSListNode *
00331 psListGetNode(PSList *list, long nodeNum)
00332 {
00333   PSListNode
00334     *node;
00335 
00336   if(!list || nodeNum < 0 || nodeNum > list->numNodes)
00337     return(NULL);
00338 
00339   node = psListFirst(list);
00340   while(node && --nodeNum)
00341     node = psListNext(list, node);
00342     
00343   return(node);
00344 }

Generated at Mon Dec 11 22:46:27 2000 for Postscript Utility Library by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000