Reputation: 4551
I am making my first public server modification for Crysis Wars and to ensure nobody steals my code, I'm putting as much as possible within the C++ based DLL (the alternative is Lua). To do this I have to put commands in the DLL, some of which require additional variables. Here's an example:
!ban [playername] [time] [reason]
How would I go about retrieving variables playername, time and reason, all of which have different character lengths? The reason variable may also have more than one word (such as 'offensive messages and cheating') that will need to be picked up.
In Lua this will be done with a simple 'string.match'; I suppose I could always do the message sorting in Lua then get it to send back to C++, but this could cause the whole chatcommand system to be dis-organised.
It would need to extract the variables from the 'const char *msg', which is analysed by the system on every message sent. I already analyse it for command messages (those which begin with '!'). What is the best way to do this?
Examples: !ban Con 5 spam- This will kick the player 'Confl!ct' (I already have the partial scan code to identify partial names) for five minutes
!ban Con spam- This will permanently ban the player 'Confl!ct'
Upvotes: 2
Views: 148
Reputation: 4551
The way to go here is to use Regular Expressions, aka regex. As I was going through my old questions, smartening them up, I quickly used txt2re to cook up a quick regex recipe, as seen below:
#include <stdlib.h>
#include <string>
#include <iostream>
#include <pme.h>
int main()
{
std::string txt="!ban command variables";
std::string re1=".*?"; // Non-greedy match on filler
std::string re2="((?:[a-z][a-z]+))"; // Word 1
std::string re3=".*?"; // Non-greedy match on filler
std::string re4="((?:[a-z][a-z]+))"; // Word 2
std::string re5=".*?"; // Non-greedy match on filler
std::string re6="((?:[a-z][a-z]+))"; // Word 3
PME re(re1+re2+re3+re4+re5+re6,"gims");
int n;
if ((n=re.match(txt))>0)
{
std::string word1=re[1].c_str();
std::string word2=re[2].c_str();
std::string word3=re[3].c_str();
std::cout << "("<<word1<<")"<<"("<<word2<<")"<<"("<<word3<<")"<< std::endl;
}
}
This code requires these libraries as C++ does not include a regular expressions function by default:
Upvotes: 0