Esempio n. 1
0
	rgb_color
	Convert<rgb_color>::FromValue(VALUE v)
	{
		int value;
		rgb_color c;

		Check_Type(v, T_ARRAY);

		switch (RARRAY_LEN(v)) {
		case 3:
			value = 0;
			break;
		case 4:
			value = NUM2INT(RARRAY_AREF(v, 3));
			break;
		default:
			rb_raise(rb_eRangeError, "given array cannot be interpreted as rgb_color (length should be 4)");
		}
		if (value < 0 || value > 255)
			rb_raise(rb_eRangeError, "given array cannot be interpreted as rgb_color (alpha out of range (0..255))");
		c. alpha = (uint8) value;
		value = NUM2INT(RARRAY_AREF(v, 0));
		if (value < 0 || value > 255)
			rb_raise(rb_eRangeError, "given array cannot be interpreted as rgb_color (red out of range (0..255))");
		c.red = (uint8)value;
		value = NUM2INT(RARRAY_AREF(v, 1));
		if (value < 0 || value > 255)
			rb_raise(rb_eRangeError, "given array cannot be interpreted as rgb_color (green out of range (0..255))");
		c.green = (uint8)value;
		value = NUM2INT(RARRAY_AREF(v, 2));
		if (value < 0 || value > 255)
			rb_raise(rb_eRangeError, "given array cannot be interpreted as rgb_color (blue out of range (0..255))");
		c.blue = (uint8)value;
		return c;
	}
