Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

List.c

Go to the documentation of this file.
00001 /*
00002  *    gui - [gega user interface] the flexible solution for user interface problems
00003  *    Copyright (C) 2002  Gergely Gati
00004  *
00005  *    This program is free software; you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation; version 2 of the License.
00008  *
00009  *    This program is distributed in the hope that it will be useful,
00010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *    GNU General Public License for more details.
00013  *
00014  *    You should have received a copy of the GNU General Public License
00015  *    along with this program; if not, write to the Free Software
00016  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *    Gergely Gati
00019  *      email:           g.gati@freemail.hu
00020  *      AIM screenname:  GatiGergely
00021  *      ICQ number:      93131690
00022  *
00023  */
00024 /*
00025  *  Author        : Ferenc Zavacki
00026  *
00027  *  Creation date : 2001/03/02
00028  *
00029  *  Description   :
00030  *
00031  */
00032 
00033 /***********************************************************************
00034 *                               Includes                               *
00035 ***********************************************************************/
00036 
00037 #include <stdlib.h>
00038 
00039 #include "debug.h"
00040 #include "List.h"
00041 #include "Memory.h"
00042 
00043 /***********************************************************************
00044 *                              Structures                              *
00045 ***********************************************************************/
00046 
00047 struct Node_s
00048 {
00049   Node_t *Prev;
00050   Node_t *Next;
00051   void *Data;
00052 };
00053 
00054 struct List_s
00055 {
00056   Node_t *Head;
00057   Node_t *Tail;
00058   int Count;
00059 };
00060 
00061 /***********************************************************************
00062 *                           Public functions                           *
00063 ***********************************************************************/
00064 
00065 /*
00066  *  This function creates an empty list and returns a pointer to it.
00067  *  Returns NULL on error.
00068  */
00069 
00070 List_t *list_CreateList(void)
00071 {
00072   List_t *List;
00073 
00074   debug_Begin();
00075 
00076   List = mem_calloc(1, sizeof(List_t));
00077 
00078   debug_End();
00079 
00080   return (List);
00081 }
00082 
00083 /*
00084  *  This function deletes the specified list. The nodes in the list are
00085  *  not deleted.
00086  */
00087 
00088 void list_DeleteList(List_t *List)
00089 {
00090   debug_Begin();
00091 
00092   if (List != NULL)
00093   {
00094     mem_free(List);
00095   }
00096 
00097   debug_End();
00098 }
00099 
00100 /*
00101  *  This function creates a node and sets its data pointer to NULL.
00102  *  Returns NULL on error.
00103  */
00104 
00105 Node_t *list_CreateNode(void)
00106 {
00107   Node_t *Node;
00108 
00109   debug_Begin();
00110 
00111   Node = mem_calloc(1, sizeof(Node_t));
00112 
00113   debug_End();
00114 
00115   return (Node);
00116 }
00117 
00118 /*
00119  *  This function deletes the specified node. The attached data is not
00120  *  deleted.
00121  */
00122 
00123 void list_DeleteNode(Node_t *Node)
00124 {
00125   debug_Begin();
00126 
00127   if (Node != NULL)
00128   {
00129     mem_free(Node);
00130   }
00131 
00132   debug_End();
00133 }
00134 
00135 /*
00136  *  This function inserts the specified node into the specified list
00137  *  before all other nodes of the list.
00138  */
00139 
00140 void list_InsertNodeHead(List_t *List, Node_t *Node)
00141 {
00142   debug_Begin();
00143 
00144   if ((List != NULL) && (Node != NULL))
00145   {
00146     Node->Prev = NULL;
00147 
00148     if ((Node->Next = List->Head) != NULL)
00149     {
00150       List->Head->Prev = Node;
00151     }
00152     else
00153     {
00154       List->Tail = Node;
00155     }
00156 
00157     List->Head = Node;
00158 
00159     List->Count++;
00160   }
00161 
00162   debug_End();
00163 }
00164 
00165 /*
00166  *  This function inserts the specified node into the specified list
00167  *  after all other nodes of the list.
00168  */
00169 
00170 void list_InsertNodeTail(List_t *List, Node_t *Node)
00171 {
00172   debug_Begin();
00173 
00174   if ((List != NULL) && (Node != NULL))
00175   {
00176     Node->Next = NULL;
00177 
00178     if ((Node->Prev = List->Tail) != NULL)
00179     {
00180       List->Tail->Next = Node;
00181     }
00182     else
00183     {
00184       List->Head = Node;
00185     }
00186 
00187     List->Tail = Node;
00188 
00189     List->Count++;
00190   }
00191 
00192   debug_End();
00193 }
00194 
00195 /*
00196  *  This function inserts the specified node into the specified list
00197  *  after the other specified node of the list.
00198  */
00199 
00200 void list_InsertNodeNext(List_t *List, Node_t *PrevNode, Node_t *Node)
00201 {
00202   debug_Begin();
00203 
00204   if ((List != NULL) && (PrevNode != NULL) && (Node != NULL))
00205   {
00206     Node->Prev = PrevNode;
00207 
00208     if ((Node->Next = PrevNode->Next) != NULL)
00209     {
00210       PrevNode->Next->Prev = Node;
00211     }
00212     else
00213     {
00214       List->Tail = Node;
00215     }
00216 
00217     PrevNode->Next = Node;
00218 
00219     List->Count++;
00220   }
00221 
00222   debug_End();
00223 }
00224 
00225 /*
00226  *  This function inserts the specified node into the specified list
00227  *  before the other specified node of the list.
00228  */
00229 
00230 void list_InsertNodePrev(List_t *List, Node_t *NextNode, Node_t *Node)
00231 {
00232   debug_Begin();
00233 
00234   if ((List != NULL) && (NextNode != NULL) && (Node != NULL))
00235   {
00236     Node->Next = NextNode;
00237 
00238     if ((Node->Prev = NextNode->Prev) != NULL)
00239     {
00240       NextNode->Prev->Next = Node;
00241     }
00242     else
00243     {
00244       List->Head = Node;
00245     }
00246 
00247     NextNode->Prev = Node;
00248 
00249     List->Count++;
00250   }
00251 
00252   debug_End();
00253 }
00254 
00255 /*
00256  *  This function removes the specified node from the specified list and
00257  *  returns a pointer to the same node.
00258  */
00259 
00260 Node_t *list_RemoveNode(List_t *List, Node_t *Node)
00261 {
00262   debug_Begin();
00263 
00264   if ((List != NULL) && (Node != NULL))
00265   {
00266     if (Node == List->Head)
00267     {
00268       List->Head = Node->Next;
00269     }
00270     else
00271     {
00272       Node->Prev->Next = Node->Next;
00273     }
00274 
00275     if (Node == List->Tail)
00276     {
00277       List->Tail = Node->Prev;
00278     }
00279     else
00280     {
00281       Node->Next->Prev = Node->Prev;
00282     }
00283 
00284     List->Count--;
00285   }
00286 
00287   debug_End();
00288 
00289   return (Node);
00290 }
00291 
00292 /*
00293  *  This function removes the first node from the specified list and
00294  *  returns a pointer to the node. Returns NULL if the list is empty.
00295  */
00296 
00297 Node_t *list_RemoveNodeHead(List_t *List)
00298 {
00299   Node_t *Node = NULL;
00300 
00301   debug_Begin();
00302 
00303   if (List != NULL)
00304   {
00305     if ((Node = List->Head) != NULL)
00306     {
00307       if ((List->Head = Node->Next) != NULL)
00308       {
00309         List->Head->Prev = NULL;
00310       }
00311       else
00312       {
00313         List->Tail = NULL;
00314       }
00315 
00316       List->Count--;
00317     }
00318   }
00319 
00320   debug_End();
00321 
00322   return (Node);
00323 }
00324 
00325 /*
00326  *  This function removes the last node from the specified list and
00327  *  returns a pointer to the node. Returns NULL if the list is empty.
00328  */
00329 
00330 Node_t *list_RemoveNodeTail(List_t *List)
00331 {
00332   Node_t *Node = NULL;
00333 
00334   debug_Begin();
00335 
00336   if (List != NULL)
00337   {
00338     if ((Node = List->Tail) != NULL)
00339     {
00340       if ((List->Tail = Node->Prev) != NULL)
00341       {
00342         List->Tail->Next = NULL;
00343       }
00344       else
00345       {
00346         List->Head = NULL;
00347       }
00348 
00349       List->Count--;
00350     }
00351   }
00352 
00353   debug_End();
00354 
00355   return (Node);
00356 }
00357 
00358 /*
00359  *  This function returns the first node of the specified list. Returns
00360  *  NULL if the list is empty.
00361  */
00362 
00363 Node_t *list_GetNodeHead(List_t *List)
00364 {
00365   Node_t *Node = NULL;
00366 
00367   debug_Begin();
00368 
00369   if (List != NULL)
00370   {
00371     Node = List->Head;
00372   }
00373 
00374   debug_End();
00375 
00376   return (Node);
00377 }
00378 
00379 /*
00380  *  This function returns the last node of the specified list. Returns
00381  *  NULL if the list is empty.
00382  */
00383 
00384 Node_t *list_GetNodeTail(List_t *List)
00385 {
00386   Node_t *Node = NULL;
00387 
00388   debug_Begin();
00389 
00390   if (List != NULL)
00391   {
00392     Node = List->Tail;
00393   }
00394 
00395   debug_End();
00396 
00397   return (Node);
00398 }
00399 
00400 /*
00401  *  This function returns a pointer to the next node of the specified
00402  *  node which can be NULL if the specified node is the last node of the
00403  *  specified list.
00404  */
00405 
00406 Node_t *list_GetNodeNext(List_t *List, Node_t *Node)
00407 {
00408   Node_t *NextNode = NULL;
00409 
00410   debug_Begin();
00411 
00412   if ((List != NULL) && (Node != NULL))
00413   {
00414     NextNode = Node->Next;
00415   }
00416 
00417   debug_End();
00418 
00419   return (NextNode);
00420 }
00421 
00422 /*
00423  *  This function returns a pointer to the previous node of the
00424  *  specified node which can be NULL if the specified node is the first
00425  *  node of the specified list.
00426  */
00427 
00428 Node_t *list_GetNodePrev(List_t *List, Node_t *Node)
00429 {
00430   Node_t *PrevNode = NULL;
00431 
00432   debug_Begin();
00433 
00434   if ((List != NULL) && (Node != NULL))
00435   {
00436     PrevNode = Node->Prev;
00437   }
00438 
00439   debug_End();
00440 
00441   return (PrevNode);
00442 }
00443 
00444 /*
00445  *  This function returns a pointer to the node at the specified
00446  *  position which can be NULL if the specified node does not exist.
00447  */
00448 
00449 Node_t *list_GetNodeAt(List_t *List, int Index)
00450 {
00451   Node_t *Node = NULL;
00452   int i;
00453 
00454   debug_Begin();
00455 
00456   if ((Index >= 0) && (Index < List->Count))
00457   {
00458     for (i = 0, Node = list_GetNodeHead(List); (i < Index) && (Node != NULL); i++, Node = list_GetNodeNext(List, Node));
00459   }
00460 
00461   debug_End();
00462 
00463   return (Node);
00464 }
00465 
00466 /*
00467  *  This function returns the ordinal number of the specified node in
00468  *  the specified list.
00469  */
00470 
00471 int list_GetNodeIndex(List_t *List, Node_t *Node)
00472 {
00473   int Index = 0;
00474 
00475   debug_Begin();
00476 
00477   if ((List != NULL) && (Node != NULL))
00478   {
00479     while ((Node = list_GetNodePrev(List, Node)) != NULL)
00480     {
00481       Index++;
00482     }
00483   }
00484 
00485   debug_End();
00486 
00487   return (Index);
00488 }
00489 
00490 /*
00491  *  This function returns the current number of nodes in the specified
00492  *  list.
00493  */
00494 
00495 int list_GetNodeCount(List_t *List)
00496 {
00497   int Count = 0;
00498 
00499   debug_Begin();
00500 
00501   if (List != NULL)
00502   {
00503     Count = List->Count;
00504   }
00505 
00506   debug_End();
00507 
00508   return (Count);
00509 }
00510 
00511 /*
00512  *  This function returns the data pointer associated with the specified
00513  *  node.
00514  */
00515 
00516 void *list_GetNodeData(Node_t *Node)
00517 {
00518   void *Data = NULL;
00519 
00520   debug_Begin();
00521 
00522   if (Node != NULL)
00523   {
00524     Data = Node->Data;
00525   }
00526 
00527   debug_End();
00528 
00529   return (Data);
00530 }
00531 
00532 /*
00533  *  This function sets the data pointer associated with the specified
00534  *  node.
00535  */
00536 
00537 void list_SetNodeData(Node_t *Node, void *Data)
00538 {
00539   debug_Begin();
00540 
00541   if (Node != NULL)
00542   {
00543     Node->Data = Data;
00544   }
00545 
00546   debug_End();
00547 }
00548 
00549 /*
00550  *  This function sorts the nodes in the specified list based on the
00551  *  return value of the specified compare function. The return values
00552  *  must be the same as returned by strcmp().
00553  */
00554 
00555 void list_SortNodes(List_t *List, int (*Compare)(void *Data1, void *Data2))
00556 {
00557   debug_Begin();
00558 
00559   if ((List != NULL) && (Compare != NULL))
00560   {
00561     Node_t *Node1, *Node2;
00562     void *Data1, *Data2;
00563     int i, j;
00564 
00565     for (i = list_GetNodeCount(List) - 1; i > 0; i--)
00566     {
00567       int Swapped = 0;
00568 
00569       for (Node1 = list_GetNodeHead(List), j = 0; j < i; j++, Node1 = list_GetNodeNext(List, Node1))
00570       {
00571         Node2 = list_GetNodeNext(List, Node1);
00572 
00573         Data1 = list_GetNodeData(Node1);
00574         Data2 = list_GetNodeData(Node2);
00575 
00576         if (Compare(Data1, Data2) > 0)
00577         {
00578           list_SetNodeData(Node1, Data2);
00579           list_SetNodeData(Node2, Data1);
00580 
00581           Swapped = 1;
00582         }
00583       }
00584 
00585       if (!Swapped) break;
00586     }
00587   }
00588 
00589   debug_End();
00590 }

Generated on Tue Jan 7 12:11:22 2003 for THEGUI by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002