Command Based Scripting Language

Started by
25 comments, last by choffstein 19 years, 3 months ago
lol
yea that is a little more complex than i want at this moment
ive tried googling for days trust me on that part
Advertisement
What you're after is really just basic file I/O stuff. It'll be covered in any introductory C++ text, or you might try asking in General Programming or For Beginners. I'd recommend using an ifstream, strings (not char[50] or similar), and the getline function.
Oh, ok. I probably misunderstood what you were having a problem with. I agree with Kylotan.
Thanks alot guys and brandon thanks for your patience
alright someone posted this

// Step 1: Open the file and store a handle to the file in pFile
//
FILE* pFile = fopen("somefile.txt", "r"); // <<< The "r" specifies that you want read only access
if ( !pFile )
{
// Failed to open
return;
}


// Step 2: Read the file until we are at the end of the file
//
while ( !feof(pFile) )
{
char szStringBuf[256];
fgets( szStringBuf, 256, pFile );

// szStringBuf now stores the current line that you are parsing
}


// Step 3: Make sure to close the file once you have finished
//
fclose(pFile);







If i do this


// Step 1: Open the file and store a handle to the file in pFile
//
FILE* pFile = fopen("somefile.txt", "r"); // <<< The "r" specifies that you want read only access
if ( !pFile )
{
// Failed to open
return;
}


// Step 2: Read the file until we are at the end of the file
//
while ( !feof(pFile) )
{
char szStringBuf[256];
fgets( szStringBuf, 256, pFile );

// ADDED
if (szStringBuf == CMD_PRINT)
{
printf("CMD_PRINT successfully called!");
}
else if( szStringBuf == CMD_WAIT )
{
printf("CMD_WAIT successfully called!");
}
else
{
printf("COMMAND NOT VALID!");
}
// END ADDED CODE

// szStringBuf now stores the current line that you are parsing
}


// Step 3: Make sure to close the file once you have finished
//
fclose(pFile);



would that work?
if so how would i get params?


I don't know if this'll help, but here's some code I wrote a long while ago when experimenting with the same thing you are:


/***************************************************************************/
/* Keep in mind that this method of things is very slow. */
/***************************************************************************/

#include<stdio.h> /* For various i/o routines */
#include<string.h> /* For strcmp */

/****Cuts the command from the front. Soon to support longer commands.****/
int chopit(char *chopline, char *instruction, char *command)
{
int i, j, n;
char *temp;
for(j=0; instruction[j] != ' '; j++)
{
command[j] = instruction[j];
n = j+2;
}
for(i=n; instruction != ';'; i++)
{
chopline[i-n] = instruction;
}
chopline[i-n] = NULL;
return 1;
}

/* You can easily add new commands. If you want to do 2 parameters, */
/* you'll have to do chopit() on the parameters and put them into */
/* new variables, like param1 and param2. */

/****Executes a single command.****/
int script(char instruction[80])
{
char parameters[79]; char command[12];
chopit(parameters, instruction, command);
/* Print statement.*/
if(!strcmp(command, "print"))
printf("%s\n", parameters);
else if(!strcmp(command, "end")) return 0;
else if(!strcmp(command, "rem")) return 1; /* ' This is a comment; */
else {printf("Unrecognized instruction.\n\n"); return 0;} /*Error*/
return 1;
}


/****The main() function.****/
int main(void)
{
char instruction[80];
char *filename;
FILE *fp;

printf("Enter instruction file: ");
scanf("%s", &filename);
if((fp = fopen(filename, "r")) == NULL)
{
printf("\nError: unable to open file\n");
return 0;
}
fgets(instruction, 80, fp);
while(script(instruction))
fgets(instruction, 80, fp);
return 1;
}
/**************EOF*************/


Basicly, you open the file and read one line at a time. chopit() cuts the line into two variables, one holds the command, and the other holds the parameter. Then, script() takes the command and checks it against other string literals which contain the in-built commands. If the comparison is true, it executes the code block given to it. This method is very slow and limited, but it works and it's a good starting point.

A sample program would look like this:


rem This is a comment!;
print This is a command!;
end; Anything that comes after end is ignored!


Notice the semicolons on the end of the lines. This is necessary to tell the chopit() function the end of the parameter.
Oh, and the lines can only be 80 characters long. You can change that if you want.

[Edited by - neverdream on January 1, 2005 7:45:02 PM]
Ill write it in pseudo code, so you can implement it however you want.

method getNextParam(current line string, current position){   create temp string   read from your current position in string until you reach a whitespace (null, space, or tab).   until the whitespace, add to the temp string   return the temp string   make sure the current position is either a pointer or global so that   it is updated}method getNextCommand(current line string){   create temp string   read until the first whitespace   make sure you update the global position pointer to where you are in the string   until whitespace, add to temp   return temp}method parseFile(){   read the file in characacter by character, creating an array of strings.  Add to the array everytime you reach a \n   Go through the array line by line.  At the beginning of each line, set the position pointer to 0.   For each line, getNextCommand.   Check what the command is   (Example: if(!strcmp(currLine, "PRINT")){ ... } )   Do the appropriate stuff for each command}



Things you need to have special exceptions for are quotes and the like. If you are in "OPEN QUOTES" (as in, you have encountered one quote), whitespaces do not count. To do this, simply have a "quote" boolean. If you encounter a quote, just reverse the boolean. If you hit a whitespace while the boolean is true, ignore it.

Here is an example file:

PRINT "HELLO WORLD"EXIT


So we parse this into an array that has:
{"PRINT "HELLO WORLD""},
{"EXIT"}

So then we go through it member by member. We start with "PRINT "HELLO WORLD"".

So we get the first command. Reading until the first whitespace, we find that the command is "PRINT". So we return this, and compare against our possible commands. Lets say our print command looks like this:

if(!strcmp(currString, "PRINT")){   printf("%s", getNextParam());}


This would mean that it prints on the string the next string. In our example, it depends how you implement getNextParam with quotes (either it counts them in the string, or requires escape sequences. A little more difficult to implement, so I wont go into it.)

So our string looks like this:

PRINT "HELLO WORLD"      ^

Where ^ is where we are in the string (our location pointer). So we find out the scripter wants to use the PRINT command. Next, our code uses the getNextParam method. The first character is a ", so we set our boolean to true. We continue reading, find a whitespace, but keep reading since we are in a string. Finally, we hit another ", ending our string, and a final whitespace (a new line.) We return the string.

So basically, what ends up happening (assuming we dont add quotes to our string when we return it), is we get the printf to look like:

printf("HELLO WORLD");


Now we are done with that line! So we go onto the next in our array.
Its "EXIT"

So we start over. We set our pointer to 0, and get our command. We go through the same if loops, and find out its the EXIT command. The exit command requires no parameters, just quits the program or something, so we dont ask for paramters. Example:

if(!strcmp(currString, "EXIT")){   exit 1;}


So basically you go through each line, find out what the command is, and read in parameters based on that.

If you need some more code, I will whip you up a sample program!

I hope that helped!

-visage

[Edited by - visage on January 2, 2005 2:13:59 PM]

This topic is closed to new replies.

Advertisement