home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / internet / ntserver / wtsource / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-08  |  4.1 KB  |  237 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.    
  4.    Brewster@think.com
  5. */
  6.  
  7. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  8.  
  9.  
  10. /* Change log:
  11.  * $Log: list.c,v $
  12.  * Revision 1.1  1993/02/16  15:05:35  freewais
  13.  * Initial revision
  14.  *
  15.  * Revision 1.4  92/03/07  19:44:00  jonathan
  16.  * changed function argument to sort_list from long to int.
  17.  * mycroft@hal.gnu.ai.mit.edu.
  18.  * 
  19.  * Revision 1.3  92/02/13  12:40:11  jonathan
  20.  * Corrected syntax for function as arguments.
  21.  * 
  22.  * Revision 1.2  92/02/12  13:33:36  jonathan
  23.  * Added "$Log" so RCS will put the log message in the header
  24.  * 
  25. */
  26.  
  27. /* List Utilities.
  28.  *
  29.  * -brewster
  30.  */
  31.  
  32.  
  33. #include "list.h"
  34.  
  35. /* a list is an end_of_list terminated array */
  36.  
  37. char empty_tmp = 'f';
  38. void *end_of_list = (void*)&empty_tmp;  /* this is special */
  39.  
  40. void* car(list)
  41. void **list;
  42. {
  43.   if(end_of_list == *list)
  44.     return(end_of_list);
  45.   else
  46.     return(*list);
  47. }
  48.  
  49. /* returns the nth element (0 is the first element).
  50.   it returns NULL if it asks for an element off the end (?).
  51.  */
  52.  
  53. void* nth(number, list)
  54. long number;
  55. void **list;
  56. {
  57.   if(number < length(list))
  58.     return(list[number]);
  59.   else
  60.     return(NULL); 
  61. }
  62.  
  63. void 
  64. setf_nth(number, elem, list)
  65. long number;
  66. void* elem;
  67. void**list;
  68. /* set the nth element */
  69. {
  70.   if(number < length(list))
  71.     list[number] = elem;
  72. }
  73.  
  74. void* first(list)
  75. void **list;
  76. {
  77.   return(car(list));
  78. }
  79.  
  80. void* second(list)
  81. void **list;
  82. {
  83.   return(car(cdr(list)));
  84. }
  85.  
  86. void* last(list)
  87. void **list;
  88. {
  89.   long len = length(list);
  90.   if (len > 0)
  91.     return(nth(len - 1,list));
  92.   else
  93.     return(NULL);
  94. }
  95.  
  96. void** cdr(list)
  97. void **list;{
  98.   if(NULL == list)
  99.     return((void**)end_of_list);
  100.   else if(end_of_list == *list)
  101.     return((void**)end_of_list);
  102.   else
  103.     return(list+1);
  104. }
  105.  
  106. void** nth_cdr(list, n)
  107. void **list;
  108. long n;{
  109.   void** l = list;
  110.   long i;
  111.   for (i = 0; i < n; i++)
  112.     l = cdr(l);
  113.   return(l);
  114. }
  115.  
  116. void** rest(list)
  117. void **list;
  118. {
  119.   return(cdr(list));
  120. }
  121.  
  122. void* cadr(list)
  123. void **list;
  124. {
  125.   if(NULL == list)
  126.     return(NULL);
  127.   else if(end_of_list == *list)
  128.     return(NULL);
  129.   else
  130.     return(*(list+1));
  131. }
  132.  
  133. /* length of a list.  returns -1 if error.*/
  134. long length(list)
  135. void** list;
  136. {
  137.   long count = 0;
  138.   if(list == NULL)
  139.     return(0);
  140.   while(end_of_list != list[count])
  141.     count++;
  142.   return(count);
  143. }
  144.  
  145. #ifdef WIN32
  146. void mapcar(void **list, void (*function)(void *))
  147. #else
  148. void mapcar(list, function)
  149. void **list;
  150. void (*function)();
  151. #endif
  152. {
  153.   if(!null(list)){
  154.     (*function)(car(list));
  155. #ifdef WIN32
  156.     mapcar(cdr(list), function);
  157. #else
  158.     mapcar(cdr(list), function);
  159. #endif
  160.   }
  161. }
  162.  
  163. /* pushes the item on the end of the list. returns the list. */
  164. void **collecting(list, item)
  165. void **list;
  166. void *item;
  167. {
  168.   long len = length(list);
  169.   if(0 == len){
  170.     list = (void**)malloc(2 * sizeof(void*));
  171.     list[0] = item;
  172.     list[1] = end_of_list;
  173.     return(list);
  174.   }
  175.   else{
  176.     list = (void **)realloc((char *)list, (len + 2) * sizeof(void*));
  177.     list[len] = item;
  178.     list[len + 1] = end_of_list;
  179.   }
  180.   return(list);
  181. }
  182.  
  183.  
  184. void setf_car(list, item)
  185. void** list;
  186. void* item;
  187. {
  188.   list[0] = item;
  189. }
  190.  
  191. /* returns true if the list is NULL */
  192. boolean null(list)
  193. void **list;
  194. {
  195.   if(list == NULL)
  196.     return(true);
  197.   if(list[0] == end_of_list)
  198.     return(true);
  199.   return(false);
  200. }
  201.  
  202. boolean free_list(list)
  203. void **list;
  204. {
  205.   if(list != NULL)
  206.     s_free(list);
  207.   return(true);
  208. }
  209.  
  210. void
  211. #ifdef WIN32
  212. sort_list(void** list, int (*cmp)(const void *,const void *))
  213. #else
  214. sort_list(list, cmp)
  215. void** list;
  216. int (*cmp)();
  217. #endif
  218.   qsort((void *)list,length(list),sizeof(void*),cmp);
  219. }
  220.  
  221. void**
  222. remove_item_from_list(list, pos)
  223. void** list;
  224. long pos;
  225. {
  226.   long count = pos;
  227.   if (list == NULL || list[0] == end_of_list)
  228.     return(NULL);
  229.   while (end_of_list != list[count])
  230.    { list[count] = list[count + 1];
  231.      count++;
  232.    }
  233.   list = (void**)realloc((char*)list,count*sizeof(void*));
  234.   return(list);
  235. }
  236.