Beispiel #1
0
VALUE
sfcc_numeric_to_str(VALUE v)
{
  VALUE tmp = rb_str_new("", 0);
  char buf[100];
  switch (TYPE(v)) {
    case T_FIXNUM: tmp = rb_fix2str(v, 10); break;
    case T_BIGNUM: tmp = rb_big2str(v, 10); break;
    case T_FLOAT:
      snprintf(buf, 100, "%f", NUM2DBL(v));
      tmp = rb_str_new2(buf);
      break;
  }
  return tmp;
}
Beispiel #2
0
static VALUE rrrd_rrd_first(int argc, VALUE *argv, VALUE self) {
  VALUE index;

  rb_scan_args(argc, argv, "01", &index);

  list arguments = rrrd_list_new();
  rrrd_add_filename_arg(self, &arguments);

  // RRA index
  if (RTEST(index)) {
    index = rb_fix2str(index, 10);
    rrrd_add_arg(&arguments, "--rraindex");
    rrrd_add_arg(&arguments, STR2CSTR(index));
  }

  time_t first = rrd_first(arguments.length, arguments.strings);
  rrrd_free_arguments(arguments);
  rrrd_check_for_errors();

  return rb_time_new(first, 0);
}
Beispiel #3
0
static mustache_value_t* convert_value(
    VALUE rb_value,
    mustache_context_t* m_ctx)
{
    switch (TYPE(rb_value)) {
        case T_OBJECT: return create_mustache_object(m_ctx, rb_value);
        case T_STRUCT: return create_mustache_object(m_ctx, rb_value);
        case T_HASH:   return create_mustache_object(m_ctx, rb_value);
        case T_TRUE:   return create_mustache_value(m_ctx, rb_str_new2("true"));
        case T_FALSE:  return create_mustache_value(m_ctx, Qnil);
        case T_NONE:   return create_mustache_value(m_ctx, Qnil);
        case T_NIL:    return create_mustache_value(m_ctx, Qnil);
        case T_FLOAT:  return create_mustache_value(m_ctx, rb_funcall(rb_value, rb_intern("to_s"), 0, 0));
        case T_STRING: return create_mustache_value(m_ctx, rb_value);
        case T_FIXNUM: return create_mustache_value(m_ctx, rb_fix2str(rb_value, 10));
        case T_BIGNUM: return create_mustache_value(m_ctx, rb_big2str(rb_value, 10));
        case T_ARRAY:  return create_mustache_list(m_ctx, rb_value);
        default:
            if (rb_class_of(rb_value) == rb_cProc) {
                return convert_value(rb_funcall(rb_value, rb_intern("call"), 0, 0), m_ctx);
            }
            return create_mustache_value(m_ctx, rb_any_to_s(rb_value));
    }
}
Beispiel #4
0
VALUE convert_value_to_php_string(VALUE v) {
    switch (TYPE(v)) {
        case T_FALSE:
            return rb_str_new_cstr("false");
        case T_TRUE:
            return rb_str_new_cstr("true");
        case T_UNDEF:
        case T_NIL:
            return rb_str_new_cstr("null");
        case T_FIXNUM:
            return rb_fix2str(v, 10);
        case T_BIGNUM:
            return rb_big2str(v, 10);
        case T_FLOAT:
            return rb_funcall(v, rb_intern("to_s"), 0);
        case T_ARRAY:
            {
                int i;
                VALUE ret = rb_str_new_cstr("array(");
                for(i=0;i<RARRAY_LEN(v);++i) {
                    VALUE p = convert_value_to_php_string(RARRAY_PTR(v)[i]);
                    if (TYPE(p) == T_STRING) {
                        rb_str_cat2(ret, StringValuePtr(p));
                    }
                    if (i != RARRAY_LEN(v)-1) {
                        rb_str_cat2(ret, ",");
                    }
                }
                rb_str_cat2(ret, ")");
                return ret;
            }
        case T_HASH:
            {
                VALUE ret = rb_str_new_cstr("array(");
                VALUE ary = rb_ary_new();
                VALUE *p;
                int i, len;

                rb_hash_foreach(v, hash_to_php_string_array, ary);

                len = RARRAY_LEN(ary);
                p = RARRAY_PTR(ary);
                for(i=0; i<len; ++i) {
                    rb_str_cat2(ret, StringValuePtr(p[i]));
                    if (i != len-1) {
                        rb_str_cat2(ret, ",");
                    }
                }

                rb_str_cat2(ret, ")");
                return ret;
            }
        case T_SYMBOL:
            {
                VALUE symbol_str = rb_sym_to_s(v);
                VALUE ret = rb_str_new_cstr("'");
                rb_str_cat2(ret, StringValuePtr(symbol_str));
                rb_str_cat2(ret, "'");
                return ret;
            }
        case T_STRING:
            {
                VALUE ret = rb_str_new_cstr("'");
                rb_str_cat2(ret, StringValuePtr(v));
                rb_str_cat2(ret, "'");
                return ret;
            }
        default:
            rb_raise(rb_eRuntimeError, "no implemented");
    }
}
Beispiel #5
0
static VALUE rrrd_rrd_fetch(int argc, VALUE *argv, VALUE self) {
  VALUE function, options;
  VALUE resolution, starts_at, ends_at;

  rb_scan_args(argc, argv, "11", &function, &options);

  list arguments = rrrd_list_new();
  rrrd_add_filename_arg(self, &arguments);

  // consolidation function
  function = rb_str_new2(rb_id2name(rb_to_id(function)));
  function = rb_funcall(function, rb_intern("upcase"), 0);
  rrrd_add_arg(&arguments, STR2CSTR(function));

  if (RTEST(options)) {
    // extract options from hash
    resolution = rb_hash_aref(options, ID2SYM(rb_intern("resolution")));
    starts_at = rb_hash_aref(options, ID2SYM(rb_intern("starts_at")));
    ends_at = rb_hash_aref(options, ID2SYM(rb_intern("ends_at")));

    // resolution
    if (RTEST(resolution)) {
      resolution = rb_fix2str(resolution, 10);

      rrrd_add_arg(&arguments, "--resolution");
      rrrd_add_arg(&arguments, STR2CSTR(resolution));
    }

    // start time
    if (RTEST(starts_at)) {
      starts_at = rb_funcall(starts_at, rb_intern("to_i"), 0);
      starts_at = rb_fix2str(starts_at, 10);

      rrrd_add_arg(&arguments, "--start");
      rrrd_add_arg(&arguments, STR2CSTR(starts_at));
    }

    // end time
    if (RTEST(ends_at)) {
      ends_at = rb_funcall(ends_at, rb_intern("to_i"), 0);
      ends_at = rb_fix2str(ends_at, 10);

      rrrd_add_arg(&arguments, "--end");
      rrrd_add_arg(&arguments, STR2CSTR(ends_at));
    }
  }

  time_t start, end;
  unsigned long step, ds_count;
  char **raw_names;
  rrd_value_t *raw_data;

  rrd_fetch(arguments.length, arguments.strings, &start, &end, &step, &ds_count, &raw_names, &raw_data);
  rrrd_free_arguments(arguments);
  rrrd_check_for_errors();

  int i;
  int index = 0;
  VALUE data = rb_ary_new();

  for (i = start + step; i <= end; i+= step) {
    int j;
    VALUE timestamp = rb_time_new(i, 0);
    VALUE values = rb_hash_new();

    for (j = 0; j < ds_count; j++) {
      VALUE key = ID2SYM(rb_intern(raw_names[j]));
      rb_hash_aset(values, key, rb_float_new(raw_data[index++]));
    }

    VALUE args[] = { timestamp, values };
    VALUE tuple = rb_class_new_instance(2, args, Tuple);

    rb_ary_push(data, tuple);
  }

  free(raw_data);

  return data;
}