stripclr snippet

Posted by Asean Novari on Fri 04 Mar 2005 02:32 AM — 21 posts, 75,275 views.

USA #0
I would appreciate some assistance in adding this snippet to swr1fuss..

/*
 * stripclr - removes the color codes from a string.
 * 
 * Notice: currently setup for the default smaug/swr color code system,
 * if you do not use these, then you will have to do some editing.
 *
 * This function should properly remove the color codes from a string
 * Not sure if its any use to anyone, but i thought i'd give it out.
 * 
 * I'm not asking for my name to be displayed, but i would like to have my name at
 * least still attached to this function. Altho, it certainly would be nice.
 *
 * Feel free to contact me if you have any problems.
 *
 * - Gavin - ur_gavin@hotmail.com
 * - Unknown Regions - http://ur.lynker.com 
 */
 * - Gavin - ur_gavin@hotmail.com
 * - Unknown Regions - http://ur.lynker.com 
 */
char *stripclr( char *text )
{
	int i = 0, j = 0;

	if (!text || text[0] == '\0')
	{
		return NULL;
	}
	else
	{
		char *buf;
		static char done[MAX_INPUT_LENGTH*2];

		done[0] = '\0';

		if ( (buf = (char *)malloc( strlen(text) * sizeof(text) )) == NULL)
			return text;      

		/* Loop through until you've hit your terminating 0 */
		while (text[i] != '\0')
		{
			while (text[i] == '&')
			{
				i += 2;
			}
			if ( text[i] != '\0' )
			{
				if ( isspace(text[i]) )
				{
					buf[j] = ' ';
					i++;
					j++;
				}
				else
				{
					buf[j] = text[i];
					i++;
					j++;
				}
			}
			else
				buf[j] = '\0';
		}

		buf[j] = '\0';

		sprintf(done, "%s", buf);
		buf = realloc(buf, j*sizeof(char));
		free( buf);

		return done;
	}
}



currently this code is in a file called stripclr.c
do i add this to my src directory and then put stripclr into the includes in the makefile?...
Amended on Fri 04 Mar 2005 07:27 PM by Asean Novari
USA #1
Quote:
then put stripclr into the includes in the makefile
I'm not sure what exactly you mean by this, but if you mean adding it to the lists of files at the top of the makefile, yes, that's what you have to do.
USA #2
Ok ive done that now.. When i Try to makefile.. i get this series of errors..

Administrator@Home ~/swr1fuss/src
$ make
make -s swreality
  Compiling o/stripclr.o....
