--- libaitio/src/Attic/url.c 2010/03/04 08:10:57 1.1.2.3 +++ libaitio/src/Attic/url.c 2010/03/04 09:54:23 1.1.2.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: url.c,v 1.1.2.3 2010/03/04 08:10:57 misho Exp $ +* $Id: url.c,v 1.1.2.4 2010/03/04 09:54:23 misho Exp $ * *************************************************************************/ #include "global.h" @@ -122,12 +122,12 @@ int ioURLGet(const char *csURL, struct tagIOURL *url) * @nargs = Requested count of arguments * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items */ -int io_MakeArray(char * __restrict psArgs, const char *csDelim, char *** __restrict args, int nargs) +inline int io_MakeArray(char * __restrict psArgs, const char *csDelim, char *** __restrict args, int nargs) { char **app; register int i; - if (!psArgs || !args || !nargs) + if (!psArgs || !csDelim || !args || !nargs) return -1; if (!(*args = malloc(sizeof(char*) * nargs))) { LOGERR; @@ -138,4 +138,92 @@ int io_MakeArray(char * __restrict psArgs, const char for (i = 0, app = *args; app < *args + nargs && (*app = strsep(&psArgs, csDelim)); **app ? i++ : i, **app ? app++ : app); return i; +} +/* + * io_SizeArray() Parse and calculate size of array + * @csArgs = Input arguments line + * @csDelim = Delimiter(s) for separate + * return: 0 error format; -1 error:: can`t read; >0 ok, number of items +*/ +inline int io_SizeArray(const char *csArgs, const char *csDelim) +{ + register int res; + char *pos; + + if (!csArgs || !csDelim) + return -1; + + for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++); + return res; +} +/* + * io_MakeAV() Parse and make attribute/value pair + * @csArgs = Input argument line + * @csDelim = Delimiter for separate + * @psAttr = Output Attribute + * @psValue = Output Value, if ==NULL this element not present value or not wanted for return + * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items +*/ +inline int io_MakeAV(const char * __restrict csArgs, const char *csDelim, + char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen) +{ + register int ret = 0; + char *pos; + + if (!csArgs || !csDelim || !psAttr || !attrLen) + return -1; + if (psValue && !valLen) + return -1; + else + memset(psValue, 0, valLen); + + pos = strpbrk(csArgs, csDelim); + if (pos) + *pos++ = 0; + ret++; + strlcpy(psAttr, csArgs, attrLen); + + if (pos && *pos) { + ret++; + if (psValue) + strlcpy(psValue, pos, valLen); + } + + return ret; +} + +/* + * ioURLGetValue() Get value from parsed URL + * @url = Input parsed URL + * @csAttr = Attribute for search + * @psValue = Return value of attribute, if ==NULL only check for existence of attribute + * @valLen = Size of psValue array + * return: 0 error attribute not find; -1 error:: can`t read; >0 ok, find at position +*/ +int ioURLGetValue(struct tagIOURL *url, const char *csAttr, char * __restrict psValue, int valLen) +{ + register int i, ret = 0; + char szBuf[BUFSIZ], **items, szElem[2][BUFSIZ]; + + if (!url || !csAttr) + return -1; + + strlcpy(szBuf, url->url_args.value, BUFSIZ); + if (io_MakeArray(szBuf, "&", &items, io_SizeArray(szBuf, "&")) < 1) + return ret; + + for (i = 0; items[i]; i++) { + if (io_MakeAV(items[i], "=", szElem[0], BUFSIZ, szElem[1], BUFSIZ) < 1) + continue; + + if (!strcmp(szElem[0], csAttr)) { + ret = i + 1; + if (psValue && valLen) + strlcpy(psValue, szElem[1], valLen); + break; + } + } + + free(items); + return ret; }