[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  ROM
. -> [Folder]  Running the server
. . -> [Subject]  Color codes counting as characters in strings
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Color codes counting as characters in strings

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Lmclarke   (13 posts)  [Biography] bio
Date Sun 17 Apr 2016 02:03 AM (UTC)

Amended on Sun 17 Apr 2016 02:18 AM (UTC) by Lmclarke

Message
What I'm trying to accomplish is this:

I have a character limit on objects' short descriptions. It works fine -- restricts the short desc to 59 characters.

However, it's counting color codes. So while it might be 59 visible characters ("a pair of blue jeans with some noticeable wear on the knees"), adding color to it ("a pair of #cblue jeans#n with some noticeable wear on the knees") counts #c and #n as 4 extra characters, thus disallowing it.

How does one make it so color codes aren't counted? Does anyone have any experience with this?
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sun 17 Apr 2016 06:11 AM (UTC)
Message
In your code where you test for the maximum length, make a special version of "strlen" which skips over "#x" sequences.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Lmclarke   (13 posts)  [Biography] bio
Date Reply #2 on Sun 17 Apr 2016 06:23 AM (UTC)
Message
What I have:


	if (!str_cmp (arg2, "room") || !str_cmp (arg2, "short"))
	{
		if (strlen (argument) > 59)
		{
			send_to_char ("Short descriptions should be 59 characters.\n\r", ch);
			return;
		}
		endchar[0] = (argument[strlen (argument) - 1]);
		endchar[1] = '\0';
		free_string (obj->short_descr);
		obj->short_descr = str_dup (argument);
		if (obj->questmaker != NULL)
			free_string (obj->questmaker);
		obj->questmaker = str_dup (ch->name);
		if ( obj->item_type == ITEM_VEHICLE ) {
			
		}
		send_to_char ("#eOkay. #wShort description#e set.#n\n\r", ch);
		return;
	}


I know this is probably a very simple thing to do, but my code knowledge is very... informal. I've googled "strlen" and looked at some examples, but I can't seem to get the syntax right.

I would add the strlen check in this line, right?

send_to_char ("Short descriptions should be 59 characters.\n\r", ch);
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #3 on Sun 17 Apr 2016 03:15 PM (UTC)
Message
It needs to be done where
if (strlen (argument) > 59) 
is calculated, probably the block of code directly before what you posted.

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sun 17 Apr 2016 07:44 PM (UTC)

Amended on Sun 17 Apr 2016 07:45 PM (UTC) by Nick Gammon

Message
Example code:


#include <stdio.h>
#include <ctype.h>

size_t strlen_exclude_colors(const char * str)
{
  const char *s;
  size_t count = 0;
  for (s = str; *s; ++s)
    {
    if (s [0] == '#' && isalpha (s [1]))
      ++s;  // skip the letter
    else
      count++;  // otherwise count the character
    }
  return count;
}

int main ()
{
  const char test [] = "#cblue #nwith";   // 13 chars
  printf ("Calculated length = %i\n", (int) strlen_exclude_colors (test));
  return 0;
}


Output:


Calculated length = 9


Once you add the function "strlen_exclude_colors" to your code, change the line:


if (strlen (argument) > 59) 


to:



if (strlen_exclude_colors (argument) > 59) 

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Lmclarke   (13 posts)  [Biography] bio
Date Reply #5 on Sun 17 Apr 2016 09:01 PM (UTC)

Amended on Sun 17 Apr 2016 09:09 PM (UTC) by Lmclarke

Message
It's working! Tested in-game, functions just as intended (Thank you!).

I'm a little bit concerned about this:


sys.c: In function `do_restring':
sys.c:1749: warning: implicit declaration of function `strlen_exclude_colors'


It compiles anyway, but the warning makes me uneasy.
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Sun 17 Apr 2016 09:19 PM (UTC)
Message
Put the new function physically before where you call it, in the source file, and the warning will go away.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Lmclarke   (13 posts)  [Biography] bio
Date Reply #7 on Sun 17 Apr 2016 10:22 PM (UTC)
Message
Thank you very much, Nick. You're the best.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


3,178 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]