本文整理汇总了C++中rb_ary_push函数的典型用法代码示例。如果您正苦于以下问题:C++ rb_ary_push函数的具体用法?C++ rb_ary_push怎么用?C++ rb_ary_push使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了rb_ary_push函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: func_init
/*
* call-seq:
*
* API::Function.new(address, prototype = 'V', return_type = 'L')
*
* Creates and returns an API::Function object. This object is similar to an
* API object, except that instead of a character function name you pass a
* function pointer address as the first argument, and there's no associated
* DLL file.
*
* Once you have your API::Function object you can then call it the same way
* you would an API object.
*
* Example:
*
* require 'win32/api'
* include Win32
*
* LoadLibrary = API.new('LoadLibrary', 'P', 'L')
* GetProcAddress = API.new('GetProcAddress', 'LP', 'L')
*
* # Play a system beep
* hlib = LoadLibrary.call('user32')
* addr = GetProcAddress.call(hlib, 'MessageBeep')
* func = Win32::API::Function.new(addr, 'L', 'L')
* func.call(0)
*/
static VALUE func_init(int argc, VALUE* argv, VALUE self){
Win32API* ptr;
int i;
VALUE v_address, v_proto, v_return;
rb_scan_args(argc, argv, "12", &v_address, &v_proto, &v_return);
Data_Get_Struct(self, Win32API, ptr);
// Convert a string prototype to an array of characters
if(rb_respond_to(v_proto, rb_intern("split")))
v_proto = rb_str_split(v_proto, "");
// Convert a nil or empty prototype to 'V' (void) automatically
if(NIL_P(v_proto) || RARRAY_LEN(v_proto) == 0){
v_proto = rb_ary_new();
rb_ary_push(v_proto, rb_str_new2("V"));
}
// Set an arbitrary limit of 20 parameters
if(20 < RARRAY_LEN(v_proto))
rb_raise(rb_eArgError, "too many parameters: %d\n", RARRAY_LEN(v_proto));
// Set the default return type to 'L' (DWORD)
if(NIL_P(v_return))
v_return = rb_str_new2("L");
ptr->function = (FARPROC)NUM2LONG(v_address);
// Push the numeric prototypes onto our int array for later use.
for(i = 0; i < RARRAY_LEN(v_proto); i++){
SafeStringValue(RARRAY_PTR(v_proto)[i]);
switch(*(char*)StringValuePtr(RARRAY_PTR(v_proto)[i])){
case 'L':
ptr->prototype[i] = _T_LONG;
break;
case 'P':
ptr->prototype[i] = _T_POINTER;
break;
case 'I': case 'B':
ptr->prototype[i] = _T_INTEGER;
break;
case 'V':
ptr->prototype[i] = _T_VOID;
break;
case 'K':
ptr->prototype[i] = _T_CALLBACK;
break;
case 'S':
ptr->prototype[i] = _T_STRING;
break;
default:
rb_raise(cAPIProtoError, "Illegal prototype '%s'",
StringValuePtr(RARRAY_PTR(v_proto)[i])
);
}
}
// Store the return type for later use.
// Automatically convert empty strings or nil to type void.
if(NIL_P(v_return) || RSTRING_LEN(v_return) == 0){
v_return = rb_str_new2("V");
ptr->return_type = _T_VOID;
}
else{
SafeStringValue(v_return);
switch(*RSTRING_PTR(v_return)){
case 'L':
ptr->return_type = _T_LONG;
break;
case 'P':
//.........这里部分代码省略.........
开发者ID:twobitfool,项目名称:win32-api,代码行数:101,代码来源:api.c
示例2: ruby_curl_multi_requests_callback
/* Hash#foreach callback for ruby_curl_multi_requests */
static int ruby_curl_multi_requests_callback(VALUE key, VALUE value, VALUE result_array) {
rb_ary_push(result_array, value);
return ST_CONTINUE;
}
开发者ID:FooBarWidget,项目名称:curb,代码行数:6,代码来源:curb_multi.c
示例3: oci8_lob_read
static VALUE oci8_lob_read(int argc, VALUE *argv, VALUE self)
{
oci8_lob_t *lob = DATA_PTR(self);
oci8_svcctx_t *svcctx = oci8_get_svcctx(lob->svc);
ub4 length;
ub4 nchar;
ub4 amt;
sword rv;
char buf[8192];
size_t buf_size_in_char;
VALUE size;
VALUE v = rb_ary_new();
rb_scan_args(argc, argv, "01", &size);
length = oci8_lob_get_length(lob);
if (length <= lob->pos) /* EOF */
return Qnil;
length -= lob->pos;
if (NIL_P(size)) {
nchar = length; /* read until EOF */
} else {
nchar = NUM2UINT(size);
if (nchar > length)
nchar = length;
}
amt = nchar;
buf_size_in_char = sizeof(buf) / lob->char_width;
do {
if (lob->state == S_BFILE_CLOSE) {
rv = OCILobFileOpen_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, OCI_FILE_READONLY);
if (rv == OCI_ERROR && oci8_get_error_code(oci8_errhp) == 22290) {
/* ORA-22290: operation would exceed the maximum number of opened files or LOBs */
/* close all opened BFILE implicitly. */
oci8_base_t *base;
for (base = &lob->base; base != &lob->base; base = base->next) {
if (base->type == OCI_DTYPE_LOB) {
oci8_lob_t *tmp = (oci8_lob_t *)base;
if (tmp->state == S_BFILE_OPEN) {
tmp->state = S_BFILE_CLOSE;
}
}
}
oci_lc(OCILobFileCloseAll_nb(svcctx, svcctx->base.hp.svc, oci8_errhp));
continue;
}
if (rv != OCI_SUCCESS)
oci8_raise(oci8_errhp, rv, NULL);
lob->state = S_BFILE_OPEN;
}
/* initialize buf in zeros everytime to check a nul characters. */
memset(buf, 0, sizeof(buf));
rv = OCILobRead_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, &amt, lob->pos + 1, buf, sizeof(buf), NULL, NULL, 0, lob->csfrm);
if (rv == OCI_ERROR && oci8_get_error_code(oci8_errhp) == 22289) {
/* ORA-22289: cannot perform FILEREAD operation on an unopened file or LOB */
if (lob->state == S_BFILE_CLOSE)
continue;
}
if (rv != OCI_SUCCESS && rv != OCI_NEED_DATA)
oci8_raise(oci8_errhp, rv, NULL);
/* Workaround when using Oracle 10.2.0.4 or 11.1.0.6 client and
* variable-length character set (e.g. AL32UTF8).
*
* When the above mentioned condition, amt may be shorter. So
* amt is increaded until a nul character to know the actually
* read size.
*/
while (amt < sizeof(buf) && buf[amt] != '\0') {
amt++;
}
if (amt == 0)
break;
/* for fixed size charset, amt is the number of characters stored in buf. */
if (amt > buf_size_in_char)
rb_raise(eOCIException, "Too large buffer fetched or you set too large size of a character.");
amt *= lob->char_width;
rb_ary_push(v, rb_str_new(buf, amt));
} while (rv == OCI_NEED_DATA);
lob->pos += nchar;
if (nchar == length) {
lob_close(lob);
bfile_close(lob);
}
if (RARRAY_LEN(v) == 0) {
return Qnil;
}
v = rb_ary_join(v, Qnil);
OBJ_TAINT(v);
if (lob->lobtype == OCI_TEMP_CLOB) {
/* set encoding */
rb_enc_associate(v, oci8_encoding);
return rb_str_conv_enc(v, oci8_encoding, rb_default_internal_encoding());
} else {
/* ASCII-8BIT */
return v;
}
}
开发者ID:aq1018,项目名称:ruby-oci8,代码行数:98,代码来源:lob.c
示例4: build_Array_each_node
static NODE *
build_Array_each_node(rb_iseq_t *iseq, NODE * node, NODE * lnode,
VALUE param_vars, VALUE local_vars)
{
/* Special block for Array#each
ary.each{|e|
BODY
}
=>
{|e, _self|
_i = 0
while _i < _self.length
e = _self[_i]
redo_point:
BODY
next_point:
_i = _i.succ
end
}
ary.each{
BODY
}
=>
{|_i, _self|
_i = 0
while _i < _self.length
redo_point:
BODY
next_point:
_i = _i.succ
end
}
*/
ID _self = rb_intern("#_self");
ID _i = rb_intern("#_i");
if (iseq->argc == 0) {
ID _e = rb_intern("#_e");
rb_ary_push(param_vars, ID2SYM(_e));
rb_ary_push(param_vars, ID2SYM(_self));
iseq->argc += 2;
rb_ary_push(local_vars, ID2SYM(_i));
node =
new_block(NEW_DASGN(_i, NEW_LIT(INT2FIX(0))),
NEW_WHILE(NEW_CALL(NEW_DVAR(_i), idLT,
new_ary(NEW_CALL
(NEW_DVAR(_self), idLength,
0), 0)),
new_block(NEW_OPTBLOCK(node),
NEW_DASGN(_i,
NEW_CALL(NEW_DVAR(_i),
idSucc, 0))),
Qundef));
}
else {
ID e = SYM2ID(rb_ary_entry(param_vars, 0));
NODE *assign;
rb_ary_push(param_vars, ID2SYM(_self));
iseq->argc++;
rb_ary_push(local_vars, ID2SYM(_i));
if (nd_type(lnode) == NODE_DASGN_CURR) {
assign = NEW_DASGN(e,
NEW_CALL(NEW_DVAR(_self), idAREF,
new_ary(NEW_DVAR(_i), 0)));
}
else {
assign = new_assign(lnode,
NEW_CALL(NEW_DVAR(_self), idAREF,
new_ary(NEW_DVAR(_i), 0)));
}
node =
new_block(NEW_DASGN(_i, NEW_LIT(INT2FIX(0))),
NEW_WHILE(NEW_CALL(NEW_DVAR(_i), idLT,
new_ary(NEW_CALL
(NEW_DVAR(_self), idLength,
0), 0)), new_block(assign,
new_block
(NEW_OPTBLOCK
(node),
NEW_DASGN
(_i,
NEW_CALL
(NEW_DVAR
(_i),
idSucc,
0)))),
Qundef));
}
return node;
}
开发者ID:RWB01,项目名称:Code-Translator,代码行数:96,代码来源:blockinlining.c
示例5: rb_provide_feature
static void
rb_provide_feature(VALUE feature)
{
rb_ary_push(get_loaded_features(), feature);
}
开发者ID:Sophrinix,项目名称:MacRuby,代码行数:5,代码来源:load.c
示例6: get_value
static VALUE get_value(const char* buffer, int* position, int type) {
VALUE value;
switch (type) {
case 1:
{
double d;
memcpy(&d, buffer + *position, 8);
value = rb_float_new(d);
*position += 8;
break;
}
case 2:
case 13:
{
*position += 4;
int value_length = strlen(buffer + *position);
value = rb_str_new(buffer+ *position, value_length);
*position += value_length + 1;
break;
}
case 3:
{
int size;
memcpy(&size, buffer + *position, 4);
if (strcmp(buffer + *position + 5, "$ref") == 0) { // DBRef
int offset = *position + 14;
VALUE argv[2];
int collection_length = strlen(buffer + offset);
argv[0] = rb_str_new(buffer + offset, collection_length);
offset += collection_length + 1;
char id_type = buffer[offset];
offset += 5;
argv[1] = get_value(buffer, &offset, (int)id_type);
value = rb_class_new_instance(2, argv, DBRef);
} else {
value = elements_to_hash(buffer + *position + 4, size - 5);
}
*position += size;
break;
}
case 4:
{
int size;
memcpy(&size, buffer + *position, 4);
int end = *position + size - 1;
*position += 4;
value = rb_ary_new();
while (*position < end) {
int type = (int)buffer[(*position)++];
int key_size = strlen(buffer + *position);
*position += key_size + 1; // just skip the key, they're in order.
VALUE to_append = get_value(buffer, position, type);
rb_ary_push(value, to_append);
}
(*position)++;
break;
}
case 5:
{
int length;
memcpy(&length, buffer + *position, 4);
int subtype = (unsigned char)buffer[*position + 4];
VALUE data;
if (subtype == 2) {
data = rb_str_new(buffer + *position + 9, length - 4);
} else {
data = rb_str_new(buffer + *position + 5, length);
}
VALUE st = INT2FIX(subtype);
VALUE argv[2] = {data, st};
value = rb_class_new_instance(2, argv, Binary);
*position += length + 5;
break;
}
case 6:
{
value = rb_class_new_instance(0, NULL, Undefined);
break;
}
case 7:
{
VALUE str = rb_str_new(buffer + *position, 12);
VALUE oid = rb_funcall(str, rb_intern("unpack"), 1, rb_str_new2("C*"));
value = rb_class_new_instance(1, &oid, ObjectID);
*position += 12;
break;
}
case 8:
{
value = buffer[(*position)++] ? Qtrue : Qfalse;
break;
}
case 9:
{
long long millis;
memcpy(&millis, buffer + *position, 8);
VALUE seconds = INT2NUM(millis / 1000);
VALUE microseconds = INT2NUM((millis % 1000) * 1000);
//.........这里部分代码省略.........
开发者ID:djsun,项目名称:mongo-ruby-driver,代码行数:101,代码来源:cbson.c
示例7: build_Integer_times_node
static NODE *
build_Integer_times_node(rb_iseq_t *iseq, NODE * node, NODE * lnode,
VALUE param_vars, VALUE local_vars)
{
/* Special Block for Integer#times
{|e, _self|
_e = e
while(e < _self)
e = _e
redo_point:
BODY
next_point:
_e = _e.succ
end
}
{|e, _self|
while(e < _self)
BODY
next_point:
e = e.succ
end
}
*/
ID _self = rb_intern("#_self");
if (iseq->argc == 0) {
ID e = rb_intern("#e");
rb_ary_push(param_vars, ID2SYM(e));
rb_ary_push(param_vars, ID2SYM(_self));
iseq->argc += 2;
node =
NEW_WHILE(NEW_CALL
(NEW_DVAR(e), idLT, new_ary(NEW_DVAR(_self), 0)),
new_block(NEW_OPTBLOCK(node),
NEW_DASGN(e,
NEW_CALL(NEW_DVAR(e), idSucc, 0))),
Qundef);
}
else {
ID _e = rb_intern("#_e");
ID e = SYM2ID(rb_ary_entry(param_vars, 0));
NODE *assign;
rb_ary_push(param_vars, ID2SYM(_self));
rb_ary_push(local_vars, ID2SYM(_e));
iseq->argc++;
if (nd_type(lnode) == NODE_DASGN_CURR) {
assign = NEW_DASGN(e, NEW_DVAR(_e));
}
else {
assign = new_assign(lnode, NEW_DVAR(_e));
}
node =
new_block(NEW_DASGN(_e, NEW_DVAR(e)),
NEW_WHILE(NEW_CALL
(NEW_DVAR(_e), idLT,
new_ary(NEW_DVAR(_self), 0)),
new_block(assign,
new_block(NEW_OPTBLOCK(node),
NEW_DASGN(_e,
NEW_CALL
(NEW_DVAR(_e),
idSucc, 0)))),
Qundef));
}
return node;
}
开发者ID:RWB01,项目名称:Code-Translator,代码行数:70,代码来源:blockinlining.c
示例8: rb_keychain_find
static VALUE rb_keychain_find(int argc, VALUE *argv, VALUE self){
VALUE kind;
VALUE attributes;
VALUE first_or_all;
rb_scan_args(argc, argv, "2:", &first_or_all, &kind, &attributes);
Check_Type(first_or_all, T_SYMBOL);
Check_Type(kind, T_STRING);
CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(query, kSecReturnAttributes, kCFBooleanTrue);
CFDictionarySetValue(query, kSecReturnRef, kCFBooleanTrue);
if(rb_to_id(first_or_all) == rb_intern("all")){
CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitAll);
}
rb_add_value_to_cf_dictionary(query, kSecClass, kind);
if(!NIL_P(attributes)){
Check_Type(attributes, T_HASH);
VALUE rb_keychains = rb_hash_aref(attributes, ID2SYM(rb_intern("keychains")));
if(!NIL_P(rb_keychains)){
Check_Type(rb_keychains, T_ARRAY);
CFMutableArrayRef searchArray = CFArrayCreateMutable(NULL, RARRAY_LEN(rb_keychains), &kCFTypeArrayCallBacks);
for(int index=0; index < RARRAY_LEN(rb_keychains); index++){
SecKeychainRef keychain = NULL;
Data_Get_Struct(RARRAY_PTR(rb_keychains)[index], struct OpaqueSecKeychainRef, keychain);
CFArrayAppendValue(searchArray, keychain);
}
CFDictionarySetValue(query, kSecMatchSearchList,searchArray);
CFRelease(searchArray);
}
VALUE limit = rb_hash_aref(attributes, ID2SYM(rb_intern("limit")));
if(!NIL_P(limit)){
Check_Type(limit, T_FIXNUM);
long c_limit = FIX2LONG(limit);
CFNumberRef cf_limit = CFNumberCreate(NULL, kCFNumberLongType, &c_limit);
CFDictionarySetValue(query, kSecMatchLimit, cf_limit);
CFRelease(cf_limit);
}
VALUE conditions = rb_hash_aref(attributes, ID2SYM(rb_intern("conditions")));
if(!NIL_P(conditions)){
Check_Type(conditions, T_HASH);
VALUE rQuery = Data_Wrap_Struct(rb_cPointerWrapper, NULL, NULL, query);
rb_block_call(conditions, rb_intern("each"), 0, NULL, RUBY_METHOD_FUNC(add_conditions_to_query), rQuery);
}
}
CFDictionaryRef result;
OSStatus status = SecItemCopyMatching(query, (CFTypeRef*)&result);
CFRelease(query);
VALUE rb_item = rb_ary_new2(0);
switch(status){
case errSecItemNotFound:
break;
default:
CheckOSStatusOrRaise(status);
if(CFArrayGetTypeID() == CFGetTypeID(result)){
CFArrayRef result_array = (CFArrayRef)result;
for(CFIndex i = 0; i < CFArrayGetCount(result_array); i++){
rb_ary_push(rb_item,rb_keychain_item_from_sec_dictionary(CFArrayGetValueAtIndex(result_array,i)));
}
}
else{
rb_ary_push(rb_item, rb_keychain_item_from_sec_dictionary(result));
}
CFRelease(result);
}
if(rb_to_id(first_or_all) == rb_intern("first")){
return rb_ary_entry(rb_item,0);
}
else{
return rb_item;
}
}
开发者ID:fcheung,项目名称:keychain_c,代码行数:87,代码来源:keychain.c
示例9: machine_marshal_dump
VALUE machine_marshal_dump( VALUE self ) {
VALUE ary = rb_ary_new();
VALUE dynamic, stack;
long i;
zmachine *zm;
Data_Get_Struct( self, zmachine, zm );
rb_ary_push( ary, rb_iv_get( self, "@program" ) );
rb_ary_push( ary, rb_iv_get( self, "@output" ) );
rb_ary_push( ary, rb_iv_get( self, "@keyboard" ) );
rb_ary_push( ary, rb_iv_get( self, "@rng" ) );
dynamic = rb_ary_new();
for( i = 0; i < zm->m->dynamic_length; i++ ) {
rb_ary_push( dynamic, UINT2NUM(zm->m->m_dynamic[i]) );
}
rb_ary_push( ary, dynamic );
stack = rb_ary_new();
for( i = 0; i < STACK_SIZE; i++ ) {
rb_ary_push( stack, UINT2NUM(zm->stack[i]) );
}
rb_ary_push( ary, stack );
rb_ary_push( ary, UINT2NUM(PC(zm)) );
rb_ary_push( ary, UINT2NUM(zm->sp - zm->stack) );
rb_ary_push( ary, UINT2NUM(zm->fp - zm->stack) );
rb_ary_push( ary, UINT2NUM(zm->frame_count) );
rb_ary_push( ary, UINT2NUM(zm->finished) );
return ary;
}
开发者ID:eki,项目名称:dorothy,代码行数:35,代码来源:machine.c
示例10: read_anything
static VALUE read_anything(VALUE protocol, field_metadata* fmd, protocol_method_table *pmt) {
VALUE result = Qnil;
if (fmd->type == TTYPE_BOOL) {
result = fastcall_call(pmt->read_bool, protocol, Qnil);
} else if (fmd->type == TTYPE_BYTE) {
result = fastcall_call(pmt->read_byte, protocol, Qnil);
} else if (fmd->type == TTYPE_I16) {
result = fastcall_call(pmt->read_i16, protocol, Qnil);
} else if (fmd->type == TTYPE_I32) {
result = fastcall_call(pmt->read_i32, protocol, Qnil);
} else if (fmd->type == TTYPE_I64) {
result = fastcall_call(pmt->read_i64, protocol, Qnil);
} else if (fmd->type == TTYPE_STRING) {
result = fastcall_call(pmt->read_string, protocol, Qnil);
} else if (fmd->type == TTYPE_DOUBLE) {
result = fastcall_call(pmt->read_double, protocol, Qnil);
} else if (fmd->type == TTYPE_STRUCT) {
result = rb_class_new_instance(0, NULL, fmd->klass_v);
if (rb_obj_is_kind_of(result, thrift_union_class)) {
union_read(result, protocol, pmt);
} else {
struct_read(result, protocol, pmt);
}
} else if (fmd->type == TTYPE_MAP) {
int i;
VALUE map_header = fastcall_call(pmt->read_map_begin, protocol, Qnil);
int key_ttype = FIX2INT(rb_ary_entry(map_header, 0));
int value_ttype = FIX2INT(rb_ary_entry(map_header, 1));
int num_entries = FIX2INT(rb_ary_entry(map_header, 2));
// Check the declared key and value types against the expected ones and skip the map contents
// if the types don't match.
field_metadata* key_md = fmd->key;
field_metadata* value_md = fmd->value;
if (key_md && value_md) {
int specified_key_type = key_md->type;
int specified_value_type = value_md->type;
if (num_entries == 0 || (specified_key_type == key_ttype && specified_value_type == value_ttype)) {
result = rb_hash_new();
for (i = 0; i < num_entries; ++i) {
VALUE key, val;
key = read_anything(protocol, key_md, pmt);
val = read_anything(protocol, value_md, pmt);
rb_hash_aset(result, key, val);
}
} else {
skip_map_contents(protocol, INT2FIX(key_ttype), INT2FIX(value_ttype), num_entries);
}
} else {
skip_map_contents(protocol, INT2FIX(key_ttype), INT2FIX(value_ttype), num_entries);
}
fastcall_call(pmt->read_map_end, protocol, Qnil);
} else if (fmd->type == TTYPE_LIST) {
int i;
VALUE list_header = fastcall_call(pmt->read_list_begin, protocol, Qnil);
int element_ttype = FIX2INT(rb_ary_entry(list_header, 0));
int num_elements = FIX2INT(rb_ary_entry(list_header, 1));
// Check the declared element type against the expected one and skip the list contents
// if the types don't match.
field_metadata* element_md = fmd->element;
if (element_md) {
int specified_element_type = element_md->type;
if (specified_element_type == element_ttype) {
result = rb_ary_new2(num_elements);
for (i = 0; i < num_elements; ++i) {
rb_ary_push(result, read_anything(protocol, element_md, pmt));
}
} else {
skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements);
}
} else {
skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements);
}
fastcall_call(pmt->read_list_end, protocol, Qnil);
} else if (fmd->type == TTYPE_SET) {
VALUE items;
int i;
VALUE set_header = fastcall_call(pmt->read_set_begin, protocol, Qnil);
int element_ttype = FIX2INT(rb_ary_entry(set_header, 0));
int num_elements = FIX2INT(rb_ary_entry(set_header, 1));
// Check the declared element type against the expected one and skip the set contents
// if the types don't match.
field_metadata* element_md = fmd->element;
if (element_md) {
//.........这里部分代码省略.........
开发者ID:ggPeti,项目名称:thrift,代码行数:101,代码来源:struct.c
示例11: rb_matrix_form
static VALUE
rb_matrix_form(VALUE self, VALUE n)
{
VALUE base_ary;
VALUE res_ary;
VALUE tmp_ary;
long ary_len = 2;
if(TYPE(n) != T_FIXNUM)
{
rb_raise(rb_eArgError, "Invalid argument for type Fixnum");
return Qnil;
}
if(RTEST(rb_funcall(n, id_lt, 1, ZERO)))
{
rb_raise(rb_eArgError, "n cannot be negative");
return Qnil;
}
else
{
base_ary = rb_ary_new2(ARY_LEN);
res_ary = rb_ary_new2(ARY_LEN);
tmp_ary = rb_ary_new2(ARY_LEN);
/* base is {{1, 1}, {1, 0}} */
rb_ary_push(tmp_ary, ONE);
rb_ary_push(tmp_ary, ONE);
rb_ary_push(base_ary, tmp_ary);
tmp_ary = rb_ary_new2(ARY_LEN);
rb_ary_push(tmp_ary, ONE);
rb_ary_push(tmp_ary, ZERO);
rb_ary_push(base_ary, tmp_ary);
/* res is {{1, 0}, {0, 1}} */
tmp_ary = rb_ary_new2(ARY_LEN);
rb_ary_push(tmp_ary, ONE);
rb_ary_push(tmp_ary, ZERO);
rb_ary_push(res_ary, tmp_ary);
tmp_ary = rb_ary_new2(ARY_LEN);
rb_ary_push(tmp_ary, ZERO);
rb_ary_push(tmp_ary, ONE);
rb_ary_push(res_ary, tmp_ary);
while(!rb_equal(n, ZERO))
{
if(rb_equal(rb_funcall(n, id_mod, 1, TWO), ZERO))
{
n = rb_funcall(n, id_div, 1, TWO);
base_ary = rb_matrix_mul(base_ary, base_ary);
}
else
{
n = rb_funcall(n, id_minus, 1, ONE);
res_ary = rb_matrix_mul(res_ary, base_ary);
}
}
}
return res_ary;
}
开发者ID:chaitanyav,项目名称:fibonacci,代码行数:63,代码来源:fibonacci.c
示例12: api_call
/*
* call-seq:
* Win32::API#call(arg1, arg2, ...)
*
* Calls the function pointer with the given arguments (if any). Note that,
* while this method will catch some prototype mismatches (raising a TypeError
* in the process), it is not fulproof. It is ultimately your job to make
* sure the arguments match the +prototype+ specified in the constructor.
*
* For convenience, nil is converted to NULL, true is converted to TRUE (1)
* and false is converted to FALSE (0).
*/
static VALUE api_call(int argc, VALUE* argv, VALUE self){
VALUE v_proto, v_args, v_arg, v_return;
Win32API* ptr;
unsigned long return_value;
int i = 0;
int len;
struct{
unsigned long params[20];
} param;
Data_Get_Struct(self, Win32API, ptr);
rb_scan_args(argc, argv, "0*", &v_args);
v_proto = rb_iv_get(self, "@prototype");
// For void prototypes, allow either no args or an explicit nil
if(RARRAY_LEN(v_proto) != RARRAY_LEN(v_args)){
char* c = StringValuePtr(RARRAY_PTR(v_proto)[0]);
if(!strcmp(c, "V")){
rb_ary_push(v_args, Qnil);
}
else{
rb_raise(rb_eArgError,
"wrong number of parameters: expected %d, got %d",
RARRAY_LEN(v_proto), RARRAY_LEN(v_args)
);
}
}
len = RARRAY_LEN(v_proto);
for(i = 0; i < len; i++){
v_arg = RARRAY_PTR(v_args)[i];
// Convert nil to NULL. Otherwise convert as appropriate.
if(NIL_P(v_arg))
param.params[i] = (unsigned long)NULL;
else if(v_arg == Qtrue)
param.params[i] = TRUE;
else if(v_arg == Qfalse)
param.params[i] = FALSE;
else
switch(ptr->prototype[i]){
case _T_LONG:
param.params[i] = NUM2ULONG(v_arg);
break;
case _T_INTEGER:
param.params[i] = NUM2INT(v_arg);
break;
case _T_POINTER:
if(FIXNUM_P(v_arg)){
param.params[i] = NUM2ULONG(v_arg);
}
else{
StringValue(v_arg);
rb_str_modify(v_arg);
param.params[i] = (unsigned long)StringValuePtr(v_arg);
}
break;
case _T_CALLBACK:
ActiveCallback = v_arg;
v_proto = rb_iv_get(ActiveCallback, "@prototype");
param.params[i] = (LPARAM)NUM2ULONG(rb_iv_get(ActiveCallback, "@address"));;
break;
case _T_STRING:
param.params[i] = (unsigned long)RSTRING_PTR(v_arg);
break;
default:
param.params[i] = NUM2ULONG(v_arg);
}
}
/* Call the function, get the return value */
return_value = ptr->function(param);
/* Return the appropriate type based on the return type specified
* in the constructor.
*/
switch(ptr->return_type){
case _T_INTEGER:
v_return = INT2NUM(return_value);
break;
case _T_LONG:
v_return = ULONG2NUM(return_value);
break;
//.........这里部分代码省略.........
开发者ID:twobitfool,项目名称:win32-api,代码行数:101,代码来源:api.c
示例13: rb_mysql_result_fetch_row
//.........这里部分代码省略.........
char msec_char[7] = {'0','0','0','0','0','0','\0'};
uint64_t seconds;
tokens = sscanf(row[i], "%4u-%2u-%2u %2u:%2u:%2u.%6s", &year, &month, &day, &hour, &min, &sec, msec_char);
if (tokens < 6) { /* msec might be empty */
val = Qnil;
break;
}
seconds = (year*31557600ULL) + (month*2592000ULL) + (day*86400ULL) + (hour*3600ULL) + (min*60ULL) + sec;
if (seconds == 0) {
val = Qnil;
} else {
if (month < 1 || day < 1) {
rb_raise(cMysql2Error, "Invalid date in field '%.*s': %s", fields[i].name_length, fields[i].name, row[i]);
val = Qnil;
} else {
if (seconds < MYSQL2_MIN_TIME || seconds > MYSQL2_MAX_TIME) { /* use DateTime for larger date range, does not support microseconds */
VALUE offset = INT2NUM(0);
if (args->db_timezone == intern_local) {
offset = rb_funcall(cMysql2Client, intern_local_offset, 0);
}
val = rb_funcall(cDateTime, intern_civil, 7, UINT2NUM(year), UINT2NUM(month), UINT2NUM(day), UINT2NUM(hour), UINT2NUM(min), UINT2NUM(sec), offset);
if (!NIL_P(args->app_timezone)) {
if (args->app_timezone == intern_local) {
offset = rb_funcall(cMysql2Client, intern_local_offset, 0);
val = rb_funcall(val, intern_new_offset, 1, offset);
} else { /* utc */
val = rb_funcall(val, intern_new_offset, 1, opt_utc_offset);
}
}
} else {
msec = msec_char_to_uint(msec_char, sizeof(msec_char));
val = rb_funcall(rb_cTime, args->db_timezone, 7, UINT2NUM(year), UINT2NUM(month), UINT2NUM(day), UINT2NUM(hour), UINT2NUM(min), UINT2NUM(sec), UINT2NUM(msec));
if (!NIL_P(args->app_timezone)) {
if (args->app_timezone == intern_local) {
val = rb_funcall(val, intern_localtime, 0);
} else { /* utc */
val = rb_funcall(val, intern_utc, 0);
}
}
}
}
}
break;
}
case MYSQL_TYPE_DATE: /* DATE field */
case MYSQL_TYPE_NEWDATE: { /* Newer const used > 5.0 */
int tokens;
unsigned int year=0, month=0, day=0;
tokens = sscanf(row[i], "%4u-%2u-%2u", &year, &month, &day);
if (tokens < 3) {
val = Qnil;
break;
}
if (year+month+day == 0) {
val = Qnil;
} else {
if (month < 1 || day < 1) {
rb_raise(cMysql2Error, "Invalid date in field '%.*s': %s", fields[i].name_length, fields[i].name, row[i]);
val = Qnil;
} else {
val = rb_funcall(cDate, intern_new, 3, UINT2NUM(year), UINT2NUM(month), UINT2NUM(day));
}
}
break;
}
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_STRING: /* CHAR or BINARY field */
case MYSQL_TYPE_SET: /* SET field */
case MYSQL_TYPE_ENUM: /* ENUM field */
case MYSQL_TYPE_GEOMETRY: /* Spatial fielda */
default:
val = rb_str_new(row[i], fieldLengths[i]);
#ifdef HAVE_RUBY_ENCODING_H
val = mysql2_set_field_string_encoding(val, fields[i], default_internal_enc, conn_enc);
#endif
break;
}
}
if (args->asArray) {
rb_ary_push(rowVal, val);
} else {
rb_hash_aset(rowVal, field, val);
}
} else {
if (args->asArray) {
rb_ary_push(rowVal, Qnil);
} else {
rb_hash_aset(rowVal, field, Qnil);
}
}
}
return rowVal;
}
开发者ID:Frantz103,项目名称:clubartizan-Rails,代码行数:101,代码来源:result.c
示例14: convert_keymod
/* Convert an OR'd list of KMODs into a Ruby array of keysyms. */
VALUE convert_keymod( SDLMod mods )
{
VALUE array;
array = rb_ary_new();
if(mods != 0)
{
/* KEY MODIFIER KEY SYM */
if(mods & KMOD_LSHIFT) rb_ary_push(array,INT2NUM( SDLK_LSHIFT ));
if(mods & KMOD_RSHIFT) rb_ary_push(array,INT2NUM( SDLK_RSHIFT ));
if(mods & KMOD_LCTRL) rb_ary_push(array,INT2NUM( SDLK_LCTRL ));
if(mods & KMOD_RCTRL) rb_ary_push(array,INT2NUM( SDLK_RCTRL ));
if(mods & KMOD_LALT) rb_ary_push(array,INT2NUM( SDLK_LALT ));
if(mods & KMOD_RALT) rb_ary_push(array,INT2NUM( SDLK_RALT ));
if(mods & KMOD_LMETA) rb_ary_push(array,INT2NUM( SDLK_LMETA ));
if(mods & KMOD_RMETA) rb_ary_push(array,INT2NUM( SDLK_RMETA ));
if(mods & KMOD_NUM) rb_ary_push(array,INT2NUM( SDLK_NUMLOCK ));
if(mods & KMOD_CAPS) rb_ary_push(array,INT2NUM( SDLK_CAPSLOCK ));
if(mods & KMOD_MODE) rb_ary_push(array,INT2NUM( SDLK_MODE ));
}
return array;
}
开发者ID:atiaxi,项目名称:rubygame,代码行数:23,代码来源:rubygame_event.c
示例15: rb_set_to_a_callback
static void
rb_set_to_a_callback(const void *value, void *context)
{
rb_ary_push((VALUE)context, (VALUE)value);
}
开发者ID:alloy,项目名称:mr-experimental,代码行数:5,代码来源:set.c
示例16: statement_Execute
static VALUE statement_Execute(VALUE self) {
int i;
CS_DATAFMT col;
CS_DATAFMT *cols;
EX_COLUMN_DATA *col_data;
CS_INT rc;
CS_INT resulttype;
CS_INT num_cols;
CS_INT col_len;
CS_INT row_count = 0;
CS_INT rows_read;
CS_INT num_errors = 0;
CS_SERVERMSG servermsg;
VALUE err;
char *error_msg;
struct timeval start, stop;
int print_rows = 1;
char message[128];
char* buf;
CS_DATEREC date_rec;
char output[200];
CS_INT output_len;
int tempInt;
CS_BIGINT tempBigInt;
double tempDouble;
CS_NUMERIC tempNumeric;
char* tempText;
char* newTempText;
int tempTextLen;
CS_INT data_rc;
int isNull = 0;
CS_DATE tempDate;
CS_DATETIME tempDateTime;
TDS_Connection* conn;
CS_COMMAND * cmd;
VALUE connection;
VALUE query;
VALUE columns;
VALUE rows;
VALUE status;
VALUE errors;
VALUE date_parts[8];
VALUE column;
VALUE row;
VALUE column_name = rb_str_new2("name");
VALUE column_type = rb_str_new2("type");
VALUE column_size = rb_str_new2("size");
VALUE column_scale = rb_str_new2("scale");
VALUE column_precision = rb_str_new2("precision");
VALUE column_value;
connection = rb_iv_get(self, "@connection");
query = rb_iv_get(self, "@query");
columns = rb_ary_new();
rb_iv_set(self, "@columns", columns);
rows = rb_ary_new();
rb_iv_set(self, "@rows", rows);
Data_Get_Struct(connection, TDS_Connection, conn);
buf = value_to_cstr(query);
rb_iv_set(self, "@status", Qnil);
rb_iv_set(self, "@messages", rb_ary_new());
errors = rb_ary_new();
rb_iv_set(self, "@errors", errors);
ct_diag(conn->connection, CS_INIT, CS_UNUSED, CS_UNUSED, NULL);
// if ( ct_callback(conn->context, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *)clientmsg_cb) != CS_SUCCEED ) {
// error_message("ct_callback CS_CLIENTMSG_CB failed\n");
// }
// if ( ct_callback(conn->context, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *)servermsg_cb) != CS_SUCCEED ) {
// error_message("ct_callback CS_SERVERMSG_CB failed\n");
// }
ct_cmd_alloc(conn->connection, &cmd);
ct_command(cmd, CS_LANG_CMD, buf, CS_NULLTERM, CS_UNUSED);
ct_send(cmd);
if ( ct_diag(conn->connection, CS_STATUS, CS_SERVERMSG_TYPE, CS_UNUSED, &num_errors) != CS_SUCCEED ) {
error_message("ct_diag CS_STATUS CS_SERVERMSG_TYPE failed");
}
if (num_errors > 0) {
// fprintf(stderr, "%d errors found\n", num_errors);
for (i = 0; i < num_errors; i++) {
if ( ct_diag(conn->connection, CS_GET, CS_SERVERMSG_TYPE, i+1, &servermsg) != CS_SUCCEED ) {
error_message("ct_diag CS_GET CS_SERVERMSG_TYPE failed");
}
if (servermsg.severity > 0) {
// error_message(servermsg.text);
rb_ary_push(errors, rb_str_new2(servermsg.text));
//.........这里部分代码省略.........
开发者ID:ebryn,项目名称:freetds4ruby,代码行数:101,代码来源:freetds.c
示例17: rb_grn_uvector_to_ruby_object
VALUE
rb_grn_uvector_to_ruby_object (grn_ctx *context, grn_obj *uvector,
grn_obj *range, VALUE related_object)
{
VALUE array = Qnil;
if (!uvector)
return Qnil;
if (!range) {
rb_raise(rb_eTypeError,
"unknown range uvector can't be converted: <%s>",
rb_grn_inspect(related_object));
}
switch (range->header.type) {
case GRN_TYPE: {
const char *current, *end;
grn_id range_id;
grn_obj value;
int value_size;
value_size = grn_obj_get_range(context, range);
array = rb_ary_new();
current = GRN_BULK_HEAD(uvector);
end = GRN_BULK_CURR(uvector);
range_id = grn_obj_id(context, range);
GRN_OBJ_INIT(&value, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, range_id);
while (current < end) {
VALUE rb_value;
GRN_TEXT_SET(context, &value, current, value_size);
rb_value = GRNBULK2RVAL(context, &value, range, related_object);
rb_ary_push(array, rb_value);
current += value_size;
}
GRN_OBJ_FIN(context, &value);
break;
}
case GRN_TABLE_HASH_KEY:
case GRN_TABLE_PAT_KEY:
case GRN_TABLE_DAT_KEY:
case GRN_TABLE_NO_KEY: {
grn_id *current, *end;
VALUE rb_range = Qnil;
array = rb_ary_new();
rb_range = GRNTABLE2RVAL(context, range, GRN_FALSE);
current = (grn_id *)GRN_BULK_HEAD(uvector);
end = (grn_id *)GRN_BULK_CURR(uvector);
while (current < end) {
VALUE record = Qnil;
if (*current != GRN_ID_NIL) {
record = rb_grn_record_new(rb_range, *current, Qnil);
}
rb_ary_push(array, record);
current++;
}
break;
}
default:
rb_raise(rb_eTypeError,
"unknown range uvector can't be converted: %s(%#x): <%s>",
rb_grn_inspect_type(range->header.type),
range->header.type,
rb_grn_inspect(related_object));
break;
}
return array;
}
开发者ID:rutice,项目名称:rroonga,代码行数:68,代码来源:rb-grn-utils.c
示例18: get_value
static VALUE get_value(const char* buffer, int* position, int type) {
VALUE value;
switch (type) {
case -1:
{
value = rb_class_new_instance(0, NULL, MinKey);
break;
}
case 1:
{
double d;
memcpy(&d, buffer + *position, 8);
value = rb_float_new(d);
*position += 8;
break;
}
case 2:
case 13:
{
int value_length;
value_length = *(int*)(buffer + *position) - 1;
*position += 4;
value = STR_NEW(buffer + *position, value_length);
*position += value_length + 1;
break;
}
case 3:
{
int size;
memcpy(&size, buffer + *position, 4);
if (strcmp(buffer + *position + 5, "$ref") == 0) { // DBRef
int offset = *position + 10;
VALUE argv[2];
int collection_length = *(int*)(buffer + offset) - 1;
char id_type;
offset += 4;
argv[0] = STR_NEW(buffer + offset, collection_length);
offset += collection_length + 1;
id_type = buffer[offset];
offset += 5;
argv[1] = get_value(buffer, &offset, (int)id_type);
value = rb_class_new_instance(2, argv, DBRef);
} else {
value = elements_to_hash(buffer + *position + 4, size - 5);
}
*position += size;
break;
}
case 4:
{
int size, end;
memcpy(&size, buffer + *position, 4);
end = *
|
请发表评论