/* lwrmatch.c */

/* Copyright 1997 by Eberhard Mattes <mattes@azu.informatik.uni-stuttgart.de>
   Donated to the public domain.  No warranty.

   1997-09-06 Initial version */

#include <stdlib.h>
#include "libemfw.h"

/* Compare NAME (NAME_LEN characters) against PATTERN (PATTERN_LEN
   characters), which may contain "*" for matching any sequence of
   characters.  Return 1 on match, 0 on mismatch. */

int lower_match (const char *pattern, size_t pattern_len,
		 const char *name, size_t name_len)
{
  DEBUG_ASSERT (pattern != NULL);
  DEBUG_ASSERT (name != NULL);

  while (pattern_len != 0 && *pattern != '*')
    {
      if (name_len == 0 || (lower_table[(unsigned char)*pattern]
			    != lower_table[(unsigned char)*name]))
	return 0;
      ++pattern; --pattern_len;
      ++name; --name_len;
    }
  if (pattern_len == 0)
    return name_len == 0;

  while (pattern_len != 0 && *pattern == '*')
    {
      ++pattern; --pattern_len;
    }
  if (pattern_len == 0)
    return 1;
  while (name_len != 0)
    {
      if (lower_match (pattern, pattern_len, name, name_len))
	return 1;
      ++name; --name_len;
    }
  return 0;
}
