new isname() function

From: d. hall (dhall@sunspot.tiac.net)
Date: 12/15/95


so far it's proven to be a drop in replacement for isname already in
hander.c.  the following file is intended as a pattern test.  change
<string.h> to which ever is compliant on your platform.  notes, comments
and critique via email.

d.

----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LOWER(c)   (((c)>='A'  && (c) <= 'Z') ? ((c)+('a'-'A')) : (c))

/* 12.15/95 dhall: attempt to incorporate "dot notation" */

int
isname(textlist, namelist)
     char *textlist;
     char *namelist;
{
  register char *cptr, *sptr;
  char *curname;
  short active, search, string;
  
  /* the active flag determines if we search for next element  */
  /* in textlist, or else seeking current element substring in */
  /* namelist					               */

  active = 0;

  /* for each element of text, we need to traverse the list */
  /* curname is the placeholder for the current element in  */
  /* textlist, this is required since each attempted match  */
  /* with namelist requires a default string position       */

  for (curname = textlist; *curname; curname++)
  {
    /* status check */

    if (active)
    {
      if (*curname == '.')
	active = 0;
      continue;
    }

    /* preceding periods will always match, skip these */

    if (*curname == '.')
      continue;

    /* the search flag determines if we search for next element */
    /* in namelist to compare against curname element           */
    
    search = 0;

    /* string is a boolean status flag for namelist, this was done   */
    /* since a string check at the start of the loop would not check */
    /* against the curname element, therefore the last element would */
    /* be truncated by 1                                             */

    string = 1;

    /* saving curname as default */
    
    cptr = curname;
    
    for (sptr = namelist; string; sptr++)
    {

      /* status check */
      if (search)
      {
	if (*sptr == ' ')
	{
	  /* reset element to default */
	  cptr = curname;
	  search = 0;
	}

	/* need to stop search mode when hit NULL */

	if (*sptr != '\0')
	  continue;
      }

      /* active is used here as a "short circuit" variable, since if   */
      /* both textlist element and namelist element match then we want */
      /* to break out with active true allowing the outer loop to seek */
      /* its next element */

      switch (*sptr)
      {
      case '\0':
	string = 0;
      case ' ':
	active = 1;
	break;
      default:
	/* set search flag true if nonmatch */
	if (LOWER(*sptr) != LOWER(*cptr))
	  search = 1;
	break;
      }

      if (*cptr == '.' || *cptr == '\0')
      {
	/* only break if both conditions are true */

	if (active)
	  break;

	if (*cptr == '\0')
	  active = 1;

	search = 0;
      }
      
      /* if active is true, the only one of two conditions was true  */
      /* meaning the substring did not match, but do not immediately */
      /* return false, since this is only the interior loop          */

      if (active)
      {
	cptr = curname;
	active = 0;
      }
      else
	/* increment if traversing current namelist element */
	
	cptr++;
    }

    /* since active "fell through" as false, then textlist element */
    /* was not found in entire namelist, so we can return false    */

    if (!active)
      return (0);
  }

  /* return true since we passed all the checks */
  return (1);
}

void
main()
{
  char *tlist = "red orange yellow green blue indigo violet";

  char *tname[] =
  { 
    "red",
    "RED",
    "ReD",
    "red.orange",
    "red.orang",
    "red.orangee",
    "..red",
    "red..",
    "..red..violet...",
    "red.orange.violet",
    "crimson",
    "crimson.red",
    NULL
  };

  short i;
  
  printf ("$LIST = %s\n", tlist);
  
  for (i = 0; tname[i] != NULL; i++)
    printf ("$LIST ~= /%s/ : %d\n", tname[i], isname (tname[i], tlist));
}



This archive was generated by hypermail 2b30 : 12/07/00 PST