コード例 #1
0
ファイル: decode.c プロジェクト: kenpratt/bert
VALUE read_complex_type(unsigned char **pData, int arity, VALUE forceToEncoding) {
  VALUE type = read_any_raw(pData, forceToEncoding);
  ID id = SYM2ID(type);
  if(id == rb_intern("nil")) {
    return Qnil;
  } else if(id == rb_intern("true")) {
    return Qtrue;
  } else if(id == rb_intern("false")) {
    return Qfalse;
  } else if(id == rb_intern("time")) {
    VALUE megasecs = read_any_raw(pData, forceToEncoding);
    VALUE msecs = rb_funcall(megasecs, rb_intern("*"), 1, INT2NUM(1000000));
    VALUE secs = read_any_raw(pData, forceToEncoding);
    VALUE microsecs = read_any_raw(pData, forceToEncoding);
    VALUE stamp = rb_funcall(msecs, rb_intern("+"), 1, secs);
    return rb_funcall(rb_cTime, rb_intern("at"), 2, stamp, microsecs);
  } else if(id == rb_intern("regex")) {
    VALUE source = read_any_raw(pData, forceToEncoding);
    VALUE opts = read_any_raw(pData, forceToEncoding);
    int flags = 0;
    if(rb_ary_includes(opts, ID2SYM(rb_intern("caseless"))))
      flags = flags | 1;
    if(rb_ary_includes(opts, ID2SYM(rb_intern("extended"))))
      flags = flags | 2;
    if(rb_ary_includes(opts, ID2SYM(rb_intern("multiline"))))
      flags = flags | 4;
    return rb_funcall(rb_cRegexp, rb_intern("new"), 2, source, INT2NUM(flags));
  } else if(id == rb_intern("dict")) {
    return read_dict(pData, forceToEncoding);
  } else {
    return Qnil;
  }
}
コード例 #2
0
ファイル: fw.c プロジェクト: michelboaventura/rgraph
static VALUE fw(VALUE arr1, VALUE path) {
  int i, j, k;
  float ij, ik, kj, new_distance;
  int size;

  size = RARRAY_LEN(arr1);

  for(k = 0; k < size; k++) {
    for (i = 0; i < size; i++){
      for (j = 0; j < size; j++) {
        ij = NUM2DBL(rb_ary_entry(rb_ary_entry(arr1, i), j));
        ik = NUM2DBL(rb_ary_entry(rb_ary_entry(arr1, i), k));
        kj = NUM2DBL(rb_ary_entry(rb_ary_entry(arr1, k), j));
        new_distance = ik + kj;
        if(ij > new_distance) {
          rb_ary_store(rb_ary_entry(arr1, i), j, rb_float_new(new_distance));
          rb_ary_store(rb_ary_entry(path, i), j, rb_ary_new3(1, rb_float_new(k)));
        }
        if((ij == new_distance) && !RTEST(rb_ary_includes(rb_ary_entry(rb_ary_entry(path, i), j), rb_float_new(k)))&&  !(i == k || j == k)) {
          rb_ary_push(rb_ary_entry(rb_ary_entry(path, i), j), rb_float_new(k));
        }
      }
    }
  }
  return arr1;
}
コード例 #3
0
ファイル: cups.c プロジェクト: butzopower/cups
/*
* call-seq:
*   PrintJob.new(filename, printer=nil)
*
* Initializes a new PrintJob object. If no target printer/class is specified, the default is chosen.
* Note the specified file does not have to exist until print is called.
*/
static VALUE job_init(int argc, VALUE* argv, VALUE self)
{
    VALUE filename, printer;

    rb_scan_args(argc, argv, "11", &filename, &printer);

    rb_iv_set(self, "@filename", filename);

    if (NIL_P(printer)) {

        // Fall back to default printer
        VALUE def_p = rb_funcall(rubyCups, rb_intern("default_printer"), 0);
        rb_iv_set(self, "@printer", def_p);

    } else {
        // First call Cups#show_destinations
        VALUE dest_list = rb_funcall(rubyCups, rb_intern("show_destinations"), 0);
        // Then check the printer arg is included in the returned array...
        if (rb_ary_includes(dest_list, printer)) {
            rb_iv_set(self, "@printer", printer);
        } else {
            rb_raise(rb_eRuntimeError, "The printer or destination doesn't exist!");
        }
    }
    return self;
}
コード例 #4
0
ファイル: db.c プロジェクト: basecamp/ruby-rpm
VALUE
rpm_transaction_available(VALUE trans, VALUE pkg, VALUE key)
{
	VALUE keys;

	if (rb_obj_is_kind_of(pkg, rpm_cPackage) == Qfalse ||
            TYPE(key) != T_STRING) {
		rb_raise(rb_eTypeError, "illegal argument type");
	}

	keys = rb_ivar_get(trans, id_keys);
	if (NIL_P(keys)) {
		keys = rb_ary_new();
		rb_ivar_set(trans, id_keys, keys);
	}
	if (rb_ary_includes(keys, key) == Qtrue) {
		rb_raise(rb_eArgError, "key must be unique");
	}
	rb_ary_push(keys, key);

#if RPM_VERSION_CODE < RPM_VERSION(4,1,0)
	rpmtransAvailablePackage(RPM_TRANSACTION(trans), RPM_HEADER(pkg),
					   RSTRING_PTR(key));
#else
    rb_raise(rb_eNotImpError, "need rpmtsAvailablePackage");
    // FIXME: What is the analog for rpmtsAvailablePackage
    // in newer RPM's ?
    //rpmtsAvailablePackage(RPM_TRANSACTION(trans), RPM_HEADER(pkg),
    //                      RSTRING(key)->ptr);
#endif

	return Qnil;
}
コード例 #5
0
ファイル: db.c プロジェクト: basecamp/ruby-rpm
/*
 * Add a upgrade operation to the transaction
 * @param [Package] pkg Package to upgrade
 */
