Skip to content

Instantly share code, notes, and snippets.

@hannahherbig
Created August 12, 2012 23:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hannahherbig/3335438 to your computer and use it in GitHub Desktop.
Save hannahherbig/3335438 to your computer and use it in GitHub Desktop.
parsing an irc message into a struct
#include <stdio.h>
#include <string.h>
typedef struct {
char* origin;
char* command;
char* parv[15];
int parc;
} msg_t;
msg_t parse(char* str) {
char* tmp;
msg_t msg;
int i;
tmp = strsep(&str, " ");
if (*tmp == ':') {
msg.origin = tmp + 1;
msg.command = strsep(&str, " ");
} else
msg.command = tmp;
for(i = 0; *str != ':' && str != NULL;) {
tmp = strsep(&str, " ");
msg.parv[i++] = tmp;
if (*str == ':')
msg.parv[i++] = str + 1;
}
msg.parc = i;
return msg;
}
void showoff(char* str) {
msg_t msg;
int i;
msg = parse(strdup(str));
printf("origin: %s\n", msg.origin);
printf("command: %s\n", msg.command);
printf("parc: %d\n", msg.parc);
for(i = 0; i < msg.parc; i++)
printf("parv[%d]: %s\n", i, msg.parv[i]);
printf("\n");
}
int main() {
showoff(":xiphias PRIVMSG #malkier :Rock and Roll McDonalds");
showoff(":andrew KICK #malkier xiphias :shut up");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment