/*
 *  	$Id: ac_addstr.c,v 1.1 2011/02/14 05:45:26 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_add_string
 *
 * Adds a string to the AC_Struct structure's keyword tree.
 *
 * NOTE:  The `id' value given must be unique to any of the strings
 *        added to the tree, and must be a small integer greater than
 *        0 (since it is used to index an array holding information
 *        about each of the strings).
 *
 *        The best id's to use are to number the strings from 1 to K.
 *
 * Parameters:   node      -  an AC_Struct structure
 *               P         -  the sequence
 *               M         -  the sequence length (or 0 to calculate it)
 *               id        -  the sequence identifier
 *
 * Returns:  non-zero on success, zero on error.
 */
int 
ac_add_string(AC_Handle node, 
	      char *P,
	      unsigned int M,
	      unsigned int id)
{
  int i, j, newsize;
  AC_priv_tree tnode, child, back, newnode, list, tail;

  /*
   * Return a zero if a previous error had occurred, or if the
   * given id equals zero.  An id value of zero is used by the 
   * algorithm to signal that no pattern ends at a node in the
   * keyword tree.  So, it can't be used as a pattern's id.
   */
  if (node->errorflag || id == 0)
    return 0;

  if (M == 0)			/* handle special case */
    M = strlen(P);
  P--;            /* Shift to make sequence be P[1],...,P[M] */

  /*
   * Allocate space for the new string's information.
   */
  if (node->Psize <= id) {
    if (node->Psize == 0) {
      newsize = (id >= 16 ? id + 1 : 16);
      node->Plengths = malloc(newsize * sizeof(unsigned int));
    }
    else {
      newsize = node->Psize + id + 1;
      node->Plengths = realloc(node->Plengths, newsize * sizeof(unsigned int));
    }
    if (node->Plengths == NULL) {
      node->errorflag = 1;
      return 0;
    }

    for (i=node->Psize; i < newsize; i++)
      node->Plengths[i] = 0;
    node->Psize = newsize;
  }

  if (node->Plengths[id] != 0) {
    warnx("Error in Aho-Corasick preprocessing: "
	  "Duplicate identifiers.");
    return 0;
  }
  
  /*
   * Add the string to the keyword tree.
   */
  tnode = node->tree;
  for (i=1; i <= M; i++) {
    /*
     * Find the child whose character is P[i].
     */
    back = NULL;
    child = tnode->children;
    while (child != NULL && child->ch < P[i]) {
      back = child;
      child = child->sibling;
    }

    if (child == NULL || child->ch != P[i])
      break;

    tnode = child;

    node->prep_old_edges++;
  }

  /*
   * If only part of the pattern exists in the tree, add the
   * rest of the pattern to the tree.
   */
  if (i <= M) {
    list = tail = NULL;
    for (j=i; j <= M; j++) {
      if ((newnode = malloc(sizeof(AC_priv_tree_node))) == NULL)
        break;
      memset(newnode, 0, sizeof(AC_priv_tree_node));
      newnode->ch = P[j];

      if (list == NULL)
        list = tail = newnode;
      else
        tail = tail->children = newnode;

      node->prep_new_edges++;
    }
    if (j <= M) {
      while (list != NULL) {
        tail = list->children;
        free(list);
        list = tail;
      }
      return 0;
    }

    list->sibling = child;
    if (back == NULL)
      tnode->children = list;
    else
      back->sibling = list;

    tnode = tail;
  }

  tnode->matchid = id;
  node->Plengths[id] = M;
  node->ispreprocessed = 0;

  return 1;
}

