Exemplo n.º 1
0
/**
 * call-seq:
 *   instance.each_property do |name, value|
 *      ...
 *   end
 *
 * enumerates properties yielding the property name and
 * its value
 *
 */
static VALUE each_property(VALUE self)
{
  CIMCInstance *ptr;
  CIMCStatus status;
  int k=0;
  int num_props=0;
  CIMCString *property_name = NULL;
  CIMCData data;
  Data_Get_Struct(self, CIMCInstance, ptr);

  num_props = ptr->ft->getPropertyCount(ptr, &status);
  if (!status.rc) {
    for (; k < num_props; ++k) {
      data = ptr->ft->getPropertyAt(ptr, k, &property_name, &status);
      if (!status.rc) {
        rb_yield_values(2, (property_name ? rb_str_intern(rb_str_new2(property_name->ft->getCharPtr(property_name, NULL))) : Qnil), sfcc_cimdata_to_value(data));
      }
      else {
        sfcc_rb_raise_if_error(status, "Can't retrieve property #%d", k);
      } 
      if (property_name) CMRelease(property_name);
    }
  }
  else {
    sfcc_rb_raise_if_error(status, "Can't retrieve property count");
  }
  return Qnil;
}
Exemplo n.º 2
0
Arquivo: sfcc.c Projeto: wca/ruby-sfcc
VALUE sfcc_cimargs_to_hash(CMPIArgs *args)
{
  CMPIArgs *ptr = NULL;
  int i = 0;
  int n = 0;
  VALUE hash;
  CMPIString *argname;
  CMPIData argdata;
  CMPIStatus status;
  char *argname_cstr = NULL;

  //Data_Get_Struct(args, CMPIArgs, ptr);
  ptr = args;
  if (!ptr) {
    rb_raise(rb_eRuntimeError, "Can't retrieve args pointer");
    return Qnil;
  }
  n = ptr->ft->getArgCount(ptr, NULL);
  hash = rb_hash_new();

  for (; i < n; ++i) {
    argname = NULL;
    argdata = ptr->ft->getArgAt(ptr, i, &argname, &status);
    if (!status.rc && argdata.state == CMPI_goodValue ) {
      argname_cstr = argname->ft->getCharPtr(argname, &status);
      if (!argname_cstr) {
        rb_raise(rb_eRuntimeError, "Can't retrieve argument name");
        return Qnil;
      }

      if (!status.rc) {
        rb_hash_aset(hash, rb_funcall(rb_str_new2(argname_cstr), rb_intern("to_sym"), 0), sfcc_cimdata_to_value(argdata));
      }
      else {
        sfcc_rb_raise_if_error(status, "Can't retrieve argument name");
        return Qnil;
      }
    }
    else {
      sfcc_rb_raise_if_error(status, "Can't retrieve argument");
      return Qnil;
    }
  }
  return hash;
}
Exemplo n.º 3
0
VALUE sfcc_cimargs_to_hash(CIMCArgs *args, VALUE client)
{
  int i = 0;
  int n = 0;
  VALUE hash;
  CIMCString *argname;
  CIMCData argdata;
  CIMCStatus status;
  char *argname_cstr = NULL;

  /* Data_Get_Struct(value, CIMCArgs, args); */
  if (!args) {
    rb_raise(rb_eRuntimeError, "Can't retrieve args pointer");
    return Qnil;
  }
  n = args->ft->getArgCount(args, NULL);
  hash = rb_hash_new();

  for (; i < n; ++i) {
    argname = NULL;
    argdata = args->ft->getArgAt(args, i, &argname, &status);
    if (!status.rc && argdata.state == CIMC_goodValue ) {
      argname_cstr = argname->ft->getCharPtr(argname, &status);
      if (!argname_cstr) {
        rb_raise(rb_eRuntimeError, "Can't retrieve argument name");
        return Qnil;
      }

      if (!status.rc) {
        rb_hash_aset(hash, rb_funcall(rb_str_new2(argname_cstr), rb_intern("to_sym"), 0), sfcc_cimdata_to_value(&argdata, client));
      }
      else {
        sfcc_rb_raise_if_error(status, "Can't retrieve argument name");
        return Qnil;
      }
    }
    else {
      sfcc_rb_raise_if_error(status, "Can't retrieve argument");
      return Qnil;
    }
  }
  return hash;
}
Exemplo n.º 4
0
/**
 * call-seq:
 *   namespace=(ns)
 *
 * Set/replace the namespace component
 */
