Link to home
Start Free TrialLog in
Avatar of roccogalati
roccogalati

asked on

How to tokenize a string in C

Hi to all!

I have to filter the commands send by the client to make them matching some criteria:

the server can only handle these commands:

L
Q

or a string like:

Vpencil,car,dog\r\n

if it receive L it show all the objects stored in a list, if it receive a Q it close the connection with the server and if it receive a V, so it have to search and find the string "string1", "string2" and so on in the list until it find \r\n.

It have to refuse all others type of commands...

i posted below the code i wrote... it works great for commands L, Q.
I handle only a single char and if it receive Lsfddfs or Qfdsfs it send to the client an error...
so it is fine...

but now...
my code is able to understand when the client send the command which begin with V, but i'm not able to correctly read the first token of the string...

if the string is

Vstring1,string2,string3\r\n

the first token is Vstring1 and not string1

and then i don't know how to read in the correct way the last token...

i have to read and tokenize the string until i find a \r\n


Can u give me some help or advices?

thanks a lot!


while(ciclo){
   
   Readline(fd, buffer, MAX_LINE-1);
   
   sscanf(buffer,"%s", comando);
   
   lenght = strlen(comando);
 
   if ( lenght <= 100 ){   		   
   if ( (strcmp(comando, "L") == 0) && (lenght = strlen(comando) == 1)) {
      
   display_list(llist, fd);
   }
   else if(strcmp(comando, "Q") == 0  &&  (lenght = strlen(comando) == 1)){
   
   ciclo = 0;
   }
   else if( comando[0] == 'V' ) { 
   
   find_value = strtok(comando, ",");
   if (find = search_value(llist, "find_value", fd) == -1){
   Writeline(fd, err, strlen(err));
   }
   
   while(find_value != NULL){
   find_value = strtok(NULL, ",");
   if (find = search_value(llist, "find_value", fd) == -1){
   Writeline(fd, err, strlen(err));
   }
   printf("Ris: %s\n", find_value);
   
   }
 
   }  
     
    
   else {
    Writeline(fd, errore, strlen(errore));
	printf("Lunghezza: %d\n", strlen(comando));
	
   }
   }
   else {
   Writeline(fd, out_of_bytes, strlen(out_of_bytes));
   }
   }
 
 
// my ReadLine is:
 
ssize_t Readline(int sockd, void *vptr, size_t maxlen) {
    ssize_t n, rc;
    char    c, *buffer;
 
    buffer = vptr;
 
    
    for ( n = 1; n < maxlen; n++ ) {
	
	if ( (rc = read(sockd, &c, 1)) == 1 ) {
	    *buffer++ = c;
	    if ( c == '\n' )
		break;
	}
	
	else if ( rc == 0 ) {
	    if ( n == 1 )
		return 0;
	    else
		break;
	}
	else {
	    if ( errno == EINTR )
		continue;
	    return -1;
	}
    }
 
    *buffer = 0;
    return n;
}

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

>>    find_value = strtok(comando, ",");

You probably want to start sareching at the second character, so :

        find_value = strtok(comando + 1, ",");


>>    if (find = search_value(llist, "find_value", fd) == -1){

Didn't you mean :

        if (find = search_value(llist, find_value, fd) == -1){

ie. without the double quotes.


Btw, instead of just "," as separator, you might want to add \r and \n : ",\r\n"
Avatar of roccogalati
roccogalati

ASKER

>>You probably want to start sareching at the second character, so :

    >>    find_value = strtok(comando + 1, ",");

Fine, i will try with this...


>>Didn't you mean :

        >>if (find = search_value(llist, find_value, fd) == -1){

i was doing some test and i tried to search for string < find_value > in the list :-)


>>Btw, instead of just "," as separator, you might want to add \r and \n : ",\r\n"

can u explain it?

what do you mean?

the string will be something like this:

Vstring1,string2,string3,string4\r\n

if i use ",\r\n" the string is supposed to be something like:

Vstring1,\r\nstring2,\r\nstring3\\r\n

or not?
>> if i use ",\r\n" the string is supposed to be something like:

Each character in the separator string will be treated as a separate separator (sorry for my choice of words heh). So, ",\r\n" means to check for the three separators ',' , '\r' and '\n'.

See the reference page of strtok for more info.
i've read the reference... and yep... it seems to work now..

i've only a problem with the code i posted below...

if i have this command:

Vstring1,string2,string3\r\n

it display:

string1
string2
strin3
<null>

and when i call:

search_values(llist, find_value, fd);

and i pass it find_value = <null>

it crash...

why it shows also a <null> token ?
   else if( comando[0] == 'V' ) {
   printf("\nSi avvia la funzione GET\n");  
   
   find_value = strtok((comando+1), ",\r\n");
   find = search_value(llist, find_value, fd);
   if (find == -1){
   Writeline(fd, err, strlen(err));
   }  
   
   while ( find_value != NULL ){
      find_value = strtok(NULL, ",\r\n");
	  find = search_value(llist, find_value, fd);
	  if (find == -1){
	  Writeline(fd, err, strlen(err));
	  }   
   
   }
   
   } 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot, Sage! :-P

U are great and always friendly! ;)
Thanks again!

it works fine!

i onyl have to setup something and to add a control in search_values function to let it handle the case when find_value is <null>

thanks again!
Thanks for the nice comments :)

I'm glad that I could be of assistance !

Btw, note that with the code from my last post, you shouldn't have a call to search_value with NULL.
yep i know... but i prefer to add a control in search_values for <null> pointer to be able to handle also this event... in order to be more sure...
>> in order to be more sure...

I applaud that ;)
it's better to solve all possible bugs when you find out them, LOL :)
Indeed ;) I assume you have learned the hard way, like me (and everyone else) heh :)
when i was 15, i made a little error with a php upload file utility on my old server, and a guy used it to delete some stuff on my public dir LOL

:)
:) Classic exploit :)
Yep :)

the hard way is the best way to learn... LOL

unfortunately, i didn't write code for about the last two years... and it's hard to begin again :)
You're doing fine !
thanks... :)

but it's not easy... i've forgotten a lot of things...  i need some time to regain experience :)


Take your time ... we're here to help you when you require it :)
thanks again :)