Reputation: 101
It seems that inside the for-loop dealing with splitting a given string, it will only execute twice before exiting and not perform the checks with the if-statements I placed within as well as in the isValid function. I'm attempting to do simple checks on input from a buffer containing a given string. If I comment out the code block underneath the first 'printf' statement it will execute properly by printing out each tokenized string. Leaving it in, it will only print the first two iterations (i is 0,1) and then nothing else will occur. I can't seem to figure out why this is happening.
Here is my code:
void processRequest (int socket)
{
int n, i=0,error_code = 1;
char buffer[256], parsed_input[256];
char* token;
n = read(socket,buffer,255);
for (token = strtok(buffer, " "); i, token; token = strtok(NULL, " "),i++)
{
printf("Argument %d: %s\n", i,token);
if(i == 0 && (strcmp(token,"bye") == 0))
{
error_code = -5;
break;
}
else if(i == 0 && (strcmp(token,"terminate") == 0))
{
exit(0);
}
if((error_code = isValid(token, i)) != 1)
break;
}
}
Also the implementation for isValid:
int isValid(char* token, int i)
{
switch(i){
case 0:
if((strcmp(token,"add") == 0) || (strcmp(token,"subtract") == 0) || (strcmp(token,"multiply") == 0))
return 1;
else
return -1;
break;
case 1:
case 2:
case 3:
case 4:
if(isdigit(token))
return 1;
else
return -4;
break;
default:
return 1;
break;
}
}
Upvotes: 0
Views: 70
Reputation: 753455
This demonstration code shows that your code can work:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isValid(char *token, int i);
void processRequest(void /*int socket*/);
void processRequest(void /*int socket*/)
{
int i = 0;
int error_code = 1;
char buffer[256] = "subtract 123 103 11 2 elephants cavorting";
char *token;
//int n = read(socket,buffer,255);
int n = strlen(buffer);
printf("[%.*s]\n", n, buffer);
for (token = strtok(buffer, " "); token; token = strtok(NULL, " "), i++)
{
printf("Argument %d: %s\n", i, token);
if (i == 0 && (strcmp(token, "bye") == 0))
{
error_code = -5;
break;
}
else if (i == 0 && (strcmp(token, "terminate") == 0))
{
exit(0);
}
if ((error_code = isValid(token, i)) != 1)
break;
printf("error_code = %d\n", error_code);
}
}
int isValid(char *token, int i)
{
switch (i)
{
case 0:
if ((strcmp(token, "add") == 0) ||
(strcmp(token, "subtract") == 0) ||
(strcmp(token, "multiply") == 0))
return 1;
else
return -1;
break;
case 1:
case 2:
case 3:
case 4:
if (isdigit(*token))
return 1;
else
return -4;
break;
default:
return 1;
break;
}
}
int main(void)
{
processRequest();
return 0;
}
Sample run:
[subtract 123 103 11 2 elephants cavorting]
Argument 0: subtract
error_code = 1
Argument 1: 123
error_code = 1
Argument 2: 103
error_code = 1
Argument 3: 11
error_code = 1
Argument 4: 2
error_code = 1
Argument 5: elephants
error_code = 1
Argument 6: cavorting
error_code = 1
Your code doesn't contain diagnostics that print what is in the buffer read from the socket — and you've not shown us what is being sent. It also doesn't ensure that the data is a null-terminated string.
What this shows is that the trouble is more probably in the data read over the socket, or the code that is calling this function, than in the main part of the function. Or the code that misused isdigit()
caused your code to fail. Were you ignoring compilation warnings? If you were not being told by your compiler that you were misusing isdigit()
, you either need to turn on more warnings or you need to get a better compiler. If you want to validate that the number in the token is a complete, you have to work harder, and probably use strtol()
or strtod()
or one of their relatives (strtoimax()
, perhaps).
Upvotes: 1