Sign in to follow this  
TFS_Waldo

Parsing "command" for a string?

Recommended Posts

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! =)

Share this post


Link to post
Share on other sites
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 <fstream>
#include <string>

Hope that helps.

ace

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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[p] != '\"') // 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[p] == '|') // New-line character
szString[p-7] = '\n'; // Convert character at 'p-7'
else // Not a special character
szString[p-7] = szCmd[p]; // Copy the character

p++; // Increment the counter
} while (szCmd[p] != '\"'); // 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! =)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this