Пример #1
0
int webapp_chat_add(const char *peer, const char *message, bool self)
{
	char time_s[256] = {0};
	int err = 0;
	struct odict *o;

	err = odict_alloc(&o, DICT_BSIZE);
	if (err)
		return ENOMEM;

	odict_entry_add(o, "message", ODICT_STRING, message);
	odict_entry_add(o, "peer", ODICT_STRING, peer);
	odict_entry_add(o, "self", ODICT_BOOL, self);

	(void)re_snprintf(time_s, sizeof(time_s), "%d", time(NULL));

	odict_entry_add(messages, time_s, ODICT_OBJECT, o);

	mem_deref(o);

	return err;
}
Пример #2
0
void webapp_odict_add(struct odict *og, const struct odict_entry *eg)
{
	struct le *le;
	struct odict *o;
	int err = 0;
	char index[64];
	size_t index_cnt=0;

	err = odict_alloc(&o, DICT_BSIZE);
	if (err)
		return;

	le = (void *)&eg->le;
	if (!le)
		return;

	for (le=le->list->head; le; le=le->next) {
		const struct odict_entry *e = le->data;
		if (!str_cmp(e->key, "command"))
			continue;
		odict_entry_add(o, e->key, e->type, e->u.str);
	}

	/* Limited Loop
	 * No one will need more than 100 accounts for a personal computer
	 */
	for (int i=0; i<100; i++) {
		re_snprintf(index, sizeof(index), "%u", index_cnt);
		if (!odict_lookup(og, index)) {
			break;
		}
		index_cnt = index_cnt + 1;
	}

	odict_entry_add(og, index, ODICT_OBJECT, o);

	mem_deref(o);
}
static int entry_add(struct odict *o, const char *name,
		     const struct json_value *val)
{
	switch (val->type) {

	case JSON_STRING:
		return odict_entry_add(o, name, ODICT_STRING, val->v.str);

	case JSON_INT:
		return odict_entry_add(o, name, ODICT_INT, val->v.integer);

	case JSON_DOUBLE:
		return odict_entry_add(o, name, ODICT_DOUBLE, val->v.dbl);

	case JSON_BOOL:
		return odict_entry_add(o, name, ODICT_BOOL, val->v.boolean);

	case JSON_NULL:
		return odict_entry_add(o, name, ODICT_NULL);

	default:
		return ENOSYS;
	}
}
static int container_add(const char *name, unsigned idx,
			 enum odict_type type, struct json_handlers *h)
{
	struct odict *o = h->arg, *oc;
	char index[64];
	int err;

	if (!name) {
		if (re_snprintf(index, sizeof(index), "%u", idx) < 0)
			return ENOMEM;

		name = index;
	}

	err = odict_alloc(&oc, hash_bsize(o->ht));
	if (err)
		return err;

	err = odict_entry_add(o, name, type, oc);
	mem_deref(oc);
	h->arg = oc;

	return err;
}