VALUE set_if_hash(VALUE rb_if_hash, struct ifaddrs *ifa, int family){
    char *if_host, *if_netmask, *if_name;
    if_name = get_if_name(ifa);
    if_host = malloc(sizeof(char) * NI_MAXHOST);
    if (! get_if_host(ifa, if_host))
        rb_raise(rb_eSystemCallError, "Can't get IP from %s", if_name);

    if_netmask = malloc(sizeof(char) * NI_MAXHOST);
    if (! get_if_netmask(ifa, if_netmask))
        rb_raise(rb_eSystemCallError, "Can't get IP from %s", if_name);

    char *str_inet_name = "inet_addr_v4";
    char *str_inet_name6 = "inet_addr_v6";
    char *str_netmask_name = "netmask_v4";
    char *str_netmask_name6 = "netmask_v6";
    VALUE rb_if_data_hash = rb_hash_aref(rb_if_hash, rb_str_intern(rb_str_new2(if_name)));

    if (rb_if_data_hash == Qnil) {
        rb_if_data_hash = rb_hash_new();
        rb_hash_aset(rb_if_hash,
            rb_str_intern(rb_str_new2(if_name)),
            rb_if_data_hash);
    }

    rb_hash_aset(rb_if_data_hash,
        rb_str_intern(rb_str_new2((family == AF_INET) ? str_inet_name : str_inet_name6)),
        rb_str_new2(if_host));
    rb_hash_aset(rb_if_data_hash,
        rb_str_intern(rb_str_new2((family == AF_INET) ? str_netmask_name : str_netmask_name6)),
        rb_str_new2(if_netmask));
    return rb_if_data_hash;
}
Exemplo n.º 2
0
int map_key_callback(void *ctx, const unsigned char *stringVal, size_t stringLen) {
  VALUE key;
#ifdef HAVE_RUBY_ENCODING_H
  rb_encoding *default_internal_enc;
#endif

  if ( ((CTX *)ctx)->symbolizeKeys ) {
#ifdef HAVE_RUBY_ENCODING_H
    ID id = rb_intern3((const char *)stringVal, stringLen, utf8Encoding);
    key = ID2SYM(id);
#else
    VALUE str = rb_str_new((const char *)stringVal, stringLen);
    key = rb_str_intern(str);
#endif
  } else {
    key = rb_str_new((const char *)stringVal, stringLen);
#ifdef HAVE_RUBY_ENCODING_H
    default_internal_enc = rb_default_internal_encoding();
    rb_enc_associate(key, utf8Encoding);
    if (default_internal_enc) {
      key = rb_str_export_to_enc(key, default_internal_enc);
    }
#endif
  }
  set_key(ctx, key);
  return 1;
}
Exemplo n.º 3
0
STATIC VALUE rb_read_object(ramf0_load_context_t* context, bool add_to_ref_cache)
{
  static ID underscore_id = (ID)0;
  if (underscore_id == (ID)0) {
    underscore_id = rb_intern("underscore");
  }
  
  VALUE object = rb_hash_new();
  
  if (add_to_ref_cache) {
    rb_ary_push(context->cache, object);
  }
  
  while (1) {
    VALUE key = rb_read_string(context, 0);
    int8_t type = c_read_int8(context);
    if (type == AMF0_OBJECT_END_MARKER) { break; }
    
    key = rb_funcall(key, underscore_id, 0);
    key = rb_str_intern(key);
    rb_hash_aset(object, key, rb_deserialize(context, type));
  }
  
  return object;
}
Exemplo n.º 4
0
static VALUE
nst_method_missing(int argc, VALUE *argv, VALUE self)
{
    VALUE s, tag, obj;

    if (argc == 2) {
        s = rb_sym_to_s(argv[0]);
        if (RSTRING_PTR(s)[RSTRING_LEN(s)-1] == '=') {
            tag = rb_str_intern(rb_str_new(RSTRING_PTR(s), RSTRING_LEN(s)-1));
            obj = nst_field(self, tag);
            if (RTEST(obj)) {
                rb_funcall(obj, rb_intern("store"), 1, argv[1]);
                return argv[1];
            }
        }
        return rb_call_super(argc,argv);
    }
    if (argc == 1) {
        obj = nst_field(self,argv[0]);
        if (RTEST(obj)) {
            return obj;
        }
    }
    return rb_call_super(argc,argv);
}
Exemplo n.º 5
0
static VALUE
struct_field_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE rbOffset = Qnil, rbName = Qnil, rbType = Qnil;
    StructField* field;
    int nargs;

    Data_Get_Struct(self, StructField, field);

    nargs = rb_scan_args(argc, argv, "3", &rbName, &rbOffset, &rbType);

    if (TYPE(rbName) != T_SYMBOL && TYPE(rbName) != T_STRING) {
        rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol/String)",
                rb_obj_classname(rbName));
    }

    Check_Type(rbOffset, T_FIXNUM);

    if (!rb_obj_is_kind_of(rbType, rbffi_TypeClass)) {
        rb_raise(rb_eTypeError, "wrong argument type %s (expected FFI::Type)",
                rb_obj_classname(rbType));
    }

    field->offset = NUM2UINT(rbOffset);
    field->rbName = (TYPE(rbName) == T_SYMBOL) ? rbName : rb_str_intern(rbName);
    field->rbType = rbType;
    Data_Get_Struct(field->rbType, Type, field->type);
    field->memoryOp = get_memory_op(field->type);

    return self;
}
Exemplo n.º 6
0
static void
hash2ptr_dispparams(VALUE hash, ITypeInfo *pTypeInfo, DISPID dispid, DISPPARAMS *pdispparams)
{
    BSTR *bstrs;
    HRESULT hr;
    UINT len, i;
    VARIANT *pvar;
    VALUE val;
    VALUE key;
    len = 0;
    bstrs = ALLOCA_N(BSTR, pdispparams->cArgs + 1);
    hr = pTypeInfo->lpVtbl->GetNames(pTypeInfo, dispid,
                                     bstrs, pdispparams->cArgs + 1,
                                     &len);
    if (FAILED(hr))
	return;

    for (i = 0; i < len - 1; i++) {
	key = WC2VSTR(bstrs[i + 1]);
        val = rb_hash_aref(hash, INT2FIX(i));
	if (val == Qnil)
	    val = rb_hash_aref(hash, key);
	if (val == Qnil)
	    val = rb_hash_aref(hash, rb_str_intern(key));
        pvar = &pdispparams->rgvarg[pdispparams->cArgs-i-1];
        ole_val2ptr_variant(val, pvar);
    }
}
Exemplo n.º 7
0
static int
rb_crustache__context_get(
	crustache_var *var,
	void *ctx,
	const char *key,
	size_t key_size)
{
	VALUE rb_ctx = (VALUE)ctx;
	VALUE rb_key, rb_val;

	rb_key = rb_str_new(key, (long)key_size);
	rb_val = Qnil;

	if (TYPE(rb_ctx) == T_HASH) {
		rb_val = rb_hash_lookup(rb_ctx, rb_key);
		if (NIL_P(rb_val))
			rb_val = rb_hash_lookup(rb_ctx, rb_str_intern(rb_key));
	} else {
		ID method = rb_to_id(rb_key);

		if (rb_respond_to(rb_ctx, method))
			rb_val = rb_funcall(rb_ctx, method, 0);

		else if (rb_respond_to(rb_ctx, rb_intern("[]")))
			rb_val = rb_funcall(rb_ctx, rb_intern("[]"), 1, key);
	}

	if (NIL_P(rb_val)) /* not found */
		return -1;

	return rb_crustache__setvar(var, rb_val);
}
Exemplo n.º 8
0
static void
hash_set_cstr(ParseInfo pi, const char *key, size_t klen, const char *str, size_t len, const char *orig) {
    Val	parent = stack_peek(&pi->stack);

    if (0 != pi->options.create_id &&
	*pi->options.create_id == *key &&
	pi->options.create_id_len == klen &&
	0 == strncmp(pi->options.create_id, key, klen)) {
	if (str < pi->json || pi->cur < str) {
	    parent->classname = oj_strndup(str, len);
	} else {
	    parent->classname = str;
	}
	parent->clen = len;
    } else {
	volatile VALUE	rstr = rb_str_new(str, len);
	volatile VALUE	rkey = rb_str_new(key, klen);

	rstr = oj_encode(rstr);
	rkey = oj_encode(rkey);
	if (Yes == pi->options.sym_key) {
	    rkey = rb_str_intern(rkey);
	}
	rb_hash_aset(parent->val, rkey, rstr);
    }
}
Exemplo n.º 9
0
/**
 * call-seq:
 *   instance.each_property do |name, value|
 *      ...
 *   end
 *
 * enumerates properties yielding the property name and
 * its value
 *
 */
