CIMCArray* sfcc_rubyarray_to_cimcarray( VALUE array, CIMCType *type) { Check_Type(array, T_ARRAY); // raise exc. if not an array CIMCArray * cimcarr = NULL; CIMCCount i = 0; int len = RARRAY_LEN(array); VALUE array_value = Qnil; CIMCData array_data; *type = CIMC_stringA; /* sfcc can't handle CIMC_null */ if (len > 0) { /* try to deduce type from first array element */ array_value = rb_ary_entry(array, 0); if (TYPE(array_value) == T_ARRAY) { rb_raise(rb_eTypeError, "nested arrays are not supported"); return cimcarr; } array_data = sfcc_value_to_cimdata(array_value); *type = array_data.type; } cimcarr = cimcEnv->ft->newArray(cimcEnv, len, *type, NULL); if (!cimcarr) { rb_raise(rb_eNoMemError, "failed to allocate new CIMCArray"); return cimcarr; } if (len > 0) { // set the first element // this method does the cloning of passed data value cimcarr->ft->setElementAt(cimcarr, i++, &(array_data.value), array_data.type); if (TYPE(array_value) != T_DATA) { // in case of string/chars/data_ptr, the data must be freed Sfcc_clear_cim_data(&array_data); } } for (; i < (typeof(i)) len; ++i) { // set the rest of elements array_value = rb_ary_entry(array, i); array_data = sfcc_value_to_cimdata(array_value); if (*type != array_data.type) { cimcarr->ft->release(cimcarr); if (TYPE(array_value) != T_DATA) { Sfcc_clear_cim_data(&array_data); } rb_raise(rb_eTypeError, "all elements of array must" " have the same type"); break; } cimcarr->ft->setElementAt(cimcarr, i, &(array_data.value), array_data.type); if (TYPE(array_value) != T_DATA) { Sfcc_clear_cim_data(&array_data); } } *type |= CIMC_ARRAY; return cimcarr; }
/** * call-seq: * set_property(name, value) * * Adds/replaces a names property */ static VALUE set_property(VALUE self, VALUE name, VALUE value) { CIMCInstance *ptr; CIMCData data; Data_Get_Struct(self, CIMCInstance, ptr); data = sfcc_value_to_cimdata(value); ptr->ft->setProperty(ptr, to_charptr(name), &data.value, data.type); return value; }
/* callback to add each hash element to a CMPIArgs */ static int hash_to_cimargs_iterator(VALUE key, VALUE value, VALUE extra) { CIMCStatus status; CIMCData data; CIMCArgs *args = (CIMCArgs *)extra; const char *key_cstr = to_charptr(key); data = sfcc_value_to_cimdata(value); status = args->ft->addArg(args, key_cstr, &data.value, data.type); if ( !status.rc ) { return ST_CONTINUE; } sfcc_rb_raise_if_error(status, "Can't add argument '%s'", to_charptr(key)); return ST_STOP; }
/* callback to add each hash element to a CMPIArgs */ static int hash_to_cimargs_iterator(VALUE key, VALUE value, VALUE extra) { CMPIStatus status; CMPIData data; CMPIArgs *args = (CMPIArgs *)extra; VALUE key_str = rb_funcall(key, rb_intern("to_s"), 0); char *key_cstr = StringValuePtr(key_str); data = sfcc_value_to_cimdata(value); status = args->ft->addArg(args, key_cstr, &data.value, data.type); if ( !status.rc ) { return ST_CONTINUE; } sfcc_rb_raise_if_error(status, "Can't add argument '%s'", StringValuePtr(key)); return ST_STOP; }