Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
 parse description bug
Today, 9:00 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
Bug in will_fall( )
Oct 23, 2017, 1:35 am
By GatewaySysop
Bug in do_zap( ), do_brandish( )
Oct 18, 2017, 1:52 pm
By GatewaySysop
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Memwatch
Author: Johan Lindh
Submitted by: Vladaar
Users Online
CommonCrawl, Yandex, DotBot

Members: 0
Guests: 18
Stats
Files
Topics
Posts
Members
Newest Member
477
3,706
19,237
608
LAntorcha
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » Codebases » AFKMud Support & Development » Problems with get_line
Forum Rules | Mark all | Recent Posts

Problems with get_line
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Apr 1, 2010, 8:34 pm
Go to the top of the page
Go to the bottom of the page

Atrox
Fledgling
GroupMembers
Posts19
JoinedFeb 14, 2005

It may just be because I'm really tired, but I'm having a small issue with the get_line function not returning the proper size string when colors are involved. I skimmed through it and colorcode, and didn't see any reason why it wouldn't return the right string size, but it's not. I pass it 60, and depending on the amount of colors, it can come out with up to 80 characters. I have a function that uses get_line to go through a chunk of text, line by line, and pad both sides with pipe characters.

	while( !bEnd )
	{
		mudstrlcat( szBuf, "&G|&g ", MSL * 2 );
		nPos = get_line( szText, nWidth-1 );  // It's nWidth-1 because for some reason, get_line sometimes returns more than asked for.

		if( nPos > 0 )
		{
			strncat( szBuf, szText, nPos );
			szText += nPos;
			nLen = nPos;
			while( nLen++ < nWidth )
				mudstrlcat( szBuf, " ", MSL * 2 );
			mudstrlcat( szBuf, " &G|\n\r", MSL * 2 );
		}
		else
		{
			mudstrlcat( szBuf, szText, MSL * 2 );
			nLen = strlen( szText );
			while( nLen++ < nWidth )
				mudstrlcat( szBuf, " ", MSL * 2 );
			mudstrlcat( szBuf, " &G|", MSL * 2 );
			bEnd = true;
		}
	}


It works great for any text without colors, but if I add 1 color code to the mix, it breaks it.

Here are some examples of what it does, and what happens when I use color:

Working Example (No Color)
| Look now shows ships, and has it all nice and boxed like the rest    |
| of it. Next I'll probably be adding items into a boxed list as       |
| well.                                                                |


Example with color (the word functionality is colored red on the mud)[s]:
| Testing out the functionality of get_line in my box_format function.  |
| It SHOULD be working fine, but it's not, and I don't know why.       |


More intense example, every letter of random uses &Z
| RANDOM RANDOM RANDOM RANDOM RANDOM RANDOM RANDOM RANDOM RANDOM RANDOM RANDOM Just demonstrating the fact that
 get_line doesn't work  |
| as intended.                                                         |
       
Post is unread #2 Apr 1, 2010, 10:01 pm
Go to the top of the page
Go to the bottom of the page

Kayle
Off the Edge of the Map
GroupAdministrators
Posts1,195
JoinedMar 21, 2006

Can you post your get_line function?
       
Post is unread #3 Apr 2, 2010, 1:59 pm
Go to the top of the page
Go to the bottom of the page

Atrox
Fledgling
GroupMembers
Posts19
JoinedFeb 14, 2005

It's the stock one from AFKMud 2.0.

int get_line( const char *desc, size_t max_len )
{
	size_t i, j = 0;

	// Return if it's short enough for one line
	if( strlen( desc ) <= max_len )
		return 0;

	// Calculate end point in string without color
	for( i = 0; i <= strlen( desc ); ++i )
	{
		char dst[20];
		int vislen;

		switch ( desc[i] )
		{
			case '&':							// NORMAL, Foreground colour 
			case '{':							// BACKGROUND colour 
			case '}':							// BLINK Foreground colour
				*dst = '\0';
				vislen = 0;
											// Skip input token
				i += colorcode( &desc[i], dst, NULL, 20, &vislen );
				j += vislen;					// Count output token length
				break;						// this was missing - if you have issues, remove it

			default:							// No conversion, just count
				++j;
				break;
		}

		if( j > max_len )
			break;
	}

	// End point is now in i, find the nearest space
	for( j = i; j > 0; --j )
	{
		if( desc[j] == ' ' )
			break;
	}

	// There could be a problem if there are no spaces on the line
	return j + 1;
}
       
Post is unread #4 Apr 2, 2010, 6:24 pm   Last edited Apr 3, 2010, 3:05 pm by Kayle
Go to the top of the page
Go to the bottom of the page

Kayle
Off the Edge of the Map
GroupAdministrators
Posts1,195
JoinedMar 21, 2006

This is the function I have in my codebase:

int get_line( char *desc, size_t max_len )
{
   size_t i, j = 0;

   /*
    * Return if it's short enough for one line 
    */
   if( strlen( desc ) <= max_len )
      return 0;

   /*
    * Calculate end point in string without color 
    */
   for( i = 0; i <= strlen( desc ); ++i )
   {
      char dst[20];
      int vislen;

      switch( desc[i] )
      {
         case '&':  /* NORMAL, Foreground colour */
         case '^':  /* BACKGROUND colour */
         case '}':  /* BLINK Foreground colour */
            *dst = '\0';
            vislen = 0;
            i += colorcode( &desc[i], dst, NULL, 20, &vislen ); /* Skip input token */
            j += vislen; /* Count output token length */
            break;   /* this was missing - if you have issues, remove it */

         default:   /* No conversion, just count */
            ++j;
            break;
      }
      
      if( j > max_len )
         break;
   }

   /*
    * End point is now in i, find the nearest space 
    */
   for( j = i; j > 0; --j )
   {
      if( desc[j] == ' ' )
         break;
   }

   /*
    * There could be a problem if there are no spaces on the line 
    */
   return j + 1;
}


Note that they are similar, but not exact. Try using mine and see if the issue is corrected, if not, we'll dig a little deeper in the issue and see what we can find. This is complete speculation on my part, but I'm guessing that passing it a const char *desc, instead of a char *desc, is causing something to go wrong somewhere. If my function does the same as the stock one, well, we'll know that's not the case.
       
Post is unread #5 Apr 2, 2010, 6:34 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

That function was modified as part of Quixadhal's contribution to the color code, so if it's misbehaving, he'll know that code best. Part of the color tagging may break if you replace it with something different.
       
Post is unread #6 Apr 3, 2010, 2:29 pm
Go to the top of the page
Go to the bottom of the page

Atrox
Fledgling
GroupMembers
Posts19
JoinedFeb 14, 2005

Samson said:

That function was modified as part of Quixadhal's contribution to the color code, so if it's misbehaving, he'll know that code best. Part of the color tagging may break if you replace it with something different.


That's what I was thinking as well. I breezed through the colorcode function, but nothing immediately caught my eye. But it's definitely not counting color codes properly when it gets the length of what it returns. I tried using your code instead Kayle, and it's the same thing. I messed around in get_line a little, and got it working "better", but still not 100%, so I just undid the changes I made, since they were a waste.
       
Pages:<< prev 1 next >>