/* findexp.c */

#include <ctype.h>
#include <string.h>

#include "findexp.h"
#include "log.h"
#include "newsfind.h"
#include "nfstr.h"

#define vb 0

#define CEQ(a,b) (newsfind_info.casesensve ? (a)==(b) : tolower(a)==tolower(b))

int findexp(const char *line, const char *expression, char terminator)
{
  int ln_ix = 0;
  int exp_ix = 0;
  int last_wild = -1;
/*char zak[256];
int vb;
strcpyC(zak, line);
if (strstr(zak, "Newsgroups")) vb=1; else vb=0;
if (vb) log_log("\nSearching line \"%s\"\n", zak);*/
  for (;;)
  {
    int esc;
    /* Check for escape char \ */
    if (expression[exp_ix] == '\\')
    {
      ++exp_ix;
      esc = 1;
    }
    else
      esc = 0;
    /* Check for single wild and skip */
    if (!esc && expression[exp_ix] == '#')
    {
      ++exp_ix;
      if (line[ln_ix++]==terminator)
        return (!expression[exp_ix]);
    }
    /* Check for multi wild */
    if (!esc && expression[exp_ix] == '*')
    {
if (vb) log_log("Found * wild\n");
      last_wild = exp_ix;
      /* If at end of expression, immediate match */
      if (!expression[++exp_ix])
      {
if (vb) log_log("Match because of wildcard at end\n");
        return 1;
      }
      /* Get next character in expression, maybe escaped */
      if (expression[exp_ix] == '\\')
        ++exp_ix;
      /* Find this in line (end of wild match) */
if (vb) log_log("Scanning line : ");
      while (line[ln_ix]!=terminator && !CEQ(line[ln_ix],expression[exp_ix]))
      {
if (vb) log_log("%c", line[ln_ix]);
        ++ln_ix;
      }
      if (line[ln_ix] == terminator)
      {
if (vb)
log_log("\nNo match because end of line while searching for wild terminator\n");
        return 0;
      }
if (vb) log_log("\nFound wild-terminating character\n");
    }
    /* Have we reached end of both strings? */
    if (line[ln_ix] == terminator && !expression[exp_ix])
    {
if (vb) log_log("Match because at end of both strings\n");
      return 1;
    }
    /* No, then end of either string? */
    if (line[ln_ix] == terminator || !expression[exp_ix])
    {
if (vb) log_log("No match because at end of only one string\n");
      return 0;
    }
    /* If characters don't match, reset to last wildcard */
if (vb)
log_log("Comparing \'%c\' and \'%c\'\n", line[ln_ix],expression[exp_ix]);
    if (!CEQ(line[ln_ix],expression[exp_ix]))
    {
if (vb) log_log("Mismatched characters ");
      if (last_wild == -1)
      {
if (vb) log_log("and no wild to fall back on so no match\n");
        return 0;
      }
if (vb) log_log("so falling back on last wild\n");
      exp_ix = last_wild;
    }
    else
      ++exp_ix;
    ++ln_ix;
  }
  return 0;
}
