Regular Expressions [was: Invalid names]

From: Mark A. Heilpern (heilpern@mindspring.com)
Date: 11/11/99


Funny the regex information should come up, at so timely a point in what
I'm working on :) I've read the man pages on regex recently and, in the
absence of any working examples, decided to shelf it for another time. Lo
and behold, an exaple to go from... Thanks Daniel!

I do have a question on the Mailer Code(tm) you included...  In the area
where you're compiling the expression, the first parameter to regcomp() is
your expression plus an offset i. You don't have a local i defined, though
you do have idx. However, you aren't using it anywhere beyond
initialization to 0. Am I correct in assuming that idx should be
incremented by 1 after each successful call to regcomp()?

Thanks for your input....
mah



At 09:56 PM 11/10/99 -0800, you wrote:
>     void
>     InitializeBadNames (void)
>     {
>         int idx = 0, t, lines = 0;
>         char line[256];
>         FILE *fp;
>
>         if ((fp = fopen(BADNAMES, "r")) == NULL) {
>             log("Could not open %s.  Bad names check disabled.",BADNAMES);
>             return;
>         }
>
>         /* Count the number of bad names then rewind the file. */
>         while (get_line(fp, line)) g_numBadNames++;
>         rewind(fp);
>
>         /* Read the bad names and fill in g_badNames. */
>         CREATE(g_badNames, regexp_t, g_numBadNames+1);
>
>         while ((t = get_line(fp, line))) {
>             lines += t;
>             if (regcomp(g_badNames+i, line, REG_ICASE | REG_NOSUB)) {
>                 log("Error compiling regexp on line %d", lines);
>                 /* Do better error reporting via regerror(). */
>                 exit(1);
>             }
>         }
>
>         log("Loaded %d bad name regular expressions.", lines);
>     }
>
>
>     /*
>         bool BadName (const char *)
>
>         Executes all of the bad name regular expressions on the passed
>         string to determine if it fits any of these.  Returns TRUE if
>         the passed string matches any of the bad name regular expressions
>         and FALSE otherwise (even on error!).
>     */
>
>     bool
>     BadName (const char *name)
>     {
>         int idx = 0;
>
>         for (idx = 0; idx < g_numBadNames; idx++)
>             if (!regexec(g_badNames+idx, name, 0, NULL, 0))
>                 break; /* Matched! */
>
>         return (idx != g_numBadNames);
>     }


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



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