Exemplo n.º 1
0
liValue* li_common_value_copy_(liValue* val) {
	liValue *n;
	if (NULL == val) return NULL;

	switch (val->type) {
	case LI_VALUE_NONE: return li_value_new_none();
	case LI_VALUE_BOOLEAN: return li_value_new_bool(val->data.boolean);
	case LI_VALUE_NUMBER: return li_value_new_number(val->data.number);
	case LI_VALUE_STRING: return li_value_new_string(g_string_new_len(GSTR_LEN(val->data.string)));
	/* list: we have to copy every value in the list! */
	case LI_VALUE_LIST:
		n = li_value_new_list();
		g_ptr_array_set_size(n->data.list, val->data.list->len);
		for (guint i = 0; i < val->data.list->len; i++) {
			g_ptr_array_index(n->data.list, i) = li_value_copy(g_ptr_array_index(val->data.list, i));
		}
		return n;
	}
	return NULL;
}
Exemplo n.º 2
0
liValue* li_value_copy(liValue* val) {
    liValue *n;
    if (NULL == val) return NULL;

    switch (val->type) {
    case LI_VALUE_NONE:
        return li_value_new_none();
    case LI_VALUE_BOOLEAN:
        return li_value_new_bool(val->data.boolean);
    case LI_VALUE_NUMBER:
        return li_value_new_number(val->data.number);
    case LI_VALUE_STRING:
        return li_value_new_string(g_string_new_len(GSTR_LEN(val->data.string)));
    /* list: we have to copy every value in the list! */
    case LI_VALUE_LIST:
        n = li_value_new_list();
        g_array_set_size(n->data.list, val->data.list->len);
        for (guint i = 0; i < val->data.list->len; i++) {
            g_array_index(n->data.list, liValue*, i) = li_value_copy(g_array_index(val->data.list, liValue*, i));
        }
        return n;
    /* hash: iterate over hashtable, clone each value */
    case LI_VALUE_HASH:
        n = li_value_new_hash();
        {
            GHashTableIter iter;
            gpointer k, v;
            g_hash_table_iter_init(&iter, val->data.hash);
            while (g_hash_table_iter_next(&iter, &k, &v))
                g_hash_table_insert(n->data.hash, g_string_new_len(GSTR_LEN((GString*)k)), li_value_copy((liValue*)v));
        }
        return n;
    case LI_VALUE_ACTION:
        li_action_acquire(val->data.val_action.action);
        n = li_value_new_action(val->data.val_action.srv, val->data.val_action.action);
        return n;
    case LI_VALUE_CONDITION:
        li_condition_acquire(val->data.val_cond.cond);
        n = li_value_new_condition(val->data.val_cond.srv, val->data.val_cond.cond);
        return n;
    }
    return NULL;
}