static VALUE each_property(VALUE self)
{
  CIMCInstance *ptr;
  CIMCStatus status;
  int k=0;
  int num_props=0;
  CIMCString *property_name = NULL;
  CIMCData data;
  Data_Get_Struct(self, CIMCInstance, ptr);

  num_props = ptr->ft->getPropertyCount(ptr, &status);
  if (!status.rc) {
    for (; k < num_props; ++k) {
      data = ptr->ft->getPropertyAt(ptr, k, &property_name, &status);
      if (!status.rc) {
        rb_yield_values(2, (property_name ? rb_str_intern(rb_str_new2(property_name->ft->getCharPtr(property_name, NULL))) : Qnil), sfcc_cimdata_to_value(data));
      }
      else {
        sfcc_rb_raise_if_error(status, "Can't retrieve property #%d", k);
      } 
      if (property_name) CMRelease(property_name);
    }
  }
  else {
    sfcc_rb_raise_if_error(status, "Can't retrieve property count");
  }
  return Qnil;
}
Exemplo n.º 10
0
STATIC VALUE rb_read_hash(ramf0_load_context_t* context)
{
  uint32_t len = c_read_word32_network(context);
  
  VALUE object = Qnil;
  
  VALUE   key = rb_read_string(context, 0);
  int8_t type = c_read_int8(context);
  
  if (type == AMF0_OBJECT_END_MARKER) {
    return rb_ary_new();
  }
  
  // We need to figure out whether this is a real hash, or whether some stupid serializer gave up
  if (rb_str_cmp( rb_big2str(rb_str_to_inum(key, 10, Qfalse),10),  key) == 0) {
    // array
    object = rb_ary_new();
    rb_ary_push(context->cache, object);
    
    rb_ary_store(object, NUM2LONG(rb_str_to_inum(key, 10, Qfalse)), rb_deserialize(context, type));
    while (1) {
      VALUE key = rb_str_to_inum(rb_read_string(context, 0), 10, Qfalse);
      int8_t type = c_read_int8(context);
      if (type == AMF0_OBJECT_END_MARKER) { break; }
      rb_ary_store(object, NUM2LONG(key), rb_deserialize(context, type));
    }
    
    return object;
  } else {
    // hash
    object = rb_hash_new();
    rb_ary_push(context->cache, object);
    
    
    key = rb_str_intern(key);
    rb_hash_aset(object, key, rb_deserialize(context, type));
    while (1) {
      VALUE key = rb_read_string(context, 0);
      int8_t type = c_read_int8(context);
      if (type == AMF0_OBJECT_END_MARKER) { break; }
      key = rb_str_intern(key);
      rb_hash_aset(object, key, rb_deserialize(context, type));
    }
    
    return object;
  }
}
Exemplo n.º 11
0
static VALUE
hash2result(VALUE hash)
{
    VALUE ret = Qnil;
    ret = rb_hash_aref(hash, rb_str_new2("return"));
    if (ret == Qnil)
	ret = rb_hash_aref(hash, rb_str_intern(rb_str_new2("return")));
    return ret;
}
Exemplo n.º 12
0
/*
 *  call-seq:
 *    context.frame_method(frame_position = 0) -> sym
 *
 *  Returns the sym of the method in the frame.
 */
