Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

comatose red

win32 command-line parsing

This topic is 5222 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do ya do it?... Ive been googling for a while now but I have yet to find a really nitty-gritty basic tutorial that teaches a clear path to this. All I know is that you have to somehow parse lpCmdLine from the main procedure. How to do this though is beyond me.

Share this post


Link to post
Share on other sites
Advertisement
Well the lpCmdLine is just a pointer to the command string so you just have to parse the string. That is copy every word from it and test them to valid commands or whatever you want to use them.

Share this post


Link to post
Share on other sites
Search for CommandLineToArgvW on msdn.microsoft.com, that should help get you started especially if you have written some unix or console programs.

Share this post


Link to post
Share on other sites
And here's a function I use to get the next word from a string.
It copies the word to word from the buffer and returns the length of the copyed word.



int ParseNextWord(char *buffer,char *word)
{
int lcount = 0,i = 0;
char ch;
/* First skip the spaces*/
while(buffer[lcount] ==' ') lcount++;
/* Next copy until space,newline,0 */
while((ch = buffer[lcount]) != '\n' && ch != ' ' && ch != '\0')
{
word[i] = ch;
lcount++;
i++;
}
word[i] = 0;
/* return length of the copied word */
return lcount;
}


[edited by - Atm97fin on February 6, 2004 4:58:01 PM]

Share this post


Link to post
Share on other sites
Alright. Call me a question-whore, but everywhere I look I find these tutorials with these ginormous command-line parser classes that seem fairly complex (for my understanding). But it is just something as simple as what you said?

Share this post


Link to post
Share on other sites
I'm not sure if there's a function to tokenize strings. If there was, all you'd have to do is to create an array of string pointers, and go through the command line and tokenize everything into seperate array elements. If there isn't, then use a loop:

int index = 0;
int currtoken = 0;
char current;
current = lpCmdLine[index];
char *tokens[];

getnexttoken:
while ( current != ' ' )
{
tokens[currtoken][index] = current;
index++;
if ( index < strlen( lpCmdLine ) )
{
current = lpCmdLine[index];
}
else
{
// Exit
goto exit;
}
}

currtoken++;
index = 0;
goto getnexttoken;

exit:

// Parse your tokens

If I did all that right, you should have an array full of tokenized strings.

Now, you can use this array to whatever purpose you want.

Ah, well, it seems Atm97fin has beat me to this function...


[edited by - fyhuang on February 6, 2004 5:01:32 PM]

Share this post


Link to post
Share on other sites
I finally got it to work for me. Did something a lil-bit different though.


int clParse(char cLine[10])
{
for(int loop = 0; loop < 10; loop++)
{
switch(cLine[loop])
{
case '-':
{
switch(cLine[loop + 1])
{
case 'm':
MessageBox(NULL,"test",
"test",MB_OK | MB_ICONINFORMATION);
break;
}
}
}
}
return 1;
}


It works at least...

[edited by - Comatose Red on February 6, 2004 5:42:56 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by comatose red
I finally got it to work for me. Did something a lil-bit different though.


int clParse(char cLine[10])
{
for(int loop = 0; loop < 10; loop++)
{
switch(cLine[loop])
{
case ''-'':
{
switch(cLine[loop + 1])
{
case ''m'':
MessageBox(NULL,"test",
"test",MB_OK | MB_ICONINFORMATION);
break;
}
}
}
}
return 1;
}


It works at least...

[edited by - Comatose Red on February 6, 2004 5:42:56 PM]


Seems to work, but if you write for example "app plim-m" you still get the test messagebox even when you typed wrong.
Or it seems like that for the first look.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!