// By Marc Olivier Chouinard #include #include #include #define AST_MAX_EXTENSION 200 struct ast_params { char name; char *value; struct ast_params *next; }; struct ast_params *ast_parseopts(char *data); struct ast_params *ast_parseoptsadd(struct ast_params *params,char name,char *value); char *ast_getparamvalue(struct ast_params *params,char name); int ast_getparam(struct ast_params *params,char name); int main(void) { struct ast_params *params; params = ast_parseopts("abcM(big\\)path)def\0"); if (!ast_getparam(params,'z')) printf("It dont exist\n"); if (ast_getparam(params,'M')) printf("M has this value '%s'\n",ast_getparamvalue(params,'M')); } char *ast_getparamvalue(struct ast_params *params,char name) { struct ast_params *tmp; tmp = params; while(tmp){ if (tmp->name == name) return tmp->value; tmp = tmp->next; } return NULL; } int ast_getparam(struct ast_params *params,char name) { struct ast_params *tmp; tmp = params; while(tmp){ if (tmp->name == name) return 1; tmp = tmp->next; } return 0; } struct ast_params *ast_parseoptsadd(struct ast_params *params,char name,char *value) { struct ast_params *tmp; tmp = malloc(sizeof(struct ast_params*)); if (params == NULL) { tmp->next = NULL; } else { tmp->next = params; } tmp->name = name; tmp->value = value; return tmp; } struct ast_params *ast_parseopts(char *data) { struct ast_params *params=NULL; char *tmp; char c; char *at; char *cur; int open=0; int cnt; at = data; while (*at) { if (open == 0 && *(at+1) && *(at+1) == '(') { c = *at; *(at++); open = 1; cnt=0; cur = at+1; } if (open == 0) { c = *at; params = ast_parseoptsadd(params,c,NULL); } if (open == 1 && *at == ')' && *(at-1) != '\\') { tmp = malloc(cnt - 1 + sizeof(char *)); memset(tmp,0,sizeof(tmp)); strncpy(tmp, cur, cnt-1); params = ast_parseoptsadd(params,c,tmp); open = 0; } if (open == 1) cnt++; *(at++); } return params; }