Re: Cursing code

From: Mysidia (jmhess@i-55.com)
Date: 05/14/03


On Tue, May 13, 2003 at 10:23:01AM -0400, Templar Viper wrote:
> A while ago, I fixed this code together. It checks the argument for
> cursing (placed in curse_list), and replaces it with a harmless beep. I
> had to use str_strr, as strstr isn't case sensitive. However, I want to
> ignore certain characters, such as spaces, full stops and more of those.
> Here is the function:
[snip]

Here's a possibility for some functions to handle whitespace:


/* Return number characters in the string to skip starting at p[0] */
int curse_skip_chars(p)
      char *p;
{
        if (p[0] == ' ' || p[0] == '\t')
                return 1;
        return 0;
}

/* Replace curse words */

int curse_replace( data, curses, curse_count, replace )
   char *data;
   char **curses;
   int  curse_count;
   int  replace;
{
    char *p, *q;
    int i, j, l, v, m;
    int word_start = 1;

    const char beep[] = { '*', 'b', 'e', 'e', 'p', '*' };

    if (!data || !curses)
        return;

    /* Start searching */
    for( p = data ; *p; p++) {
        /* Curses don't start at the end */
        if (*p == ' ' || *p == '\t')
            word_start = 1;

        if (*(p+1) == '\0')
            continue;

        /* Skip characters */
        if ((m = curse_skip_chars(p)) > 0) {
            p += (m - 1);
            continue;
        }

/* Uncomment to restrict matches to start at the beginning of a word */
/*        if (word_start)*/
        for( i = 0, j = 0, v = 0; i < curse_count; i++) {
                   /* See if this letter possibly begins a curse */
            if (UPPER(*p) == UPPER(*curses[i]))
            {
                l = strlen(curses[i]);
                for(q = p; *q && j < l; q++) {
                    /* Skip characters in the search */
                    if ((m = curse_skip_chars(q)) > 0) {
                        q += (m - 1);
                        continue;
                    }

                    /* It's not the curse */
                    if (UPPER(*q) != UPPER(curses[i][j]))
                        break;
                    j++;
                }

                /* j < l, not a full match */
                if (j < l)
                    continue;
                else if (replace) {
                      /* If applicable replace curse */
                    if (*q) {
                        memset(p, '*', v=(int)(q-p));
                    }
                    else if ((int)(q - p) > 0) {
                        memset(p, '*', v=(int)(q-p-1));
                    }
                    else v = 0;

                       /* If it will fit, write out the 'beep' */
                    if (v >= sizeof beep) {
                        memcpy(p, beep, sizeof beep);
                    }

                        /* Return 1 to indicate curse found*/
                    return (1);
                }
                else return (1);
            }
        word_start = 0;
        }
    }

     /* No curse found */
    return (0);
}

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/26/03 PDT