Advertisement
swarley

Parser -- Complete

Mar 3rd, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct _s {
  6.     char *command;
  7.     char *prefix;
  8.     char *postfix;
  9.     int arg_count;
  10. } event;
  11.  
  12. event parse(char *str)
  13. {
  14.     char *save = str;
  15.     int i,j = 0;
  16.     event ret;
  17.     ret.arg_count = 0;
  18.     for(i = 0;(*str != ' ') && (*str != '\0'); *str++) i++;
  19.     ret.command = strndup(save, i);
  20.     if(*str == '\0')
  21.     {
  22.         ret.prefix  = "";
  23.         ret.postfix = "";
  24.         return ret;
  25.     }
  26.     while((*str == ' ') && (*str != '\0')) *str++;    
  27.     save = str;
  28.     if(*str == '\0')
  29.     {
  30.         ret.prefix  = "";
  31.         ret.postfix = "";
  32.         return ret;
  33.     }
  34.     for(i = 0; (*str != ':') && (*str != '\0'); i++) *str++;
  35.     if(*str == '\0')
  36.     {
  37.         ret.prefix = "";
  38.         ret.postfix = "";
  39.         return ret;
  40.     }
  41.     ret.prefix = strndup(save, i);
  42.     char *tmp = ret.prefix;
  43.     for(i = 0; *tmp != '\0'; *tmp++ ) if(*tmp == ' ') i++;
  44.     ret.arg_count = i + 1;
  45.     *str++;
  46.     ret.postfix = str;
  47.     return ret;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement