Ok, first of all I would like to thank you for your reply and effort
writing this new function. Second there's a problem with it. =)
Ohh and btw of course I had to change the p_name pointer to a constant
otherwise it wouldn't let me compile with my compiler flags.
Now to the problem, this function crash the MUD.

Here's the GDB info:
Program received signal SIGSEGV, Segmentation fault.
0x0804bf71 in color_string (name=0x402b15d4 "Dahkneth", colors=0x8067e90
"kwW")
    at act_info.c:1044
1044            color_buf[i++] = *p_name;

(gdb) bt

#0  0x0804bf71 in color_string (name=0x402b15d4 "Dahkneth",
    colors=0x8067e90 "kwW") at act_info.c:1044
#1  0x0804c00d in who_show_char (ch=0x402aff8c, wch=0x1, buf=0x402b21b0)
    at act_info.c:1178
#2  0x0804c179 in do_who (ch=0x402aff8c, argument=0x402af5e0 "")
    at act_info.c:1232
#3  0x0805ebef in interpret (ch=0x402aff8c, argument=0x402af5e0 "")
    at interp.c:1398
#4  0x08056033 in game_loop (control=4) at comm.c:381
#5  0x08055c64 in main (argc=4, argv=0xbffff994) at comm.c:206
#6  0x4008d76c in __libc_start_main () from /lib/libc.so.6

-----Ursprungligt meddelande-----
Från: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] För Jeremy Hill
Skickat: den 6 maj 2004 06:50
Till: [email protected]
Ämne: Re: colorize_string

>Er, return for the error condition wasn't valid, here's fixed:
>
>char *colorize_string(const char *name, const char *colors)
>{
>    static char color_buf[2*MSL];
>    char *p_name = name; //may need to make this const char *p_name
>    int i = 0;
>    int j = 0;
>
>    color_buf[0] = '\0';
>
>    if (colors[0] == '\0' || name[0] == '\0' ||
>        (strlen (name) + strlen (colors) > MSL))
>        return color_buf;
>
>    while (p_name)
>    {
>        color_buf[i++] = '&';
>        color_buf[i++] = colors[j++];
>        j %= strlen(colors);
>        color_buf[i++] = *p_name++;
I changed this to color_buf[i++] = *p_name; followed by p_name++; to be
safe as you wrote, it crashed anyway. I've even tried playing without
constants and it'll crash anyway here.

>    }
>
>    color_buf[i] = '\0';
>    return color_buf;
>}


> char *colorize_string(const char *name, const char *colors)
> {
>     static char color_buf[2*MSL];
>     char *p_name = name; //may need to make this const char *p_name
>     int i = 0;
>     int j = 0;
>
>     if (colors[0] == '\0' || name[0] == '\0' ||
>         (strlen (name) + strlen (colors) > MSL))
>         return;
>
>     color_buf[0] = '\0';
>
>     while (p_name)
>     {
>         color_buf[i++] = '&';
>         color_buf[i++] = colors[j++];
>         j %= strlen(colors);
>         color_buf[i++] = *p_name++;
>     }
>
>     color_buf[i] = '\0';
>     return color_buf;
> }


----- Original Message ----- 
From: "Jeremy Hill" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Wednesday, May 05, 2004 11:43 PM
Subject: Re: colorize_string


> char *colorize_string(const char *name, const char *colors)
> {
>     static char color_buf[2*MSL];
>     char *p_name = name; //may need to make this const char *p_name
>     int i = 0;
>     int j = 0;
>
>     if (colors[0] == '\0' || name[0] == '\0' ||
>         (strlen (name) + strlen (colors) > MSL))
>         return;
>
>     color_buf[0] = '\0';
>
>     while (p_name)
>     {
>         color_buf[i++] = '&';
>         color_buf[i++] = colors[j++];
>         j %= strlen(colors);
>         color_buf[i++] = *p_name++;
>     }
>
>     color_buf[i] = '\0';
>     return color_buf;
> }
>
> One word of warning:
> *pname++
> is not ANSI defined.  If you want to be safe, do:
>         color_buf[i++] = *p_name;
>         p_name++;
>
> Also, not sure if it's
>         j %= strlen(colors);
> or
>         j %= strlen(colors) - 1;
>
>
> To call:
>
> colorize_string(name, "Rrw");
>
>
> ----- Original Message ----- 
> From: "Robin Björklund" <[EMAIL PROTECTED]>
> To: <[email protected]>
> Sent: Wednesday, May 05, 2004 11:00 PM
> Subject: colorize_string
>
>
> Hi, sorry to bother you guys.
> I'm still new to coding and you'll notice if you take a look on the
code
> I'm posting below.
> I use this piece of code to colorize a name 3 - 12 letters.
>
> For example if my name is Desindit and I would like to colorize it.
> I would do something like colorize_string(name, "R", "r", "w");
> And the string would become "&RDe&rs&win&rd&Rit"
> If you notice this code isn't finished yet, since it only colorize
> strings with 8 letters. Though looking at my code
> It's pretty much a mess in my eyes, I figured I should use some loops
to
> colorize the strings, but since the colorizing will be different
> depending on how long the name is, I figured it would still be a mess
> using loops.
> I would be greatful to any help and comments to the mess. Ohh and I
> better get some sleep now and when I wake up I'll realize how stupid I
> was that I didn't get some sleep before writing code. ;)
>
> char *colorize_string (const char *str, const char *c1, const char
*c2,
> const char *c3 )
> {
> char newbuf[MSL];
> static char tempbuf[MSL];
>
> tempbuf[0] = '\0';
>
> if (strlen(str) == 7)
> {
> sprintf(newbuf, "&%c%c", *c1, str[0]);
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "%c", str[1]);
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "&%c%c", *c2, str[2]);
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "&%c%c", *c3, str[3]);
>
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "&%c%c", *c2, str[4]);
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "&%c%c", *c1, str[5]);
>
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "%c", str[6]);
> strcat(tempbuf, newbuf);
> }
>
> if (strlen(str) == 8)
> {
> sprintf(newbuf, "&%c%c", *c1, str[0]);
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "%c", str[1]);
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "&%c%c", *c2, str[2]);
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "&%c%c", *c3, str[3]);
>
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "%c", str[4]);
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "&%c%c", *c2, str[5]);
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "&%c%c", *c1, str[6]);
>
> strcat(tempbuf, newbuf);
> sprintf(newbuf, "%c", str[7]);
> strcat(tempbuf, newbuf);
> }
>
> return tempbuf;
> }
>
>
> -- 
> ROM mailing list
> [email protected]
> http://www.rom.org/cgi-bin/mailman/listinfo/rom
>
>
> -- 
> ROM mailing list
> [email protected]
> http://www.rom.org/cgi-bin/mailman/listinfo/rom


-- 
ROM mailing list
[email protected]
http://www.rom.org/cgi-bin/mailman/listinfo/rom

__________ NOD32 1.751 (20040505) Information __________

This message was checked by NOD32 antivirus system.
http://www.nod32.com



Reply via email to