示例#1
0
static VALUE
lazy_take_func(VALUE val, VALUE args, int argc, VALUE *argv)
{
    long remain;
    VALUE memo = rb_ivar_get(argv[0], id_memo);
    if (NIL_P(memo)) {
	memo = args;
    }

    rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
    if ((remain = NUM2LONG(memo)-1) == 0) {
	return Qundef;
    }
    else {
	rb_ivar_set(argv[0], id_memo, LONG2NUM(remain));
	return Qnil;
    }
}
示例#2
0
/*
 * Check the arguments to a deserialize function and process args
 */
static void des_check_deserialize_args(AMF_DESERIALIZER *des, int argc, VALUE *argv) {
    VALUE src;
    rb_scan_args(argc, argv, "01", &src);
    if(des->depth == 0) {
        if(src != Qnil) {
            des_set_src(des, src);
        } else {
            rb_raise(rb_eArgError, "Missing deserialization source");
        }
    } else {
        if(src != Qnil) {
            rb_raise(rb_eArgError, "Already deserializing a source - don't pass a new one");
        } else {
            // Make sure pos matches src pos in case StringIO source pos changed
            des->pos = NUM2LONG(rb_funcall(des->src, rb_intern("pos"), 0));
        }
    }
}
示例#3
0
/*
 * call-seq: memory.put_string(offset, str)
 * @param [Numeric] offset
 * @param [String] str
 * @return [self]
 * @raise {SecurityError} when writing unsafe string to memory
 * @raise {IndexError} if +offset+ is too great
 * @raise {NullPointerError} if memory not initialized
 * Put a string in memory.
 */
