|
iDirt has lots of strings with embedded color codes, especially if your zone authors are liberal
in their use of color. Unforunately, there are also a number of places in iDirt that try to
chop a string to a particular size -- and fail miserably, because of those color codes.
The following function is something I whipped out last night and then beat on some more this morning. It's use is to return a shortened version of a string with color codes into a new buffer and it ignores the following color code formats when doing so:
These are the only color codes in use at my MUD; looking at the code, it should be obvious how to extend it to cover other color code situations.
char * strntrnc(char * dest, char * target, long length) {
long pos = 0;
long currLen = 0;
memset( dest, 0, strlen(target)+1 );
strncpy( dest, target, strlen(target) );
while ( dest[pos] != '\0' && currLen < length ) {
if ( dest[pos] == '&' ) {
++pos;
switch( dest[pos] ) {
case '*': // disable color codes; also fall through
case 'B': // fall through
case 'b': // blink
pos+=1;
break;
case '+': // fall through
case '-': // 1 color argument
pos+=2;
break;
case '=': // 2 color arguments
pos+=3;
break;
}
} else {
++pos;
++currLen;
}
}
dest[pos] = '\0';
return dest;
}
|