Echo.. echo... echo?!

Posted by Frobozz on Thu 18 Nov 2004 05:48 AM — 7 posts, 28,372 views.

#0
Goal:

Be able to echo to the mud more than one string of text at a time using the do_echo command.

Problem:

No idea how to either allow multiple lines of input OR
add on something like a text editor, similar to the one used in describe or redit desc.

Solution?

USA #1
I'm not sure what you mean. Can you provide an example? If you want to echo multiple lines, just call the do_echo function multiple times in the same function, one after another. For example...

do_echo( ch, "line 1" );
do_echo( ch, "line 2" );
USA #2
I know this is a cheesy way around it, but you could make a new command like do_decho and make it have howevery many arguments you want it to have. EG:

send_to_char( arg1 )
send_to_char (arg2)
so on and so on...so it'd be like...

decho "whatever" "whatever2" and it would show up like

whatever
whatever2

i know that's flawed, but it's general idea.

Canada #3
You could copy do_bio, modify it to to be appropriate, and then have it send as the string argument to do_echo the return of the copy_buffer.
USA #4
Don't forget to free the string and all that appropriately, else you'll be leaking little droplets of memory every time you use the function.

Or, if the lines are simple enough, you could just be using \n markers in the string and parse out "\n" to be an actual newline character.

e.g.

multiecho Hello there!\nThis is on a new line.\nAnd this is on the third line.

would print -->

Hello there!
This is one a new line.
And this is on the third line.

It's a pretty straightforward parsing algorithm to go through and replace literal "\n" with a newline. It's only slightly more complex if you want to be able to have literal \n in your output, in which case you would use an escape system e.g. "\\n" translations to "\n", not a newline.
Canada #5
Quite true, I guess I shouldn't assume everyone knows for copy_buffer works.

What you want is probably something like this to go with what Ksilyan suggested:
char * convert_newline(char *str)
{
        static char ret[MAX_STRING_LENGTH];
        char     *retptr;

        retptr = ret;
        for (; *str != '\0'; str++)
        {
            if (*str == '\\')
            {
                if ( *(str-1) == '\\' )
                    continue;
                else if ( *(str+1) == 'n' )
                {
                    *retptr = '\n';
                    retptr++;
                    str++;
                    continue;
                }
            }
            *retptr = *str;
            retptr++;
        }
        *retptr = '\0';
        return ret;
}


Then all you have to do is modify do_echo like this:
        echo_to_all(color, convert_newline(argument), target);
This is what you would see:

echo testmore
testmore

echo test\nmore
test
more

echo test\\nmore
test\nmore

Hope that helps

[EDIT] I modified this slightly, this will be a little more effeicent.
Amended on Sat 20 Nov 2004 07:21 PM by Greven
Australia #6
if ( *(str-1) == '\\' )

If str hasn't been increased yet (ie. you're at the start of the string), then str - 1 would point to no-man's land. Might want to fix that.