stripclr.c: In function `stripclr':
stripclr.c:24: error: `NULL' undeclared (first use in this function)
stripclr.c:24: error: (Each undeclared identifier is reported only once
stripclr.c:24: error: for each function it appears in.)
stripclr.c:29: error: `MAX_INPUT_LENGTH' undeclared (first use in this function)
stripclr.c:33: warning: implicit declaration of function `malloc'
stripclr.c:33: warning: implicit declaration of function `strlen'
stripclr.c:45: warning: implicit declaration of function `isspace'
stripclr.c:64: warning: implicit declaration of function `sprintf'
stripclr.c:65: warning: implicit declaration of function `realloc'
stripclr.c:65: warning: assignment makes pointer from integer without a cast
stripclr.c:66: warning: implicit declaration of function `free'
stripclr.c:29: warning: unused variable `done'
stripclr.c:29: error: storage size of `done' isn't known
make[1]: *** [o/stripclr.o] Error 1
make: *** [all] Error 2


Once again I am a novice coder and apologize if i do not understand the errors right away.

USA #3
What did the instructions say to do? Did the code come with the function in its own file?

This would go away if you included mud.h at the top of the .c file but please also answer the questions. :)
USA #4
No instructions were given as to how to install it..
This is the site i got it from http://www.geocities.com/gendi_uk/areas.html

The code was all in a file called stripclr.c

Trying your suggestion on the #include "mud.h" thing it gave more errors upon trying to make..


Administrator@Home ~/swr1fuss/src
$ make
make -s swreality
  Compiling o/stripclr.o....
In file included from mud.h:294,
                 from stripclr.c:19:
i3.h:388: error: parse error before "FILE"
i3.h:388: warning: function declaration isn't a prototype
i3.h:389: error: parse error before "FILE"
i3.h:389: warning: function declaration isn't a prototype
In file included from mud.h:297,
                 from stripclr.c:19:
imc.h:406: error: parse error before "FILE"
imc.h:406: warning: function declaration isn't a prototype
imc.h:407: error: parse error before "FILE"
imc.h:407: warning: function declaration isn't a prototype
In file included from stripclr.c:19:
mud.h:3167: error: parse error before '*' token
mud.h:3167: warning: type defaults to `int' in declaration of `fpArea'
mud.h:3167: warning: data definition has no type or storage class
mud.h:3299: error: parse error before '*' token
mud.h:3299: warning: type defaults to `int' in declaration of `fpReserve'
mud.h:3299: warning: data definition has no type or storage class
mud.h:3300: error: parse error before '*' token
mud.h:3300: warning: type defaults to `int' in declaration of `fpLOG'
mud.h:3300: warning: data definition has no type or storage class
mud.h:4155: error: parse error before '*' token
mud.h:4155: warning: function declaration isn't a prototype
mud.h:4156: error: parse error before '*' token
mud.h:4156: warning: function declaration isn't a prototype
mud.h:4157: error: parse error before '*' token
mud.h:4157: warning: function declaration isn't a prototype
mud.h:4158: error: parse error before '*' token
mud.h:4158: warning: function declaration isn't a prototype
mud.h:4159: error: parse error before '*' token
mud.h:4159: warning: function declaration isn't a prototype
mud.h:4160: error: parse error before '*' token
mud.h:4160: warning: function declaration isn't a prototype
mud.h:4161: error: parse error before '*' token
mud.h:4161: warning: function declaration isn't a prototype
mud.h:4472: error: parse error before "FILE"
mud.h:4472: warning: function declaration isn't a prototype
mud.h:4473: error: parse error before "FILE"
mud.h:4473: warning: function declaration isn't a prototype
stripclr.c: In function `stripclr':
stripclr.c:36: warning: implicit declaration of function `strlen'
stripclr.c:48: warning: implicit declaration of function `isspace'
stripclr.c:67: warning: implicit declaration of function `sprintf'
make[1]: *** [o/stripclr.o] Error 1
make: *** [all] Error 2
Amended on Fri 04 Mar 2005 03:42 AM by Asean Novari
USA #5
Well how about moving the function to an existing file? Like comm.c or the sort. That way you don't need to include mud.h and so on.
USA #6
Interesting random note regarding Firefox and code tags. As I read this thread, the first code block was normal, second small, third normal. As I reply to it, the first and second are small and the third is normal. Very, very weird...

As for the problem, the simplest is to just stick it into comm.c if you don't want to figure out and fix the mistakes.
USA #7
Where in comm.c do i stick it?.. I put it in in more or less
a random spot in between functions and when i try to make i
get over 4k redundancy warnings for ALL of the different
commands in the game and on top of that i also get alot of
errors..
Amended on Fri 04 Mar 2005 09:39 PM by Asean Novari
USA #8
If that was the whole file you pasted then you are missing a few things. The include to mud.h is one of them, though it may not be important. Secondly you need:

#include <stdio.h>

As I believe that it is not included in mud.h and it will give you errors such as what you posted. Try throwing that into the top and see how things go.

USA #9
This'll support Smaug too right? I don't see any SWR specific functions. I've been meaning to make a strclr function.
USA #10
I have given up with placing it anywhere else other than
Color.c as the other locations give too many errors that I do
not wish to deal with.

Administrator@Home ~/swr1fuss/src
$ make
make -s swreality
  Compiling o/color.o....
