Exemple #1
0
static VALUE
rb_struct_each_pair(VALUE s)
{
    VALUE members;
    long i;

    RETURN_SIZED_ENUMERATOR(s, 0, 0, rb_struct_size);
    members = rb_struct_members(s);
    for (i=0; i<RSTRUCT_LEN(s); i++) {
	VALUE key = rb_ary_entry(members, i);
	VALUE value = RSTRUCT_PTR(s)[i];
	rb_yield(rb_assoc_new(key, value));
    }
    return s;
}
Exemple #2
0
static VALUE
env_assoc(VALUE env, SEL sel, VALUE key)
{
    rb_secure(4);

    const char *s = StringValuePtr(key);
    if (strlen(s) != RSTRING_LEN(key)) {
	rb_raise(rb_eArgError, "bad environment variable name");
    }
    const char *e = getenv(s);
    if (e != NULL) {
	return rb_assoc_new(key, rb_tainted_str_new2(e));
    }
    return Qnil;
}
static VALUE
bf_get_icon(VALUE self, VALUE uri)
{
    gchar* href;
    gchar* mime_type;
    GError *error = NULL;
    gboolean ret = g_bookmark_file_get_icon(_SELF(self),
                                            (const gchar *)RVAL2CSTR(uri),
                                            &href, &mime_type,
                                            &error);
    if (!ret){
        if (error) RAISE_GERROR(error);
        return Qnil;
    }
    return rb_assoc_new(CSTR2RVAL_FREE(href), CSTR2RVAL_FREE(mime_type)); 
}
static VALUE
gdkpmap_colormap_create_from_xpm(VALUE self, VALUE win, VALUE colormap, VALUE tcolor, VALUE fname)
{
    GdkPixmap *result;
    GdkBitmap *mask;

    result = gdk_pixmap_colormap_create_from_xpm(NIL_P(win) ? NULL : GDK_WINDOW(RVAL2GOBJ(win)), 
                                                 GDK_COLORMAP(RVAL2GOBJ(colormap)),
                                                 &mask,
                                                 RVAL2GDKCOLOR(tcolor),
                                                 RVAL2CSTR(fname));
    if (result == NULL)
        rb_raise(rb_eArgError, "Pixmap not created from %s", RVAL2CSTR(fname));

    return rb_assoc_new(GOBJ2RVAL(result), GOBJ2RVAL(mask));
}
static VALUE
gdkpmap_create_from_xpm_d(VALUE self, VALUE win, VALUE tcolor, VALUE data)
{
    GdkPixmap *result;
    GdkBitmap *mask;
    const gchar **buf = RVAL2STRV(data);

    result = gdk_pixmap_create_from_xpm_d(GDK_WINDOW(RVAL2GOBJ(win)),
                                          &mask,
                                          RVAL2GDKCOLOR(tcolor),
                                          (gchar **)buf);

    g_free(buf);

    return rb_assoc_new(GOBJ2RVAL(result), GOBJ2RVAL(mask));
}
Exemple #6
0
static VALUE
env_to_a(VALUE rcv, SEL sel)
{
    rb_secure(4);

    VALUE ary = rb_ary_new();
    char **env = GET_ENVIRON();
    while (*env != NULL) {
	const char *s = strchr(*env, '=');
	if (s != NULL) {
	    rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s - *env),
			env_str_new2(s + 1)));
	}
	env++;
    }
    return ary;
}
static VALUE
rg_x_to_index(VALUE self, VALUE text, VALUE analysis, VALUE x_pos)
{
    int index;
    int trailing;

    StringValue(text);
    pango_glyph_string_x_to_index(_SELF(self),
                                  /* NOTE: This is a bug in the signature of
                                   * pango_glyph_string_index_to_x */
                                  (char *)RSTRING_PTR(text),
                                  RSTRING_LEN(text),
                                  RVAL2PANGOANALYSIS(analysis),
                                  NUM2INT(x_pos),
                                  &index, &trailing);
    return rb_assoc_new(INT2NUM(index), CBOOL2RVAL(trailing));
}
Exemple #8
0
static VALUE
env_shift(VALUE rcv, SEL sel)
{
    rb_secure(4);

    char **env = GET_ENVIRON();
    if (*env != NULL) {
	const char *s = strchr(*env, '=');
	if (s != NULL) {
	    VALUE key = env_str_new(*env, s - *env);
	    VALUE val = env_str_new2(getenv(RSTRING_PTR(key)));
	    env_delete(Qnil, key);
	    return rb_assoc_new(key, val);
	}
    }
    return Qnil;
}
Exemple #9
0
static VALUE
rg_attach_points(VALUE self)
{
    GdkPoint* points;
    gint n_points, i;
    gboolean ret = gtk_icon_info_get_attach_points(_SELF(self), &points, &n_points);
    VALUE ary = Qnil;

    if (ret){
        ary = rb_ary_new();
        for (i = 0; i < n_points; i++){
            rb_ary_push(ary, rb_assoc_new(INT2NUM(points[i].x), INT2NUM(points[i].y)));
        }
        g_free(points);
    }
    return ary;
}
Exemple #10
0
/*
 * call-seq: DB.add_message(path) => MESSAGE, isdup
 *
 * Add a message to the database and return it.
 *
 * +isdup+ is a boolean that specifies whether the added message was a
 * duplicate.
 */
