VALUE counts_insert(int argc, VALUE *argv, VALUE self) { VALUE rb_index, rb_value; rb_scan_args(argc, argv, "02", &rb_index, &rb_value); switch (TYPE(rb_index)) { case T_FIXNUM: break; default: rb_raise(rb_eTypeError, "index is not a fixnum"); break; } long index = rb_fix2int(rb_index); switch (TYPE(rb_value)) { case T_FIXNUM: break; default: rb_raise(rb_eTypeError, "value is not a fixnum"); break; } long value = rb_fix2int(rb_value); Counts *counts; Data_Get_Struct(self, Counts, counts); if (value < 0) { rb_raise(rb_eArgError, "negative value"); } if (index < 0) { rb_raise(rb_eArgError, "negative index value"); } else if (index >= counts->max_size) { rb_raise(rb_eArgError, "index value out of bounds"); } else if (index == counts->size) { counts->values[index] = value; counts->size++; } else if (index < counts->size) { for (long i = counts->size - 1; i >= index; i--) { counts->values[i+1] = counts->values[i]; } counts->values[index] = value; counts->size++; } else { rb_raise(rb_eArgError, "out of order insert"); } return Qnil; }
/* * convert a ruby hash to TDigipassBlob structure */ static void rbhash_to_digipass(VALUE data, TDigipassBlob* dpdata) { memset(dpdata, 0, sizeof(dpdata)); VALUE blob = rb_hash_aref(data, rb_str_new2("blob")); VALUE serial = rb_hash_aref(data, rb_str_new2("serial")); VALUE app_name = rb_hash_aref(data, rb_str_new2("app_name")); VALUE flag1 = rb_hash_aref(data, rb_str_new2("flags1")); VALUE flag2 = rb_hash_aref(data, rb_str_new2("flags2")); strcpy(dpdata->Blob, rb_string_value_cstr(&blob)); strncpy(dpdata->Serial, rb_string_value_cstr(&serial), sizeof(dpdata->Serial)); strncpy(dpdata->AppName, rb_string_value_cstr(&app_name), sizeof(dpdata->AppName)); dpdata->DPFlags[0] = rb_fix2int(flag1); dpdata->DPFlags[1] = rb_fix2int(flag2); }
VALUE counts_delete_at_m(int argc, VALUE *argv, VALUE self) { VALUE rb_index; rb_scan_args(argc, argv, "01", &rb_index); switch (TYPE(rb_index)) { case T_FIXNUM: break; default: rb_raise(rb_eTypeError, "not valid value"); break; } long index = rb_fix2int(rb_index); Counts *counts; Data_Get_Struct(self, Counts, counts); if (index < counts->size) { double current_value = counts->values[index]; for (long i = index; i < counts->size - 1; i++) { counts->values[i] = counts->values[i+1]; } counts->size--; return rb_long2num_inline(current_value); } rb_raise(rb_eArgError, "index out of range"); }
static VALUE data_initialize(int argc, VALUE* argv, VALUE self) { Data *data; Data_Get_Struct(self, Data, data); VALUE rb_max_size; rb_scan_args(argc, argv, "10", &rb_max_size); long max_size = rb_fix2int(rb_max_size); Page **pages = malloc(sizeof(Page*)*max_size); if (pages == NULL) { rb_raise(rb_eNoMemError, "allocation of pages failed"); } VALUE *rb_pages = (VALUE*)malloc(sizeof(VALUE)*max_size); if (rb_pages == NULL) { rb_raise(rb_eNoMemError, "allocation of rb_pages failed"); } data->size = 0; data->max_size = max_size; data->pages = pages; data->rb_pages = rb_pages; return self; }
VALUE data_delete_at_m(int argc, VALUE *argv, VALUE self) { VALUE rb_index; rb_scan_args(argc, argv, "01", &rb_index); switch (TYPE(rb_index)) { case T_FIXNUM: break; default: rb_raise(rb_eTypeError, "not valid value"); break; } long index = rb_fix2int(rb_index); Data *data; Data_Get_Struct(self, Data, data); if (index < data->size) { VALUE current_value = data->rb_pages[index]; for (long i = index; i < data->size - 1; i++) { data->rb_pages[i] = data->rb_pages[i+1]; } data->size--; return current_value; } rb_raise(rb_eArgError, "index out of range"); }
/* * set kernel parameters */ static void vacman_set_kernal_param(VALUE module, VALUE paramname, VALUE rbval) { char* name = rb_string_value_cstr(¶mname); int val = rb_fix2int(rbval); if (strcmp(name, "itimewindow") == 0) return KernelParms.ITimeWindow = val; else { char buffer[256]; sprintf(buffer, "invalid kernal param %s", name); rb_raise(e_vacmanerror, buffer); return; } }
VALUE data_insert(int argc, VALUE *argv, VALUE self) { VALUE rb_index, rb_page; rb_scan_args(argc, argv, "02", &rb_index, &rb_page); switch (TYPE(rb_index)) { case T_FIXNUM: break; default: rb_raise(rb_eTypeError, "index is not a fixnum"); break; } long index = rb_fix2int(rb_index); Data *data; Data_Get_Struct(self, Data, data); Page *page; Data_Get_Struct(rb_page, Page, page); if (index < 0) { rb_raise(rb_eArgError, "negative index value"); } else if (index >= data->max_size) { rb_raise(rb_eArgError, "index value out of bounds"); } else if (index == data->size) { data->rb_pages[index] = rb_page; data->pages[index] = page; data->size++; } else if (index < data->size) { for (long i = data->size - 1; i >= index; i--) { data->rb_pages[i+1] = data->rb_pages[i]; data->pages[i+1] = data->pages[i]; } data->rb_pages[index] = rb_page; data->pages[index] = page; data->size++; } else { rb_raise(rb_eArgError, "out of order insert"); } return Qnil; }
static int rf_mintval(const char *path,ID method,char *methname,int def) { VALUE arg = rf_mcall(path,method,methname,Qnil); VALUE retval; int error; if (FIXNUM_P(arg)) { return rb_fix2int(arg); } else if (RTEST(arg)) { if (!rb_respond_to(arg,id_to_i)) { return def; } retval = rb_protect(rf_int_protected, arg, &error); /* Did it error? */ if (error) return def; return rb_num2long(retval); } else { return def; } }
VALUE counts_aref(int argc, VALUE *argv, VALUE self) { VALUE rb_index; rb_scan_args(argc, argv, "01", &rb_index); switch (TYPE(rb_index)) { case T_FIXNUM: break; default: rb_raise(rb_eTypeError, "not valid value"); break; } Counts *counts; Data_Get_Struct(self, Counts, counts); long index = rb_fix2int(rb_index); if (index < counts->size) { return rb_long2num_inline(counts->values[index]); } return Qnil; }
VALUE data_aref(int argc, VALUE *argv, VALUE self) { VALUE rb_index; rb_scan_args(argc, argv, "01", &rb_index); switch (TYPE(rb_index)) { case T_FIXNUM: break; default: rb_raise(rb_eTypeError, "not valid value"); break; } long index = rb_fix2int(rb_index); Data *data; Data_Get_Struct(self, Data, data); if (index < data->size) { return data->rb_pages[index]; } return Qnil; }
static VALUE counts_initialize(int argc, VALUE* argv, VALUE self) { Counts *counts; Data_Get_Struct(self, Counts, counts); VALUE rb_page_size; rb_scan_args(argc, argv, "01", &rb_page_size); if (NIL_P(rb_page_size)) rb_page_size = rb_int2inum(DEFAULT_PAGE_SIZE); long page_size = rb_fix2int(rb_page_size); long *values = (long*)malloc(sizeof(long)*page_size); if (values == NULL) { rb_raise(rb_eNoMemError, "allocation of values failed"); } counts->max_size = page_size; counts->size = 0; counts->values = values; return self; }
zval *value_to_zval(VALUE v) { zval *zv; MAKE_STD_ZVAL(zv); switch (TYPE(v)) { case T_FALSE: ZVAL_FALSE(zv); return zv; case T_TRUE: ZVAL_TRUE(zv); return zv; case T_UNDEF: case T_NIL: ZVAL_NULL(zv); return zv; case T_FIXNUM: ZVAL_LONG(zv, rb_fix2int(v)); return zv; case T_BIGNUM: ZVAL_LONG(zv, rb_big2long(v)); // FIXME: bignum over long return zv; case T_FLOAT: ZVAL_DOUBLE(zv, RFLOAT_VALUE(v)); return zv; case T_ARRAY: { int i; array_init(zv); for(i=0;i<RARRAY_LEN(v);++i) { zval *add = value_to_zval(RARRAY_PTR(v)[i]); zend_hash_next_index_insert(Z_ARRVAL_P(zv), &add, sizeof(zval *), NULL); } return zv; } case T_HASH: { array_init(zv); rb_hash_foreach(v, hash_to_zval, (VALUE)zv); return zv; } case T_SYMBOL: { VALUE symbol_str = rb_sym_to_s(v); ZVAL_STRINGL(zv, StringValuePtr(symbol_str), RSTRING_LEN(symbol_str), 1); return zv; } case T_STRING: { ZVAL_STRINGL(zv, StringValuePtr(v), RSTRING_LEN(v), 1); return zv; } default: { if (CLASS_OF(v) == cPhpEmbedValue) { php_value* pv; Data_Get_Struct(v, php_value, pv); MAKE_COPY_ZVAL(&pv->value, zv); return zv; } } FREE_ZVAL(zv); rb_raise(rb_eRuntimeError, "no implemented"); } }