Esempio n. 2
0
static VALUE
ossl_x509crl_set_revoked(VALUE self, VALUE ary)
{
    X509_CRL *crl;
    X509_REVOKED *rev;
    STACK_OF(X509_REVOKED) *sk;
    long i;

    Check_Type(ary, T_ARRAY);
    /* All ary members should be X509 Revoked */
    for (i=0; i<RARRAY_LEN(ary); i++) {
	OSSL_Check_Kind(RARRAY_AREF(ary, i), cX509Rev);
    }
    GetX509CRL(self, crl);
    if ((sk = X509_CRL_get_REVOKED(crl))) {
	while ((rev = sk_X509_REVOKED_pop(sk)))
	    X509_REVOKED_free(rev);
    }
    for (i=0; i<RARRAY_LEN(ary); i++) {
	rev = DupX509RevokedPtr(RARRAY_AREF(ary, i));
	if (!X509_CRL_add0_revoked(crl, rev)) { /* NO DUP - don't free! */
	    X509_REVOKED_free(rev);
	    ossl_raise(eX509CRLError, "X509_CRL_add0_revoked");
	}
    }
    X509_CRL_sort(crl);

    return ary;
}
Esempio n. 3
0
AccessClass::AccessClass(RubyValue className, RubyValue methodInfos, RubyValue signalInfos, RubyValue propertyInfos)
{
    setClassName(className.to<QByteArray>());
    protect([&] {
        rb_check_array_type(methodInfos);
        rb_check_array_type(signalInfos);
        rb_check_array_type(propertyInfos);
    });
    for (int i = 0; i < RARRAY_LEN(VALUE(methodInfos)); ++i) {
        RubyValue info = RARRAY_AREF(VALUE(methodInfos), i);
        auto nameSym = info.send("name");
        addMethod(nameSym.to<QByteArray>(),
                  nameSym.toID(),
                  info.send("params").to<QList<QByteArray>>());
    }
    for (int i = 0; i < RARRAY_LEN(VALUE(signalInfos)); ++i) {
        RubyValue info = RARRAY_AREF(VALUE(signalInfos), i);
        auto nameSym = info.send("name");
        addSignal(nameSym.to<QByteArray>(),
                  nameSym.toID(),
                  info.send("params").to<QList<QByteArray>>());
    }
    for (int i = 0; i < RARRAY_LEN(VALUE(propertyInfos)); ++i) {
        RubyValue info = RARRAY_AREF(VALUE(propertyInfos), i);
        addProperty(info.send("name").to<QByteArray>(),
                    info.send("getter").toID(),
                    info.send("setter").toID(),
                    Property::Flag::Readable | Property::Flag::Writable,
                    true,
                    info.send("notifier").toID());
    }
}
Esempio n. 4
0
wxSize unwrap< wxSize >(const VALUE &vsize)
{
	if(rb_obj_is_kind_of(vsize, rb_cArray) && RARRAY_LEN(vsize) == 2 ){
			wxSize size;
			size.SetWidth(RB_NUM2INT(RARRAY_AREF(vsize,0)));
			size.SetHeight(RB_NUM2INT(RARRAY_AREF(vsize,1)));
			return size;
	}else if(rb_obj_is_kind_of(vsize, rb_cHash)){
		wxSize size;
		size.SetWidth(RB_NUM2INT(rb_hash_aref(vsize,RB_ID2SYM(rwxID_width))));
		size.SetHeight(RB_NUM2INT(rb_hash_aref(vsize,RB_ID2SYM(rwxID_height))));
		return size;
	}else if(rb_obj_is_kind_of(vsize, rb_cWXRect)){
		return unwrapTypedPtr<wxRect>(vsize, rb_cWXRect)->GetSize();
	}else if(!rb_obj_is_kind_of(vsize, rb_cWXSize) &&
		rb_respond_to(vsize,rwxID_width) &&
		rb_respond_to(vsize,rwxID_height)){
		wxSize size;
		size.SetWidth(RB_NUM2INT(rb_funcall(vsize,rwxID_width,0)));
		size.SetHeight(RB_NUM2INT(rb_funcall(vsize,rwxID_height,0)));
		return size;
	}else{
		return *unwrap<wxSize*>(vsize);
	}
}
Esempio n. 5
0
static void
iter_nstruct_to_a(na_loop_t *const lp)
{
    long    i, len;
    VALUE   opt, types, defs, def;
    VALUE   elmt, velm, vary;
    size_t  ofs, pos;
    narray_view_t *ne;

    opt = lp->option;
    types = RARRAY_AREF(opt,0);
    defs = RARRAY_AREF(opt,1);
    pos = lp->args[0].iter[0].pos;

    len = RARRAY_LEN(types);
    vary = rb_ary_new2(len);

    for (i=0; i<len; i++) {
        def  = RARRAY_AREF(defs,i);
        ofs  = NUM2SIZET(RARRAY_AREF(def,2));
        //ofs  = NUM2SIZET(RARRAY_AREF(ofsts,i));
        elmt = RARRAY_AREF(types,i);
        GetNArrayView(elmt,ne);
        ne->offset = pos + ofs;
        if (ne->base.ndim==0) {
            velm = rb_funcall(elmt,rb_intern("extract"),0);
        } else {
            velm = rb_funcall(elmt,rb_intern("to_a"),0);
        }
        rb_ary_push(vary, velm);
    }
    rb_ary_push(lp->args[1].value, vary);
}
Esempio n. 6
0
sf::Color unwrap< sf::Color >(const VALUE &vcolor)
{
	if(!rb_obj_is_kind_of(vcolor, rb_cSFMLColor) &&
		rb_respond_to(vcolor,rbSFML_IDred) &&
		rb_respond_to(vcolor,rbSFML_IDgreen) &&
		rb_respond_to(vcolor,rbSFML_IDblue) &&
		rb_respond_to(vcolor,rbSFML_IDalpha)){

		sf::Color color;
		set_value(color.r,vcolor,rbSFML_IDred);
		set_value(color.g,vcolor,rbSFML_IDgreen);
		set_value(color.b,vcolor,rbSFML_IDblue);
		set_value(color.a,vcolor,rbSFML_IDalpha);

		return color;
	}else if(rb_obj_is_kind_of(vcolor,rb_cArray)) {
			sf::Color color;

			color.r = NUM2INT(RARRAY_AREF(vcolor,0));
			color.g = NUM2INT(RARRAY_AREF(vcolor,1));
			color.b = NUM2INT(RARRAY_AREF(vcolor,2));

			if(RARRAY_LEN(vcolor) > 3) {
				VALUE val = RARRAY_AREF(vcolor,3);
				if(!NIL_P(val))
					color.a = NUM2INT(val);
			}

			return color;
	}else{
		return *unwrap<sf::Color*>(vcolor);
	}


}
Esempio n. 7
0
/*
 * call-seq:
 *   marshal_load(array) -> nil
 *
 * Provides marshalling support for use by the Marshal library.
 *
 *
 */
