home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume23 / tua / part01 / commtree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-23  |  2.2 KB  |  82 lines

  1. /* $Id: commtree.c,v 3.3 1991/09/01 14:02:03 piggy Rel $
  2.  * Handle the command trees.
  3.  *
  4.  *   Copyright (C) 1991  Lele Gaifax (piggy@idea.sublink.org)
  5.  *
  6.  *   This program is free software; you can redistribute it and/or modify
  7.  *   it under the terms of the GNU General Public License as published by
  8.  *   the Free Software Foundation; either version 1, or (at your option)
  9.  *   any later version.
  10.  *
  11.  *   This program is distributed in the hope that it will be useful,
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *   GNU General Public License for more details.
  15.  *
  16.  *   You should have received a copy of the GNU General Public License
  17.  *   along with this program; if not, write to the Free Software
  18.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * SYNOPSIS:
  21.  * int    InsertComm(Tcommrep_t Tree, char * comm);
  22.  *      As usual, look for comm in the tree; if it is already there
  23.  *      return a pointer to it, otherwise insert it and reset its infos.
  24.  *
  25.  * void EnquiryComm(Tcommrep_t Tree, void (*funct)(commrep_t comm))
  26.  *      For each command in the tree, process it through the function.
  27.  *
  28.  */
  29.  
  30. #include    <stdio.h>
  31. #include    "mem.h"
  32. #include    <string.h>
  33. #include    "hdbstat.h"
  34.  
  35. #define    TNULL    (Tcommrep_t) NULL
  36.  
  37. static FlistHead CommFlist =
  38. {
  39.   0, 0, 0
  40. };
  41.  
  42. commrep_t *
  43. InsertComm (tree, comm)
  44.      Tcommrep_t *tree;
  45.      char *comm;
  46.  
  47. {
  48.   int cmpres;
  49.  
  50.   if (*tree == TNULL)
  51.     {
  52.       if (CommFlist.size == 0)
  53.     MemInit (&CommFlist, sizeof (commreptree_t), 100, 50);
  54.       *tree = (Tcommrep_t) new (&CommFlist);
  55.       (*tree)->L = (*tree)->R = TNULL;
  56.       (*tree)->Node.Command = strdup (comm);
  57.       (*tree)->Node.Number = 0;
  58.       return (&(*tree)->Node);
  59.     }
  60.   else if ((cmpres = strcmp (comm, (*tree)->Node.Command)) < 0)
  61.     return InsertComm (&(*tree)->L, comm);
  62.   else if (cmpres > 0)
  63.     return InsertComm (&(*tree)->R, comm);
  64.   else
  65.     return (&(*tree)->Node);
  66. }
  67.  
  68.  
  69. void 
  70. EnquiryComm (tree, funct)
  71.      Tcommrep_t tree;
  72.      void (*funct) ();
  73.  
  74. {
  75.   if (tree != TNULL)
  76.     {
  77.       EnquiryComm (tree->L, funct);
  78.       (*funct) (tree->Node);
  79.       EnquiryComm (tree->R, funct);
  80.     }
  81. }
  82.