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

simple custom command parsing

Started by DeadFish Apr 13, 2005 at 7:48 AM 0 replies 400+ views
Original Post
DeadFish
DeadFish
I am expiermenting with scripting, and want opinions on what Ive done so far. The code below opens a text file and can execute functions associated with commands in the text file. Youll need to make a text file for this example to work. The only 'words' this program knows are F1 F2 F3 and F4. My text file looks like this : F1 F2 F1 F3 Anyway, heres the code :

/*
Steps I took to get to simple command parsing :

  1) have a text file that holds the commands (F1, F2, DoWhatever, DrawPlayer, etc...)
  2) declare a ifstream object and attach it to that file : ifstream infile("commands.txt");
  3) use getline and a char buffer to store the text : char buffer; getline(buffer, 128);//wish i could stuff it into a string
  4) to make some things easier, i copied the buffer into a string : string s = buffer;
  5) seperate the commands from each other and stuff em into an array of strings : commandbuffer[4] ///use a vector so
         number of commands dosent matter??////
  6) using a for loop, send each commandbuffer[] array element to a command processor : DoCommand(string s)
  7) The command processor looks at each string, and compares it to another string. If it matches, whatever is in 
         that if block is executed.

*/

//CONTENTS OF test.txt : F1 F2 F1 F3

#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <iostream>

using namespace std;

//this basically can route a command to the function(s) that need to be called
//the command processor
void DoCommand(string s)
{
	if(s == "F1") cout << "\nF1() called!\n";//could call other functions etc here
	if(s == "F2") cout << "\nF2() called!\n";
	if(s == "F3") cout << "\nF3() called!\n";
	if(s == "F4") cout << "\nF4() called!\n";
}

int main()
{
		
	char buffer[128];//will have the contents of test.txt read into it
	ifstream infile("test.txt");
	infile.getline(buffer, 128);
	
	//cant use a string in infile.getline(<string>, 64);
	cout << "\nbuffer = " << buffer << "\n";//to make sure its working

	//make a string buffer array that will hold commands, and run the commands
	string buff = buffer;
	string commandbuffer[4];
	
	int b = 0; int a = 0;
	for(a = 0; a < buff.size(); a++)
	{	
		if(buff.at(a) == ' ')	b++;//if this spot is blank, start filling the next commandbuffer[]
		else //otherwise add this char to the current commandbuffer[]
		{
			commandbuffer+=buff.at(a);
		}
	}

	//display all 4 command bufers (to make sure its working right)
	for(a = 0; a < 4; a++)
	{
		cout << "commandbuffer[" << a  << "] = " <<  commandbuffer[a] << "\n";
	}

	for(a = 0; a < 4; a++)
	{
		DoCommand(commandbuffer[a]);
	}
}

JensB
JensB
there is a scripting forum where im sure you can get some expert help, found a link to a tutorial about scripting in there: http://www.peroxide.dk/tuts_scr.shtml

about your code, if all you need to do is calling a sequence of different commands (no flow control, or conditions etc), your approach will work fine - if you want to evolve into more of a scripting language, you might want to look on some of the tutorials out there (or even just using a already finished language, like lua).
// Jens

Topic Locked

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

Sign in to reply to this topic.