VALUE
rpm_transaction_upgrade(VALUE trans, VALUE pkg, VALUE key)
{
	VALUE keys;
	if (rb_obj_is_kind_of(pkg, rpm_cPackage) == Qfalse ||
            TYPE(key) != T_STRING) {
		rb_raise(rb_eTypeError, "illegal argument type");
	}

	keys = rb_ivar_get(trans, id_keys);

	if (NIL_P(keys)) {
		keys = rb_ary_new();
		rb_ivar_set(trans, id_keys, keys);
	}
	if (rb_ary_includes(keys, key) == Qtrue) {
		rb_raise(rb_eArgError, "key must be unique");
	}
	rb_ary_push(keys, key);
#if RPM_VERSION_CODE < RPM_VERSION(4,1,0)
	rpmtransAddPackage(RPM_TRANSACTION(trans), RPM_HEADER(pkg), NULL,
					   RSTRING_PTR(key), 1, NULL);
#else
	rpmtsAddInstallElement(RPM_TRANSACTION(trans), RPM_HEADER(pkg),
					   RSTRING_PTR(key), 1, NULL);
#endif

	return Qnil;
}
コード例 #6
0
ファイル: miyako_layout.c プロジェクト: cyross/ruby-miyako
static VALUE layout_add_snap_child(VALUE self, VALUE spr)
{
  VALUE layout   = get_layout(self);
  VALUE snap     = *(RSTRUCT_PTR(layout)+3);
  VALUE children = *(RSTRUCT_PTR(snap)+1);
  if(rb_ary_includes(children, spr)==Qfalse){ rb_ary_push(children, spr); }
  return self;
}
コード例 #7
0
ファイル: selector.c プロジェクト: zanaka/Grama
/* Create a new selector. This is more or less the pure Ruby version
   translated into an MRI cext */