static VALUE
Context_frame_method(int argc, VALUE * argv, VALUE self)
{
  VALUE loc;

  FRAME_SETUP;

  loc = dc_frame_location(context, frame_n);

  return rb_str_intern(rb_funcall(loc, rb_intern("label"), 0));
}
Exemplo n.º 13
0
static void set_state_ivars(VALUE hash, VALUE state)
{
    VALUE ivars = rb_obj_instance_variables(state);
    int i = 0;
    for (i = 0; i < RARRAY_LEN(ivars); i++) {
        VALUE key = rb_funcall(rb_ary_entry(ivars, i), i_to_s, 0);
        long key_len = RSTRING_LEN(key);
        VALUE value = rb_iv_get(state, StringValueCStr(key));
        rb_hash_aset(hash, rb_str_intern(rb_str_substr(key, 1, key_len - 1)), value);
    }
}
Exemplo n.º 14
0
Arquivo: strict.c Projeto: ebenoist/oj
static VALUE
calc_hash_key(ParseInfo pi, Val parent) {
    volatile VALUE	rkey = parent->key_val;

    if (Qundef == rkey) {
	rkey = rb_str_new(parent->key, parent->klen);
    }
    rkey = oj_encode(rkey);
    if (Yes == pi->options.sym_key) {
	rkey = rb_str_intern(rkey);
    }
    return rkey;
}
Exemplo n.º 15
0
Arquivo: app.c Projeto: gensym/shoes
void
shoes_app_style(shoes_app *app, VALUE klass, VALUE hsh)
{
  long i;
  VALUE keys = rb_funcall(hsh, s_keys, 0);
  for ( i = 0; i < RARRAY_LEN(keys); i++ )
  {
    VALUE key = rb_ary_entry(keys, i);
    VALUE val = rb_hash_aref(hsh, key);
    if (!SYMBOL_P(key)) key = rb_str_intern(key);
    shoes_style_set(app->styles, klass, key, val);
  }
}
Exemplo n.º 16
0
static VALUE
data_type_set(VALUE self, VALUE value)
{
    if (TYPE(value) != T_SYMBOL) {
        value = rb_str_intern(rb_String(value));
    }
    if (rb_hash_aref(rb_hTypes, value) == Qnil) {
        rb_raise(rb_eArgError, "invalid data type %s",
                 RSTRING_PTR(rb_inspect(value)));
    }

    rb_ivar_set(self, id_data_type, value);
    return self;
}
Exemplo n.º 17
0
grn_bool
rb_grn_equal_option (VALUE option, const char *key)
{
    VALUE key_string, key_symbol;

    key_string = rb_str_new2(key);
    if (RVAL2CBOOL(rb_funcall(option, rb_intern("=="), 1, key_string)))
        return GRN_TRUE;

    key_symbol = rb_str_intern(key_string);
    if (RVAL2CBOOL(rb_funcall(option, rb_intern("=="), 1, key_symbol)))
        return GRN_TRUE;

    return GRN_FALSE;
}
Exemplo n.º 18
0
/*
 * call-seq: initialize(name, offset, type)
 * @param [String,Symbol] name
 * @param [Fixnum] offset
 * @param [FFI::Type] type
 * @return [self]
 * A new FFI::StructLayout::Field instance.
 */
