diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/config-parser.c | 42 | ||||
-rw-r--r-- | common/config-parser.h | 3 |
2 files changed, 45 insertions, 0 deletions
diff --git a/common/config-parser.c b/common/config-parser.c index 905e019..c54b91e 100644 --- a/common/config-parser.c +++ b/common/config-parser.c @@ -314,3 +314,45 @@ cfg_parse_dir(const char* dirname, void* data) return ret; } + +const char* +cfg_parse_uri (char *uri, char** scheme, char** host, char** user, char** path) +{ + char* t; + + *scheme = NULL; + *host = NULL; + *user = NULL; + *path = NULL; + + *scheme = strsep(&uri, ":"); + if(uri == NULL || (uri[0] != '/' && uri[1] != '/')) + return "invalid uri"; + + uri += 2; + *host = strsep(&uri, "/"); + if(*host[0]) + { + /* Parse the community out from the host */ + t = strchr(*host, '@'); + if(t) + { + *t = 0; + *user = *host; + *host = t + 1; + } + } + + if(!*host[0]) + return "invalid uri: no host name found"; + + if(!uri || !uri[0] || !uri[1]) + return "invalid uri: no path found"; + + *path = uri; + + while((*path)[0] == '/') + (*path)++; + + return NULL; +} diff --git a/common/config-parser.h b/common/config-parser.h index 174bac2..11949a2 100644 --- a/common/config-parser.h +++ b/common/config-parser.h @@ -47,4 +47,7 @@ extern int cfg_error(const char* filename, const char* errmsg, void* data); int cfg_parse_dir(const char* dirname, void* data); int cfg_parse_file(const char* filename, void* data, char** memory); +/* A helper for parsing URIs */ +const char* cfg_parse_uri (char *uri, char** scheme, char** host, char** user, char** path); + #endif /* __CONFIG_PARSER_H__ */ |