static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self)
{
    ID backend_id;
    VALUE backend;
    VALUE lock;

    struct NIO_Selector *selector;
    unsigned int flags = 0;

    Data_Get_Struct(self, struct NIO_Selector, selector);

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

    if(backend != Qnil) {
        if(!rb_ary_includes(NIO_Selector_supported_backends(CLASS_OF(self)), backend)) {
            rb_raise(rb_eArgError, "unsupported backend: %s",
                RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0)));
        }

        backend_id = SYM2ID(backend);

        if(backend_id == rb_intern("epoll")) {
            flags = EVBACKEND_EPOLL;
        } else if(backend_id == rb_intern("poll")) {
            flags = EVBACKEND_POLL;
        } else if(backend_id == rb_intern("kqueue")) {
            flags = EVBACKEND_KQUEUE;
        } else if(backend_id == rb_intern("select")) {
            flags = EVBACKEND_SELECT;
        } else if(backend_id == rb_intern("port")) {
            flags = EVBACKEND_PORT;
        } else {
            rb_raise(rb_eArgError, "unsupported backend: %s",
                RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0)));
        }
    }

    /* Ensure the selector loop has not yet been initialized */
    assert(!selector->ev_loop);

    selector->ev_loop = ev_loop_new(flags);
    if(!selector->ev_loop) {
        rb_raise(rb_eIOError, "error initializing event loop");
    }

    ev_io_start(selector->ev_loop, &selector->wakeup);

    rb_ivar_set(self, rb_intern("selectables"), rb_hash_new());
    rb_ivar_set(self, rb_intern("lock_holder"), Qnil);

    lock = rb_class_new_instance(0, 0, rb_const_get(rb_cObject, rb_intern("Mutex")));
    rb_ivar_set(self, rb_intern("lock"), lock);
    rb_ivar_set(self, rb_intern("lock_holder"), Qnil);

    return Qnil;
}
コード例 #8
0
VALUE cIGraph_include(VALUE self, VALUE v){

  VALUE v_ary;
  igraph_t *igraph;

  Data_Get_Struct(self, igraph_t, igraph);
  v_ary = ((VALUE*)igraph->attr)[0];

  return rb_ary_includes(v_ary,v);
}
コード例 #9
0
ファイル: env.c プロジェクト: Hunter-Dolan/MacRuby
static int
env_replace_i(VALUE key, VALUE val, VALUE keys)
{
    if (key != Qundef) {
	env_aset(Qnil, 0, key, val);
	if (rb_ary_includes(keys, key)) {
	    rb_ary_delete(keys, key);
	}
    }
    return ST_CONTINUE;
}
コード例 #10
0
ファイル: TimerEvent.cpp プロジェクト: ArekX/RAGE
		void TimerEvent::unregister_callback(VALUE proc)
		{
			RAGE_CHECK_DISPOSED(disposed);

			if (TYPE(rb_ary_includes(timer_observer, proc)) == T_TRUE) 
			{ 
				if (rb_class_of(proc) != rb_cProc) 
					rb_raise(rb_eTypeError, RAGE_RB_PROC_ERROR); 
				else
					rb_ary_delete(timer_observer, proc);
			}
		}