static VALUE set_namespace(VALUE self, VALUE val)
{
  CIMCObjectPath *ptr;
  CIMCStatus status;
  Data_Get_Struct(self, CIMCObjectPath, ptr);
  status = ptr->ft->setNameSpace(ptr, to_charptr(val));
  if (!status.rc)
    return val;
  sfcc_rb_raise_if_error(status, "Can't set namespace");
  return Qnil;
}
Exemplo n.º 5
0
/**
 * call-seq:
 *   namespace=(ns)
 *
 * Set/replace the namespace component
 */
static VALUE set_namespace(VALUE self, VALUE val)
{
  CMPIObjectPath *ptr = NULL;
  CMPIStatus status;
  Data_Get_Struct(self, CMPIObjectPath, ptr);
  status = ptr->ft->setNameSpace(ptr, StringValuePtr(val));
  if (!status.rc)
    return val;
  sfcc_rb_raise_if_error(status, "Can't set namespace");
  return Qnil;
}
Exemplo n.º 6
0
/**
 * call-seq:
 *   property(name)
 *
 * Gets a named property value, where name is a Symbol or String
 */
static VALUE property(VALUE self, VALUE name)
{
  CIMCInstance *ptr;
  CIMCStatus status;
  CIMCData data;
  Data_Get_Struct(self, CIMCInstance, ptr);
  data = ptr->ft->getProperty(ptr, to_charptr(name), &status);
  if ( !status.rc )
    return sfcc_cimdata_to_value(data);
  sfcc_rb_raise_if_error(status, "Can't retrieve property '%s'", to_charptr(name));
  return Qnil;
}
Exemplo n.º 7
0
VALUE
sfcc_cimcarray_to_rubyarray(CIMCArray *array, VALUE client)
{
  CIMCCount size;
  CIMCCount i;
  CIMCStatus st = { 0, NULL };
  VALUE ary;
  if (!array)
    rb_raise(rb_eArgError, "Cannot convert NULL array");
  size = array->ft->getSize(array, &st);
  sfcc_rb_raise_if_error(st, "Can't get array size");
  ary = rb_ary_new2(size);
  for (i = 0; i < size; ++i) {
    CIMCData data;
    VALUE value;
    array->ft->getElementAt(array,i,&st);
    sfcc_rb_raise_if_error(st, "Can't get array element %d of %d", i, size);
    value = sfcc_cimdata_to_value(&data, client);
    rb_ary_store(ary,i,value);
  }
  return ary;
}
Exemplo n.º 8
0
/**
 * call-seq:
 *   qualifier(name)
 *
 * gets a named qualifier value
 */
static VALUE qualifier(VALUE self, VALUE name)
{
  CIMCInstance *ptr;
  CIMCStatus status;
  CIMCData data;
  memset(&status, 0, sizeof(CIMCStatus));
  Data_Get_Struct(self, CIMCInstance, ptr);
  data = ptr->ft->getQualifier(ptr, to_charptr(name), &status);
  if ( !status.rc )
    return sfcc_cimdata_to_value(data);

  sfcc_rb_raise_if_error(status, "Can't retrieve qualifier '%s'", to_charptr(name));
  return Qnil;
}
Exemplo n.º 9
0
/* 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;
}
Exemplo n.º 10
0
Arquivo: sfcc.c Projeto: wca/ruby-sfcc
/* 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;
}
Exemplo n.º 11
0
/**
 * call-seq:
 *   set_property_filter(property_list, keys)
 *
 * Directs CIMC to ignore any setProperty operations for this
 * instance for any properties not in this list.
 *
 * +property_list+ If not nil, the members of the array define one
 * or more Property names to be accepted by set_property operations.
 *
 * +keys+ Array of key property names of this instance. This array
 * must be specified.
 *
 */
