[DGD]*sighs* I hate feelin like a newbie again... :P

BladeDarkmour at aol.com BladeDarkmour at aol.com
Tue Apr 25 03:57:54 CEST 2000


Allo peeps.. Yet another newbish question from me.
Ok, I'm trying to set it up so that I can type "say `ryadda yadda" and that 
would output, assuming the recipient is capable of ansi colors, yadda yadda 
in dark red.  the actual C code to do this for the various ` codes is 
simple... just manipulation of a char*, but when I try to put this into the 
driver, it either a) segfaults, or b)returns garbage anytime I use an escape 
code.  I've tried doing this 3 diff ways... one putting it in conn_write in 
connect.c, b) comm_write in comm.c, and c) making a seperate function, 
parse_color(char* txt, int length), which i tried to use in the above 
functions... none of hte ways worked.  I've fixed the line in comm_write so 
that itll let out the ansi escape code, (adding && *p != ESC and defining 
ESC...).  I'll include the code to parse the color below. *Grumbles to 
himself about feelin like a dunce*  Think you people could take a look and 
offer a few suggestions?

Yes, this could be done more cleanly, but it suffices:

/*
 * NAME:    parse_color()
 * DESCRIPTION: parse ansi color codes
 */
char* parse_color(char* txt, int length)
{
    int i, k;
    char ch, *tmptxt;
    /* Expand color codes */
    for( i = 0, k = 0; i < length; i++ )
    {       
        /* See if expansion necessary */
        if ( txt[i] != '`' )
            tmptxt[k++] = txt[i];
        else
        {
            /* Insert general ANSI codes */
            tmptxt[k++] = 27;
            tmptxt[k++] = '[';
            tmptxt[k++] = '0';
            tmptxt[k++] = ';';
           
            /* Get second character of code */
            ch = txt[++i];
            switch( ch )
            {
            /* Deal with double "`" */
            case '`' :
                k -= 4;
                tmptxt[k++] = '`';
                continue;
            /* Deal with lower case */
            case 'r' :
                tmptxt[k++] = '3';
                tmptxt[k++] = '1';
                break;
            case 'g' :
                tmptxt[k++] = '3';
                tmptxt[k++] = '2';
                break;
            case 'y' :
                tmptxt[k++] = '3';
                tmptxt[k++] = '3';
                break;
            case 'b' :
                tmptxt[k++] = '3';
                tmptxt[k++] = '4';
                break;
            case 'm' :
                tmptxt[k++] = '3';
                tmptxt[k++] = '5';
                break;
            case 'c' :
                tmptxt[k++] = '3';
                tmptxt[k++] = '6';
                break;
            case 'w' :
                tmptxt[k++] = '3';
                tmptxt[k++] = '7';
                break;
            /* Deal with black */
            case 'l' :
                tmptxt[k++] = '1';
                tmptxt[k++] = ';';
                tmptxt[k++] = '3';
                tmptxt[k++] = '0';
                break;
            /* Deal with upper case */
            case 'R' :
                tmptxt[k++] = '1';
                tmptxt[k++] = ';';
                tmptxt[k++] = '3';
                tmptxt[k++] = '1';
                break;
            case 'G' :
                tmptxt[k++] = '1';
                tmptxt[k++] = ';';
                tmptxt[k++] = '3';
                tmptxt[k++] = '2';
                break;
            case 'Y' :
                tmptxt[k++] = '1';
                tmptxt[k++] = ';';
                tmptxt[k++] = '3';
                tmptxt[k++] = '3';
                break;
            case 'B' :
                tmptxt[k++] = '1';
                tmptxt[k++] = ';';
                tmptxt[k++] = '3';
                tmptxt[k++] = '4';
                break;
            case 'M' :
                tmptxt[k++] = '1';
                tmptxt[k++] = ';';
                tmptxt[k++] = '3';
                tmptxt[k++] = '5';
                break;
            case 'C' :
                tmptxt[k++] = '1';
                tmptxt[k++] = ';';
                tmptxt[k++] = '3';
                tmptxt[k++] = '6';
                break;
            case 'W' :
                tmptxt[k++] = '1';
                tmptxt[k++] = ';';
                tmptxt[k++] = '3';
                tmptxt[k++] = '7';
                break;
            default :
                break;
            }
            /* Finish ANSI code */
            tmptxt[k++] = 'm';
        }
    }
    
    return txt;
}

Thanx for any forthcoming help,
JD

List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list