コード例 #11
0
CerializeType Rcerialize_storageTypeForRubyClass( VALUE rb_class )  {

  CerializeType  c_type  =  CerializeType_Raw;
  
  if (      rb_class == rb_cFile )  {
    c_type  =  CerializeType_FileContents;  
  }
  else if ( rb_class == rb_cSymbol )  {
    c_type  =  CerializeType_Symbol;
  }
  else if ( rb_class == rb_cRegexp )  {
    c_type  =  CerializeType_Regexp;
  }
  else if ( rb_class == rb_cClass )  {
    c_type  =  CerializeType_ClassName;
  }
  else if ( rb_class == rb_cString )  {
    c_type  =  CerializeType_String;
  }
  else if ( rb_class == rb_cInteger )  {
    c_type  =  CerializeType_Integer;
  }
  else if ( rb_class == rb_cFloat )  {
    c_type  =  CerializeType_Float;
  }
  else if ( rb_class == rb_cComplex )  {
    c_type  =  CerializeType_Complex;
  }
  else if ( rb_class == rb_cRational )  {
    c_type  =  CerializeType_Rational;
  }
  else if (  rb_class == rb_cTrueClass
        ||  rb_class == rb_cFalseClass )  {
    c_type  =  CerializeType_TrueFalse;
  }
  else if ( rb_class == rb_cArray )  {
    c_type  =  CerializeType_Array;
  }
  else if ( rb_class == rb_cHash )  {
    c_type  =  CerializeType_Hash;
  }
  else if (    rb_class == rb_cStruct
          ||  rb_ary_includes(  rb_mod_ancestors( rb_class ),
                                rb_cStruct ) )  {
    c_type  =  CerializeType_Struct;
  }
  else {
    rb_raise( rb_eArgError, "Provided data was invalid." );    
  }

  return c_type;
}
コード例 #12
0
ファイル: load.c プロジェクト: Sophrinix/MacRuby
static bool
path_ok(const char *path, VALUE *out)
{
    struct stat s;
    if (stat(path, &s) == 0 && S_ISREG(s.st_mode)) {
	VALUE found_path = rb_str_new2(path);
	VALUE features = get_loaded_features();
	*out = rb_ary_includes(features, found_path) == Qtrue
	    ? 0 : found_path;
	return true;
    }
    return false;
}
コード例 #13
0
ファイル: class.c プロジェクト: MSch/MacRuby
VALUE
rb_mod_include_p(VALUE mod, SEL sel, VALUE mod2)
{
    return rb_ary_includes(rb_mod_included_modules(mod), mod2);
}
コード例 #14
0
ファイル: class.c プロジェクト: MSch/MacRuby
void
rb_include_module2(VALUE klass, VALUE orig_klass, VALUE module, bool check,
	bool add_methods)
{
    if (check) {
	rb_frozen_class_p(klass);
	if (!OBJ_TAINTED(klass)) {
	    rb_secure(4);
	}
	Check_Type(module, T_MODULE);
    }

    // Register the module as included in the class.
    VALUE ary = rb_attr_get(klass, idIncludedModules);
    if (ary == Qnil) {
	ary = rb_ary_new();
	rb_ivar_set(klass, idIncludedModules, ary);
    }
    else {
	if (rb_ary_includes(ary, module)) {
	    return;
	}
    }
    rb_ary_insert(ary, 0, module);

    // Mark the module as included somewhere.
    const long v = RCLASS_VERSION(module) | RCLASS_IS_INCLUDED;
    RCLASS_SET_VERSION(module, v);

    // Register the class as included in the module.
    ary = rb_attr_get(module, idIncludedInClasses);
    if (ary == Qnil) {
	ary = rb_ary_new();
	rb_ivar_set(module, idIncludedInClasses, ary);
    }
    rb_ary_push(ary, klass);

    // Delete the ancestors array if it exists, since we just changed it.
    CFMutableDictionaryRef iv_dict = rb_class_ivar_dict(klass);
    if (iv_dict != NULL) {
	CFDictionaryRemoveValue(iv_dict, (const void *)idAncestors);
    }

    if (add_methods) {
	// Copy methods. If original class has the basic -initialize and if the
	// module has a customized -initialize, we must copy the customized
	// version to the original class too.
	rb_vm_copy_methods((Class)module, (Class)klass);

	// When including into the class Class, also copy the methods to the
	// singleton class of NSObject.
	if (klass == rb_cClass || klass == rb_cModule) {
            rb_vm_copy_methods((Class)module, *(Class *)rb_cNSObject);
	}

	if (orig_klass != 0 && orig_klass != klass) {
	    Method m = class_getInstanceMethod((Class)orig_klass,
		    selInitialize);
	    Method m2 = class_getInstanceMethod((Class)klass, selInitialize);
	    if (m != NULL && m2 != NULL
		&& method_getImplementation(m) == (IMP)rb_objc_init
		&& method_getImplementation(m2) != (IMP)rb_objc_init) {
		rb_vm_copy_method((Class)orig_klass, m2);
	    }
	}
    }
}
コード例 #15
0
ファイル: array_spec.c プロジェクト: DanielVartanov/rubinius
static VALUE array_spec_rb_ary_includes(VALUE self, VALUE ary, VALUE item) {
  return rb_ary_includes(ary, item);
}
コード例 #16
0
ファイル: cups.c プロジェクト: bigtunacan/cups
int printer_exists(VALUE printer){
  // First call Cups#show_destinations
  VALUE dest_list = rb_funcall(rubyCups, rb_intern("show_destinations"), 0);
  // Then check the printer arg is included in the returned array...
  return rb_ary_includes(dest_list, printer) ? 1 : 0;
}
コード例 #17
0
ファイル: cIGraph.c プロジェクト: RogerPodacter/igraph
VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self){

  igraph_t *graph;
  igraph_vector_t edge_v;
  VALUE vertex;
  VALUE directed;
  VALUE edges;
  VALUE attrs;
  VALUE v_ary;
  int vertex_n = 0;
  int current_vertex_id;
  int i;

  igraph_vector_ptr_t vertex_attr;
  igraph_vector_ptr_t edge_attr;

  igraph_i_attribute_record_t v_attr_rec;
  v_attr_rec.name  = "__RUBY__";
  v_attr_rec.type  = IGRAPH_ATTRIBUTE_PY_OBJECT;
  v_attr_rec.value = (void*)rb_ary_new();

  igraph_i_attribute_record_t e_attr_rec;
  e_attr_rec.name  = "__RUBY__";
  e_attr_rec.type  = IGRAPH_ATTRIBUTE_PY_OBJECT;
  e_attr_rec.value = (void*)rb_ary_new();

  rb_scan_args(argc,argv,"12", &edges, &directed, &attrs);

  //Initialize edge vector
  IGRAPH_FINALLY(igraph_vector_destroy,&edge_v);
  IGRAPH_FINALLY(igraph_vector_ptr_destroy,&vertex_attr);
  IGRAPH_FINALLY(igraph_vector_ptr_destroy,&edge_attr);

  IGRAPH_CHECK(igraph_vector_init_int(&edge_v,0));

  IGRAPH_CHECK(igraph_vector_ptr_init(&vertex_attr,0));
  IGRAPH_CHECK(igraph_vector_ptr_init(&edge_attr,0));

  Data_Get_Struct(self, igraph_t, graph);

  v_ary = rb_ary_new();

  if(!directed)
    IGRAPH_CHECK(igraph_to_undirected(graph,IGRAPH_TO_UNDIRECTED_COLLAPSE));

  //Loop through objects in edge Array
  for (i=0; i<RARRAY_LEN(edges); i++) {
    vertex = RARRAY_PTR(edges)[i];
    if(rb_ary_includes(v_ary,vertex)){
      //If @vertices includes this vertex then look up the vertex number
      current_vertex_id = NUM2INT(rb_funcall(v_ary,rb_intern("index"),1,vertex));
    } else {
      //Otherwise add to the list of vertices
      rb_ary_push(v_ary,vertex);
      current_vertex_id = vertex_n;
      vertex_n++;
      
      //Add object to list of vertex attributes
      rb_ary_push((VALUE)v_attr_rec.value,vertex);
      
    }
    IGRAPH_CHECK(igraph_vector_push_back(&edge_v,current_vertex_id));
    if (i % 2){
      if (attrs != Qnil){
	rb_ary_push((VALUE)e_attr_rec.value,RARRAY_PTR(attrs)[i/2]);
      } else {
	rb_ary_push((VALUE)e_attr_rec.value,Qnil);
      }
    }
  }

  IGRAPH_CHECK(igraph_vector_ptr_push_back(&vertex_attr, &v_attr_rec));
  IGRAPH_CHECK(igraph_vector_ptr_push_back(&edge_attr,   &e_attr_rec));

  if(igraph_vector_size(&edge_v) > 0){
    IGRAPH_CHECK(igraph_add_vertices(graph,vertex_n,&vertex_attr));
    IGRAPH_CHECK(igraph_add_edges(graph,&edge_v,&edge_attr));
  }

  igraph_vector_destroy(&edge_v);
  igraph_vector_ptr_destroy(&vertex_attr);
  igraph_vector_ptr_destroy(&edge_attr);

  IGRAPH_FINALLY_CLEAN(3);

  return self;

}
コード例 #18
0
ファイル: rcsv.c プロジェクト: fiksu/rcsv
/* This procedure is called for every parsed field */
void end_of_field_callback(void * field, size_t field_size, void * data) {
  const char * field_str = (char *)field;
  struct rcsv_metadata * meta = (struct rcsv_metadata *) data;
  char row_conversion = 0;
  VALUE parsed_field;

  /* No need to parse anything until the end of the line if skip_current_row is set */
  if (meta->skip_current_row) {
    return;
  }

  /* Skip the row if its position is less than specifed offset */
  if (meta->current_row < meta->offset_rows) {
    meta->skip_current_row = true;
    return;
  }

  /* Get row conversion char specifier */
  if (meta->current_col < meta->num_row_conversions) {
    row_conversion = (char)meta->row_conversions[meta->current_col];
  }

  /* Convert the field from string into Ruby type specified by row_conversion */
  if (row_conversion != ' ') { /* spacebar skips the column */
    if (field_size == 0) {
      /* Assigning appropriate default value if applicable. */
      if (meta->current_col < meta->num_row_defaults) {
        parsed_field = meta->row_defaults[meta->current_col];
      } else { /* It depends on empty_field_is_nil if we convert empty strings to nils */
        if (meta->empty_field_is_nil || field_str == NULL) {
          parsed_field = Qnil;
        } else {
          parsed_field = ENCODED_STR_NEW("", 0, meta->encoding_index);
        }
      }
    } else {
      if (meta->current_col < meta->num_row_conversions) {
        switch (row_conversion){
          case 's': /* String */
            parsed_field = ENCODED_STR_NEW(field_str, field_size, meta->encoding_index);
            break;
          case 'i': /* Integer */
            parsed_field = LL2NUM(atoll(field_str));
            break;
          case 'f': /* Float */
            parsed_field = rb_float_new(atof(field_str));
            break;
          case 'b': /* TrueClass/FalseClass */
            switch (field_str[0]) {
              case 't':
              case 'T':
              case '1':
                parsed_field = Qtrue;
                break;
              case 'f':
              case 'F':
              case '0':
                parsed_field = Qfalse;
                break;
              default:
                RAISE_WITH_LOCATION(
                  meta->current_row,
                  meta->current_col,
                  field_str,
                  "Bad Boolean value. Valid values are strings where the first character is T/t/1 for true or F/f/0 for false."
                );
            }
            break;
          default:
            RAISE_WITH_LOCATION(
              meta->current_row,
              meta->current_col,
              field_str,
              "Unknown deserializer '%c'.",
              row_conversion
            );
        }
      } else { /* No conversion happens */
        parsed_field = ENCODED_STR_NEW(field_str, field_size, meta->encoding_index); /* field */
      }
    }

    /* Filter by row values listed in meta->only_rows */
    if ((meta->only_rows != NULL) &&
        (meta->current_col < meta->num_only_rows) &&
        (meta->only_rows[meta->current_col] != Qnil) &&
        (!rb_ary_includes(meta->only_rows[meta->current_col], parsed_field))) {
      meta->skip_current_row = true;
      return;
    }

    /* Filter out by row values listed in meta->except_rows */
    if ((meta->except_rows != NULL) &&
        (meta->current_col < meta->num_except_rows) &&
        (meta->except_rows[meta->current_col] != Qnil) &&
        (rb_ary_includes(meta->except_rows[meta->current_col], parsed_field))) {
      meta->skip_current_row = true;
      return;
    }

    /* Assign the value to appropriate hash key if parsing into Hash */
    if (meta->row_as_hash) {
      if (meta->current_col >= meta->num_columns) {
        RAISE_WITH_LOCATION(
          meta->current_row,
          meta->current_col,
          field_str,
          "There are at least %d columns in a row, which is beyond the number of provided column names (%d).",
          (int)meta->current_col + 1,
          (int)meta->num_columns
        );
      } else {
        rb_hash_aset(meta->last_entry, meta->column_names[meta->current_col], parsed_field); /* last_entry[column_names[current_col]] = field */
      }
    } else { /* Parse into Array */
      rb_ary_push(meta->last_entry, parsed_field); /* last_entry << field */
    }
  }

  /* Increment column counter */
  meta->current_col++;
  return;
}
コード例 #19
0
ファイル: js_land_proxy.c プロジェクト: aasmith/johnson
static bool global_p(char* name)
{
  return *name == '$' && rb_ary_includes(rb_f_global_variables(), rb_str_new2(name));
}
コード例 #20
0
ファイル: class.c プロジェクト: 1nueve/MacRuby
VALUE
rb_mod_include_p(VALUE mod, SEL sel, VALUE mod2)
{
    Check_Type(mod2, T_MODULE);
    return rb_ary_includes(rb_mod_included_modules(mod), mod2);
}
コード例 #21
0
/*
ポリゴン描画
*/
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;
}
コード例 #22
0
ファイル: decode.c プロジェクト: HumbleRepose/bert
static VALUE bert_read_complex(struct bert_buf *buf, uint32_t arity)
{
	VALUE rb_type;
	ID id_type;

	rb_type = bert_read(buf);
	Check_Type(rb_type, T_SYMBOL);

	id_type = SYM2ID(rb_type);

	if (id_type == rb_intern("nil")) {
		bert_ensure_arity(arity, 2);
		return Qnil;

	} else if (id_type == rb_intern("true")) {
		bert_ensure_arity(arity, 2);
		return Qtrue;

	} else if (id_type == rb_intern("false")) {
		bert_ensure_arity(arity, 2);
		return Qfalse;

	} else if (id_type == rb_intern("time")) {
		VALUE rb_megasecs, rb_secs, rb_microsecs, rb_stamp, rb_msecs;

		bert_ensure_arity(arity, 5);

		rb_megasecs = bert_read(buf);
		rb_secs = bert_read(buf);
		rb_microsecs = bert_read(buf);

		rb_msecs = rb_funcall(rb_megasecs, rb_intern("*"), 1, INT2NUM(1000000));
		rb_stamp = rb_funcall(rb_msecs, rb_intern("+"), 1, rb_secs);

		return rb_funcall(rb_cTime, rb_intern("at"), 2, rb_stamp, rb_microsecs);

	} else if (id_type == rb_intern("regex")) {
		VALUE rb_source, rb_opts;
		int flags = 0;
		
		bert_ensure_arity(arity, 4);

		rb_source = bert_read(buf);
		rb_opts = bert_read(buf);

		Check_Type(rb_source, T_STRING);
		Check_Type(rb_opts, T_ARRAY);

		if (rb_ary_includes(rb_opts, ID2SYM(rb_intern("caseless"))))
			flags = flags | 1;

		if (rb_ary_includes(rb_opts, ID2SYM(rb_intern("extended"))))
			flags = flags | 2;

		if (rb_ary_includes(rb_opts, ID2SYM(rb_intern("multiline"))))
			flags = flags | 4;

		return rb_funcall(rb_cRegexp, rb_intern("new"), 2, rb_source, INT2NUM(flags));

	} else if (id_type == rb_intern("dict")) {
		bert_ensure_arity(arity, 3);
		return bert_read_dict(buf);
	}

	rb_raise(rb_eTypeError, "Invalid tag for complex value");
	return Qnil;
}
コード例 #23
0
ファイル: rb_Kernel.c プロジェクト: stephenprater/sender
VALUE rb_RPRuby_Sender_Kernel_internal_backtraceHashForControlFrame(	rb_control_frame_t**		c_current_frame )	{

	const char*		c_method_name		=	NULL;
	int				c_sourcefile_line	=	0;
	
	//	create new hash for this frame
	VALUE	rb_frame_hash			=	rb_hash_new();
		
	VALUE	rb_sourcefile_name		=	Qnil;
	VALUE	rb_sourcefile_line		=	Qnil;
	VALUE	rb_method_name			=	Qnil;
	VALUE	rb_object_for_frame		=	Qnil;
	
	if ( ( *c_current_frame )->iseq != 0 ) {
		
		if ( ( *c_current_frame )->pc != 0 ) {
			
			rb_iseq_t *iseq			= ( *c_current_frame )->iseq;
			
			//	get sourcefile name and set in hash
			rb_sourcefile_name		=	iseq->filename;
			
			//	get sourcefile line and set in hash
			c_sourcefile_line		=	rb_vm_get_sourceline( *c_current_frame );
			rb_sourcefile_line		=	INT2FIX( c_sourcefile_line );
			
			//	get name of instruction sequence
			rb_method_name			=	ID2SYM( rb_intern( StringValuePtr( iseq->name ) ) );				
			rb_object_for_frame	=	( *c_current_frame )->self;
		}
	}
	else if ( RUBYVM_CFUNC_FRAME_P( *c_current_frame ) ) {
		
		//	get name of method

		#if RUBY_PATCHLEVEL >= -1
		//	For 1.9.2:
		const rb_method_entry_t*	c_method_for_frame	=	( *c_current_frame )->me;
		c_method_name				=	rb_id2name( c_method_for_frame->called_id );
		#else
		//	For 1.9.1:
		c_method_name				=	rb_id2name( ( *c_current_frame )->method_id );
		#endif
		
		rb_method_name				=	( c_method_name == NULL ? Qnil : ID2SYM( rb_intern( c_method_name ) ) );
		rb_object_for_frame	=	( *c_current_frame )->self;		
	}
	//	we have to test this case - it works for blocks but there may be other cases too
	else if (	( *c_current_frame )->block_iseq != 0
			 &&	( *c_current_frame )->pc == 0)	{
	
		//	If we got here we have a fiber
		//	There doesn't seem to be much that we can tell about a fiber's context
			
		VALUE			rb_current_fiber	=	rb_fiber_current();
		rb_fiber_t*		c_current_fiber		=	NULL;

		GetFiberPtr(	rb_current_fiber, 
						c_current_fiber);
						
		rb_context_t*	c_context			=	& c_current_fiber->cont;
		
//		rb_block_t*	c_blockptr = RUBY_VM_GET_BLOCK_PTR_IN_CFP( *c_current_frame );
		 
		rb_object_for_frame	=	( *c_current_frame )->self;
		
		//	get sourcefile name and set in hash
		rb_sourcefile_name		=	Qnil;

		//	get sourcefile line and set in hash
		rb_sourcefile_line		=	Qnil;
		
		//	get name of instruction sequence
		rb_method_name			=	rb_str_new2( "<Fiber>" );		
		
		//	if we have a fiber we also include its ruby reference since we have so little other context
		rb_hash_aset(	rb_frame_hash,
						ID2SYM( rb_intern( "fiber" ) ),
						c_context->self );
		
		//	The one time that we know a fiber is in use in the Ruby base is with Enumerators
		//	For now we will handle that with a special case
		
		VALUE	rb_enumerator_class	=	rb_const_get(	rb_cObject,
														rb_intern( "Enumerator" ) );
		
		VALUE	rb_object_for_frame_klass	=	( ( TYPE( rb_object_for_frame ) == T_CLASS ) ? rb_object_for_frame : rb_funcall( rb_object_for_frame, rb_intern( "class" ), 0 ) );

		VALUE	rb_ancestors	=	rb_funcall(	rb_object_for_frame_klass,
												rb_intern( "ancestors" ),
												0 );
		
		if ( rb_ary_includes(	rb_ancestors,
								rb_enumerator_class ) )	{
			
			struct enumerator* c_enumerator		=	enumerator_ptr( rb_object_for_frame );
			
			rb_object_for_frame	=	c_enumerator->obj;
			rb_method_name		=	ID2SYM( c_enumerator->meth );			
		}
		
	}
	else if (	( *c_current_frame )->block_iseq == 0
			 &&	( *c_current_frame )->pc == 0)	{
		//	this happens after we had a fiber and we try to go up - which doesn't make sense with a fiber
		//	not sure what we want to do here, if anything
		return Qnil;
	}
	else {
		//	The third possibility is that we have an iseq frame with nil params for what we want
		//	In that case we can simply return the next frame
		*c_current_frame	=	RUBY_VM_PREVIOUS_CONTROL_FRAME( *c_current_frame );
		
		//	in theory this could crash because we are going forward a frame when we don't know what's there
		//	in practice I think we are ok, since we are only jumping forward from nil frames which should never be at the end
		//	at least - I don't think they should... we shall see.
		//	
		//	a fix would be to check the next frame, but that requires access to the thread or the limit cfp, 
		//	which requires passing more context; so for now, I'm leaving it there
		
		return rb_RPRuby_Sender_Kernel_internal_backtraceHashForControlFrame( c_current_frame );
	}

	//	Push values to return hash

	rb_hash_aset(	rb_frame_hash,
					ID2SYM( rb_intern( "object" ) ),
					rb_object_for_frame );
					
	rb_hash_aset(	rb_frame_hash,
					ID2SYM( rb_intern( "file" ) ),
					rb_sourcefile_name );

	rb_hash_aset(	rb_frame_hash,
					ID2SYM( rb_intern( "line" ) ),
					rb_sourcefile_line );

	rb_hash_aset(	rb_frame_hash,
					ID2SYM( rb_intern( "method" ) ),
					rb_method_name );
	
	return rb_frame_hash;
}
コード例 #24
0
ファイル: main.c プロジェクト: Quintus/ruby-alpm
/** Takes a Ruby array of Ruby Symbols and computes the C
 * alpm_siglevel_t from it. Raises an exception if `ary'
 * doesn’t respond to #to_ary. */