DLL_LOCAL VALUE _marshal_load(VALUE self, VALUE data)
{
    data = rb_Array(data);
    _setWidth(self, RARRAY_AREF(data,0));
    _setHeight(self, RARRAY_AREF(data,1));
    return Qnil;
}
Esempio n. 8
0
static VALUE
console_cursor_set(VALUE io, VALUE cpos)
{
    cpos = rb_convert_type(cpos, T_ARRAY, "Array", "to_ary");
    if (RARRAY_LEN(cpos) != 2) rb_raise(rb_eArgError, "expected 2D coordinate");
    return console_goto(io, RARRAY_AREF(cpos, 0), RARRAY_AREF(cpos, 1));
}
Esempio n. 9
0
static VALUE
lazy_zip_func(VALUE val, VALUE zip_args, int argc, VALUE *argv)
{
    VALUE yielder, ary, arg, v;
    long i;

    yielder = argv[0];
    arg = rb_attr_get(yielder, id_memo);
    if (NIL_P(arg)) {
	arg = rb_ary_new2(RARRAY_LEN(zip_args));
	for (i = 0; i < RARRAY_LEN(zip_args); i++) {
	    rb_ary_push(arg, rb_funcall(RARRAY_AREF(zip_args, i), id_to_enum, 0));
	}
	rb_ivar_set(yielder, id_memo, arg);
    }

    ary = rb_ary_new2(RARRAY_LEN(arg) + 1);
    rb_ary_push(ary, argv[1]);
    for (i = 0; i < RARRAY_LEN(arg); i++) {
	v = rb_rescue2(call_next, RARRAY_AREF(arg, i), next_stopped, 0,
		       rb_eStopIteration, (VALUE)0);
	rb_ary_push(ary, v);
    }
    rb_funcall(yielder, id_yield, 1, ary);
    return Qnil;
}
Esempio n. 10
0
/*
 * call-seq:
 *   marshal_load(array) -> nil
 *
 * Provides marshalling support for use by the Marshal library.
 *
 *
 */
VALUE _marshal_load(VALUE self, VALUE data)
{
    _set_r(self, RARRAY_AREF(data,0));
    _set_g(self, RARRAY_AREF(data,1));
    _set_b(self, RARRAY_AREF(data,2));
    _set_a(self, RARRAY_AREF(data,3));
    return Qnil;
}
Esempio n. 11
0
VALUE
nst_check_compatibility(VALUE nst, VALUE ary)
{
    VALUE defs, def, type, item;
    long len, i;
    narray_t *nt;

    if (TYPE(ary) != T_ARRAY) {
        if (nst==CLASS_OF(ary)) { // same Struct
            return Qtrue;
        }
        return Qfalse;
    }
    defs = nst_definitions(nst);
    len = RARRAY_LEN(defs);

    if (len != RARRAY_LEN(ary)) {
        //puts("pass2");
        return Qfalse;
    }
    for (i=0; i<len; i++) {
        def  = RARRAY_AREF(defs,i);
        type = RARRAY_AREF(def,1);
        GetNArray(type,nt);
        item = RARRAY_AREF(ary,i);
        if (nt->ndim == 0) {
            if (check_array(item)) {
                //puts("pass3");
                return Qfalse;
            }
        } else if (nt->ndim == 1) {
            if (!check_array_1d(item, nt->size)) {
                //puts("pass4");
                return Qfalse;
            }
        } else {
            // multi-dimension member
            volatile VALUE vnc;
            na_compose_t *nc;
            int j;

            //rb_p(item);
            vnc = na_ary_composition(item);
            //puts("pass2");
            Data_Get_Struct(vnc, na_compose_t, nc);
            if (nt->ndim != nc->ndim) {
                return Qfalse;
            }
            for (j=0; j<nc->ndim; j++) {
                if (nc->shape[j] != nt->shape[j]) {
                    return Qfalse;
                }
            }
            return Qtrue;
        }
    }
    return Qtrue;
}
Esempio n. 12
0
/*
 * call-seq:
 *   marshal_load(array) -> nil
 *
 * Provides marshalling support for use by the Marshal library.
 *
 *
 */
