// Convert C-string to a Ruby instance of Ruby type "type" static VALUE typecast(const char *value, long length, const VALUE type, int encoding) { if(NULL == value) { return Qnil; } if (type == rb_cInteger) { return rb_cstr2inum(value, 10); } else if (type == rb_cString) { return DO_STR_NEW(value, length, encoding); } else if (type == rb_cFloat) { return rb_float_new(rb_cstr_to_dbl(value, Qfalse)); } else if (type == rb_cBigDecimal) { return rb_funcall(rb_cBigDecimal, ID_NEW, 1, rb_str_new(value, length)); } else if (type == rb_cDate) { return parse_date(value); } else if (type == rb_cDateTime) { return parse_date_time(value); } else if (type == rb_cTime) { return parse_time(value); } else if (type == rb_cTrueClass) { return (0 == value || 0 == strcmp("0", value)) ? Qfalse : Qtrue; } else if (type == rb_cByteArray) { return rb_funcall(rb_cByteArray, ID_NEW, 1, rb_str_new(value, length)); } else if (type == rb_cClass) { return rb_funcall(mDO, rb_intern("full_const_get"), 1, rb_str_new(value, length)); } else if (type == rb_cObject) { return rb_marshal_load(rb_str_new(value, length)); } else if (type == rb_cNilClass) { return Qnil; } else { return DO_STR_NEW(value, length, encoding); } }
static VALUE typecast(const char *value, long length, const VALUE type, int encoding) { if (type == rb_cInteger) { return rb_cstr2inum(value, 10); } else if (type == rb_cString) { return DO_STR_NEW(value, length, encoding); } else if (type == rb_cFloat) { return rb_float_new(rb_cstr_to_dbl(value, Qfalse)); } else if (type == rb_cBigDecimal) { return rb_funcall(rb_cBigDecimal, ID_NEW, 1, rb_str_new(value, length)); } else if (type == rb_cDate) { return parse_date(value); } else if (type == rb_cDateTime) { return parse_date_time(value); } else if (type == rb_cTime) { return parse_time(value); } else if (type == rb_cTrueClass) { return *value == 't' ? Qtrue : Qfalse; } else if (type == rb_cByteArray) { size_t new_length = 0; char* unescaped = (char *)PQunescapeBytea((unsigned char*)value, &new_length); VALUE byte_array = rb_funcall(rb_cByteArray, ID_NEW, 1, rb_str_new(unescaped, new_length)); PQfreemem(unescaped); return byte_array; } else if (type == rb_cClass) { return rb_funcall(rb_cObject, rb_intern("full_const_get"), 1, rb_str_new(value, length)); } else if (type == rb_cObject) { return rb_marshal_load(rb_str_new(value, length)); } else if (type == rb_cNilClass) { return Qnil; } else { return DO_STR_NEW(value, length, encoding); } }
/* Fetches the object from some remote machine and returns it as the result. YID * id: id of the object to fetch. */ VALUE yard_remote_fetch_object(YID * id) { YARD_NET_MESSAGE * message = yard_net_send_message(id, sizeof(YID), id->cookie, 0, 0, YARD_NET_FETCH_OBJECT, YARD_NET_BLOCKING); if (message->operation == YARD_NET_FETCH_OBJECT_SUCCESS) { return rb_marshal_load(pchar_to_string(message->data, message->size)); } }
static VALUE typecast(sqlite3_stmt *stmt, int i, VALUE ruby_class) { const char *ruby_type; VALUE ruby_value = Qnil; int original_type = sqlite3_column_type(stmt, i); int length = sqlite3_column_bytes(stmt, i); if ( original_type == SQLITE_NULL ) { return ruby_value; } if ( original_type == SQLITE_BLOB ) { return TAINTED_STRING((char*)sqlite3_column_blob(stmt, i), length); } if(ruby_class == Qnil) { switch(original_type) { case SQLITE_INTEGER: { ruby_type = "Integer"; break; } case SQLITE_FLOAT: { ruby_type = "Float"; break; } default: { ruby_type = "String"; break; } } } else { ruby_type = rb_class2name(ruby_class); } if ( strcmp(ruby_type, "Class") == 0) { return rb_funcall(rb_cObject, rb_intern("full_const_get"), 1, TAINTED_STRING((char*)sqlite3_column_text(stmt, i), length)); } else if ( strcmp(ruby_type, "Object") == 0 ) { return rb_marshal_load(rb_str_new2((char*)sqlite3_column_text(stmt, i))); } else if ( strcmp(ruby_type, "TrueClass") == 0 ) { return strcmp((char*)sqlite3_column_text(stmt, i), "t") == 0 ? Qtrue : Qfalse; } else if ( strcmp(ruby_type, "Integer") == 0 || strcmp(ruby_type, "Fixnum") == 0 || strcmp(ruby_type, "Bignum") == 0 ) { return LL2NUM(sqlite3_column_int64(stmt, i)); } else if ( strcmp(ruby_type, "BigDecimal") == 0 ) { return rb_funcall(rb_cBigDecimal, ID_NEW, 1, TAINTED_STRING((char*)sqlite3_column_text(stmt, i), length)); } else if ( strcmp(ruby_type, "Float") == 0 ) { return rb_float_new(sqlite3_column_double(stmt, i)); } else if ( strcmp(ruby_type, "Date") == 0 ) { return parse_date((char*)sqlite3_column_text(stmt, i)); } else if ( strcmp(ruby_type, "DateTime") == 0 ) { return parse_date_time((char*)sqlite3_column_text(stmt, i)); } else if ( strcmp(ruby_type, "Time") == 0 ) { return parse_time((char*)sqlite3_column_text(stmt, i)); } else { return TAINTED_STRING((char*)sqlite3_column_text(stmt, i), length); } }
VALUE rb_fairy_xmarshaled_queue_pop(VALUE self) { fairy_xmarshaled_queue_t *mq; VALUE buf; struct rb_fairy_xmarshaled_queue_pop_arg arg; GetFairyXMarshaledQueuePtr(self, mq); while (NIL_P(mq->pop_queue) || RARRAY_LEN(mq->pop_queue) == 0) { buf = rb_xthread_fifo_pop(mq->buffers); if (NIL_P(buf)) { arg.self = self; arg.buf = Qnil; mq->mon_synchronize(mq->buffers_mon, rb_fairy_xmarshaled_queue_pop_wait, &arg); buf = arg.buf; } if (EOS_P(buf)) { mq->pop_queue = rb_ary_new3(1, buf); } else if (CLASS_OF(buf) == rb_cFairyFastTempfile) { buf = rb_fairy_xmarshaled_queue_restore(self, buf); if (CLASS_OF(buf) == rb_cFairyStringBuffer) { VALUE tmp = buf; buf = rb_fairy_string_buffer_to_a(tmp); rb_fairy_string_buffer_clear(tmp); } mq->pop_queue = buf; } else { buf = rb_marshal_load(buf); if (CLASS_OF(buf) == rb_cFairyStringBuffer) { mq->pop_queue = rb_fairy_string_buffer_to_a(buf); rb_fairy_string_buffer_clear(buf); mq->buffers_cache_no--; } else { mq->pop_queue = buf; mq->buffers_cache_no--; } } } return rb_ary_shift(mq->pop_queue); }
static VALUE rb_queue_marshal_load(VALUE self, VALUE data) { Queue *queue; VALUE array; Data_Get_Struct(self, Queue, queue); array = rb_marshal_load(data); if (TYPE(array) != T_ARRAY) { rb_raise(rb_eRuntimeError, "expected Array of queue data"); } if (RARRAY_LEN(array) < 1) { rb_raise(rb_eRuntimeError, "missing capacity value"); } queue->capacity = NUM2ULONG(rb_ary_shift(array)); push_multiple_list(&queue->values, RARRAY_PTR(array), (unsigned)RARRAY_LEN(array)); return self; }
static VALUE rb_fairy_xmarshaled_queue_restore(VALUE self, VALUE tmpbuf) { VALUE buf; VALUE io; fairy_xmarshaled_queue_t *mq; GetFairyXMarshaledQueuePtr(self, mq); if (mq->log_mstore_p) { rb_fairy_debug(self, "START M.RESTORE"); } io = rb_funcall(tmpbuf, id_open, 0); buf = rb_marshal_load(io); if (mq->log_mstore_p) { rb_fairy_debug(self, "FINISH M.RESTORE"); } return buf; }
VALUE marshal_spec_rb_marshal_load(VALUE self, VALUE data) { return rb_marshal_load(data); }