color.c: In function `stripclr':
color.c:1454: warning: implicit declaration of function `isspace'
make[1]: *** [o/color.o] Error 1
make: *** [all] Error 2

*snippet placed at end of color.c*

Any help with getting rid of this error would be greatly appreciated..
USA #11
Just add the function at the bottom of any file. Add this at the top of the file:
#include <ctype.h>
USA #12
Ok thanks Zeno doing that made it compile without any errors.. but now how do i use it?...

I make an item with color codes in it but it also puts those
color codes in the name where they shouldnt be.. Otherwise
you have to enter the color codes with the name of the item
when you try to do anything with it..
USA #13
How are you making the object? If you're using colors in the name field, you shouldn't. The name field on objects are the keywords for the object. If you want to use color, use it with the short and long desc. As for the function, something like this can be done:
stripclr( buf );
Canada #14
Well, in SWR, for example, on of the problems was when people put colored names to items with the engineering skills. A fix is to do this in do_makecontainer

if ( obj->name )
    STRFREE(obj->name);
obj->name = STRALLOC(stripclr(arg1) );

Now, don't quote me on the exact argument variables since I'm at work, but thats basically how its used.

BTW, Gavin codes on the same mud as me, so I'll let him know and, if possible, see if he can get it updated for gendi's site.


However, I suggest you take a look at this:

http://www.swreality.net/color.htm
Amended on Fri 04 Mar 2005 11:20 PM by Greven
USA #15
Thanks for that website Greven... works great..
USA #16
Ugh, I just had a crash with this snippet.
#0  0x008a7402 in __kernel_vsyscall ()
#1  0x00979118 in raise () from /lib/libc.so.6
#2  0x0097a888 in abort () from /lib/libc.so.6
#3  0x009ae22a in __libc_message () from /lib/libc.so.6
#4  0x009b6045 in _int_realloc () from /lib/libc.so.6
#5  0x009b6a30 in realloc () from /lib/libc.so.6
#6  0x080c33bb in stripclr (text=0xbffc5349 "ok ^.^&") at comm.c:4929
#7  0x0804a29f in talk_channel (ch=0x9ca52d8, argument=0xbffc5349 "ok ^.^&", channel=2048, verb=0x81c6563 "clantalk")
    at act_comm.c:425
#8  0x0804b1f4 in do_clantalk (ch=0x9ca52d8, argument=0xbffc5349 "ok ^.^&") at act_comm.c:740
#9  0x080e9ea1 in interpret (ch=0x9ca52d8, argument=0xbffc5349 "ok ^.^&") at interp.c:585
#10 0x080bcb46 in game_loop () at comm.c:1226
#11 0x080bbee7 in main (argc=2, argv=0xbffc68a4) at comm.c:590

frame 6:
(gdb) info local
buf = 0x9f06c68 "ok ^.^ me and take me to the fire u dork"
done = "ok ^.^ me and take me to the fire u dork\000k\000rth\000eak in front of such beauty\000and over the glass to hear him\000third <if any> then I will start placing my rooms/links/names/desc and such", '\0' <repeats 1866 times>
i = 42
j = 40
USA #17
Looks like it barfs when the '&' is at the end of the string and doesn't have a color code following it. The snippet doesn't check for edge cases, which is a big nono. Fixing it is a relatively simple matter of making sure that as you traverse the string, you don't stop over the bounds.
USA #18
Ah yes that explains it, I understand why it crashed now. How would I check it? Something like:
if (text[i++] != '\0')

Should do it?
Canada #19
Sorry ^^

That was really old code, i write better stuff now :)
USA #20
Zeno: I would personally get the length of the string at the beginning and use that as a reference, e.g. i < len. You have to be very careful when you do things like i++.

It has while str\[i\] == &, i+=2. But, what if the last character is &? You'll end up stepping right over the end of the string ... including the \0 at the end of it.