VALUE _marshal_load( VALUE self, VALUE data )
{
    _set_left(self, RARRAY_AREF(data,0));
    _set_top(self, RARRAY_AREF(data,1));
    _set_width(self, RARRAY_AREF(data,2));
    _set_height(self, RARRAY_AREF(data,3));

    return Qnil;
}
Esempio n. 13
0
/* :nodoc: */
static VALUE
nucomp_marshal_load(VALUE self, VALUE a)
{
    Check_Type(a, T_ARRAY);
    if (RARRAY_LEN(a) != 2)
	rb_raise(rb_eArgError, "marshaled complex must have an array whose length is 2 but %ld", RARRAY_LEN(a));
    rb_ivar_set(self, id_i_real, RARRAY_AREF(a, 0));
    rb_ivar_set(self, id_i_imag, RARRAY_AREF(a, 1));
    return self;
}
Esempio n. 14
0
OCIInterval *oci8_set_ociinterval_ym(OCIInterval *intvl, VALUE val)
{
    sb4 year;
    sb4 month;

    Check_Type(val, T_ARRAY);
    if (RARRAY_LEN(val) != 2) {
        rb_raise(rb_eRuntimeError, "invalid array size %ld", RARRAY_LEN(val));
    }
    year = NUM2INT(RARRAY_AREF(val, 0));
    month = NUM2INT(RARRAY_AREF(val, 1));
    chkerr(OCIIntervalSetYearMonth(oci8_envhp, oci8_errhp,
                                   year, month, intvl));
    return intvl;
}
Esempio n. 15
0
VALUE
nst_field_view(VALUE self, VALUE idx)
{
    VALUE def, type, ofs;

    def = nst_definition(self, idx);
    if (!RTEST(def)) {
        idx = rb_funcall(idx, rb_intern("to_s"), 0);
        rb_raise(rb_eTypeError, "Invalid field: '%s' for struct %s",
                 StringValuePtr(idx), rb_class2name(CLASS_OF(self)));
    }
    type = RARRAY_AREF(def,1);
    ofs  = RARRAY_AREF(def,2);
    return na_make_view_struct(self, type, ofs);
}
Esempio n. 16
0
/* Construct expanded load path and store it to cache.
   We rebuild load path partially if the cache is invalid.
   We don't cache non string object and expand it every time. We ensure that
   string objects in $LOAD_PATH are frozen.
 */