alpm_siglevel_t siglevel_from_ruby(VALUE ary)
{
  alpm_siglevel_t level = 0;

  if (!(RTEST(ary = rb_check_array_type(ary)))) { /*  Single = intended */
    VALUE str = rb_inspect(level);
    rb_raise(rb_eTypeError, "Not an array (#to_ary): %s", StringValuePtr(str));
    return Qnil;
  }

  if (rb_ary_includes(ary, STR2SYM("package")))
    level |= ALPM_SIG_PACKAGE;
  if (rb_ary_includes(ary, STR2SYM("package_optional")))
    level |= ALPM_SIG_PACKAGE_OPTIONAL;
  if (rb_ary_includes(ary, STR2SYM("package_marginal_ok")))
    level |= ALPM_SIG_PACKAGE_MARGINAL_OK;
  if (rb_ary_includes(ary, STR2SYM("package_unknown_ok")))
    level |= ALPM_SIG_PACKAGE_UNKNOWN_OK;
  if (rb_ary_includes(ary, STR2SYM("database")))
    level |= ALPM_SIG_DATABASE;
  if (rb_ary_includes(ary, STR2SYM("database_optional")))
    level |= ALPM_SIG_DATABASE_OPTIONAL;
  if (rb_ary_includes(ary, STR2SYM("database_marginal_ok")))
    level |= ALPM_SIG_DATABASE_MARGINAL_OK;
  if (rb_ary_includes(ary, STR2SYM("database_unknown_ok")))
    level |= ALPM_SIG_DATABASE_UNKNOWN_OK;
  if (rb_ary_includes(ary, STR2SYM("package_set")))
    level |= ALPM_SIG_PACKAGE_SET;
  if (rb_ary_includes(ary, STR2SYM("package_trust_set")))
    level |= ALPM_SIG_PACKAGE_TRUST_SET;
  if (rb_ary_includes(ary, STR2SYM("use_default")))
    level |= ALPM_SIG_USE_DEFAULT;

  return level;
}