static VALUE
struct_field_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE rbOffset = Qnil, rbName = Qnil, rbType = Qnil;
    StructField* field;
    int nargs;

    Data_Get_Struct(self, StructField, field);

    nargs = rb_scan_args(argc, argv, "3", &rbName, &rbOffset, &rbType);

    if (TYPE(rbName) != T_SYMBOL && TYPE(rbName) != T_STRING) {
        rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol/String)",
                rb_obj_classname(rbName));
    }

    Check_Type(rbOffset, T_FIXNUM);

    if (!rb_obj_is_kind_of(rbType, rbffi_TypeClass)) {
        rb_raise(rb_eTypeError, "wrong argument type %s (expected FFI::Type)",
                rb_obj_classname(rbType));
    }

    field->offset = NUM2UINT(rbOffset);
    field->rbName = (TYPE(rbName) == T_SYMBOL) ? rbName : rb_str_intern(rbName);
    field->rbType = rbType;
    Data_Get_Struct(field->rbType, Type, field->type);
    field->memoryOp = get_memory_op(field->type);
    field->referenceIndex = -1;

    switch (field->type->nativeType == NATIVE_MAPPED ? ((MappedType *) field->type)->type->nativeType : field->type->nativeType) {
        case NATIVE_FUNCTION:
        case NATIVE_CALLBACK:
        case NATIVE_POINTER:
            field->referenceRequired = true;
            break;

        default:
            field->referenceRequired = (rb_respond_to(self, rb_intern("reference_required?"))
                    && RTEST(rb_funcall2(self, rb_intern("reference_required?"), 0, NULL)))
                    || (rb_respond_to(rbType, rb_intern("reference_required?"))
                        && RTEST(rb_funcall2(rbType, rb_intern("reference_required?"), 0, NULL)));
            break;
    }
    
    return self;
}
Exemplo n.º 19
0
Arquivo: range.c Projeto: DashYang/sim
static VALUE
sym_step_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arg))
{
    VALUE *iter = (VALUE *)arg;

    if (FIXNUM_P(iter[0])) {
	iter[0] -= INT2FIX(1) & ~FIXNUM_FLAG;
    }
    else {
	iter[0] = rb_funcall(iter[0], '-', 1, INT2FIX(1));
    }
    if (iter[0] == INT2FIX(0)) {
	rb_yield(rb_str_intern(i));
	iter[0] = iter[1];
    }
    return Qnil;
}
Exemplo n.º 20
0
Arquivo: struct.c Projeto: 7u83/thrift
static VALUE rb_thrift_union_read(VALUE self, VALUE protocol) {
  // read struct begin
  default_read_struct_begin(protocol);

  VALUE struct_fields = STRUCT_FIELDS(self);

  VALUE field_header = default_read_field_begin(protocol);
  VALUE field_type_value = rb_ary_entry(field_header, 1);
  int field_type = FIX2INT(field_type_value);

  // make sure we got a type we expected
  VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2));

  if (!NIL_P(field_info)) {
    int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym));
    if (field_type == specified_type) {
      // read the value
      VALUE name = rb_hash_aref(field_info, name_sym);
      rb_iv_set(self, "@setfield", rb_str_intern(name));
      rb_iv_set(self, "@value", read_anything(protocol, field_type, field_info));
    } else {
      rb_funcall(protocol, skip_method_id, 1, field_type_value);
    }
  } else {
    rb_funcall(protocol, skip_method_id, 1, field_type_value);
  }

  // read field end
  default_read_field_end(protocol);

  field_header = default_read_field_begin(protocol);
  field_type_value = rb_ary_entry(field_header, 1);
  field_type = FIX2INT(field_type_value);

  if (field_type != TTYPE_STOP) {
    rb_raise(rb_eRuntimeError, "too many fields in union!");
  }

  // read struct end
  default_read_struct_end(protocol);

  // call validate
  rb_funcall(self, validate_method_id, 0);

  return Qnil;
}
Exemplo n.º 21
0
static VALUE
sym_step_i(VALUE i, void *arg)
{
    VALUE *iter = arg;

    if (FIXNUM_P(iter[0])) {
	iter[0] -= INT2FIX(1) & ~FIXNUM_FLAG;
    }
    else {
	iter[0] = rb_funcall(iter[0], '-', 1, INT2FIX(1));
    }
    if (iter[0] == INT2FIX(0)) {
	rb_yield(rb_str_intern(i));
	iter[0] = iter[1];
    }
    return Qnil;
}
Exemplo n.º 22
0
static VALUE bert_read_atom(struct bert_buf *buf)
{
	VALUE rb_atom;
	uint32_t atom_len;

	bert_buf_ensure(buf, 2);
	atom_len = bert_buf_read16(buf);

	/* Instead of trying to build the symbol
	 * from here, just create a Ruby string
	 * and internalize it. this will be faster for
	 * unique symbols */
	bert_buf_ensure(buf, atom_len);
	rb_atom = rb_str_new((char *)buf->data, atom_len);
	buf->data += atom_len;

	return rb_str_intern(rb_atom);
}
Exemplo n.º 23
0
static VALUE rb_tinytds_result_fields(VALUE self) {
  RETCODE dbsqlok_rc, dbresults_rc;
  VALUE fields_processed;
  GET_RESULT_WRAPPER(self);
  dbsqlok_rc = rb_tinytds_result_ok_helper(rwrap->client);
  dbresults_rc = rb_tinytds_result_dbresults_retcode(self);
  fields_processed = rb_ary_entry(rwrap->fields_processed, rwrap->number_of_results);
  if ((dbsqlok_rc == SUCCEED) && (dbresults_rc == SUCCEED) && (fields_processed == Qnil)) {
    /* Default query options. */
    int symbolize_keys = 0;
    VALUE qopts = rb_iv_get(self, "@query_options");
    if (rb_hash_aref(qopts, sym_symbolize_keys) == Qtrue)
       symbolize_keys = 1;
    /* Set number_of_fields count for this result set. */
    rwrap->number_of_fields = dbnumcols(rwrap->client);
    if (rwrap->number_of_fields > 0) {
      /* Create fields for this result set. */
      unsigned int fldi = 0;
      VALUE fields = rb_ary_new2(rwrap->number_of_fields);
      for (fldi = 0; fldi < rwrap->number_of_fields; fldi++) {
        char *colname = dbcolname(rwrap->client, fldi+1);
        VALUE field = symbolize_keys ? rb_str_intern(ENCODED_STR_NEW2(colname))  : rb_obj_freeze(ENCODED_STR_NEW2(colname));
        rb_ary_store(fields, fldi, field);
      }
      /* Store the fields. */
      if (rwrap->number_of_results == 0) {
        rwrap->fields = fields;
      } else if (rwrap->number_of_results == 1) {
        VALUE multi_rs_fields = rb_ary_new();
        rb_ary_store(multi_rs_fields, 0, rwrap->fields);
        rb_ary_store(multi_rs_fields, 1, fields);
        rwrap->fields = multi_rs_fields;
      } else {
        rb_ary_store(rwrap->fields, rwrap->number_of_results, fields);
      }
    }
    rb_ary_store(rwrap->fields_processed, rwrap->number_of_results, Qtrue);
  }
  return rwrap->fields;
}
Exemplo n.º 24
0
static VALUE
rsym_succ(VALUE sym, SEL sel)
{
    return rb_str_intern(rb_str_succ(rb_sym_to_s(sym)));
}
/*
ポリゴン描画
*/
static VALUE drawing_draw_polygon(int argc, VALUE *argv, VALUE self)
{
  VALUE vdst;
  VALUE pairs;
  VALUE mcolor;
  VALUE fill;
  VALUE aa;
  Uint8 alpha;
  Uint32 color;
  int i, vertexes;
  
  rb_scan_args(argc, argv, "32", &vdst, &pairs, &mcolor, &fill, &aa);
  
  // bitmapメソッドを持っていれば、メソッドの値をvdstとする
  VALUE methods = rb_funcall(vdst, rb_intern("methods"), 0);
  if(rb_ary_includes(methods, rb_str_intern(rb_str_new2("to_unit"))) == Qfalse &&
     rb_ary_includes(methods, rb_str_intern(rb_str_new2("bitmap"))) == Qfalse
    )
    rb_raise(eMiyakoError, "this method needs sprite have to_method or bitmap method!");
  if(rb_ary_includes(methods, rb_str_intern(rb_str_new2("to_unit"))) == Qtrue)
    vdst = rb_funcall(vdst, rb_intern("to_unit"), 0);
  vdst = rb_funcall(vdst, rb_intern("bitmap"), 0);

  vertexes = RARRAY_LEN(pairs);
  // 頂点数チェック
  if(vertexes > 65536)
    rb_raise(eMiyakoError, "too many pairs. pairs is less than 65536.");
  
  // 範囲チェック
  for(i=0; i<vertexes; i++)
  {
    VALUE vertex = *(RARRAY_PTR(pairs)+i);
    Sint16 x, y;
    get_position(vertex, &x, &y);
  }
  
	SDL_Surface  *dst = GetSurface(vdst)->surface;

  color = value_2_color(rb_funcall(cColor, rb_intern("to_rgb"), 1, mcolor), dst->format, &alpha);

  if(RTEST(fill) && RTEST(aa) && alpha < 255)
    rb_raise(eMiyakoError, "can't draw filled antialiased alpha polygon");

  Sint16 *px = (Sint16 *)malloc(sizeof(Sint16) * vertexes);
  Sint16 *py = (Sint16 *)malloc(sizeof(Sint16) * vertexes);
  for(i=0; i<vertexes; i++)
  {
    VALUE vertex = *(RARRAY_PTR(pairs)+i);
    get_position(vertex, px+i, py+i);
  }
  
  if(!RTEST(fill) && !RTEST(aa) && alpha == 255)
  {
    for(i=0; i<vertexes-1; i++)
      sge_Line(dst, px[i], py[i], px[i+1], py[i+1], color);
    sge_Line(dst, px[vertexes-1], py[vertexes-1], px[0], py[0], color);
  }
  else if(!RTEST(fill) && !RTEST(aa) && alpha < 255)
  {
    for(i=0; i<vertexes-1; i++)
      sge_LineAlpha(dst, px[i], py[i], px[i+1], py[i+1], color, alpha);
    sge_LineAlpha(dst, px[vertexes-1], py[vertexes-1], px[0], py[0], color, alpha);
  }
  else if(!RTEST(fill) && RTEST(aa) && alpha == 255)
  {
    for(i=0; i<vertexes-1; i++)
      sge_AALine(dst, px[i], py[i], px[i+1], py[i+1], color);
    sge_AALine(dst, px[vertexes-1], py[vertexes-1], px[0], py[0], color);
  }
  else if(!RTEST(fill) && RTEST(aa) && alpha < 255)
  {
    for(i=0; i<vertexes-1; i++)
      sge_AALineAlpha(dst, px[i], py[i], px[i+1], py[i+1], color, alpha);
    sge_AALineAlpha(dst, px[vertexes-1], py[vertexes-1], px[0], py[0], color, alpha);
  }
  else if(RTEST(fill) && !RTEST(aa) && alpha == 255)
    sge_FilledPolygon(dst, (Uint16)vertexes, px, py, color);
  else if(RTEST(fill) && !RTEST(aa) && alpha < 255)
    sge_FilledPolygonAlpha(dst, (Uint16)vertexes, px, py, color, alpha);
  else if(RTEST(fill) && RTEST(aa) && alpha == 255)
    sge_AAFilledPolygon(dst, (Uint16)vertexes, px, py, color);
  
  free(py);
  free(px);

  return Qnil;
}
Exemplo n.º 26
0
void
strb_InitializeSdlFont(void)
{
  if (TTF_Init()) {
    rb_raise_sdl_ttf_error();
  }
  fontFileInfos = ALLOC(FontFileInfo);
  fontFileInfos->rbFontNameSymbol = Qundef;
  fontFileInfos->rbFileNameSymbol = Qundef;
  fontFileInfos->ttcIndex         = -1;
  fontFileInfos->next             = NULL;
  FontFileInfo* currentInfo = fontFileInfos;
  (void)currentInfo;

#ifdef WIN32
  HKEY hKey;
  TCHAR* regPath =
    _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
  if (SUCCEEDED(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regPath, 0,
                             KEY_READ, &hKey))) {
    DWORD fontNameBuffMaxLength;
    DWORD fileNameBuffMaxByteLength;
    RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                    &fontNameBuffMaxLength, &fileNameBuffMaxByteLength,
                    NULL, NULL);
    TCHAR fontNameBuff[fontNameBuffMaxLength + 1];
    BYTE fileNameByteBuff[fileNameBuffMaxByteLength];
    for (DWORD dwIndex = 0; ;dwIndex++) {
      ZeroMemory(fontNameBuff, sizeof(fontNameBuff));
      ZeroMemory(fileNameByteBuff, sizeof(fileNameByteBuff));
      DWORD fontNameBuffLength = sizeof(fontNameBuff) / sizeof(TCHAR);
      DWORD fileNameBuffByteLength = fileNameBuffMaxByteLength;
      LONG result = RegEnumValue(hKey, dwIndex,
                                 fontNameBuff, &fontNameBuffLength,
                                 NULL, NULL,
                                 fileNameByteBuff, &fileNameBuffByteLength);
      TCHAR* fileNameBuff = (TCHAR*)fileNameByteBuff;
      DWORD fileNameBuffLength = _tcslen(fileNameBuff);
      if (result == ERROR_SUCCESS) {
        const TCHAR* ext = &(fileNameBuff[fileNameBuffLength - 3]);
        if (tolower(ext[0]) == _T('t') &&
            tolower(ext[1]) == _T('t') &&
            (tolower(ext[2]) == _T('f') ||
             tolower(ext[2]) == _T('c'))) {
          TCHAR* fontName = fontNameBuff;
          const TCHAR* fileName = fileNameBuff;
          // A TTF font name must end with ' (TrueType)'.
          fontName[fontNameBuffLength - 11] = _T('\0');
          for (int i = fileNameBuffLength - 1; 0 <= i; i--) {
            if (fileName[i] == _T('\\')) {
              fileName += i + 1;
              break;
            }
          }
          int length =
            WideCharToMultiByte(CP_UTF8, 0,
                                fontName, -1,
                                NULL, 0,
                                NULL, NULL);
          char fontNameUTF8[length];
          WideCharToMultiByte(CP_UTF8, 0,
                              fontName, -1,
                              fontNameUTF8, length,
                              NULL, NULL);
          volatile VALUE rbFontName = rb_str_new2(fontNameUTF8);
          length =
            WideCharToMultiByte(CP_ACP, 0,
                                fileName, -1,
                                NULL, 0,
                                NULL, NULL);
          char fileNameANSI[length];
          WideCharToMultiByte(CP_ACP, 0,
                              fileName, -1,
                              fileNameANSI, length,
                              NULL, NULL);
          volatile VALUE rbFileName = rb_str_new2(fileNameANSI);
          if (strchr(StringValueCStr(rbFontName), '&')) {
            volatile VALUE rbArr = rb_str_split(rbFontName, "&");
            const int arrLength = RARRAY_LEN(rbArr);
            int ttcIndex = 0;
            for (int i = 0; i < arrLength; i++) {
              volatile VALUE rbFontName = rb_ary_entry(rbArr, i);
              rb_funcall(rbFontName, rb_intern("strip!"), 0);
              if (0 < RSTRING_LEN(rbFontName)) {
                volatile VALUE rbFontNameSymbol = rb_str_intern(rbFontName);
                volatile VALUE rbFileNameSymbol = rb_str_intern(rbFileName);
                ADD_INFO(currentInfo, rbFontNameSymbol, rbFileNameSymbol,
                         ttcIndex);
                ttcIndex++;
              }
            }
          } else {
            volatile VALUE rbFontNameSymbol = rb_str_intern(rbFontName);
            volatile VALUE rbFileNameSymbol = rb_str_intern(rbFileName);
            ADD_INFO(currentInfo, rbFontNameSymbol, rbFileNameSymbol, -1);
          }
        }
      } else {
        break;
      }
    }
    RegCloseKey(hKey);
  } else {
    rb_raise(strb_GetStarRubyErrorClass(),
             "Win32API error: %d", (int)GetLastError());
  }
  TCHAR szWindowsFontDirPath[MAX_PATH + 1];
  if (FAILED(SHGetFolderPath(NULL, CSIDL_FONTS, NULL,
                             SHGFP_TYPE_CURRENT,
                             szWindowsFontDirPath))) {
    rb_raise(strb_GetStarRubyErrorClass(),
             "Win32API error: %d", (int)GetLastError());
  }
  int length =
    WideCharToMultiByte(CP_UTF8, 0,
                        szWindowsFontDirPath, -1,
                        NULL, 0,
                        NULL, NULL);
  char szWindowsFontDirPathUTF8[length];
  WideCharToMultiByte(CP_UTF8, 0,
                      szWindowsFontDirPath, -1,
                      szWindowsFontDirPathUTF8, length,
                      NULL, NULL);
  volatile VALUE rbWindowsFontDirPath = rb_str_new2(szWindowsFontDirPathUTF8);
  rbWindowsFontDirPathSymbol = rb_str_intern(rbWindowsFontDirPath);
