--- libaitio/src/Attic/url.c 2010/03/04 09:54:23 1.1.2.4 +++ libaitio/src/Attic/url.c 2010/03/22 14:48:38 1.2.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: url.c,v 1.1.2.4 2010/03/04 09:54:23 misho Exp $ +* $Id: url.c,v 1.2.2.1 2010/03/22 14:48:38 misho Exp $ * *************************************************************************/ #include "global.h" @@ -115,11 +115,12 @@ int ioURLGet(const char *csURL, struct tagIOURL *url) } /* - * io_MakeArray() Parse and make array of arguments values - * @psArgs = Input arguments line + * io_MakeArray() Parse and make array of arguments values ... + * (input string will be modified! and output array must be free) + * @psArgs = Input arguments line, after execute string is modified!!! * @csDelim = Delimiter(s) for separate * @args = Output array of arguments ... (must be free() after procced function!) - * @nargs = Requested count of arguments + * @nargs = Maximum requested count of arguments from input string psArgs * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items */ inline int io_MakeArray(char * __restrict psArgs, const char *csDelim, char *** __restrict args, int nargs) @@ -161,14 +162,16 @@ inline int io_SizeArray(const char *csArgs, const char * @csArgs = Input argument line * @csDelim = Delimiter for separate * @psAttr = Output Attribute + * @attrLen = Size of attribute array * @psValue = Output Value, if ==NULL this element not present value or not wanted for return + * @valLen = Size of value array * 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; + char *pos, *psBuf; if (!csArgs || !csDelim || !psAttr || !attrLen) return -1; @@ -176,12 +179,17 @@ inline int io_MakeAV(const char * __restrict csArgs, c return -1; else memset(psValue, 0, valLen); + psBuf = strdup(csArgs); + if (!psBuf) { + LOGERR; + return -1; + } - pos = strpbrk(csArgs, csDelim); + pos = strpbrk(psBuf, csDelim); if (pos) *pos++ = 0; ret++; - strlcpy(psAttr, csArgs, attrLen); + strlcpy(psAttr, psBuf, attrLen); if (pos && *pos) { ret++; @@ -189,10 +197,54 @@ inline int io_MakeAV(const char * __restrict csArgs, c strlcpy(psValue, pos, valLen); } + free(psBuf); return ret; } /* + * io_Path2File() Parse and make path/filename pair + * @csArgs = Input argument line + * @psPath = Output Path, if ==NULL path not returned + * @pathLen = Size of path array + * @psFile = Output File + * @fileLen = Size of file array + * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items +*/ +inline int io_Path2File(const char * __restrict csArgs, char * __restrict psPath, int pathLen, + char * __restrict psFile, int fileLen) +{ + char *pos, *psBuf; + + if (!csArgs || !psFile || !fileLen) + return -1; + if (psPath && !pathLen) + return -1; + else + memset(psPath, 0, pathLen); + psBuf = strdup(csArgs); + if (!psBuf) { + LOGERR; + return -1; + } + + pos = strrchr(psBuf, '/'); + if (!pos) { + strlcpy(psFile, psBuf, fileLen); + + free(psBuf); + return 1; + } else + *pos++ = 0; + + strlcpy(psFile, pos, fileLen); + if (psPath) + strlcpy(psPath, psBuf, pathLen); + + free(psBuf); + return 2; +} + +/* * ioURLGetValue() Get value from parsed URL * @url = Input parsed URL * @csAttr = Attribute for search @@ -204,15 +256,16 @@ int ioURLGetValue(struct tagIOURL *url, const char *cs { register int i, ret = 0; char szBuf[BUFSIZ], **items, szElem[2][BUFSIZ]; + int len; if (!url || !csAttr) return -1; strlcpy(szBuf, url->url_args.value, BUFSIZ); - if (io_MakeArray(szBuf, "&", &items, io_SizeArray(szBuf, "&")) < 1) + if (io_MakeArray(szBuf, "&", &items, (len = io_SizeArray(szBuf, "&"))) < 1) return ret; - for (i = 0; items[i]; i++) { + for (i = 0; i < len && items[i]; i++) { if (io_MakeAV(items[i], "=", szElem[0], BUFSIZ, szElem[1], BUFSIZ) < 1) continue; @@ -226,4 +279,25 @@ int ioURLGetValue(struct tagIOURL *url, const char *cs free(items); return ret; +} + +/* + * ioURLGetFile() Get file from parsed URL + * @url = Input parsed URL + * @psValue = Return filename, if not specified file in url path, replace with / + * @valLen = Size of psValue array + * return: -1 error:: can`t read; 0 ok +*/ +int ioURLGetFile(struct tagIOURL *url, char * __restrict psValue, int valLen) +{ + if (!url || !psValue || !valLen) + return -1; + + if (io_Path2File(url->url_path.value, NULL, 0, psValue, valLen) < 1) + return -1; + + // If not specified file in path, default replace to / + if (!*psValue) + strlcpy(psValue, "/", valLen); + return 0; }