Development How does this work? (part of the Quake II source code)

Discussion in 'Software' started by xen0morph, 24 Feb 2003.

  1. xen0morph

    xen0morph Bargain wine connoisseur

    Joined:
    30 Jun 2002
    Posts:
    2,925
    Likes Received:
    1
    I have a bit of code here that is supposed to parse the command line in Quake II and Quake. I can see how it's supposed to get each argument and save it in ArgV[x], and I can see that ArgC holds the current arg when stepping through the arguments, but I can't see how. It uses a rediculous amount of logical operators.

    Can anyone tell me how this works?

    Code:
    void ParseCommandLine (LPSTR lpCmdLine)
    {
    	argc = 1;
    	argv[0] = "exe";
    
    	while (*lpCmdLine && (argc < MAX_NUM_ARGVS))
    	{
    		while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
    			lpCmdLine++;
    
    		if (*lpCmdLine)
    		{
    			argv[argc] = lpCmdLine;
    			argc++;
    
    			while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
    				lpCmdLine++;
    
    			if (*lpCmdLine)
    			{
    				*lpCmdLine = 0;
    				lpCmdLine++;
    			}
    			
    		}
    	}
    
    }
    
     
  2. Hargle

    Hargle What's a Dremel?

    Joined:
    13 Oct 2001
    Posts:
    404
    Likes Received:
    1
    Code:
    	argc = 1;
    	argv[0] = "exe";
    The first argument in a command line is the command itself so that bit of code skips that first 'argument' basically. The argument counter, argc, is set to 1 to represent the second argument in the list and argv[0] (the first argument) is assigned to a new character string, "exe".

    Code:
    while (*lpCmdLine && (argc < MAX_NUM_ARGVS))
    This basically says, loop while the contents of the memory location stored in the pointer "lpCmdLine" is not null and the argument counter is below the maximum number of arguments allowed.

    Code:
    		while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
    			lpCmdLine++;
    The ASCII values between 32 and 126 are ones that can exist in arguments, so this loop increments the memory location stored in lpCmdLine until it finds the first valid character. The loop also breaks when the end of the argument string is reached. That's the reason the next line checks this case.

    The memory location stored in the lpCmdLine pointer is copied to the argv array at the current argc index location. This indicates the start of the argv[argc] string, which needs to be null terminated. To do this a similar loop is used as before but is breaks when it finds a character which is NOT valid in an argument (ie the end of the current argument). It sets this memory location to 0 (NULL). This is now a valid NULL terminated string. The lpCmdLine pointer is incremented past the NULL value and the whole thing loops again.

    Quite difficult to explain I suppose. It's just basically lots of looping :D

    I'll try and clarify if you point out any place where I wasn't clear (all of it?)
     
  3. xen0morph

    xen0morph Bargain wine connoisseur

    Joined:
    30 Jun 2002
    Posts:
    2,925
    Likes Received:
    1
    Thanks, I think I understand this now.

    Just to clarify: the argument string is stored in ArgV[x] as each seperate word of the argument list? Or do I have it completely wrong.

    Like, if:

    test.exe -this -is -a -test

    were passed, would I end up with the following in ArgV:

    ArgV[2] = -this
    ArgV[3] = -is
    ArgV[4] = -a
    ArgV[5] = -test

    Thanks.

    :)
     
  4. Hargle

    Hargle What's a Dremel?

    Joined:
    13 Oct 2001
    Posts:
    404
    Likes Received:
    1
    argv[1] = -this
    argv[2] = -is
    argv[3] = -a
    argv[4] = -test

    The "test.exe" is discarded basically and the argv[0] entry is just "exe".
     

Share This Page