static VALUE
memory_put_string(VALUE self, VALUE offset, VALUE str)
{
    AbstractMemory* ptr = MEMORY(self);
    long off, len;

    Check_Type(str, T_STRING);
    off = NUM2LONG(offset);
    len = RSTRING_LEN(str);

    checkWrite(ptr);
    checkBounds(ptr, off, len + 1);

    memcpy(ptr->address + off, RSTRING_PTR(str), len);
    *((char *) ptr->address + off + len) = '\0';

    return self;
}
示例#4
0
// Run a test command, and store the result in two separate strings
//
// Example:
//   out, err, rc, major, minor = target.test_and_store_results_separately("find /etc -type l", "nobody")
// Input:
//   command: the command to run
//   user: the user under which to run the command
//         (optional, defaults to "root")
//   timeout: the time in seconds after which the command is aborted 
//            (optional, defaults to 60L)
// Output:
//   out: the standard output of the command
//   err: the standard error of the command
//   rc: the return code of the testing platform
//   major: the return code of the system under test
//   minor: the return code of the command
VALUE method_test_and_store_results_separately(VALUE self, VALUE ruby_args)
{
  long len;
  VALUE ruby_command,
        ruby_user,
        ruby_timeout;
  struct twopence_target *target;
  twopence_buf_t stdout_buf, stderr_buf;
  twopence_status_t status;
  int rc;

  Check_Type(ruby_args, T_ARRAY);
  len = RARRAY_LEN(ruby_args);
  if (len < 1 || len > 3)
    rb_raise(rb_eArgError, "wrong number of arguments");
  ruby_command = rb_ary_entry(ruby_args, 0);
  if (len >= 2)
  {
    ruby_user = rb_ary_entry(ruby_args, 1);
    Check_Type(ruby_user, T_STRING);
  }
  else ruby_user = rb_str_new2("root");
  if (len >= 3)
  {
    ruby_timeout = rb_ary_entry(ruby_args, 2);
    Check_Type(ruby_timeout, T_FIXNUM);
  }
  else ruby_timeout = LONG2NUM(60L);
  Data_Get_Struct(self, struct twopence_target, target);

  twopence_buf_init(&stdout_buf);
  twopence_buf_init(&stderr_buf);
  twopence_buf_resize(&stdout_buf, 65536);
  twopence_buf_resize(&stderr_buf, 65536);

  rc = twopence_test_and_store_results_separately(target,
         StringValueCStr(ruby_user), NUM2LONG(ruby_timeout), StringValueCStr(ruby_command),
         &stdout_buf, &stderr_buf, &status);

  return rb_ary_new3(5,
                     buffer_value(&stdout_buf),
                     buffer_value(&stderr_buf),
                     INT2NUM(rc), INT2NUM(status.major), INT2NUM(status.minor));
}
示例#5
0
static VALUE
lazy_take(VALUE obj, VALUE n)
{
    long len = NUM2LONG(n);
    VALUE lazy;

    if (len < 0) {
	rb_raise(rb_eArgError, "attempt to take negative size");
    }
    if (len == 0) {
	VALUE len = INT2FIX(0);
	lazy = lazy_to_enum_i(obj, sym_cycle, 1, &len, 0);
    }
    else {
	lazy = rb_block_call(rb_cLazy, id_new, 1, &obj,
					 lazy_take_func, n);
    }
    return lazy_set_method(lazy, rb_ary_new3(1, n), lazy_take_size);
}
示例#6
0
static VALUE
lazy_zip_arrays_func(VALUE val, VALUE arrays, int argc, VALUE *argv)
{
    VALUE yielder, ary, memo;
    long i, count;

    yielder = argv[0];
    memo = rb_attr_get(yielder, id_memo);
    count = NIL_P(memo) ? 0 : NUM2LONG(memo);

    ary = rb_ary_new2(RARRAY_LEN(arrays) + 1);
    rb_ary_push(ary, argv[1]);
    for (i = 0; i < RARRAY_LEN(arrays); i++) {
	rb_ary_push(ary, rb_ary_entry(RARRAY_AREF(arrays, i), count));
    }
    rb_funcall(yielder, id_yield, 1, ary);
    rb_ivar_set(yielder, id_memo, LONG2NUM(++count));
    return Qnil;
}
示例#7
0
文件: struct.c 项目: Chatto/VGdesk
VALUE
rb_struct_aref(VALUE s, VALUE idx)
{
    long i;

    if (RB_TYPE_P(idx, T_STRING) || RB_TYPE_P(idx, T_SYMBOL)) {
	return rb_struct_aref_id(s, rb_to_id(idx));
    }

    i = NUM2LONG(idx);
    if (i < 0) i = RSTRUCT_LEN(s) + i;
    if (i < 0)
        rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
		 i, RSTRUCT_LEN(s));
    if (RSTRUCT_LEN(s) <= i)
        rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
		 i, RSTRUCT_LEN(s));
    return RSTRUCT_PTR(s)[i];
}
示例#8
0
文件: error.c 项目: technohippy/oruby
static VALUE
syserr_initialize(int argc, VALUE *argv, VALUE self)
{
#if !defined(_WIN32)
    char *strerror();
#endif
    const char *err;
    VALUE mesg, error;
    VALUE klass = rb_obj_class(self);

    if (klass == rb_eSystemCallError) {
	rb_scan_args(argc, argv, "11", &mesg, &error);
	if (argc == 1 && FIXNUM_P(mesg)) {
	    error = mesg; mesg = Qnil;
	}
	if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &klass)) {
	    /* change class */
	    if (TYPE(self) != T_OBJECT) { /* insurance to avoid type crash */
		rb_raise(rb_eTypeError, "invalid instance type");
	    }
	    RBASIC(self)->klass = klass;
	}
    }
    else {
	rb_scan_args(argc, argv, "01", &mesg);
	error = rb_const_get(klass, rb_intern("Errno"));
    }
    if (!NIL_P(error)) err = strerror(NUM2INT(error));
    else err = "unknown error";
    if (!NIL_P(mesg)) {
	VALUE str = mesg;

	StringValue(str);
	mesg = rb_sprintf("%s - %.*s", err,
			  (int)RSTRING_LEN(str), RSTRING_PTR(str));
    }
    else {
	mesg = rb_str_new2(err);
    }
    rb_call_super(1, &mesg);
    rb_iv_set(self, "errno", error);
    return self;
}
示例#9
0
VALUE
rb_struct_aref(VALUE s, VALUE idx)
{
    long i;

    if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
	return rb_struct_aref_id(s, rb_to_id(idx));
    }

    i = NUM2LONG(idx);
    if (i < 0) i = RSTRUCT(s)->len + i;
    if (i < 0)
        rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
		 i, RSTRUCT(s)->len);
    if (RSTRUCT(s)->len <= i)
        rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
		 i, RSTRUCT(s)->len);
    return RSTRUCT(s)->ptr[i];
}
示例#10
0
static VALUE
rb_struct_aref_imp(VALUE s, SEL sel, VALUE idx)
{
    long i;

    if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
	return rb_struct_aref_id(s, rb_to_id(idx));
    }

    i = NUM2LONG(idx);
    if (i < 0) i = RSTRUCT_LEN(s) + i;
    if (i < 0)
        rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
		 i, RSTRUCT_LEN(s));
    if (RSTRUCT_LEN(s) <= i)
        rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
		 i, RSTRUCT_LEN(s));
    return RSTRUCT_PTR(s)[i];
}
示例#11
0
文件: struct.c 项目: Chatto/VGdesk
static VALUE
recursive_hash(VALUE s, VALUE dummy, int recur)
{
    long i, len;
    st_index_t h;
    VALUE n, *ptr;

    h = rb_hash_start(rb_hash(rb_obj_class(s)));
    if (!recur) {
	ptr = RSTRUCT_PTR(s);
	len = RSTRUCT_LEN(s);
	for (i = 0; i < len; i++) {
	    n = rb_hash(ptr[i]);
	    h = rb_hash_uint(h, NUM2LONG(n));
	}
    }
    h = rb_hash_end(h);
    return INT2FIX(h);
}
示例#12
0
inline static VALUE
f_imul(long a, long b)
{
    VALUE r;
    long c;

    if (a == 0 || b == 0)
	return ZERO;
    else if (a == 1)
	return LONG2NUM(b);
    else if (b == 1)
	return LONG2NUM(a);

    c = a * b;
    r = LONG2NUM(c);
    if (NUM2LONG(r) != c || (c / a) != b)
	r = rb_big_mul(rb_int2big(a), rb_int2big(b));
    return r;
}
示例#13
0
jobject rho_cast_helper<jobject, VALUE>::operator()(JNIEnv *env, VALUE value)
{
    RAWTRACE("rho_cast<jobject, VALUE>");

    if (NIL_P(value))
        return 0;

    if (!initConvertor(env))
        return 0;

    switch(TYPE(value))
    {
    case RUBY_T_SYMBOL:
        value = rb_funcall(value, rb_intern("to_s"), 0);
    case RUBY_T_STRING:
        RAWTRACE("Convert to String object");
        return env->NewStringUTF(RSTRING_PTR(value));
    case RUBY_T_ARRAY:
        RAWTRACE("Convert to Collection object");
        return convertRubyArrayToJavaCollection(value);
    case RUBY_T_HASH:
        RAWTRACE("Convert to Map object");
        return convertRubyHashToJavaMap(value);
    case RUBY_T_TRUE:
        RAWTRACE("Convert to TRUE Boolean obeject");
        return RhoJniConvertor::getBooleanObject(true);
    case RUBY_T_FALSE:
        RAWTRACE("Convert to FALSE Boolean object");
        return RhoJniConvertor::getBooleanObject(false);
    case RUBY_T_FIXNUM:
    case RUBY_T_BIGNUM:
        RAWTRACE("Convert to Integer object");
        return RhoJniConvertor::getIntegerObject(NUM2LONG(value));
    case RUBY_T_FLOAT:
    case RUBY_T_RATIONAL:
        RAWTRACE("Convert to Double object");
        return RhoJniConvertor::getDoubleObject(NUM2DBL(value));
    }

    RAWLOG_ERROR1("rho_cast<jobject, VALUE>: wrong type of VALUE: %d", TYPE(value));
    return 0;
}
示例#14
0
文件: recnum.c 项目: tevren/ruby-bdb
static VALUE
bdb_sary_fetch(int argc, VALUE *argv, VALUE obj)
{
    VALUE pos, ifnone;
    bdb_DB *dbst;
    long idx;

    GetDB(obj, dbst);
    rb_scan_args(argc, argv, "11", &pos, &ifnone);
    idx = NUM2LONG(pos);

    if (idx < 0) {
	idx +=  dbst->len;
    }
    if (idx < 0 || dbst->len <= idx) {
	return ifnone;
    }
    pos = INT2NUM(idx);
    return bdb_get(1, &pos, obj);
}
示例#15
0
/*
 * Set the source of the amf reader to a StringIO object, creating a new one to
 * wrap the source if it's only a string
 */
