ok keep in mind im 36, didnt learn this in school and am learning from the internet so if i do something completely absurd please bear with me.
in this code i am trying to parse a string into 2 things, a number of arguments, and a number of words.
for my sake a game command will never have more than 5 arguments such as "rob admin of 10 gold" or "give admin broadsword" and as small as "stats"
so i locked my arguments at 5 positions at 256 characters each.
so here we go.
i tested this with a small routine like this:
and it worked great, however i learned a long time ago that just because something works the first time does not always mean its the right way.
plus during my research i heard strtok was not good to use.
as always thanks.
[EDIT] Moderator: quoted forum codes.
in this code i am trying to parse a string into 2 things, a number of arguments, and a number of words.
for my sake a game command will never have more than 5 arguments such as "rob admin of 10 gold" or "give admin broadsword" and as small as "stats"
so i locked my arguments at 5 positions at 256 characters each.
so here we go.
static void ProcessGameCommands (string & sLine, Player * p)
{
if(sLine.empty ())
{
Send(p, "Invalid command.\r\n");
}
char input[256];
strncpy(input,str(sLine),256);
int argc = 1;
char argv[5][256];
int i;
int pos = 0;
for(i=0; i < (int) strlen(input); i++)
{
if(isspace(input[i])) argc++;
}
Send(p,"Number of arguments = %d.\r\n",argc);
char * pch;
pch = strtok (input,"'\40'");
while (pch != NULL)
{
if( pos < 5)
{
strncpy(argv[pos],pch,256);
pos++;
pch = strtok (NULL, "'\40");
}
}
i=-1;
while(++i<pos)
Send(p,"Argument[%d] = %s\r\n",i,argv[i]);
}
i tested this with a small routine like this:
if(argc == 2 && !strcmp(argv[0],"exit") &&
!strcmp(argv[1],"now"))
Send(p, "I should exit the game now as requested.\r\n");
and it worked great, however i learned a long time ago that just because something works the first time does not always mean its the right way.
plus during my research i heard strtok was not good to use.
as always thanks.
[EDIT] Moderator: quoted forum codes.