#endif
}
Exemplo n.º 27
0
static VALUE
sym_each_i(VALUE v, void *arg)
{
    rb_yield(rb_str_intern(v));
    return Qnil;
}
Exemplo n.º 28
0
static void
process_arguments_of_image_initialize(int const argc, VALUE* const argv,
	VALUE* buffer_ptr,
	rb_image_file_image_pixel_format_t* pixel_format_ptr,
	long* width_ptr,
	long* height_ptr,
	long* stride_ptr
	)
{
    VALUE params;
    VALUE buffer = Qnil;
    VALUE pixel_format = Qnil;
    VALUE width = Qnil;
    VALUE height = Qnil;
    VALUE stride = Qnil;

    rb_image_file_image_pixel_format_t pf;
    long wd, ht, st;
    long min_len;

    ID id_pixel_format, id_data, id_width, id_height, id_row_stride;
    CONST_ID(id_data,  "data");
    CONST_ID(id_pixel_format,  "pixel_format");
    CONST_ID(id_width,  "width");
    CONST_ID(id_height, "height");
    CONST_ID(id_row_stride,  "row_stride");

    rb_scan_args(argc, argv, "01", &params);
    if (TYPE(params) == T_HASH) {
	buffer = rb_hash_lookup(params, ID2SYM(id_data));
	pixel_format = rb_hash_lookup(params, ID2SYM(id_pixel_format));
	width = rb_hash_lookup(params, ID2SYM(id_width));
	height = rb_hash_lookup(params, ID2SYM(id_height));
	stride = rb_hash_lookup(params, ID2SYM(id_row_stride));
    }

    if (!NIL_P(buffer)) {
	Check_Type(buffer, T_STRING);
	buffer = rb_str_dup(buffer);
    }

    if (NIL_P(pixel_format))
	rb_raise(rb_eArgError, "missing image pixel format");
    if (TYPE(pixel_format) == T_STRING)
	pixel_format = rb_str_intern(pixel_format);
    pf = check_pixel_format(pixel_format);

    if (NIL_P(width))
	rb_raise(rb_eArgError, "missing image width");
    wd = NUM2LONG(width);
    if (wd <= 0)
	rb_raise(rb_eArgError, "zero or negative image width");

    if (NIL_P(height))
	rb_raise(rb_eArgError, "missing image height");
    ht = NUM2LONG(height);
    if (ht <= 0)
	rb_raise(rb_eArgError, "zero or negative image height");

    if (NIL_P(stride)) {
#ifdef HAVE_RB_CAIRO_H
	st = cairo_format_stride_for_width(pixel_format_to_cairo_format(pf), (int)wd) / pixel_format_size(pf);
	stride = INT2NUM(st);
#else
	stride = width;
#endif
    }
    st = NUM2LONG(stride);
    if (st <= 0)
	rb_raise(rb_eArgError, "zero or negative image row-stride");
    else if (st < wd) {
	rb_warning("the given row-stride is less than the given image width.");
	st = wd;
    }

    min_len = minimum_buffer_size(pf, st, ht);
    if (NIL_P(buffer)) {
	buffer = rb_str_new(NULL, min_len);
    }
    else if (RSTRING_LEN(buffer) < min_len) {
	void rb_str_modify_expand(VALUE, long);
	rb_warning("the size of the given data is too short for the given size of image");
	rb_str_modify_expand(buffer, min_len - RSTRING_LEN(buffer));
    }

    *buffer_ptr = buffer;
    *pixel_format_ptr = pf;
    *width_ptr = wd;
    *height_ptr = ht;
    *stride_ptr = st;
}
Exemplo n.º 29
0
VALUE string_spec_rb_str_intern(VALUE self, VALUE str) {
  return rb_str_intern(str);
}
Exemplo n.º 30
0
Arquivo: range.c Projeto: DashYang/sim
static VALUE
sym_each_i(RB_BLOCK_CALL_FUNC_ARGLIST(v, arg))
{
    rb_yield(rb_str_intern(v));
    return Qnil;
}