Skip to main content
GameDev.net gamedev.net
🔒 Locked

Parsing "command" for a string?

Started by TFS_Waldo Aug 1, 2005 at 6:30 PM 5 replies 700+ views
Original Post
TFS_Waldo
TFS_Waldo
Hey, everyone. I know that it's not really the BEST thing to do. But I am creating a very simple scripting engine. By simple, I mean just commands like 'print', 'add', 'sub', 'mult', 'div', etc. Well, I know how to parse the math functions, but how do I get a string from the print function? ex.: print "This is a string." After I 'sscanf' the parameter passed to 'ParseCommand' to find the 'print' function, how do I place "This is a string." into a 'char' array? Because if I 'sscanf' like this: 'sscanf(szString, "%s %s", szCmd, szParam1);' it cuts off 'szParam1' at the first space. How do I extract "This is a string."? Any help would be greatly appreciated. Thanks a lot! =)
:==- Why don't the voices just leave me alone?! -==:
dave
dave
You should really be using the string and fstream for this.

fstream has member functions where you can read to the next specified character etc.

#include
#include

Hope that helps.

ace
TFS_Waldo
TFS_Waldo
Well, I know exactly what you are talking about. But right now, the "scripts" are not being executed from a file. I have a dialog box that pops up when I push 'F2' to run commands. How would I do it then?
:==- Why don't the voices just leave me alone?! -==:
dave
dave
Am i assuming that you have the total string recieved from the dialogue and just want to break it down?

C++ or C# here?

ace
TFS_Waldo
TFS_Waldo
I pass the entire command to 'ParseCommand' from the dialog box. The whole command is 'print "This is a string."'. And I want to get the text just between the two '"' marks and put them in a separate string (ex. szParam1).
:==- Why don't the voices just leave me alone?! -==:
dave
dave
Well you could write your own functions to do it.

-Loop through copying characters from the string until a ' ' is found. Check the command ("print").
- If print then read until the end of the line.
- Remove quotes from second part of string.

ace
TFS_Waldo
TFS_Waldo
Okay, well, I sort of figured it out. My code organization may not be the best. But it's the way I feel comfortable coding. The structure is just something I made a habit of. I mean, I know everyone has their own coding style, but I'm trying to apologize if you cannot read mine TOO well. =) Well, here is what I have:

// Basic Script.cpp : Defines the entry point for the console application.//#include "stdafx.h"void ParseCommand(const char *szCmd) {	char szCmdName[15], szParam1[256];	if (strstr(szCmd, "print"))       // Command is 'print'; print specififed string to screen	{		strcpy(szCmdName, "print");   // Copy the command name		int p = (strlen(szCmdName) + 1);   // Index for loop, etc.; make equal to index of first space in 'szCmd'		if (szCmd != '\"')     // No opening quotation mark?		{			cout << "Error. No string specified in \"print\".\n";    // Print error			return;     // Return from function		}		else     // Opening quotation mark		{			char szString[256];    // Parameter (string) to 'print'			if (szCmd[strlen(szCmd)-1] != '\"')      // No closing quotation			{				cout << "Error. Must close string with `\"` character.\n";   // Print error				return; // Return from function			}			p++;    // Increment the counter			do     // Enter a 'do ... while' loop			{				if (szCmd == '|')       // New-line character					szString[p-7] = '\n';  // Convert character at 'p-7'				else     // Not a special character				szString[p-7] = szCmd;  // Copy the character				p++;     // Increment the counter			} while (szCmd != '\"');      // As long as we are not at the end of the string			szString[p-7] = '\0';    // Null-terminate the string			cout << szString;    // Print the string			cout << "\n";        // Print a new line		}	}}int main(int argc, char* argv[]){	ParseCommand("print \"I am a test string. 'print' me! :)\"\0");    // Parse a basic command	return 0;}


That's what I could come up with. It works, but I would like to know what I could do to improve it. Is there anything? (I'm SURE there is =P)

Thanks for the help. I never really thought of that. Stupid me, huh? =P Thanks again! =)
:==- Why don't the voices just leave me alone?! -==:

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.