void des_set_src(AMF_DESERIALIZER *des, VALUE src) {
    VALUE klass = CLASS_OF(src);
    if(klass == cStringIO) {
        VALUE str = rb_funcall(src, rb_intern("string"), 0);
        des->src = src;
        des->stream = RSTRING_PTR(str);
        des->pos = NUM2LONG(rb_funcall(src, rb_intern("pos"), 0));
        des->size = RSTRING_LEN(str);
    } else if(klass == rb_cString) {
        VALUE args[1] = {src};
        des->src = rb_class_new_instance(1, args, cStringIO);
        des->stream = RSTRING_PTR(src);
        des->pos = 0;
        des->size = RSTRING_LEN(src);
    } else {
        rb_raise(rb_eArgError, "Invalid source type to deserialize from");
    }

    if(des->pos >= des->size) rb_raise(rb_eRangeError, "already at the end of the source");
}
示例#16
0
文件: rb_u_string_index.c 项目: now/u
/* @overload index(pattern, offset = 0)
 *
 *   Returns the minimal index of the receiver where PATTERN matches, equal to or
 *   greater than _i_, where _i_ = OFFSET if OFFSET ≥ 0, _i_ = {#length} -
 *   abs(OFFSET) otherwise, or nil if there is no match.
 *
 *   If PATTERN is a Regexp, the Regexp special variables `$&`, `$'`,
 *   <code>$\`</code>, `$1`, `$2`, …, `$`_n_ are updated accordingly.
 *
 *   If PATTERN responds to #to_str, the matching is performed by byte
 *   comparison.
 *
 *   @param [Regexp, #to_str] pattern
 *   @param [#to_int] offset
 *   @return [Integer, nil]
 *   @see #rindex */
