예제 #1
0
파일: do_mysql.c 프로젝트: NZX/do
// 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);
  }

}
예제 #2
0
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);
  }

}
예제 #3
0
double oci8_onum_to_dbl(OCINumber *s, OCIError *errhp)
{
    if (oci8_float_conversion_type_is_ruby) {
        char buf[256];
        sword rv;

        rv = oranumber_to_str(s, buf, sizeof(buf));
        if (rv <= 0) {
            char buf[ORANUMBER_DUMP_BUF_SIZ];

            oranumber_dump(s, buf);
            rb_raise(eOCIException, "Invalid internal number format: %s", buf);
        }
        if (strcmp(buf, "~") == 0) {
            return INFINITY;
        } else if (strcmp(buf, "-~") == 0) {
            return -INFINITY;
        }
        return rb_cstr_to_dbl(buf, Qtrue);
    } else {
        double dbl;

        chkerr(OCINumberToReal(errhp, s, sizeof(double), &dbl));
        return dbl;
    }
}
예제 #4
0
파일: do_mysql_ext.c 프로젝트: sunfmin/do
// Convert C-string to a Ruby instance of Ruby type "type"
static VALUE typecast(const char* value, unsigned long length, char* type) {
  if (NULL == value)
    return Qnil;

  if ( strcmp(type, "Class") == 0) {
    return rb_funcall(mDO, rb_intern("find_const"), 1, TAINTED_STRING(value, length));
  } else if ( strcmp(type, "Integer") == 0 || strcmp(type, "Fixnum") == 0 || strcmp(type, "Bignum") == 0 ) {
    return rb_cstr2inum(value, 10);
  } else if (0 == strcmp("String", type)) {
    return TAINTED_STRING(value, length);
  } else if (0 == strcmp("Float", type) ) {
    return rb_float_new(rb_cstr_to_dbl(value, Qfalse));
  } else if (0 == strcmp("BigDecimal", type) ) {
    return rb_funcall(rb_cBigDecimal, ID_NEW, 1, TAINTED_STRING(value, length));
  } else if (0 == strcmp("TrueClass", type) || 0 == strcmp("FalseClass", type)) {
    return (0 == value || 0 == strcmp("0", value)) ? Qfalse : Qtrue;
  } else if (0 == strcmp("Date", type)) {
    return parse_date(value);
  } else if (0 == strcmp("DateTime", type)) {
    return parse_date_time(value);
  } else if (0 == strcmp("Time", type)) {
    return parse_time(value);
  } else {
    return TAINTED_STRING(value, length);
  }
}
예제 #5
0
/*
=begin
--- OraNumber#to_f()
=end
*/
static VALUE ora_number_to_f(VALUE self)
{
  ora_vnumber_t *ovn = get_ora_number(self);
  unsigned char buf[ORA_NUMBER_BUF_SIZE];

  ora_number_to_str(buf, NULL, &(ovn->num), ovn->size);
  return rb_float_new(rb_cstr_to_dbl(TO_CHARPTR(buf), Qfalse));
}
예제 #6
0
static VALUE
str2num(char *s)
{
    if (strchr(s, '/'))
	return rb_cstr_to_rat(s, 0);
    if (strpbrk(s, ".eE"))
	return DBL2NUM(rb_cstr_to_dbl(s, 0));
    return rb_cstr_to_inum(s, 10, 0);
}
예제 #7
0
/*
 * Document-class: PG::TextDecoder::Float < PG::SimpleDecoder
 *
 * This is a decoder class for conversion of PostgreSQL float4 and float8 types
 * to Ruby Float objects.
 *
 */
static VALUE
pg_text_dec_float(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
{
	switch(*val) {
		case 'N':
			return s_nan;
		case 'I':
			return s_pos_inf;
		case '-':
			if (val[1] == 'I') {
				return s_neg_inf;
			} else {
				return rb_float_new(rb_cstr_to_dbl(val, Qfalse));
			}
		default:
			return rb_float_new(rb_cstr_to_dbl(val, Qfalse));
	}
}
예제 #8
0
/*
 * Common typecasting logic that can be used or overriden by Adapters.
 */
extern VALUE data_objects_typecast(const char *value, long length, const VALUE type, int encoding) {
#ifdef HAVE_RUBY_ENCODING_H
  rb_encoding *internal_encoding = rb_default_internal_encoding();
#else
  void *internal_encoding = NULL;
#endif

  if (type == rb_cInteger) {
    return rb_cstr2inum(value, 10);
  }
  else if (type == rb_cString) {
    return DATA_OBJECTS_STR_NEW(value, length, encoding, internal_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, DO_ID_NEW, 1, rb_str_new(value, length));
  }
  else if (type == rb_cDate) {
    return data_objects_parse_date(value);
  }
  else if (type == rb_cDateTime) {
    return data_objects_parse_date_time(value);
  }
  else if (type == rb_cTime) {
    return data_objects_parse_time(value);
  }
  else if (type == rb_cTrueClass) {
    return (!value || strcmp("0", value) == 0) ? Qfalse : Qtrue;
  }
  else if (type == rb_cByteArray) {
    return rb_funcall(rb_cByteArray, DO_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_cNilClass) {
    return Qnil;
  }
  else {
    return DATA_OBJECTS_STR_NEW(value, length, encoding, internal_encoding);
  }
}