static void
rb_construct_expanded_load_path(int type, int *has_relative, int *has_non_cache)
{
    rb_vm_t *vm = GET_VM();
    VALUE load_path = vm->load_path;
    VALUE expanded_load_path = vm->expanded_load_path;
    VALUE ary;
    long i;
    int level = rb_safe_level();

    ary = rb_ary_tmp_new(RARRAY_LEN(load_path));
    for (i = 0; i < RARRAY_LEN(load_path); ++i) {
	VALUE path, as_str, expanded_path;
	int is_string, non_cache;
	char *as_cstr;
	as_str = path = RARRAY_AREF(load_path, i);
	is_string = RB_TYPE_P(path, T_STRING) ? 1 : 0;
	non_cache = !is_string ? 1 : 0;
	as_str = rb_get_path_check_to_string(path, level);
	as_cstr = RSTRING_PTR(as_str);

	if (!non_cache) {
	    if ((type == EXPAND_RELATIVE &&
		    rb_is_absolute_path(as_cstr)) ||
		(type == EXPAND_HOME &&
		    (!as_cstr[0] || as_cstr[0] != '~')) ||
		(type == EXPAND_NON_CACHE)) {
		    /* Use cached expanded path. */
		    rb_ary_push(ary, RARRAY_AREF(expanded_load_path, i));
		    continue;
	    }
	}
	if (!*has_relative && !rb_is_absolute_path(as_cstr))
	    *has_relative = 1;
	if (!*has_non_cache && non_cache)
	    *has_non_cache = 1;
	/* Freeze only string object. We expand other objects every time. */
	if (is_string)
	    rb_str_freeze(path);
	as_str = rb_get_path_check_convert(path, as_str, level);
	expanded_path = rb_file_expand_path_fast(as_str, Qnil);
	rb_str_freeze(expanded_path);
	rb_ary_push(ary, rb_fstring(expanded_path));
    }
    rb_obj_freeze(ary);
    vm->expanded_load_path = ary;
    rb_ary_replace(vm->load_path_snapshot, vm->load_path);
}
Esempio n. 17
0
File: index.c Progetto: kou/narray
static void
na_parse_enumerator(VALUE enum_obj, int orig_dim, ssize_t size, na_index_arg_t *q)
{
    int len;
    ssize_t step;
    struct enumerator *e;

    if (!RB_TYPE_P(enum_obj, T_DATA)) {
        rb_raise(rb_eTypeError,"wrong argument type (not T_DATA)");
    }
    e = (struct enumerator *)DATA_PTR(enum_obj);

    if (rb_obj_is_kind_of(e->obj, rb_cRange)) {
        if (e->meth == id_each) {
            na_parse_range(e->obj, 1, orig_dim, size, q);
        }
        else if (e->meth == id_step) {
            if (TYPE(e->args) != T_ARRAY) {
                rb_raise(rb_eArgError,"no argument for step");
            }
            len = RARRAY_LEN(e->args);
            if (len != 1) {
                rb_raise(rb_eArgError,"invalid number of step argument (1 for %d)",len);
            }
            step = NUM2SSIZET(RARRAY_AREF(e->args,0));
            na_parse_range(e->obj, step, orig_dim, size, q);
        } else {
            rb_raise(rb_eTypeError,"unknown Range method: %s",rb_id2name(e->meth));
        }
    } else {
        rb_raise(rb_eTypeError,"not Range object");
    }
}
Esempio n. 18
0
static size_t
check_array_1d(VALUE item, size_t size) {
    narray_t *na;
    size_t i, len;

    if (TYPE(item) == T_ARRAY) {
        len = RARRAY_LEN(item);
        if (size != len) {
            return 0;
        }
        for (i=0; i<len; i++) {
            if (!check_array(RARRAY_AREF(item,i))) {
                return 0;
            }
        }
        return 1;
    }
    if (RTEST(rb_obj_is_kind_of(item, cNArray))) {
        GetNArray(item,na);
        if (na->ndim == 1 && na->size == size) {
            return 1;
        } else {
            return 0;
        }
    }
    return 0;
}
Esempio n. 19
0
/*
  Foo = Numo::Struct.new {
    int8     :byte
    float64  :float, [2,2]
    dcomplex :compl
  }
 */
