Color_String.txt
snippets · 5.7 KB · 2010-03-30
Two functions, count_color_string and check_*, for finding the real length of a string while ignoring color codes.
Download « Back to the snippets list
/***********************************************************************
* This snip contains two functions: *
* *
* count_color_string will allow you to parse through color codes and *
* determine what the actual string length is, minus the color codes. *
* *
* check_color_string will return true if the end of the string is not *
* '{' followed by '\0'. This fixes tweeks who like to "gos test{" *
* *
* This code is freely given with many thanks to many people, in the *
* hopes of starting to return the favor. *
* *
* Please be kind and return the favor with your own releases. *
* *
* --Snafu Life. *
***********************************************************************/
/************************************************
* In the beginning there was Diku. Thank You. *
* Then there was Merc. Thank You. *
* Then there was Rom. Thank You. *
* Then there was QuickMud. Thank You. *
* There were also Snips. Thank You. *
* *
* -- Snafu Life. *
************************************************/
/*************************************************************
* File Name: merc.h *
* *
* Place the following in a suitable place. *
* I chose to do a search for act_info.c and place it there. *
* I then placed both functions at the end of act_info.c *
* *
*************************************************************/
int count_color_string args( (char *string ) );
bool check_color_string args( (char *string ) );
/*************************************************************
File Name: act_info.c
Name: count_color_string
Purpose: Returns the string length minus color codes.
Written By: Snafu Life.
*************************************************************/
int count_color_string( char *string )
{
int i,count=0;
char arg[MSL]; /* I have MSL defined as MAX_STRING_LENGTH
* I am, as many others are, LAZY!!!
*/
/* limit the length of string */
strcpy(arg,string);
/* Begin the loop through the string */
for (i=0;i<=MIL;i++) {
/* An out as soon as the end of the string
* is reached, so we don't parse through
* the entire MSL, unnessisarily.
*/
if (arg[i] == '\0') {
break;
}
/* If string[i] isn't '{', then add to count & continue.
* Placed here to save time as most chars won't be '{'
*/
else if(string[i] != '{' ) {
count++;
continue;
}
/* If arg[i] and arg[i+1] is '{', add only one
* to the count and skip the second '{'
*/
if (arg[i] == '{' && arg[i+1] == '{' ) {
i++;
count++;
continue;
}
/* If arg[i] is '{' and arg[i+1] isn't '{' or '\0'
* Don't add to the count but skip the next char.
*/
else if(arg[i] == '{' && arg[i+1] != '{' && arg[i+1] != '\0') {
i += 1;
continue;
}
/* If arg[i] is '{' and arg[i+1] is '\0'
* stop the procession and don't count the char.
*
*/
else if(arg[i] == '{' && arg[i+1] != '{' && arg[i+1] == '\0') {
break;
}
/* isn't need, but makes me feel better ;) */
else {
count++;
continue;
}
}
return count;
}
/*
* NOTE: You might want to add bool check_color_string
*
* If you pre-color define your gossips,who, etc. By typing { at
* the end you "litter"ally bypass the color.
*
* Example: gos test
* Result: {CYou gossip '{Rtest{C'{x
* As Shown: You gossip 'test'
*
* Example: gos test{
* Result: {CYou gossip '{Rtest{{C'{x
* As Shown: You gossip 'test{C'
*
* Bool check_color returns false if '{' is at the end.
*/
/*************************************************************
File Name: act_info.c
Name: check_color_string
Purpose: Returns true if end of string is '{'
Written By: Snafu Life.
*************************************************************/
bool check_color_string(char *string )
{
int i;
/* Sanity check for empty strings */
if(string[0] == '\0') {
return FALSE;
}
/* Start looping through *string */
for(i=0;i<strlen(string);i++) {
/* When the end is reached, immediately quit
and return false */
if(string[i] == '\0') {
return FALSE;
}
/* If string[i] isn't '{', then continue.
* Placed here to save time as most chars won't be
*/
else if(string[i] != '{' ) {
continue;
}
/* If string[i] is '{' and string[i+1] isn't '\0'
* Skip the next char to save time
*/
else if(string[i] == '{' && string[i+1] != '\0') {
i++;
continue;
}
/* If string[i] is '{' and string[i+1] is '\0'
* Whoa, wrong, return true.
*/
else if(string[i] == '{' && string[i+1] == '\0' ) {
return TRUE;
}
/* Isn't needed, but makes me feel better ;)
*/
else { continue; }
}
return FALSE;
}
/*********************************************************
That is all there is to it.
-- Snafu Life.
*********************************************************/