Created
August 12, 2012 23:45
-
-
Save hannahherbig/3335438 to your computer and use it in GitHub Desktop.
parsing an irc message into a struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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