Ejemplo n.º 1
0
bool mdict_put_str(struct MDict *dict, const char *key, unsigned klen, const char *val, unsigned vlen)
{
	char *kptr, *vptr = NULL;
	struct MDictElem *el;

	if (val) {
		vptr = cx_alloc(dict->cx, vlen + 1);
		if (!vptr)
			return false;
		memcpy(vptr, val, vlen);
		vptr[vlen] = 0;
	}
	el = cbtree_lookup(dict->tree, key, klen);
	if (el) {
		cx_free(dict->cx, mbuf_data(&el->val));
		mbuf_init_fixed_reader(&el->val, vptr, vlen);
	} else {
		kptr = cx_alloc(dict->cx, klen + 1);
		if (!kptr)
			return false;
		memcpy(kptr, key, klen);
		kptr[klen] = 0;

		el = cx_alloc(dict->cx, sizeof(*el));
		if (!el)
			return false;

		mbuf_init_fixed_reader(&el->key, kptr, klen);
		mbuf_init_fixed_reader(&el->val, vptr, vlen);
		if (!cbtree_insert(dict->tree, el))
			return false;
	}
	return true;
}
Ejemplo n.º 2
0
bool mdict_urldecode(struct MDict *dict, const char *str, unsigned len)
{
	const char *s = str;
	const char *end = s + len;
	const char *k, *v;
	unsigned klen, vlen;
	struct MDictElem *el;

	while (s < end) {
		v = NULL;
		vlen = 0;
		el = NULL;

		/* read key */
		k = urldec_str(dict->cx, &s, end, &klen);
		if (!k)
			goto fail;

		/* read value */
		if (s < end && *s == '=') {
			s++;
			v = urldec_str(dict->cx, &s, end, &vlen);
			if (!v)
				goto fail;
		}
		if (s < end && *s == '&')
			s++;

		/* insert value */
		el = cbtree_lookup(dict->tree, k, klen);
		if (el) {
			cx_free(dict->cx, mbuf_data(&el->val));
			mbuf_init_fixed_reader(&el->val, v, vlen);
		} else {
			el = cx_alloc(dict->cx, sizeof(*el));
			if (!el)
				goto fail;

			mbuf_init_fixed_reader(&el->key, k, klen);
			mbuf_init_fixed_reader(&el->val, v, vlen);
			if (!cbtree_insert(dict->tree, el))
				goto fail;
		}
	}
	return true;
fail:
	if (k) cx_free(dict->cx, k);
	if (v) cx_free(dict->cx, v);
	if (el) cx_free(dict->cx, el);
	return false;
}
Ejemplo n.º 3
0
/* get new reference to str */
struct PStr *strpool_get(struct StrPool *sp, const char *str, int len)
{
	struct PStr *cstr;
	bool ok;

	pthread_mutex_lock(&sp->mutex);
	if (len < 0)
		len = strlen(str);

	/* search */
	cstr = cbtree_lookup(sp->tree, str, len);
	if (cstr) {
		cstr->refcnt++;
		pthread_mutex_unlock(&sp->mutex);
		return cstr;
	}

	/* create */
	cstr = cx_alloc(sp->ca, sizeof(*cstr) + len + 1);
	if (!cstr) {
		pthread_mutex_unlock(&sp->mutex);
		return NULL;
	}
	cstr->pool = sp;
	cstr->refcnt = 1;
	cstr->len = len;
	memcpy(cstr->str, str, len + 1);

	/* insert */
	ok = cbtree_insert(sp->tree, cstr);
	if (!ok) {
		cx_free(sp->ca, cstr);
		pthread_mutex_unlock(&sp->mutex);
		return NULL;
	}
	sp->count++;
	pthread_mutex_unlock(&sp->mutex);
	return cstr;
}