static VALUE
nst_s_new(int argc, VALUE *argv, VALUE klass)
{
    VALUE name=Qnil, rest, size;
    VALUE st, members;
    ID id;

    rb_scan_args(argc, argv, "0*", &rest);
    if (RARRAY_LEN(rest)>0) {
        name = RARRAY_AREF(rest,0);
        if (!NIL_P(name)) {
            VALUE tmp = rb_check_string_type(name);
            if (!NIL_P(tmp)) {
                rb_ary_shift(rest);
            } else {
                name = Qnil;
            }
        }
    }

    if (NIL_P(name)) {
        st = rb_define_class_id(name, klass);
        rb_funcall(klass, rb_intern("inherited"), 1, st);
    }
    else {
        char *cname = StringValuePtr(name);
        id = rb_intern(cname);
        if (!rb_is_const_id(id)) {
            rb_name_error(id, "identifier %s needs to be constant", cname);
        }
        if (rb_const_defined_at(klass, id)) {
            rb_warn("redefining constant Struct::%s", cname);
            rb_mod_remove_const(klass, ID2SYM(id));
        }
        st = rb_define_class_under(klass, rb_id2name(id), klass);
    }

    rb_iv_set(st, "__members__", rb_ary_new());
    rb_iv_set(st, "__offset__", INT2FIX(0));

    if (rb_block_given_p()) {
        rb_mod_module_eval(0, 0, st);
    }

    size = rb_iv_get(st, "__offset__");
    members = rb_iv_get(st, "__members__");
    //printf("size=%d\n",NUM2INT(size));
    rb_define_const(st, CONTIGUOUS_STRIDE, size);
    rb_define_const(st, ELEMENT_BYTE_SIZE, size);
    rb_define_const(st, ELEMENT_BIT_SIZE,  rb_funcall(size,'*',1,INT2FIX(8)));

    OBJ_FREEZE(members);
    rb_define_const(st, "DEFINITIONS", members);

    rb_define_singleton_method(st, "new", rb_class_new_instance, -1);
    //rb_define_singleton_method(st, "[]", rb_class_new_instance, -1);
    rb_define_method(st, "allocate", nst_allocate, 0);

    return st;
}
Esempio n. 20
0
VALUE
rb_HTTPRequest_response(VALUE self, VALUE argv){
    
    VALUE evhttp_p = rb_funcall(self, rb_intern("magic_request_id"), 0);
    
    VALUE r_code = rb_funcall(argv, rb_intern("code"), 0);
    VALUE r_data = rb_funcall(argv, rb_intern("data"), 0);
    VALUE r_header = rb_funcall(argv, rb_intern("headers"), 0);
    
    Check_Type(r_code, T_FIXNUM);
    Check_Type(r_data, T_STRING);
    Check_Type(r_header, T_ARRAY);
    
    long i = 0;
    struct evbuffer *buf;
    struct evhttp_request* req = (struct evhttp_request*)NUM2ULONG(evhttp_p);
    
    buf = evbuffer_new();
    evbuffer_add_printf(buf, "%s", StringValueCStr(r_data));
    
    for(i = 0 ; i < RARRAY_LEN(r_header) ; i++){
        VALUE header = RARRAY_AREF(r_header, i);
        VALUE key = rb_funcall(header, rb_intern("key"), 0);
        VALUE value = rb_funcall(header, rb_intern("value"), 0);
        evhttp_add_header(req->output_headers, StringValueCStr(key), StringValueCStr(value));
    }
    
    evhttp_send_reply(req, FIX2INT(r_code), "OK", buf);
    evbuffer_free(buf);
    
    return self;

}
Esempio n. 21
0
static void
print_backtrace(const VALUE eclass, const VALUE errat, int reverse)
{
    if (!NIL_P(errat)) {
	long i;
	long len = RARRAY_LEN(errat);
        int skip = eclass == rb_eSysStackError;

#define TRACE_MAX (TRACE_HEAD+TRACE_TAIL+5)
#define TRACE_HEAD 8
#define TRACE_TAIL 5

	for (i = 1; i < len; i++) {
	    VALUE line = RARRAY_AREF(errat, reverse ? len - i : i);
	    if (RB_TYPE_P(line, T_STRING)) {
		warn_print_str(rb_sprintf("\tfrom %"PRIsVALUE"\n", line));
	    }
	    if (skip && i == TRACE_HEAD && len > TRACE_MAX) {
		warn_print_str(rb_sprintf("\t ... %ld levels...\n",
					  len - TRACE_HEAD - TRACE_TAIL));
		i = len - TRACE_TAIL;
	    }
	}
    }
}
Esempio n. 22
0
File: main.cpp Progetto: Hanmac/rwx
int unwrapenum(const VALUE &arg, const std::string& name)
{
	enumregistertype::iterator it = enumregister.find(name);
	if(it != enumregister.end())
	{
		enumtype* etype = it->second;
		if(NIL_P(arg))
			return etype->defaults;
		else if(RB_SYMBOL_P(arg))
		{
			ID id(RB_SYM2ID(arg));

			for(enumtype::value_type::iterator it2 = etype->values.begin();
					it2 != etype->values.end();
					++it2)
			{
				if(it2->second == id)
					return it2->first;
			}
			rb_raise(rb_eTypeError,"%s is not a %s-Enum.",rb_id2name(id),etype->name.c_str());
		}else if(rb_obj_is_kind_of(arg,rb_cArray) && etype->allow_array )
		{
			int result = 0;
			size_t count = RARRAY_LEN(arg);
			for(size_t i = 0; i < count; ++i)
				result = result | unwrapenum(RARRAY_AREF(arg,i),name);
		}else
			return RB_NUM2INT(arg);
	}
	return 0;
}
Esempio n. 23
0
/*
 *  call-seq:
 *     Numo::DataType.new(shape)             => narray
 *     Numo::DataType.new(size1, size2, ...) => narray
 *
 *  Constructs a narray using the given <i>DataType</i> and <i>shape</i> or
 *  <i>sizes</i>.
 */
