I wrote the IRC protocol parsing today ... I tried to stick to the RFC as close as possible. However, I'm not very familiar with the IRC protocol
I'm actually surprised it could be written so concisely! Here's what I wrote:
char * PopToken(char *&str) {
str += strspn(str, " ");
char *tmp = str;
str += strcspn(str, " ");
if (*str != '\0') {
*str++ = '\0';
str += strspn(str, " ");
}
return tmp;
}
void IrcClient::OnProcessLine(char *line) {
const char *pPrefix = NULL, *pCommand = NULL, *pParams[16] = { NULL };
unsigned int numParams = 0;
puts(line);
if (*line == ':')
pPrefix = PopToken(line)+1;
pCommand = PopToken(line);
while (*line != '\0' && numParams < 16) {
if (*line == ':') {
pParams[numParams++] = line+1;
break;
}
pParams[numParams++] = PopToken(line);
}
if (isdigit(pCommand[0])) {
// This is probably a numeric
int numeric = strtol(pCommand,NULL,10);
OnNumeric(numeric, pParams, numParams);
return;
}
if (!strcmp(pCommand,"PING")) {
OnPing(pPrefix, pParams[0]);
}
}
Does it look OK? Do you suppose there is any case that I don't handle adequately?