File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / Attic / url.c
Revision 1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Thu Mar 4 08:10:57 2010 UTC (14 years, 7 months ago) by misho
Branches: io1_0
change comment

/*************************************************************************
* (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: url.c,v 1.1.2.3 2010/03/04 08:10:57 misho Exp $
*
*************************************************************************/
#include "global.h"


/*
 * ioURLGet() Parse and get data from input URL
 * @csURL = Input URL line
 * @url = Output parsed URL
 * return: 0 error format not find tech:// and return URL like path; 
 		-1 error:: can`t read; >0 ok, up bits for known elements
*/
int ioURLGet(const char *csURL, struct tagIOURL *url)
{
	char *pos, *at, *cl, *sl;
	int ret = 0;

	if (!url)
		return -1;
	else
		memset(url, 0, sizeof(*url));

	strlcpy((char*) url->url_line, csURL, BUFSIZ);
	// Tech
	if (!(pos = strstr((char*) url->url_line, "://"))) {
		url->url_path.vallen = strlen((char*) url->url_line);
		url->url_path.value = (char*) url->url_line;
		return ret;
	} else {
		url->url_tech.value = (char*) url->url_line;
		url->url_tech.vallen = pos - (char*) url->url_line;
		if (url->url_tech.vallen)
			ret |= 1;

		*pos = 0;
		pos += 3;
	}

	// User
	if ((at = strchr(pos, '@'))) {
		*at++ = 0;
		// Pass
		if ((cl = strchr(pos, ':'))) {
			*cl++ = 0;

			url->url_pass.value = cl;
			url->url_pass.vallen = at - cl - 1;
			if (url->url_pass.vallen)
				ret |= 4;
		} else
			cl = at;

		url->url_user.value = pos;
		url->url_user.vallen = cl - pos - 1;
		if (url->url_user.vallen)
			ret |= 2;

		pos = at;
	}

	// Host
	if ((sl = strchr(pos, '/')))
		*sl++ = 0;
	else
		sl = pos + strlen(pos) + 1;
	// Port
	if ((cl = strchr(pos, ':'))) {
		*cl++ = 0;

		url->url_port.value = cl;
		url->url_port.vallen = sl - cl - 1;
		if (url->url_port.vallen)
			ret |= 16;
	} else
		cl = sl;

	url->url_host.value = pos;
	url->url_host.vallen = cl - pos - 1;
	if (url->url_host.vallen)
		ret |= 8;

	pos = sl;

	// Args
	if ((at = strchr(pos, '?'))) {
		*at++ = 0;

		url->url_args.value = at;
		url->url_args.vallen = strlen(at);
		if (url->url_args.vallen)
			ret |= 64;
	} else
		at = pos + strlen(pos) + 1;

	// Path
	url->url_path.value = pos;
	url->url_path.vallen = at - pos - 1;
	if (url->url_path.vallen)
		ret |= 32;

	pos = at + strlen(at);

	// Reserved
	url->url_reserved = pos;
	if (*pos)
		ret |= 128;

	return ret;
}

/*
 * io_MakeArray() Parse and make array of arguments values
 * @psArgs = Input arguments line
 * @csDelim = Delimiter(s) for separate
 * @args = Output array of arguments ... (must be free() after procced function!)
 * @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)
{
	char **app;
	register int i;

	if (!psArgs || !args || !nargs)
		return -1;
	if (!(*args = malloc(sizeof(char*) * nargs))) {
		LOGERR;
		return -1;
	} else
		memset(*args, 0, sizeof(char*) * nargs);

	for (i = 0, app = *args; app < *args + nargs && (*app = strsep(&psArgs, csDelim)); 
				**app ? i++ : i, **app ? app++ : app);
	return i;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>