PDA

View Full Version : Input the command from file



TZR
10-05-2002, 11:13 PM
Hi,

I'm writing a program that needs to read a list of command lines either from keyboard or from a file.
Each command line should be correspoding to a drawing funcion, such as drawline, drawcircle ..etc.
The input parameter should look like "< FileName.in"


Currently, I'm writint the program by using "glutIdleFunc(commandInput)"

The testing code for commandInput is:


===============================

void commandInput(void){

char command[100];

if (IfReadFile==0)
{
ReadFile();
IfReadFile=1;
}


cout<<"Enter command:\n";

cin.getline(command,100, '\n');


if (strcmp(command,"square")==0)
{
cout<<"Enterd Square\n";
drawSquare();
}
/*else
{
cout<<"Compare fail\n";
}*/

}

===========================================

But, I found out the input parameters keep on messing up the commandInput program.
Is anyone can help me find the problem?
Or, is there anyway to write this program except the method I'm trying to use?


Thanks!

nexusone
10-06-2002, 04:22 AM
One thing you should do is look in to how a compiler breaks down say a C file.

Read in a line at a time:

example: drawCube( 1, 2, 3);

first we look at the line for a command word, I would make a list something like this:

char *command_list[] = "drawCube", "drawSphere", etc.

Once we find a command word, we tag it.
with a id. Say draw cube, first command. id = 1;

now we make a list of how many peramitors are used by 1.

int command_per[] = 3, 1, 2, etc.

Now we found draw_cube which is our 1 command and we know it has 3 peramitors to be processed.
First we look at the string for a starting '(', then we read in the data until we reach a ',', then read in two more times and last time we look for a ')'.

If I had a little more time I would write a little code example.... maybe later is this does not help...



Here is some things to look at adding:
===============================

void commandInput(void){

char command[100];
char c_store[32];
char command[] = "square"
int char_length;
int i;

if (IfReadFile==0)
{
ReadFile();
IfReadFile=1;
}


cout<<"Enter command:\n";

cin.getline(command,100, '\n');


// Find command
char_length = strlen( command ); long many charactors are in the command line.
i = 0;
while( (i < char_length) &#0124; &#0124; ( command[i] !='(' )
{
if ( isalpha(command[i])
{// look for letters only
c_store[i] = command[i];
}
}
// now process your other line items
// i is now pointing past the command word.
while( i < char_length)
{
// in here look for ',' and ')' to seperate you data fields....
Sorry but out of time for now....


if (strcmp(c_store,"square")==0)
{
cout<<"Enterd Square\n";
drawSquare();
}
/*else
{
cout<<"Compare fail\n";
}*/

}



[This message has been edited by nexusone (edited 10-06-2002).]