VALUE
notmuch_rb_database_add_message (VALUE self, VALUE pathv)
{
    const char *path;
    notmuch_status_t ret;
    notmuch_message_t *message;
    notmuch_database_t *db;

    Data_Get_Notmuch_Database (self, db);

    SafeStringValue (pathv);
    path = RSTRING_PTR (pathv);

    ret = notmuch_database_add_message (db, path, &message);
    notmuch_rb_status_raise (ret);
    return rb_assoc_new (Data_Wrap_Struct (notmuch_rb_cMessage, NULL, NULL, message),
        (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
}
Exemple #11
0
static VALUE
ossl_bn_coerce(VALUE self, VALUE other)
{
    switch(TYPE(other)) {
    case T_STRING:
	self = ossl_bn_to_s(0, NULL, self);
	break;
    case T_FIXNUM:
    case T_BIGNUM:
	self = ossl_bn_to_i(self);
	break;
    default:
	if (!RTEST(rb_obj_is_kind_of(other, cBN))) {
	    ossl_raise(rb_eTypeError, "Don't know how to coerce");
	}
    }
    return rb_assoc_new(other, self);
}
static VALUE
settings_rc_property_parse_requisition(VALUE self, VALUE spec, VALUE str)
{
    gboolean ret; 
    GValue prop = {0,};
    VALUE val = Qnil;
    GString* gstr = g_string_new(RVAL2CSTR(str));
    g_value_init(&prop, GTK_TYPE_REQUISITION);
    ret = gtk_rc_property_parse_requisition(G_PARAM_SPEC(RVAL2GOBJ(spec)), 
                                            gstr, &prop);
    if (ret){
        GtkRequisition* req = g_value_get_boxed(&prop);
        val = rb_assoc_new(INT2NUM(req->width), INT2NUM(req->height));
    }
    g_string_free(gstr, TRUE);
    g_value_unset(&prop);
    return val;    
}
static VALUE
rg_add_items(VALUE self, VALUE items)
{
    gint i, len;
    GList* list = (GList*)NULL;
    VALUE items_internal = rb_ivar_get(self, id_items_internal);

    items = rb_ary_to_ary(items);
    len = RARRAY_LEN(items);
    for (i = 0; i < len; i ++){
        VALUE data = RARRAY_PTR(items)[i];
        VALUE item = rb_assoc_new(self, data);
        list = g_list_append(list, (gpointer)item);
        rb_hash_aset(items_internal, data, item);
    }
    g_completion_add_items(_SELF(self), list);

    return self;
}
Exemple #14
0
static VALUE
rhash_shift(VALUE hash, SEL sel)
{
    VALUE args[2] = {0, 0};
    rhash_modify(hash);
    rhash_foreach(hash, shift_i, (st_data_t)args);
    if (args[0] != 0 && args[1] != 0) {
	rhash_delete_key(hash, args[0]);
	return rb_assoc_new(args[0], args[1]);
    }

    if (RHASH(hash)->ifnone != Qnil) {
	if (RHASH(hash)->has_proc_default) {
	    return rb_funcall(RHASH(hash)->ifnone, id_yield, 2, hash,
		    Qnil);
	}
	return RHASH(hash)->ifnone;
    }
    return Qnil;
}
static VALUE
rg_complete(VALUE self, VALUE prefix)
{
    gchar* new_prefix;
    VALUE ary = rb_ary_new();
#if GLIB_CHECK_VERSION(2,4,0)
    GList* list = g_completion_complete_utf8(_SELF(self),
                                            (const gchar*)RVAL2CSTR(prefix),
                                            &new_prefix);
#else
    GList* list = g_completion_complete(_SELF(self),
                                        RVAL2CSTR(prefix),
                                        &new_prefix);
#endif
    while (list) {
        rb_ary_push(ary, RARRAY_PTR((VALUE)list->data)[1]);
        list = list->next;
    }

    return rb_assoc_new(ary, new_prefix ? CSTR2RVAL(new_prefix) : Qnil);
}
Exemple #16
0
static VALUE
rg_get_pixel_extents(int argc, VALUE *argv, VALUE self)
{
    VALUE ink_rect, logical_rect;
    PangoRectangle rink, rlog;

    rb_scan_args(argc, argv, "02", &ink_rect, &logical_rect);

    if (NIL_P(ink_rect)){
        rink.x = 0;
        rink.y = 0;
        rink.width = 0;
        rink.height = 0;
    } else {
        PangoRectangle* rect = (PangoRectangle*)RVAL2BOXED(ink_rect, PANGO_TYPE_RECTANGLE);
        rink.x = rect->x;
        rink.y = rect->y;
        rink.width = rect->width;
        rink.height = rect->height;
    }
    if (NIL_P(logical_rect)){
        rlog.x = 0;
        rlog.y = 0;
        rlog.width = 0;
        rlog.height = 0;
    } else {
        PangoRectangle* rect = (PangoRectangle*)RVAL2BOXED(logical_rect, PANGO_TYPE_RECTANGLE);
        rlog.x = rect->x;
        rlog.y = rect->y;
        rlog.width = rect->width;
        rlog.height = rect->height;
    }   

    pango_layout_get_pixel_extents(_SELF(self), &rink, &rlog);

    return rb_assoc_new(BOXED2RVAL(&rink, PANGO_TYPE_RECTANGLE),
                        BOXED2RVAL(&rlog, PANGO_TYPE_RECTANGLE));
}
Exemple #17
0
static VALUE
env_each_pair(VALUE ehash, SEL sel)
{
    RETURN_ENUMERATOR(ehash, 0, 0);

    rb_secure(4);
    VALUE ary = rb_ary_new();
    char **env = GET_ENVIRON();
    while (*env != NULL) {
	const char *s = strchr(*env, '=');
	if (s != NULL) {
	    rb_ary_push(ary, env_str_new(*env, s - *env));
	    rb_ary_push(ary, env_str_new2(s + 1));
	}
	env++;
    }

    for (long i = 0, count = RARRAY_LEN(ary); i < count; i += 2) {
	rb_yield(rb_assoc_new(RARRAY_AT(ary, i), RARRAY_AT(ary, i + 1)));
	RETURN_IF_BROKEN();
    }
    return ehash;
}
static VALUE
rglyph_extents(int argc, VALUE *argv, VALUE self)
{
    VALUE font, start_index, end_index;
    PangoRectangle ink_rect;
    PangoRectangle logical_rect;

    rb_scan_args(argc, argv, "12", &font, &start_index, &end_index);

    if (NIL_P(start_index)){
        pango_glyph_string_extents(_SELF(self), 
                                   PANGO_FONT(RVAL2GOBJ(font)),
                                   &ink_rect, &logical_rect);
    } else {
        pango_glyph_string_extents_range(_SELF(self), 
                                         NUM2INT(start_index), NUM2INT(end_index),
                                         PANGO_FONT(RVAL2GOBJ(font)),
                                         &ink_rect, &logical_rect);
    }

    return rb_assoc_new(BOXED2RVAL(&ink_rect, PANGO_TYPE_RECTANGLE),
                        BOXED2RVAL(&logical_rect, PANGO_TYPE_RECTANGLE));
}
Exemple #19
0
static VALUE
nst_create_member_views(VALUE self)
{
    VALUE defs, def, types, type, elmt;
    long  i, len;
    narray_view_t *ne;

    defs = nst_definitions(CLASS_OF(self));
    len = RARRAY_LEN(defs);
    types = rb_ary_new2(len);
    //ofsts = rb_ary_new2(len);
    for (i=0; i<len; i++) {
        def  = RARRAY_AREF(defs,i);
        type = RARRAY_AREF(def,1);
        //ofst = RARRAY_AREF(def,2);
        elmt = na_make_view(type);
        rb_ary_push(types, elmt);
        //rb_ary_push(ofsts, ofst);
        GetNArrayView(elmt,ne);
        ne->data = na_original_data(self);
    }
    return rb_assoc_new(types,defs);
}
Exemple #20
0
static VALUE
env_rassoc(VALUE dmy, SEL sel, VALUE obj)
{
    rb_secure(4);

    obj = rb_check_string_type(obj);
    if (NIL_P(obj)) {
	return Qnil;
    }
    char **env = GET_ENVIRON();
    while (*env != NULL) {
	const char *s = strchr(*env, '=');
	if (s++ != NULL) {
	    const long len = strlen(s);
	    if (RSTRING_LEN(obj) == len
		    && strncmp(s, RSTRING_PTR(obj), len) == 0) {
		return rb_assoc_new(rb_tainted_str_new(*env, s - *env - 1),
			obj);
	    }
	}
	env++;
    }
    return Qnil;
}
static VALUE
rb_struct_each_pair(VALUE s)
{
    VALUE members;
    long i;

    RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
    members = rb_struct_members(s);
    if (rb_block_arity() > 1) {
        for (i=0; i<RSTRUCT_LEN(s); i++) {
            VALUE key = rb_ary_entry(members, i);
            VALUE value = RSTRUCT_GET(s, i);
            rb_yield_values(2, key, value);
        }
    }
    else {
        for (i=0; i<RSTRUCT_LEN(s); i++) {
            VALUE key = rb_ary_entry(members, i);
            VALUE value = RSTRUCT_GET(s, i);
            rb_yield(rb_assoc_new(key, value));
        }
    }
    return s;
}
Exemple #22
0
static VALUE
bdb_i_each_log_get(VALUE obj, int flag)
{
#if ! HAVE_ST_DB_ENV_LOG_CURSOR
    bdb_ENV *envst;
#endif
    struct dblsnst *lsnst, *lsnst1;
    DBT data;
    VALUE lsn, lsn1;
    VALUE res;
    int ret, init, flags;

    init = BDB_LOG_INIT;
#if ! HAVE_ST_DB_ENV_LOG_CURSOR
    GetEnvDB(obj, envst);
#else
    lsn = obj;
    Data_Get_Struct(obj, struct dblsnst, lsnst);
    flag = lsnst->flags;
    if (lsnst->cursor == 0) {
	DB_LSN *lsn1;

	init = BDB_LOG_SET;
	lsn1 = lsnst->lsn;
	lsn = bdb_makelsn(lsnst->env);
	Data_Get_Struct(lsn, struct dblsnst, lsnst);
	MEMCPY(lsnst->lsn, lsn1, DB_LSN, 1);
	bdb_log_cursor(lsn);
    }
#endif

    do {
#if ! HAVE_ST_DB_ENV_LOG_CURSOR
	lsn = bdb_makelsn(obj);
	Data_Get_Struct(lsn, struct dblsnst, lsnst);
#endif
	MEMZERO(&data, DBT, 1);
	data.flags |= DB_DBT_MALLOC;
	switch (init) {
	case BDB_LOG_INIT:
	    flags = (flag == DB_NEXT)?DB_FIRST:DB_LAST;
	    break;
	case BDB_LOG_SET:
	    flags = DB_SET;
	    break;
	default:
	    flags = flag;
	    break;
	}
	init = BDB_LOG_NEXT;
#if HAVE_ST_DB_ENV_LG_INFO
	if (!envst->envp->lg_info) {
	    rb_raise(bdb_eFatal, "log region not open");
	}
	ret = bdb_test_error(log_get(envst->envp->lg_info, lsnst->lsn, &data, flags));
#elif HAVE_ST_DB_ENV_LOG_CURSOR
	ret = bdb_test_error(lsnst->cursor->get(lsnst->cursor, lsnst->lsn, &data, flags));
	lsn1 = bdb_makelsn(lsnst->env);
	Data_Get_Struct(lsn1, struct dblsnst, lsnst1);
	MEMCPY(lsnst1->lsn, lsnst->lsn, DB_LSN, 1);
#else
	ret = bdb_test_error(log_get(envst->envp, lsnst->lsn, &data, flags));
	lsn1 = lsn;
#endif
	if (ret == DB_NOTFOUND) {
	    return Qnil;
	}
	res = rb_tainted_str_new(data.data, data.size);
	free(data.data);
	rb_yield(rb_assoc_new(res, lsn));
    } while (1);
    return Qnil;
}
Exemple #23
0
/*
 * call-seq:
 *    num.polar  ->  array
 *
 * Returns an array; [num.abs, num.arg].
 */
static VALUE
numeric_polar(VALUE self)
{
    return rb_assoc_new(f_abs(self), f_arg(self));
}
Exemple #24
0
/*
 * call-seq:
 *    num.rect  ->  array
 *    num.rectangular  ->  array
 *
 * Returns an array; [num, 0].
 */
static VALUE
numeric_rect(VALUE self)
{
    return rb_assoc_new(self, INT2FIX(0));
}
Exemple #25
0
/*
 * call-seq:
 *    cmp.rect         ->  array
 *    cmp.rectangular  ->  array
 *
 * Returns an array; [cmp.real, cmp.imag].
 *
 *    Complex(1, 2).rectangular  #=> [1, 2]
 */
static VALUE
nucomp_rect(VALUE self)
{
    get_dat1(self);
    return rb_assoc_new(dat->real, dat->imag);
}
Exemple #26
0
VALUE
rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
{
    rb_io_t *fptr;
    VALUE str, klass;
    struct recvfrom_arg arg;
    VALUE len, flg;
    long buflen;
    long slen;

    rb_scan_args(argc, argv, "11", &len, &flg);

    if (flg == Qnil) arg.flags = 0;
    else             arg.flags = NUM2INT(flg);
    buflen = NUM2INT(len);

    GetOpenFile(sock, fptr);
    if (rb_io_read_pending(fptr)) {
	rb_raise(rb_eIOError, "recv for buffered IO");
    }
    arg.fd = fptr->fd;
    arg.alen = (socklen_t)sizeof(arg.buf);

    arg.str = str = rb_tainted_str_new(0, buflen);
    klass = RBASIC(str)->klass;
    rb_obj_hide(str);

    while (rb_io_check_closed(fptr),
	   rsock_maybe_wait_fd(arg.fd),
	   (slen = BLOCKING_REGION_FD(recvfrom_blocking, &arg)) < 0) {
        if (!rb_io_wait_readable(fptr->fd)) {
            rb_sys_fail("recvfrom(2)");
        }
	if (RBASIC(str)->klass || RSTRING_LEN(str) != buflen) {
	    rb_raise(rb_eRuntimeError, "buffer string modified");
	}
    }

    rb_obj_reveal(str, klass);
    if (slen < RSTRING_LEN(str)) {
	rb_str_set_len(str, slen);
    }
    rb_obj_taint(str);
    switch (from) {
      case RECV_RECV:
	return str;
      case RECV_IP:
#if 0
	if (arg.alen != sizeof(struct sockaddr_in)) {
	    rb_raise(rb_eTypeError, "sockaddr size differs - should not happen");
	}
#endif
	if (arg.alen && arg.alen != sizeof(arg.buf)) /* OSX doesn't return a from result for connection-oriented sockets */
	    return rb_assoc_new(str, rsock_ipaddr(&arg.buf.addr, arg.alen, fptr->mode & FMODE_NOREVLOOKUP));
	else
	    return rb_assoc_new(str, Qnil);

#ifdef HAVE_SYS_UN_H
      case RECV_UNIX:
        return rb_assoc_new(str, rsock_unixaddr(&arg.buf.un, arg.alen));
#endif
      case RECV_SOCKET:
	return rb_assoc_new(str, rsock_io_socket_addrinfo(sock, &arg.buf.addr, arg.alen));
      default:
	rb_bug("rsock_s_recvfrom called with bad value");
    }
}
Exemple #27
0
static VALUE
string_to_c_internal(VALUE self)
{
    VALUE s;

    s = self;

    if (RSTRING_LEN(s) == 0)
	return rb_assoc_new(Qnil, self);

    {
	VALUE m, sr, si, re, r, i;
	int po;

	m = f_match(comp_pat0, s);
	if (!NIL_P(m)) {
	    sr = rb_reg_nth_match(1, m);
	    si = rb_reg_nth_match(2, m);
	    re = rb_reg_match_post(m);
	    po = 1;
	}
	if (NIL_P(m)) {
	    m = f_match(comp_pat1, s);
	    if (!NIL_P(m)) {
		sr = Qnil;
		si = rb_reg_nth_match(1, m);
		if (NIL_P(si))
		    si = rb_usascii_str_new2("");
		{
		    VALUE t;

		    t = rb_reg_nth_match(2, m);
		    if (NIL_P(t))
			t = rb_usascii_str_new2("1");
		    rb_str_concat(si, t);
		}
		re = rb_reg_match_post(m);
		po = 0;
	    }
	}
	if (NIL_P(m)) {
	    m = f_match(comp_pat2, s);
	    if (NIL_P(m))
		return rb_assoc_new(Qnil, self);
	    sr = rb_reg_nth_match(1, m);
	    if (NIL_P(rb_reg_nth_match(2, m)))
		si = Qnil;
	    else {
		VALUE t;

		si = rb_reg_nth_match(3, m);
		t = rb_reg_nth_match(4, m);
		if (NIL_P(t))
		    t = rb_usascii_str_new2("1");
		rb_str_concat(si, t);
	    }
	    re = rb_reg_match_post(m);
	    po = 0;
	}
	r = INT2FIX(0);
	i = INT2FIX(0);
	if (!NIL_P(sr)) {
	    if (strchr(RSTRING_PTR(sr), '/'))
		r = f_to_r(sr);
	    else if (strpbrk(RSTRING_PTR(sr), ".eE"))
		r = f_to_f(sr);
	    else
		r = f_to_i(sr);
	}
	if (!NIL_P(si)) {
	    if (strchr(RSTRING_PTR(si), '/'))
		i = f_to_r(si);
	    else if (strpbrk(RSTRING_PTR(si), ".eE"))
		i = f_to_f(si);
	    else
		i = f_to_i(si);
	}
	if (po)
	    return rb_assoc_new(rb_complex_polar(r, i), re);
	else
	    return rb_assoc_new(rb_complex_new2(r, i), re);
    }
}
Exemple #28
0
static VALUE array_spec_rb_assoc_new(VALUE self, VALUE first, VALUE second) {
  return rb_assoc_new(first, second);
}
Exemple #29
0
VALUE
rsock_s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
{
    rb_io_t *fptr;
    VALUE str;
    union_sockaddr buf;
    socklen_t alen = (socklen_t)sizeof buf;
    VALUE len, flg;
    long buflen;
    long slen;
    int fd, flags;
    VALUE addr = Qnil;
    socklen_t len0;

    rb_scan_args(argc, argv, "11", &len, &flg);

    if (flg == Qnil) flags = 0;
    else             flags = NUM2INT(flg);
    buflen = NUM2INT(len);

#ifdef MSG_DONTWAIT
    /* MSG_DONTWAIT avoids the race condition between fcntl and recvfrom.
       It is not portable, though. */
    flags |= MSG_DONTWAIT;
#endif

    GetOpenFile(sock, fptr);
    if (rb_io_read_pending(fptr)) {
	rb_raise(rb_eIOError, "recvfrom for buffered IO");
    }
    fd = fptr->fd;

    str = rb_tainted_str_new(0, buflen);

    rb_io_check_closed(fptr);
    rb_io_set_nonblock(fptr);
    len0 = alen;
    slen = recvfrom(fd, RSTRING_PTR(str), buflen, flags, &buf.addr, &alen);
    if (slen != -1 && len0 < alen)
        alen = len0;

    if (slen < 0) {
	switch (errno) {
	  case EAGAIN:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
	  case EWOULDBLOCK:
#endif
            rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "recvfrom(2) would block");
	}
	rb_sys_fail("recvfrom(2)");
    }
    if (slen < RSTRING_LEN(str)) {
	rb_str_set_len(str, slen);
    }
    rb_obj_taint(str);
    switch (from) {
      case RECV_RECV:
        return str;

      case RECV_IP:
        if (alen && alen != sizeof(buf)) /* connection-oriented socket may not return a from result */
            addr = rsock_ipaddr(&buf.addr, alen, fptr->mode & FMODE_NOREVLOOKUP);
        break;

      case RECV_SOCKET:
        addr = rsock_io_socket_addrinfo(sock, &buf.addr, alen);
        break;

      default:
        rb_bug("rsock_s_recvfrom_nonblock called with bad value");
    }
    return rb_assoc_new(str, addr);
}
Exemple #30
0
/**
 * filter_html
 **/
static VALUE
t_filter_html(VALUE self, VALUE str)
{
    node root, now, ret;
    bool in_tag;
    char *text;
    const char* inner_tag;
    long i, head_i, tail_i, copy_head_i, total_len;
    VALUE change_str, url_base, word;
    rb_encoding *enc;

    change_str = rb_str_new2(EMPTY_STRING);
    enc = rb_enc_get(str);
    text = StringValuePtr(str);

    Data_Get_Struct(self, struct _node, root);
    url_base = rb_iv_get(self, LINK_URL_VARIABLE);
    if (url_base == Qnil) {
        url_base = rb_str_new2(DEAULT_LINK_URL);
    }

    now = root;
    total_len = strlen(text);
    head_i = -1;
    tail_i = -1;
    copy_head_i = 0;
    in_tag = false;
    inner_tag = NULL;

    for(i = 0; i <= total_len; i++) {
        if (!in_tag && text[i] == BEGIN_TAG) {
            in_tag = true;
            if (strncasecmp(&text[i + 1], A_TAG, strlen(A_TAG)) == 0) {
                inner_tag = A_TAG;
            } else if (strncasecmp(&text[i + 1], SCRIPT_TAG, strlen(SCRIPT_TAG)) == 0) {
                inner_tag = SCRIPT_TAG;
            } else if (strncasecmp(&text[i + 1], PRE_TAG, strlen(PRE_TAG)) == 0) {
                inner_tag = PRE_TAG;
            } else if (strncasecmp(&text[i + 1], IFRAME_TAG, strlen(IFRAME_TAG)) == 0) {
                inner_tag = IFRAME_TAG;
            } else if (strncasecmp(&text[i + 1], OBJECT_TAG, strlen(OBJECT_TAG)) == 0) {
                inner_tag = OBJECT_TAG;
            }
            continue;
        }

        if (in_tag && !inner_tag && text[i] == END_TAG) {
            in_tag = false;
            continue;
        }

        if (inner_tag && text[i] == BEGIN_TAG) {
            if (strncasecmp(&text[i + 2], inner_tag, strlen(inner_tag)) == 0) {
                inner_tag = NULL;
                continue;
            }
        }

        if (in_tag) {
            continue;
        }

        ret = search_child(now, text[i]);

        if (ret && i != total_len) {
            if (head_i == -1) {
                head_i = i;
            }

            if (ret->end_flag) {
                tail_i = i;
            }
            now = ret;
        } else {
            if (head_i != -1) {
                if (tail_i != -1) {
                    if (copy_head_i < head_i) {
                        rb_funcall(
                            change_str, 
                            rb_intern("concat"),
                            1,
                            add_encode(rb_str_new(&text[copy_head_i], (head_i - copy_head_i)), enc)
                        );
                    }

                    word = rb_str_new(&text[head_i], (tail_i - head_i + 1));
                    rb_funcall(
                        change_str,
                        rb_intern("concat"),
                        1,
                        add_encode(rb_funcall(url_base, rb_intern("%"), 1, rb_assoc_new(word, word)), enc)
                    );
                    i = tail_i;
                    copy_head_i = tail_i + 1;
                    tail_i = -1;
                } else {
                    i = head_i;
                }
                head_i = -1;
            }
            now = root;
        }
    }

    if (copy_head_i == 0) {
        return str;
    } else {
        rb_funcall(
            change_str,
            rb_intern("concat"),
            1,
            add_encode(rb_str_new(&text[copy_head_i], (total_len - copy_head_i)), enc)
        );
        return change_str;
    }
}