Gammon Forum
Entire forum
ROM
Running the server
Color codes counting as characters in strings
Color codes counting as characters in strings
|
It is now over 60 days since the last post. This thread is closed.
  Refresh page
Posted by
| Lmclarke
(26 posts) 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? | top |
|
Posted by
| Nick Gammon
Australia (22,975 posts) 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 | top |
|
Posted by
| Lmclarke
(26 posts) 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);
| top |
|
Posted by
| Meerclar
USA (733 posts) 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 | top |
|
Posted by
| Nick Gammon
Australia (22,975 posts) 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:
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 | top |
|
Posted by
| Lmclarke
(26 posts) 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. | top |
|
Posted by
| Nick Gammon
Australia (22,975 posts) 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 | top |
|
Posted by
| Lmclarke
(26 posts) bio
|
Date
| Reply #7 on Sun 17 Apr 2016 10:22 PM (UTC) |
Message
| Thank you very much, Nick. You're the best. | 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.
20,486 views.
It is now over 60 days since the last post. This thread is closed.
  Refresh page
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.