Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ Reserve, global wildcards

Reserve, global wildcards

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


Posted by Zeno   USA  (2,871 posts)  Bio
Date Tue 22 Feb 2005 12:59 AM (UTC)
Message
Does anyone have a reserve function (the Smaug command) where it instead of just preceding with a wildcard, it also allows after the name usage. For example stock will handle:
*zeno
But not:
zeno*

Ran into problems with certain users making bad names, yet zeno* was already reserved.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Raz   (32 posts)  Bio
Date Reply #1 on Tue 22 Feb 2005 02:25 AM (UTC)
Message
I only have a few minutes, so I'll try to explain how to do it instead of writing code atm:

Find the is_reserved_name in comm.c. What you need to do is add a test that when the last character is a '*', that you should take the str_prefix() of the name and compare it to the input. You are going to need to copy the string into a buffer in order to use the str_prefix() function though (since you need to eliminate the '*' character from the string you wish to test).

Test some code and post it and I can try to help a bit more tomorrow.

-Raz
C++ Wiki: http://danday.homelinux.org/dan/cppwiki/index.php
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #2 on Tue 22 Feb 2005 08:52 PM (UTC)
Message
Or, use a wildcard matching function. If you're using C++ this will be helpful:

bool Matches(const std::string & pattern, const std::string  & str)
{
	bool result;

	// base case: if the pattern is the string, then we've succeeded.
	if (pattern == str)
	{
		return true;
	}
	// If the pattern is empty, then it can't match a string, hmm?
	if (pattern == "")
	{
		return false;
	}

	// * : 0 or more characters
	if (pattern[0] == '*')
	{
		// First try 0 characters... remove the star from the pattern, and use same string
		result = Matches(pattern.substr(1),  str);
		if (str !=  "" && !result)
		{
			// ok, that didn't work... so now eat one character and continue with the star in the pattern.
			result = Matches(pattern,  str.substr(1));
		}
		return result;
	}
	// ? : 0 or 1 characters
	else if (pattern[0] == '?')
	{
		// First try 0 characters... remove ?, and use same string.
		result = Matches(pattern.substr(1),  str);
		if (str !=  "" && !result)
		{
			// ok, that didn't work... so eat the ?, eat one character, and try again.
			result = Matches(pattern.substr(1), str.substr(1));
		}
		return result;
	}
	// no wildcard: just check the two characters for equality.
	else if (str != "" && pattern[0] == str[0])
	{
		// munch a character on each and try again.
		return Matches(pattern.substr(1),  str.substr(1));
	}
	return false;
}
I haven't tested this or even looked at it for a while, so I don't 100% guarantee it, but it should do the trick. Changing it to case-insensitive is a rather easy manner.

Not to be immodest but this kind of method is much better than the somewhat hackish approach that SMAUG has. The algorithm is not the most efficient; it could be improved e.g. by using indices for the strings, to avoid having to continuously copy them. That's fairly simple though, so I'll leave it as an exercise to the reader. :P The recursion makes it easy(ier) to read, so I leave it at that to make it as clear as possible.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #3 on Wed 23 Feb 2005 02:35 AM (UTC)

Amended on Wed 23 Feb 2005 03:00 AM (UTC) by Zeno

Message
I know the basic concept how to do it, I'm just not positive on how to handle the last character and so on.

Well I'm not using C++ yet, but as I mentioned I plan to.

Basically was wondering if SWR/etc had something like this done already, so I could easily put it in, else I wouldn't put it in for a while with what I've got planned to do.

Call me lazy if you wish. ;)

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #4 on Wed 23 Feb 2005 03:05 AM (UTC)
Message
The algorithm I gave could be edited to straight C fairly easily. Instead of substr'ing the string to advance it, you'd just give ptr+1. And of course instead of comparing to "" you'd do strlen() == 0 or something.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
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.


17,146 views.

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

Go to topic:           Search the forum


[Go to top] top

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