/*
 *  	$Id: ac_delstr.c,v 1.1 2011/02/14 05:45:21 rec Exp $
 *
 * Implementation of the Aho-Corasick algorithm.
 *
 * This file is public domain.
 */

#include <stdio.h>
#include <stdlib.h>		/* for malloc(3) */
#include <string.h>		/* for strlen(3) */
#include "ac.h"
/*
 * ac_del_string
 *
 * Deletes a string from the keyword tree.
 *
 * Parameters:   node  -  an AC_Struct structure
 *               P     -  the sequence to be deleted
 *               M     -  its length (or 0 to calculate it)
 *               id    -  its identifier
 *
 * Returns:  non-zero on success, zero on error.
 */
int 
ac_del_string(AC_Handle node,
	      char *P, 
	      unsigned int M, 
	      unsigned int id)
{
  int i, flag;
  AC_priv_tree tnode, tlast, tback, child, back;

  if (node->errorflag || 
      id > node->Psize ||
      node->Plengths[id] == 0)
    return 0;

  if (M == 0)			/* handle special case */
    M = strlen(P);

  P--;            /* Shift to make sequence be P[1],...,P[M] */

  /*
   * Scan the tree for the path corresponding to the keyword to be deleted.
   */
  flag = 1;
  tlast = tnode = node->tree;
  tback = NULL;

  for (i=1; i <= M; i++) {
    /*
     * Find the child matching P[i].  It must be there.
     */
    child = tnode->children;
    back = NULL;
    while (child != NULL && child->ch != P[i]) {
      back = child;
      child = child->sibling;
    }

    if (child == NULL) {
      warnx("Error in Aho-Corasick preprocessing: String to be "
              "deleted is not in tree.");
      return 0;
    }

    /*
     * Try to find the point where the pattern to be deleted branches off
     * from the paths of the other patterns in the tree.  This point must
     * be at the latest node which satisfies one of these two conditions:
     *
     *    1) Another pattern ends at that node (and so
     *       `child->matchid != 0').  In this case, the branch point is
     *       just below this node and so the children of this node
     *       should be removed.
     *    2) A node has other siblings.  In this case, the node itself
     *       is the branch point, and it and its children should be
     *       removed.
     */
    if (i < M && child->matchid != 0) {
      flag = 1;
      tlast = child;
    }
    else if (back != NULL || child->sibling != NULL) {
      flag = 2;
      tlast = child;
      tback = (back == NULL ? tnode : back);
    }

    tnode = child;
  }

  /*
   * If the node corresponding to the end of the keyword has children,
   * then the tree should not be altered, except to remove the keyword's
   * identifier from the tree.
   *
   * Otherwise, apply the appropriate removal, as described above.
   */
  if (tnode->children != NULL) {
    tnode->matchid = 0;
  }
  else {
    if (flag == 1) {
      child = tlast->children;
      tlast->children = NULL;
      tlast = child;
    }
    else {
      if (tback->children == tlast)
        tback->children = tlast->sibling;
      else
        tback->sibling = tlast->sibling;
    }

    while (tlast != NULL) {
      child = tlast->children;
      free(tlast);
      tlast = child;
    }
  }

  node->Plengths[id] = 0;
  node->ispreprocessed = 0;

  return 1;
}
