home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 52 / af052sub.adf / lyr-o-mat.lha / Lyr-O-Mat / Source / Source.lha / words_list.c < prev    next >
C/C++ Source or Header  |  1993-04-23  |  6KB  |  224 lines

  1.  
  2. #include "words.h"
  3.  
  4.  
  5. void newpattern(UBYTE *name)
  6. {
  7.  struct patternnode *p;
  8.  UBYTE              *s;
  9.  if(edit.pattern)
  10.  {
  11.   if(winfo.currentpattern)
  12.   {
  13.    free(winfo.currentpattern->pt_Node.ln_Name);
  14.    winfo.currentpattern->pt_Node.ln_Name = strdup(name);
  15.   }
  16.  }
  17.  else if(p = (struct patternnode *)calloc(1,sizeof(struct patternnode)))
  18.  {
  19.   p->pt_Node.ln_Name = strdup(name);
  20.   s = p->pt_Node.ln_Name + strlen(p->pt_Node.ln_Name) - 1;
  21.   while((*s == ' ') || (*s == '\n'))*s-- = '\0';
  22.   ++winfo.numpattern;
  23.   AddTail(&winfo.pattern,(struct Node *)p);
  24.   winfo.currentpattern = p;
  25.  }
  26. }
  27. void newclass(UBYTE *name)
  28. {
  29.  UBYTE            *s;
  30.  struct classnode *c;
  31.  if(edit.class)
  32.  {
  33.   if(winfo.currentclass)
  34.   {
  35.    free(winfo.currentclass->cl_Node.ln_Name);
  36.    winfo.currentclass->cl_Node.ln_Name = strdup(name);
  37.   }
  38.  }
  39.  else if(c = (struct classnode *)calloc(1,sizeof(struct classnode)))
  40.  {
  41.   c->cl_Node.ln_Name = strdup(name);
  42.   c->cl_NumWords = 0L;
  43.   s = c->cl_Node.ln_Name + strlen(c->cl_Node.ln_Name) - 1;
  44.   while((*s == ' ') || (*s == '\n'))*s-- = '\0';
  45.   NewList(&c->cl_Words);
  46.   ++winfo.numclass;
  47.   c->cl_Node.ln_Pri = strlen(c->cl_Node.ln_Name);
  48.   Enqueue(&winfo.class,(struct Node *)c);
  49.   winfo.currentclass = c;
  50.  }
  51. }
  52. void newword(UBYTE *name)
  53. {
  54.  UBYTE            *s;
  55.  struct wordnode  *w;
  56.  if(winfo.currentclass)
  57.  {
  58.   if(edit.word)
  59.   {
  60.    if(winfo.currentword)
  61.    {
  62.     free(winfo.currentword->wn_Node.ln_Name);
  63.     winfo.currentword->wn_Node.ln_Name = strdup(name);
  64.    }
  65.   }
  66.   else  if(w = (struct wordnode *)calloc(1,sizeof(struct wordnode)))
  67.   {
  68.    w->wn_Node.ln_Name = strdup(name);
  69.    s = w->wn_Node.ln_Name + strlen(w->wn_Node.ln_Name) - 1;
  70.    while((*s == ' ') || (*s == '\n'))*s-- = '\0';
  71.    winfo.currentclass->cl_NumWords++;
  72.    w->wn_Class = winfo.currentclass;
  73.    w->wn_Node.ln_Pri = strcmp("",w->wn_Node.ln_Name);
  74.    Enqueue(&winfo.currentclass->cl_Words,(struct Node *)w);
  75.    winfo.currentword = w;
  76.   }
  77.  } 
  78. }
  79. void delpattern(void)
  80. {
  81.  if(winfo.currentpattern)
  82.  {
  83.   list_off(LIST_PT);
  84.   Remove((struct Node *)winfo.currentpattern);
  85.   free(winfo.currentpattern->pt_Node.ln_Name);
  86.   winfo.numpattern -= (winfo.numpattern > 0) ? 1 :0 ;
  87.   free(winfo.currentpattern);
  88.   winfo.currentpattern = NULL;
  89.   list_on(LIST_PT);
  90.  }
  91. }
  92. void delclass(void)
  93. {
  94.  if(winfo.currentclass)
  95.  {
  96.   list_off(LIST_CL);
  97.   Remove((struct Node *)winfo.currentclass);
  98.   free(winfo.currentclass->cl_Node.ln_Name);
  99.   winfo.numclass -= (winfo.numclass > 0) ? 1 :0 ;
  100.   delwordlist(winfo.currentclass);
  101.   free(winfo.currentclass);
  102.   winfo.currentclass = NULL;
  103.   list_on(LIST_CL);
  104.  }
  105. }
  106. void delword(void)
  107. {
  108.  if(winfo.currentword)
  109.  {
  110.   list_off(LIST_WD);
  111.   Remove((struct Node *)winfo.currentword);
  112.   free(winfo.currentword->wn_Node.ln_Name);
  113.   winfo.currentword->wn_Class->cl_NumWords -= 
  114.        (winfo.currentword->wn_Class->cl_NumWords > 0) ? 1 : 0;
  115.   free(winfo.currentword);
  116.   winfo.currentword = NULL;
  117.   list_on(LIST_WD);
  118.  }
  119. }
  120.  
  121. void delwordlist(struct classnode *cl)
  122. {
  123.  struct Node *n;
  124.  list_off(LIST_WD);
  125.  while(n = RemHead(&cl->cl_Words))
  126.  {
  127.   free(n->ln_Name);
  128.   free(n);
  129.  }
  130.  list_on(LIST_WD);
  131. }
  132.  
  133. void delAll(void)
  134. {
  135.  struct Node *n;
  136.  struct classnode *cl;
  137.  winfo.currentpattern = NULL;
  138.  winfo.currentclass   = NULL;
  139.  winfo.currentword    = NULL;
  140.  winfo.numpattern     = 0;
  141.  winfo.numclass       = 0;
  142.  edit.pattern = 0;
  143.  edit.class   = 0;
  144.  edit.word    = 0;
  145.  if(winfo.nach){free(winfo.nach);winfo.nach = NULL;};
  146.  list_off(LIST_PT);list_off(LIST_CL);list_off(LIST_WD);
  147.  while(n = RemHead(&winfo.pattern))
  148.  {
  149.   free(n->ln_Name);
  150.   free(n);
  151.  }
  152.  while(cl = (struct classnode *)RemHead(&winfo.class))
  153.  {
  154.   while(n = RemHead(&cl->cl_Words))
  155.   {
  156.    free(n->ln_Name);
  157.    free(n);
  158.   }
  159.   free(cl->cl_Node.ln_Name);
  160.   free(cl);
  161.  }
  162.  list_on(LIST_PT);list_on(LIST_CL);list_on(LIST_WD);
  163. }
  164.  
  165. struct Node *NumToNode(struct List *list,UWORD Num)
  166. {
  167.  struct Node *n = list->lh_Head;
  168.  for(;Num;Num--)
  169.  {
  170.   n = n->ln_Succ;
  171.  }
  172.  return n;
  173. }
  174.  
  175. void list_off(ULONG which)
  176. {
  177.  switch(which)
  178.  {
  179.   case LIST_PT : 
  180.     GT_SetGadgetAttrs(Project0Gadgets[GDX_templatelist],Project0Wnd,NULL,GTLV_Labels,-1, TAG_DONE);
  181.     break;
  182.   case LIST_CL :
  183.     GT_SetGadgetAttrs(Project0Gadgets[GDX_classlist],Project0Wnd,NULL,GTLV_Labels,-1, TAG_DONE);
  184.     break;
  185.   case LIST_WD :
  186.     GT_SetGadgetAttrs(Project0Gadgets[GDX_wordlist],Project0Wnd,NULL,GTLV_Labels,-1, TAG_DONE);
  187.     break;
  188.  }
  189. }
  190. void list_on(ULONG which)
  191. {
  192.  ULONG num;
  193.  switch(which)
  194.  {
  195.   case LIST_PT : num = (winfo.currentpattern) ? NodeToNum(&winfo.pattern,(struct Node *)winfo.currentpattern) : NodeToNum(&winfo.pattern,winfo.pattern.lh_TailPred);
  196.      GT_SetGadgetAttrs(Project0Gadgets[GDX_templatelist],Project0Wnd,NULL,GTLV_Labels,&winfo.pattern,GTLV_Top, num ,GTLV_Selected,num,TAG_DONE);
  197.      break;
  198.   case LIST_CL : num = (winfo.currentclass) ? NodeToNum(&winfo.class,(struct Node *)winfo.currentclass) : NodeToNum(&winfo.class,winfo.class.lh_TailPred);
  199.      GT_SetGadgetAttrs(Project0Gadgets[GDX_classlist],Project0Wnd,NULL,GTLV_Labels,&winfo.class, GTLV_Top, num,GTLV_Selected,num,TAG_DONE);
  200.      if(winfo.currentclass)
  201.      {
  202.       num = (winfo.currentword) ? NodeToNum(&winfo.currentclass->cl_Words,(struct Node *)winfo.currentword) : NodeToNum(&winfo.currentclass->cl_Words,winfo.currentclass->cl_Words.lh_TailPred);
  203.       GT_SetGadgetAttrs(Project0Gadgets[GDX_wordlist],Project0Wnd,NULL,GTLV_Labels,&winfo.currentclass->cl_Words,GTLV_Top,num, GTLV_Selected,num,TAG_DONE);
  204.      }
  205.      break;
  206.   case LIST_WD :
  207.      if(winfo.currentclass)
  208.      {
  209.       num = (winfo.currentword) ? NodeToNum(&winfo.currentclass->cl_Words,(struct Node *)winfo.currentword) : NodeToNum(&winfo.currentclass->cl_Words,winfo.currentclass->cl_Words.lh_TailPred);
  210.       GT_SetGadgetAttrs(Project0Gadgets[GDX_wordlist],Project0Wnd,NULL,GTLV_Labels,&winfo.currentclass->cl_Words,GTLV_Top,num, GTLV_Selected,num,TAG_DONE);
  211.      }
  212.      break;
  213.  }
  214. }
  215.  
  216. ULONG NodeToNum(struct List *list,struct Node *node)
  217. {
  218.  ULONG num = 0;
  219.  struct Node *n;
  220.  for(n = list->lh_Head;n->ln_Succ;n = n->ln_Succ,num++)
  221.   if(node == n) return num;
  222. }
  223.  
  224.