static VALUE
na_initialize(VALUE self, VALUE args)
{
    VALUE v;
    size_t *shape=NULL;
    int ndim;

    if (RARRAY_LEN(args) == 1) {
        v = RARRAY_AREF(args,0);
        if (TYPE(v) != T_ARRAY) {
            v = args;
        }
    } else {
        v = args;
    }
        ndim = RARRAY_LEN(v);
        if (ndim > NA_MAX_DIMENSION) {
            rb_raise(rb_eArgError,"ndim=%d exceeds maximum dimension",ndim);
        }
        shape = ALLOCA_N(size_t, ndim);
        // setup size_t shape[] from VALUE shape argument
        na_array_to_internal_shape(self, v, shape);
    na_setup(self, ndim, shape);

    return self;
}
Esempio n. 24
0
void
na_array_to_internal_shape(VALUE self, VALUE ary, size_t *shape)
{
    size_t    i, n, c, s;
    VALUE     v;
    narray_t *na;
    int       flag = 0;

    n = RARRAY_LEN(ary);

    if (RTEST(self)) {
        GetNArray(self, na);
        flag = TEST_COLUMN_MAJOR(na);
    }
    if (flag) {
        c = n-1;
        s = -1;
    } else {
        c = 0;
        s = 1;
    }
    for (i=0; i<n; i++) {
        v = RARRAY_AREF(ary,i);
        if (!FIXNUM_P(v) && !rb_obj_is_kind_of(v, rb_cInteger)) {
            rb_raise(rb_eTypeError, "array size must be Integer");
        }
        if (RTEST(rb_funcall(v, rb_intern("<"), 1, INT2FIX(0)))) {
            rb_raise(rb_eArgError,"size must be non-negative");
        }
        shape[c] = NUM2SIZE(v);
        c += s;
    }
}
Esempio n. 25
0
static VALUE application_initialize(VALUE self, VALUE args) {
    if (rb_thread_main() != rb_thread_current()) {
        rb_raise(rb_eThreadError, "Initializing QML::Application outside the main thread");
    }

    application_t *data;
    TypedData_Get_Struct(self, application_t, &data_type, data);

    if (rb_type(args) != T_ARRAY) {
        rb_raise(rb_eTypeError, "Expected Array");
    }

    args = rb_ary_concat(rb_ary_new_from_args(1, rb_argv0), args);

    int argc = RARRAY_LEN(args);
    char **argv = malloc(argc * sizeof(char *));

    for (int i = 0; i < argc; ++i) {
        VALUE arg = RARRAY_AREF(args, i);
        argv[i] = rb_string_value_cstr(&arg);
    }

    data->application = qmlbind_application_new(argc, argv);

    return self;
}
Esempio n. 26
0
static VALUE
initialize(int argc, VALUE argv[], VALUE self)
{
    ffi_cif * cif;
    ffi_type **arg_types, *rtype;
    ffi_status result;
    VALUE ptr, args, ret_type, abi, kwds, ary;
    int i, len;
    int nabi;
    void *cfunc;

    rb_scan_args(argc, argv, "31:", &ptr, &args, &ret_type, &abi, &kwds);
    ptr = rb_Integer(ptr);
    cfunc = NUM2PTR(ptr);
    PTR2NUM(cfunc);
    nabi = NIL_P(abi) ? FFI_DEFAULT_ABI : NUM2INT(abi);
    abi = INT2FIX(nabi);
    i = NUM2INT(ret_type);
    rtype = INT2FFI_TYPE(i);
    ret_type = INT2FIX(i);

    Check_Type(args, T_ARRAY);
    len = RARRAY_LENINT(args);
    Check_Max_Args("args", len);
    ary = rb_ary_subseq(args, 0, len);
    for (i = 0; i < RARRAY_LEN(args); i++) {
	VALUE a = RARRAY_PTR(args)[i];
	int type = NUM2INT(a);
	(void)INT2FFI_TYPE(type); /* raise */
	if (INT2FIX(type) != a) rb_ary_store(ary, i, INT2FIX(type));
    }
    OBJ_FREEZE(ary);

    rb_iv_set(self, "@ptr", ptr);
    rb_iv_set(self, "@args", args);
    rb_iv_set(self, "@return_type", ret_type);
    rb_iv_set(self, "@abi", abi);

    if (!NIL_P(kwds)) rb_hash_foreach(kwds, parse_keyword_arg_i, self);

    TypedData_Get_Struct(self, ffi_cif, &function_data_type, cif);

    arg_types = xcalloc(len + 1, sizeof(ffi_type *));

    for (i = 0; i < RARRAY_LEN(args); i++) {
	int type = NUM2INT(RARRAY_AREF(args, i));
	arg_types[i] = INT2FFI_TYPE(type);
    }
    arg_types[len] = NULL;

    result = ffi_prep_cif(cif, nabi, len, rtype, arg_types);

    if (result)
	rb_raise(rb_eRuntimeError, "error creating CIF %d", result);

    return self;
}
Esempio n. 27
0
static VALUE
lazy_take_size(VALUE generator, VALUE args, VALUE lazy)
{
    VALUE receiver = lazy_size(lazy);
    long len = NUM2LONG(RARRAY_AREF(rb_ivar_get(lazy, id_arguments), 0));
    if (NIL_P(receiver) || (FIXNUM_P(receiver) && FIX2LONG(receiver) < len))
	return receiver;
    return LONG2NUM(len);
}
Esempio n. 28
0
static void
print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg)
{
    const char *einfo = "";
    long elen = 0;
    VALUE mesg;

    if (emesg != Qundef) {
	if (NIL_P(errat) || RARRAY_LEN(errat) == 0 ||
	    NIL_P(mesg = RARRAY_AREF(errat, 0))) {
	    error_pos();
	}
	else {
	    warn_print_str(mesg);
	    warn_print(": ");
	}

	if (!NIL_P(emesg)) {
	    einfo = RSTRING_PTR(emesg);
	    elen = RSTRING_LEN(emesg);
	}
    }

    if (eclass == rb_eRuntimeError && elen == 0) {
	warn_print("unhandled exception\n");
    }
    else {
	VALUE epath;

	epath = rb_class_name(eclass);
	if (elen == 0) {
	    warn_print_str(epath);
	    warn_print("\n");
	}
	else {
	    const char *tail = 0;
	    long len = elen;

	    if (RSTRING_PTR(epath)[0] == '#')
		epath = 0;
	    if ((tail = memchr(einfo, '\n', elen)) != 0) {
		len = tail - einfo;
		tail++;		/* skip newline */
	    }
	    warn_print_str(tail ? rb_str_subseq(emesg, 0, len) : emesg);
	    if (epath) {
		warn_print(" (");
		warn_print_str(epath);
		warn_print(")\n");
	    }
	    if (tail) {
		warn_print_str(rb_str_subseq(emesg, tail - einfo, elen - len - 1));
	    }
	    if (tail ? einfo[elen-1] != '\n' : !epath) warn_print2("\n", 1);
	}
    }
}
Esempio n. 29
0
DLL_LOCAL VALUE _setCustomColors(VALUE self,VALUE val)
{
	rb_check_frozen(self);
	VALUE dp = rb_Array(val);
	wxColourData &data = _self->GetColourData();
	for(std::size_t i = 0; i < wxColourData::NUM_CUSTOM && i < (std::size_t)RARRAY_LEN(dp); ++i)
		data.SetCustomColour(i,unwrap<wxColor>(RARRAY_AREF(dp,i)));

	return val;
}
Esempio n. 30
0
/*
 * call-seq:
 *   marshal_load(array) -> nil
 *
 * Provides marshalling support for use by the Marshal library.
 *
 *
 */
DLL_LOCAL VALUE _marshal_load(VALUE self,VALUE data)
{
	std::size_t count = RARRAY_LEN(data) / 3;

	unsigned char red[count];
	unsigned char green[count];
	unsigned char blue[count];

	for(std::size_t i = 0; i < count; ++i)
	{
		red[i]   = NUM2UINT(RARRAY_AREF(data,i*3));
		green[i] = NUM2UINT(RARRAY_AREF(data,i*3+1));
		blue[i]  = NUM2UINT(RARRAY_AREF(data,i*3+2));
	}

	_self->Create(count,red,green,blue);

	return self;
}