ext/mpfr/ruby_mpfr.c in ruby-mpfr-0.0.10 vs ext/mpfr/ruby_mpfr.c in ruby-mpfr-0.0.11

- old
+ new

@@ -32,12 +32,11 @@ } else if (rnd_id == id_rndd) { return MPFR_RNDD; } else if (rnd_id == id_rnda) { return MPFR_RNDA; } - rb_raise(rb_eArgError, "Argument must be Rounding Mode."); - /* rb_raise(rb_eArgError, "Argument must be Rounding Mode: %s", rb_class2name(rnd)); */ + rb_raise(rb_eArgError, "Argument must be a constant meaning rounding mode, but the objects is %s.", rb_obj_classname(rnd)); } /* If argc equals max, convert last argument to rounding mode number. */ /* Otherwise, return defoult rounding mode number. */ mp_rnd_t r_mpfr_rnd_from_optional_argument(int min, int max, int argc, VALUE *argv) @@ -405,11 +404,11 @@ MPFR *ptr_obj; volatile VALUE tmp = rb_funcall(obj, to_fr, 0); r_mpfr_get_struct(ptr_obj, tmp); mpfr_set(ptr, ptr_obj, rnd); } else { - rb_raise(rb_eArgError, "Invalid class %s for making MPFR.", rb_class2name(obj)); + rb_raise(rb_eArgError, "Invalid class %s for making MPFR.", rb_obj_classname(obj)); } break; } } } @@ -417,11 +416,11 @@ VALUE r_mpfr_robj_to_mpfr(VALUE obj, int argc, VALUE *argv) { if (rb_respond_to(obj, to_fr)) { return rb_funcall2(obj, to_fr, argc, argv); } - rb_raise(rb_eArgError, "The object of %s can not been converted to MPFR.", rb_class2name(obj)); + rb_raise(rb_eArgError, "The object of %s can not been converted to MPFR.", rb_obj_classname(obj)); } /* If obj is MPFR instance, then this method returns obj. */ /* Otherwise it returns MPFR.new(obj). */ VALUE r_mpfr_new_fr_obj(VALUE obj) @@ -719,11 +718,13 @@ { MPFR *ptr_self; r_mpfr_get_struct(ptr_self, self); char *format = StringValuePtr(format_str); char *ret_str; - mpfr_asprintf(&ret_str, format, ptr_self); + if (!mpfr_asprintf(&ret_str, format, ptr_self)) { + rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf."); + } VALUE ret_val = rb_str_new2(ret_str); mpfr_free_str(ret_str); return ret_val; } @@ -731,11 +732,13 @@ static VALUE r_mpfr_to_s(VALUE self) { MPFR *ptr_self; r_mpfr_get_struct(ptr_self, self); char *ret_str; - mpfr_asprintf(&ret_str, "%.Re", ptr_self); + if (!mpfr_asprintf(&ret_str, "%.Re", ptr_self)) { + rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf."); + } VALUE ret_val = rb_str_new2(ret_str); mpfr_free_str(ret_str); return ret_val; } @@ -743,11 +746,14 @@ static VALUE r_mpfr_inspect(VALUE self) { MPFR *ptr_s; r_mpfr_get_struct(ptr_s, self); char *ret_str; - mpfr_asprintf(&ret_str, "#<MPFR:%lx,'%0.Re',%d>", NUM2LONG(rb_funcall(self, object_id, 0)), ptr_s, mpfr_get_prec(ptr_s)); + if (!mpfr_asprintf(&ret_str, "#<MPFR:%lx,'%0.Re',%d>", NUM2LONG(rb_funcall(self, object_id, 0)), + ptr_s, mpfr_get_prec(ptr_s))) { + rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf."); + } VALUE ret_val = rb_str_new2(ret_str); mpfr_free_str(ret_str); return ret_val; } @@ -2759,21 +2765,21 @@ static VALUE r_mpfr_buildopt_decimal_p(VALUE self) { return mpfr_buildopt_decimal_p() == 0 ? Qnil : Qtrue; } -static VALUE r_mpfr_marshal_dump(VALUE self) +char *r_mpfr_dump_to_string(MPFR *ptr_s) { - MPFR *ptr_s; - r_mpfr_get_struct(ptr_s, self); char *ret_str; if (mpfr_regular_p(ptr_s)) { mpz_t m; mp_exp_t e; mpz_init(m); e = mpfr_get_z_2exp(m, ptr_s); - mpfr_asprintf(&ret_str, "%c%ld\t%ld\t%Zd", MPFR_DUMP_NUMBER, mpfr_get_prec(ptr_s), (long int)e, m); + if (!mpfr_asprintf(&ret_str, "%c%ld\t%ld\t%Zd", MPFR_DUMP_NUMBER, mpfr_get_prec(ptr_s), (long int)e, m)) { + rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf."); + } mpz_clear(m); } else { char type; if (mpfr_zero_p(ptr_s)) { if (mpfr_sgn(ptr_s) >= 0) { @@ -2786,25 +2792,31 @@ } else if (mpfr_sgn(ptr_s) >= 0) { type = MPFR_DUMP_PINF; } else { type = MPFR_DUMP_MINF; } - mpfr_asprintf(&ret_str, "%c%ld", type, mpfr_get_prec(ptr_s)); + if (!mpfr_asprintf(&ret_str, "%c%ld", type, mpfr_get_prec(ptr_s))) { + rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf."); + } } + return ret_str; +} + +static VALUE r_mpfr_marshal_dump(VALUE self) +{ + MPFR *ptr_s; + r_mpfr_get_struct(ptr_s, self); + char *ret_str = r_mpfr_dump_to_string(ptr_s); VALUE ret_val = rb_str_new2(ret_str); mpfr_free_str(ret_str); return ret_val; } -static VALUE r_mpfr_marshal_load(VALUE self, VALUE dump_string) +void r_mpfr_load_string(MPFR *ptr_s, const char *dump) { long int prec; - MPFR *ptr_s; - r_mpfr_get_struct(ptr_s, self); - Check_Type(dump_string, T_STRING); - char *dump, type; - dump = RSTRING_PTR(dump_string); + char type; type = dump[0]; dump++; if (type == MPFR_DUMP_NUMBER) { mpz_t m; long int e; @@ -2837,9 +2849,17 @@ mpfr_set_inf(ptr_s, -1); } else { rb_raise(rb_eArgError, "Invalid dumped data for marshal_load."); } } +} + +static VALUE r_mpfr_marshal_load(VALUE self, VALUE dump_string) +{ + MPFR *ptr_s; + r_mpfr_get_struct(ptr_s, self); + Check_Type(dump_string, T_STRING); + r_mpfr_load_string(ptr_s, RSTRING_PTR(dump_string)); return self; } /* Conversion to MPFR. */