VALUE
rb_u_string_index_m(int argc, VALUE *argv, VALUE self)
{
        VALUE sub, rboffset;
        long offset = 0;
        if (rb_scan_args(argc, argv, "11", &sub, &rboffset) == 2)
                offset = NUM2LONG(rboffset);

        const struct rb_u_string *string = RVAL2USTRING(self);

        const char *begin = rb_u_string_begin_from_offset(string, offset);
        if (begin == NULL) {
                if (TYPE(sub) == T_REGEXP)
                        rb_backref_set(Qnil);

                return Qnil;
        }

        switch (TYPE(sub)) {
        case T_REGEXP:
                offset = rb_u_string_index_regexp(self, begin, sub, false);
                break;
        default: {
                VALUE tmp = rb_check_string_type(sub);
                if (NIL_P(tmp))
                        rb_u_raise(rb_eTypeError, "type mismatch: %s given",
                                   rb_obj_classname(sub));

                sub = tmp;
        }
                /* fall through */
        case T_STRING:
                offset = rb_u_string_index(self, sub, offset);
                break;
        }

        if (offset < 0)
                return Qnil;

        return LONG2NUM(offset);
}
示例#17
0
static double
measure_gc_time()
{
#if defined(HAVE_RB_GC_TOTAL_TIME)
#define MEASURE_GC_TIME_ENABLED Qtrue
    return rb_gc_total_time();

#elif defined(HAVE_RB_GC_TIME)
#define MEASURE_GC_TIME_ENABLED Qtrue
    const double conversion = 1000000.0;
#if HAVE_LONG_LONG
    return NUM2LL(rb_gc_time()) / conversion;
#else
    return NUM2LONG(rb_gc_time()) / conversion;
#endif

#else
#define MEASURE_GC_TIME_ENABLED Qfalse
    return 0.0;
#endif
}
示例#18
0
/*
 *  call-seq:
 *    e.with_index(offset = 0) {|(*args), idx| ... }
 *    e.with_index(offset = 0)
 *
 *  Iterates the given block for each element with an index, which
 *  starts from +offset+.  If no block is given, returns an enumerator.
 *
 */
