[Include] cmdparam - The simplest command splitter
#1

Simplest + Extremely efficient
Command Splitter
Made by me
Link:
http://pastebin.com/BvUU5FjD

Код:
cmdparam(full[], first[], length=sizeof first)
full[] - the whole string
first[] - the output; the part of the string until the first ' ' (space)
length - the maximum size of first[]
Example usage:

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
	new command[32];
	cmdparam(cmdtext, command);
	printf("command: %s | params: %s", command, cmdtext);
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	new command[32], param[2][128];
	cmdparam(cmdtext, command);
	cmdparam(cmdtext, param[0]);
	cmdparam(cmdtext, param[1]);
	printf("command: %s | param 1: %s | param 2: %s | param 3: %s", command, param[0], param[1], cmdtext);
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    new command[32];
    cmdparam(cmdtext, command);
    if(strcmp("/pm", command, true) == 0)
    {
            new param_id[6];
	    cmdparam(cmdtext, param_id);
	    new id = strval(param_id);
            ........
            SendClientMessage(id, 0xffffffff, cmdtext);
    }
}
Is basically cuts off the first part of the string and puts it in another string.
* I didn't test any of the examples, they are just for demonstration.


Version 2 (optional):
This version is way more comfortable as long you use it right.

Link:
http://pastebin.com/3fskuTxc

Example:

Код:
    new cmdtext[128], param1[24], param2[24];
	format(cmdtext, 128, "/param1 param2 param3");
	param1 = cmdparam(cmdtext);
	param2 = cmdparam(cmdtext);
	printf("%s|%s|%s", param1, param2, cmdtext);
Will output: "/param1|param2|param3"

Код:
    new cmdtext[128], command[24], entrancefee, price;
	format(cmdtext, 128, "/createbiz 100 10000 Business Name");
	command = cmdparam(cmdtext);
	entrancefee = strval(cmdparam(cmdtext));
	price = strval(cmdparam(cmdtext));
	printf("%s|%d|%d|%s", command, entrancefee, price, cmdtext);
Will output: "/createbiz|100|10000|Business Name"

In case a player doesn't use the command properly, this:
Код:
    new cmdtext[128], command[24], entrancefee, price;
	format(cmdtext, 128, "/createbiz");
	command = cmdparam(cmdtext);
	entrancefee = strval(cmdparam(cmdtext));
	price = strval(cmdparam(cmdtext));
	printf("%s|%d|%d|%s", command, entrancefee, price, cmdtext);
Will output: "/createbiz|-1|-1|"
So you can identify when a player doesn't insert an integer value properly e.g. if(entrancefee == -1) return SendClientMessage(..);

Note:
the compiler will generate an error if you try to load a param with less than MAX_PARAM_LEN cells, which is legitimate because I can't think of a word/value with more than the set default of 24 chars. It doesn't affect the last param though, that should be used for the rest of the string that includes spaces such as a message or a name.
Reply
#2

I don't understand:
pawn Код:
SendClientMessage(id, 0xffffffff, cmdtext);
why cmdtext, that will print command?
No sense man
Reply
#3

Do you know about the existence of sscanf?
Reply
#4

Good try but sscanf is still easier IMO.
Reply
#5

Quote:
Originally Posted by Flyfishes
Посмотреть сообщение
Good try but sscanf is still easier IMO.
@System64 read the thread again then.

sscanf is pretty complex and may be hard for beginners, and is still faster than this, but still cmdparam is double as fast compared to strtok, and really easy to use.
Reply
#6

Sscanf s better then this, and ZCMD and Y_CMD are faster command processor... but good try..
Reply
#7

Quote:
Originally Posted by Incubator
Посмотреть сообщение
@System64 read the thread again then.

sscanf is pretty complex and may be hard for beginners, and is still faster than this, but still cmdparam is double as fast compared to strtok, and really easy to use.
Why would someone cope with this crap over sscanf? It isn't that hard to understand anyways, plus there are countless tutorials on creating commands with sscanf. Get over yourself!
Reply
#8

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
Why would someone cope with this crap over sscanf? It isn't that hard to understand anyways, plus there are countless tutorials on creating commands with sscanf. Get over yourself!
Whoa, chill out, you treat this like I made some sin by releasing this. Hell, if someone is still affixed on the strtok-kind of splitters and prefers this method over some more speed of others (not that cmdparam is that slow), he may choose to use this!
Reply
#9

Quote:
Originally Posted by Incubator
Посмотреть сообщение
Whoa, chill out, you treat this like I made some sin by releasing this. Hell, if someone is still affixed on the strtok-kind of splitters and prefers this method over some more speed of others (not that cmdparam is that slow), he may choose to use this!
If someone gonna rescript his script from strtok to something else it will be sscanf for sure.
By the way isnt sscanf just way better?

pawn Код:
new string[24],integer,OptionalString[24],OptionalInteger;
sscanf(cmdtext, "s[24]dS[24](Empty)D(0)", string , integer, OptionalString , OptionalInteger);
Also there are many more combinations its just a example...
Reply
#10

Quote:
Originally Posted by Incubator
Посмотреть сообщение
Whoa, chill out, you treat this like I made some sin by releasing this. Hell, if someone is still affixed on the strtok-kind of splitters and prefers this method over some more speed of others (not that cmdparam is that slow), he may choose to use this!
I didn't mean to come across like that but it's amazing how many of these systems people create. Nobody really uses them and it's stupid for people to create them. There are systems out there which are a billion times better. They may be slightly more advanced, but once you do it once correctly, people tend to do it again without problems. This is just the same as learning a new system. It will take time understanding.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)