rho_param *rho_param_dup(rho_param *p) { int i, lim; rho_param *ret = NULL; if (!p) return NULL; switch (p->type) { case RHO_PARAM_STRING: ret = rho_param_str(p->v.string); break; case RHO_PARAM_ARRAY: ret = rho_param_array(p->v.array->size); for (i = 0, lim = p->v.array->size; i < lim; ++i) ret->v.array->value[i] = rho_param_dup(p->v.array->value[i]); break; case RHO_PARAM_HASH: ret = rho_param_hash(p->v.hash->size); for (i = 0, lim = p->v.hash->size; i < lim; ++i) { ret->v.hash->name[i] = strdup(p->v.hash->name[i]); ret->v.hash->value[i] = rho_param_dup(p->v.hash->value[i]); } break; } return ret; }
rho_param *rho_param_fromvalue(VALUE v) { int i, size; /* if (v == NULL) { return NULL; } if (rho_ruby_is_NIL(v)) { return NULL; } */ switch(TYPE(v)) { case T_ARRAY: { rho_param *p = 0; size = RARRAY_LEN(v); p = rho_param_array(size); for (i = 0; i < size; ++i) { VALUE item = rb_ary_entry(v, i); p->v.array->value[i] = rho_param_fromvalue(item); } return p; } break; case T_HASH: { VALUE keys = rb_funcall(v, rb_intern("keys"), 0, NULL); rho_param *p = 0; size = RARRAY_LEN(keys); p = rho_param_hash(size); for (i = 0; i < size; ++i) { VALUE key = rb_ary_entry(keys, i); VALUE value = rb_hash_aref(v, key); p->v.hash->name[i] = strdup(RSTRING_PTR(rb_String(key))); p->v.hash->value[i] = rho_param_fromvalue(value); } return p; } break; default: { VALUE value = v; if (TYPE(v) != T_STRING) value = rb_funcall(value, rb_intern("to_s"), 0, NULL); return rho_param_str(RSTRING_PTR(value)); } } }