static VALUE set_property_filter(VALUE self, VALUE property_list, VALUE keys)
{
  CIMCStatus status;
  CIMCInstance *ptr;
  char **prop_a;
  char **key_a;

  Data_Get_Struct(self, CIMCInstance, ptr);

  prop_a = sfcc_value_array_to_string_array(property_list);
  key_a = sfcc_value_array_to_string_array(keys);

  status = ptr->ft->setPropertyFilter(ptr, prop_a, key_a);
  free(prop_a);
  free(key_a);

  sfcc_rb_raise_if_error(status, "Can't set property filter");
  return self;
}
Exemplo n.º 12
0
VALUE sfcc_cimdata_to_value(CIMCData data)
{
  CIMCString *cimstr = NULL;
  VALUE rbval;
  CIMCStatus status;

  if (data.type & CMPI_ARRAY) {
    int k = 0;
    int n = 0;
    VALUE rbarray = rb_ary_new();

    if (!data.value.array)
      return rb_ary_new();

    n = data.value.array->ft->getSize(data.value.array, &status);
    if (!status.rc) {
      for (k = 0; k < n; ++k) {
        CIMCData element = data.value.array->ft->getElementAt(data.value.array, k, NULL);
        rb_ary_push(rbarray, sfcc_cimdata_to_value(element));
      }
      return rbarray;
    }
    sfcc_rb_raise_if_error(status, "Can't retrieve array size");
    return Qnil;
  }
  else if (data.type & CMPI_ENC) {
    switch (data.type) {
    case CMPI_instance:
      return data.value.inst ? Sfcc_wrap_cim_instance(data.value.inst->ft->clone(data.value.inst, NULL)) : Qnil;
    case CMPI_class:
      return data.value.cls ? Sfcc_wrap_cim_class(data.value.cls->ft->clone(data.value.cls, NULL)) : Qnil;
    case CMPI_ref:
      return data.value.ref ? Sfcc_wrap_cim_object_path(data.value.ref->ft->clone(data.value.ref, NULL)) : Qnil;
    case CMPI_args:
      return data.value.args ? sfcc_cimargs_to_hash(data.value.args) : Qnil;
    case CMPI_filter:
      return Qnil;
    case CMPI_numericString:
    case CMPI_booleanString:
    case CMPI_dateTimeString:
    case CMPI_classNameString:
      break;
    case CMPI_string:
      return data.value.string ? rb_str_new2((char*)data.value.string->ft->getCharPtr(data.value.string, NULL)) : Qnil;
    case CMPI_charsptr:
      return data.value.chars ? rb_str_new((char*)data.value.dataPtr.ptr, data.value.dataPtr.length) : Qnil;
    case CMPI_dateTime:
      cimstr = data.value.dateTime ? CMGetStringFormat(data.value.dateTime,NULL) : NULL;
      rbval = cimstr ? rb_str_new2(cimstr->ft->getCharPtr(cimstr, NULL)) : Qnil;
      if (cimstr) CMRelease(cimstr);
      return rbval;
    }
  }
  else if (data.type & CMPI_SIMPLE) {
    switch (data.type) {
    case CMPI_boolean: return data.value.boolean ? Qtrue : Qfalse;
    case CMPI_char16: return UINT2NUM(data.value.char16);
    }
  }
  else if (data.type & CMPI_INTEGER) {
    switch (data.type) {
    case CMPI_uint8: return UINT2NUM(data.value.uint8);
    case CMPI_sint8: return INT2NUM(data.value.sint8);
    case CMPI_uint16: return UINT2NUM(data.value.uint16);
    case CMPI_sint16: return INT2NUM(data.value.sint16);
    case CMPI_uint32: return UINT2NUM(data.value.uint32);
    case CMPI_sint32: return INT2NUM(data.value.sint32);
    case CMPI_uint64: return UINT2NUM(data.value.uint64);
    case CMPI_sint64: return INT2NUM(data.value.sint64);
    }
  }
  else if (data.type & CMPI_REAL) {
    switch (data.type) {
    case CMPI_real32: return LONG2NUM(data.value.real32);
    case CMPI_real64: return LONG2NUM(data.value.real64);
    }
  }
  else if (data.type & CMPI_null ) {
    return Qnil;
  }
  rb_raise(rb_eTypeError, "unsupported data data type %d", data.type);
  return Qnil;
}
Exemplo n.º 13
0
VALUE sfcc_cimdata_to_value(CIMCData *data, VALUE client)
{
  VALUE rbval;
  CIMCStatus status;

  if ((data->state != CIMC_goodValue)
      && (data->state != CIMC_keyValue)) {
    if (data->state & CIMC_nullValue)
      return Qnil;
    if (data->state & CIMC_notFound)
      rb_raise(rb_eRuntimeError, "Value not found");
    if (data->state & CIMC_badValue)
      rb_raise(rb_eArgError, "Bad value");
  }
  if (data->type & CIMC_ARRAY) {
    int k = 0;
    int n = 0;
    VALUE rbarray = rb_ary_new();

    if (!data->value.array)
      return rb_ary_new();

    n = data->value.array->ft->getSize(data->value.array, &status);
    if (!status.rc) {
      for (k = 0; k < n; ++k) {
        CIMCData element = data->value.array->ft->getElementAt(data->value.array, k, NULL);
        rb_ary_push(rbarray, sfcc_cimdata_to_value(&element, client));
      }
      return rbarray;
    }
    sfcc_rb_raise_if_error(status, "Can't retrieve array size");
    return Qnil;
  }
  else if (data->type & CIMC_ENC) {
    switch (data->type) {
    case CIMC_instance:
      return data->value.inst ? Sfcc_wrap_cim_instance(data->value.inst->ft->clone(data->value.inst, NULL), client) : Qnil;
    case CIMC_ref:
      return data->value.ref ? Sfcc_wrap_cim_object_path(data->value.ref->ft->clone(data->value.ref, NULL), client) : Qnil;
    case CIMC_args:
      return data->value.args ? sfcc_cimargs_to_hash(data->value.args, client) : Qnil;
    case CIMC_class:
      return data->value.cls ? Sfcc_wrap_cim_class(data->value.cls->ft->clone(data->value.cls, NULL)) : Qnil;
    case CIMC_filter:
      return Qnil;
    case CIMC_enumeration:
      return data->value.Enum ? Sfcc_wrap_cim_enumeration(data->value.Enum->ft->clone(data->value.Enum, NULL), client) : Qnil;
    case CIMC_string:
      if (data->value.string) {
        const char *strval = data->value.string->ft->getCharPtr(data->value.string, NULL);
        /* getCharPtr() might return NULL and rb_str_new2 doesn't like that */
        if (strval)
          return rb_str_new2(strval);
      }
      return Qnil;
    case CIMC_chars:
      return data->value.chars ? rb_str_new2(data->value.chars) : Qnil;
    case CIMC_charsptr:
      return data->value.chars ? rb_str_new((char*)data->value.dataPtr.ptr, data->value.dataPtr.length) : Qnil;
    case CIMC_dateTime:
      if (data->value.dateTime) {
        CIMCUint64 bintime;
        bintime = data->value.dateTime->ft->getBinaryFormat(data->value.dateTime, NULL);
        rbval = rb_time_new((time_t) (bintime / 1000000L), (time_t) (bintime % 1000000));
      }
      else {
        rbval = Qnil;
      }
      return rbval;
    default:
      rb_raise(rb_eTypeError, "Unhandled type 0x%04x", data->type);
    }
  }
  else if (data->type & CIMC_SIMPLE) {
    switch (data->type) {
    case CIMC_boolean: return data->value.boolean ? Qtrue : Qfalse;
    case CIMC_char16: return UINT2NUM(data->value.char16);
    }
  }
  else if (data->type & CIMC_INTEGER) {
    switch (data->type) {
    case CIMC_uint8: return UINT2NUM(data->value.uint8);
    case CIMC_sint8: return INT2NUM(data->value.sint8);
    case CIMC_uint16: return UINT2NUM(data->value.uint16);
    case CIMC_sint16: return INT2NUM(data->value.sint16);
    case CIMC_uint32: return UINT2NUM(data->value.uint32);
    case CIMC_sint32: return INT2NUM(data->value.sint32);
    case CIMC_uint64: return UINT2NUM(data->value.uint64);
    case CIMC_sint64: return INT2NUM(data->value.sint64);
    }
  }
  else if (data->type & CIMC_REAL) {
    switch (data->type) {
    case CIMC_real32: return rb_float_new(data->value.real32);
    case CIMC_real64: return rb_float_new(data->value.real64);
    }
  }
  else if (data->type & CIMC_null ) {
    return Qnil;
  }
  rb_raise(rb_eTypeError, "unsupported data type %d", data->type);
  return Qnil;
}