Tokenization


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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  
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  
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  
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 for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #791
Difficulty: Easy
Logic gates cannot be used to store data.
True or False?

Featured Tech Videos