Tokenization


 
Thread Tools Search this Thread
Top Forums Programming Tokenization
# 1  
Old 09-20-2007
Tokenization

I have a program that reads the input from keyboard using readline()
. its like a simple shell that reads the input string and acts on the commands being passed.
the strtok() function looks simple but how do i implement it. Assuming the string from input i get is called "reply".

thanks in advance
# 2  
Old 09-20-2007
strtok - C++ Reference

My understanding is you provide the string and a list of delimiters (commas, spaces etc)

Code:
char *reply_ptr=reply;
char *token=NULL;

do
{
     token=strtok(reply_ptr,";. \r\n");

     reply_ptr=NULL;

     if (token) { .... }

} while (token);

# 3  
Old 09-20-2007
what i have

ok so this is what i have so far:

int
main()
{
const char* prompt = "shell> ";

int bailout = 0;
while (!bailout) {

char* reply=readline(prompt);
/* token code here */
char *result=NULL;
result=strtok(reply,"; . \r\n");
while (result !=NULL)
reply=NULL;

/* switch case statements here for token[0] */

and i'm guessing that result holds the value of the current token.. right?

does this make any sense...?
# 4  
Old 09-20-2007
Quote:
Originally Posted by noobie
and i'm guessing that result holds the value of the current token.. right?
My understanding is the token is a string, not a number, switch() takes a number.

Personally I would not do it like that.... Smilie

I would have a loop what reads a logical line and splits it up into basically an argc/argv type array. Then check argv[0] and match it in a table, if it's in the table, the table will have a function pointer to a handler, if not then just fork/exec and the argc/argv is already setup.

When you want to add macro expansion etc, just plonk that in the logical read line.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question