static VALUE
enumerator_with_index(int argc, VALUE *argv, VALUE obj)
{
    struct enumerator *e;
    VALUE memo;

    rb_scan_args(argc, argv, "01", &memo);
    RETURN_ENUMERATOR(obj, argc, argv);
    memo = NIL_P(memo) ? 0 : (VALUE)NUM2LONG(memo);
    e = enumerator_ptr(obj);
    if (e->args) {
	argc = RARRAY_LENINT(e->args);
	argv = RARRAY_PTR(e->args);
    }
    else {
	argc = 0;
	argv = NULL;
    }
    return rb_block_call(e->obj, e->meth, argc, argv,
			 enumerator_with_index_i, (VALUE)&memo);
}
示例#19
0
文件: itimer.c 项目: rubyworks/carats
void
step_sec_timeval( VALUE secs, struct timeval *t)
{
    switch (TYPE(secs)) {
      case T_FIXNUM:
        t->tv_sec = FIX2LONG(secs), t->tv_usec = 0;
        if (t->tv_sec < 0)
            rb_raise( rb_eArgError, "time interval must be positive");
        break;

      case T_FLOAT:
        if (RFLOAT(secs)->value < 0.0)
            rb_raise( rb_eArgError, "time interval must be positive");
        else {
            double f, d;

            d = modf( RFLOAT(secs)->value, &f);
            t->tv_sec = (time_t) f, t->tv_usec = (time_t) (d*1e6+0.5);
            if (f != t->tv_sec)
                rb_raise( rb_eRangeError, "time interval out of Time range",
                    RFLOAT(secs)->value);
        }
        break;

      case T_BIGNUM:
        t->tv_sec = NUM2LONG(secs), t->tv_usec = 0;
        if (t->tv_sec < 0)
            rb_raise(rb_eArgError, "time interval must be positive");
        break;

      case T_NIL:
        t->tv_sec = 0, t->tv_usec = 0;
        break;

      default:
        rb_raise( rb_eTypeError, "can't convert %s into time interval",
                 rb_obj_classname( secs));
        break;
    }
}
示例#20
0
static void num2timespec(struct timespec *ts, VALUE t)
{
	switch (TYPE(t)) {
	case T_FIXNUM:
	case T_BIGNUM:
		ts->tv_sec = NUM2TIMET(t);
		ts->tv_nsec = 0;
		break;
	case T_FLOAT: {
		double f, d;
		double val = RFLOAT_VALUE(t);

		d = modf(val, &f);
		if (d >= 0) {
			ts->tv_nsec = (long)(d * 1e9 + 0.5);
		} else {
			ts->tv_nsec = (long)(-d * 1e9 + 0.5);
			if (ts->tv_nsec > 0) {
				ts->tv_nsec = 1000000000 - ts->tv_nsec;
				f -= 1;
			}
		}
		ts->tv_sec = (time_t)f;
		if (f != ts->tv_sec)
			rb_raise(rb_eRangeError, "%f out of range", val);
		}
		break;
	default: {
		VALUE f;
		VALUE ary = rb_funcall(t, id_divmod, 1, INT2FIX(1));

		Check_Type(ary, T_ARRAY);

		ts->tv_sec = NUM2TIMET(rb_ary_entry(ary, 0));
		f = rb_ary_entry(ary, 1);
		f = rb_funcall(f, '*', 1, INT2FIX(1000000000));
		ts->tv_nsec = NUM2LONG(f);
		}
	}
}
示例#21
0
VALUE
rb_struct_aset(VALUE s, VALUE idx, VALUE val)
{
    long i;

    if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
	return rb_struct_aset_id(s, rb_to_id(idx), val);
    }

    i = NUM2LONG(idx);
    if (i < 0) i = RSTRUCT_LEN(s) + i;
    if (i < 0) {
        rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
		 i, RSTRUCT_LEN(s));
    }
    if (RSTRUCT_LEN(s) <= i) {
        rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
		 i, RSTRUCT_LEN(s));
    }
    rb_struct_modify(s);
    return RSTRUCT_PTR(s)[i] = val;
}
示例#22
0
static VALUE
lazy_take(VALUE obj, VALUE n)
{
    NODE *memo;
    long len = NUM2LONG(n);
    int argc = 1;
    VALUE argv[3];

    if (len < 0) {
	rb_raise(rb_eArgError, "attempt to take negative size");
    }
    argv[0] = obj;
    if (len == 0) {
	argv[1] = sym_cycle;
	argv[2] = INT2NUM(0);
	argc = 3;
    }
    memo = NEW_MEMO(0, 0, len);
    return lazy_set_method(rb_block_call(rb_cLazy, id_new, argc, argv,
					 lazy_take_func, (VALUE) memo),
			   rb_ary_new3(1, n));
}
示例#23
0
VALUE
rb_str_axe( int argc, VALUE *argv, VALUE str)
{
    VALUE n;
    VALUE ret;
    long newlen, oldlen;

    if (rb_scan_args( argc, argv, "01", &n) == 1 && !NIL_P( n))
        newlen = NUM2LONG( n);
    else
        newlen = 80;
    if (newlen < 0)
        return Qnil;

#ifdef HAVE_HEADER_RUBY_H
    oldlen = RSTRING_LEN( str);
#else
    oldlen = rb_str_strlen( str);
#endif
    if (newlen < oldlen) {
        VALUE ell;
        long e;

        ell = rb_str_new2( "...");
#ifdef HAVE_HEADER_RUBY_H
        e = RSTRING_LEN( ell);
#else
        e = rb_str_strlen( ell);
#endif
        if (newlen > e) {
          ret = rb_str_substr( str, 0, newlen - e);
          rb_str_append( ret, ell);
        } else
            ret = rb_str_substr( str, 0, newlen);
        OBJ_INFECT( ret, str);
    } else
        ret = str;
    return ret;
}
示例#24
0
VALUE
datetime_gregorian__components(int argc, VALUE *argv, VALUE self)
{
	long cyear, cday_number; // those should/will be a 64bit int
	int cmonth, cweek, cdayofyear, cdayofmonth, cdayofweek, cdays, isleap;
	struct tm *tm_now;
	struct timeval tv_now;
	VALUE year, month, week, dayofyear, dayofmonth, dayofweek, ps_number, timezone, language;

	rb_scan_args(argc, argv, "9", &year, &month, &week, &dayofyear, &dayofmonth, &dayofweek, &ps_number, &timezone, &language);
	
	if (gettimeofday(&tv_now, 0) < 0) { // stolen from time.c (ruby18)
		rb_sys_fail("gettimeofday");
	}
  tm_now = gmtime(&(tv_now.tv_sec));


	if (!(NIL_P(year) && NIL_P(month) && NIL_P(week) && NIL_P(dayofyear) && NIL_P(dayofmonth) && NIL_P(dayofweek))) {
		cyear = NIL_P(year) ? (long)tm_now->tm_year+1900 : NUM2LONG(year);

		if (!(NIL_P(month) && NIL_P(dayofmonth))) {
			int cdoy;
			isleap = (((cyear%4==0) && !(cyear%100==0)) || (cyear%400==0));

			cmonth      = NIL_P(month) ? 1 : FIX2INT(month);
			if (1 > cmonth || cmonth > 12) {
				rb_raise(rb_eArgError, "Month out of bounds"); 
			}
			if (cdayofmonth > (isleap ? DAYS_IN_MONTH2[month] : DAYS_IN_MONTH1[month])) {
				rb_raise(rb_eArgError, "Day of month out of bounds");
			}
			cdayofmonth = NIL_P(dayofmonth) ? 1 : FIX2INT(dayofmonth);
			cdays       = cyear*365+(cyear/4)-(cyear/100)+(cyear/400)+(cyear%4!=0)+(cyear%100!=0)+(cyear%400!=0);
			cdoy        = (isleap ? DAYS_UNTIL_MONTH2[month-1] : DAYS_UNTIL_MONTH1[month-1]) + dayofmonth;
			cday_number = cdays+cdoy;
		}
	}
	return rb_funcall(rb_cDatetimeGregorian, id_new, 4, INT2NUM(cday_number), ps_number, timezone, language);
}
示例#25
0
static VALUE
memory_get_array_of_string(int argc, VALUE* argv, VALUE self)
{
    VALUE offset = Qnil, countnum = Qnil, retVal = Qnil;
    AbstractMemory* ptr;
    long off;
    int count;

    rb_scan_args(argc, argv, "11", &offset, &countnum);
    off = NUM2LONG(offset);
    count = (countnum == Qnil ? 0 : NUM2INT(countnum));
    retVal = rb_ary_new2(count);

    Data_Get_Struct(self, AbstractMemory, ptr);
    checkRead(ptr);

    if (countnum != Qnil) {
        int i;

        checkBounds(ptr, off, count * sizeof (char*));
        
        for (i = 0; i < count; ++i) {
            const char* strptr = *((const char**) (ptr->address + off) + i);
            rb_ary_push(retVal, (strptr == NULL ? Qnil : rb_tainted_str_new2(strptr)));
        }

    } else {
        checkBounds(ptr, off, sizeof (char*));
        for ( ; off < ptr->size - (long) sizeof (void *); off += (long) sizeof (void *)) {
            const char* strptr = *(const char**) (ptr->address + off);
            if (strptr == NULL) {
                break;
            }
            rb_ary_push(retVal, rb_tainted_str_new2(strptr));
        }
    }

    return retVal;
}
示例#26
0
文件: recnum.c 项目: tevren/ruby-bdb
static VALUE
bdb_sary_insert(int argc, VALUE *argv, VALUE obj)
{
    long pos;

    if (argc < 2) {
	rb_raise(rb_eArgError, "wrong number of arguments(at least 2)");
    }
    pos = NUM2LONG(argv[0]);
    if (pos == -1) {
	bdb_DB *dbst;

	GetDB(obj, dbst);
 	pos = dbst->len;
    }
    else if (pos < 0) {
	pos++;
    }

    bdb_sary_replace(obj, pos, 0, rb_ary_new4(argc-1, argv+1));
    return obj;
}
static VALUE
spgd_compute_name(VALUE self, VALUE split_rule, VALUE values)
{
    VALUE res = 0;
    int encoding = -1;
    char *result = (char*) xmalloc(INITIAL_CAPA);
    int pos = 0, capa = INITIAL_CAPA;
    long i, rule_len = RARRAY_LEN(split_rule);
    if (!result) {
	rb_memerror();
    }
    for (i = 0; i < rule_len; i++) {
	VALUE rule = rb_ary_entry(split_rule, i);
	if (rb_class_of(rule) == rb_cArray) {
	    long fieldnum = NUM2LONG(rb_ary_entry(rule, 0));
	    VALUE actions = rb_ary_entry(rule, 1);
	    rule = rb_ary_entry(values, fieldnum);
	    encoding = ENCODING_GET(rule);
	    if (RTEST(actions) && RARRAY_LEN(actions)) {
		rule = apply_actions(rule, actions);
	    }
	}
	if (rb_class_of(rule) == rb_cString) {
	    long size = RSTRING_LEN(rule);
	    if (capa < pos + size + 1) {
		char *tmp;
		capa = pos + size + 1;
		if (i + 1 != rule_len) capa = (capa * 3) >> 1;
		tmp = (char*) xrealloc(result, capa);
		if (!tmp) {
		    xfree(result);
		    rb_memerror();
		}
		result = tmp;
	    }
	    if (encoding == -1) encoding = ENCODING_GET(rule);
	    strncpy(result + pos, RSTRING_PTR(rule), size + 1);
	    pos += size;
	}
示例#28
0
OCIDate *oci8_set_ocidate(OCIDate *od, VALUE val)
{
    long year, month, day, hour, minute, second;

    Check_Type(val, T_ARRAY);
    if (RARRAY_LEN(val) != 6) {
        rb_raise(rb_eRuntimeError, "invalid array size %ld", RARRAY_LEN(val));
    }
    /* year */
    year = NUM2LONG(RARRAY_PTR(val)[0]);
    if (year < -4712 || 9999 < year) {
        rb_raise(rb_eRuntimeError, "out of year range: %ld", year);
    }
    od->OCIDateYYYY = (sb2)year;
    /* month */
    month = NUM2LONG(RARRAY_PTR(val)[1]);
    if (month < 0 || 12 < month) {
        rb_raise(rb_eRuntimeError, "out of month range: %ld", month);
    }
    od->OCIDateMM = (ub1)month;
    /* day */
    day = NUM2LONG(RARRAY_PTR(val)[2]);
    if (day < 0 || 31 < day) {
        rb_raise(rb_eRuntimeError, "out of day range: %ld", day);
    }
    od->OCIDateDD = (ub1)day;
    /* hour */
    hour = NUM2LONG(RARRAY_PTR(val)[3]);
    if (hour < 0 || 23 < hour) {
        rb_raise(rb_eRuntimeError, "out of hour range: %ld", hour);
    }
    od->OCIDateTime.OCITimeHH = (ub1)hour;
    /* minute */
    minute = NUM2LONG(RARRAY_PTR(val)[4]);
    if (minute < 0 || 59 < minute) {
        rb_raise(rb_eRuntimeError, "out of minute range: %ld", minute);
    }
    od->OCIDateTime.OCITimeMI = (ub1)minute;
    /* second */
    second = NUM2LONG(RARRAY_PTR(val)[5]);
    if (second < 0 || 59 < second) {
        rb_raise(rb_eRuntimeError, "out of second range: %ld", second);
    }
    od->OCIDateTime.OCITimeSS = (ub1)second;
    return od;
}
示例#29
0
static VALUE
memory_put_string(VALUE self, VALUE offset, VALUE str)
{
    AbstractMemory* ptr = MEMORY(self);
    long off, len;

    Check_Type(str, T_STRING);
    off = NUM2LONG(offset);
    len = RSTRING_LEN(str);

    checkWrite(ptr);
    checkBounds(ptr, off, len + 1);
    
    if (rb_safe_level() >= 1 && OBJ_TAINTED(str)) {
        rb_raise(rb_eSecurityError, "Writing unsafe string to memory");
        return Qnil;
    }

    memcpy(ptr->address + off, RSTRING_PTR(str), len);
    *((char *) ptr->address + off + len) = '\0';

    return self;
}
示例#30
0
/* ClientRestack {{{ */
VALUE
ClientRestack(VALUE self,
  int detail)
{
  VALUE win = Qnil;
  SubMessageData data = { { 0, 0, 0, 0, 0 } };

  /* Check ruby object */
  rb_check_frozen(self);
  GET_ATTR(self, "@win", win);

  subextSubtlextConnect(NULL); ///< Implicit open connection

  /* Send message */
  data.l[0] = 2; ///< Claim to be a pager
  data.l[1] = NUM2LONG(win);
  data.l[2] = detail;

  subSharedMessage(display, DefaultRootWindow(display),
    "_NET_RESTACK_WINDOW", data, 32, True);

  return self;
} /* }}} */