コード例 #1
0
ファイル: response.c プロジェクト: miyucy/aspirin
static int
aspirin_response_each_header(VALUE _key, VALUE _val, VALUE headers)
{
    VALUE key = rb_str_to_str(_key);
    VALUE val = rb_str_to_str(_val);
    if(RSTRING_LEN(key) > 0 && RSTRING_LEN(val) > 0)
    {
        evhttp_remove_header(DATA_PTR(headers), RSTRING_PTR(key));
        evhttp_add_header(DATA_PTR(headers), RSTRING_PTR(key), RSTRING_PTR(val));
    }
    return ST_CONTINUE;
}
コード例 #2
0
ファイル: log.c プロジェクト: mallowlabs/ruby-bdb
static VALUE
bdb_s_log_put_internal(VALUE obj, VALUE a, int flag)
{
    bdb_ENV *envst;
    VALUE ret;
    DBT data;
    struct dblsnst *lsnst;

    GetEnvDB(obj, envst);
    if (TYPE(a) != T_STRING) a = rb_str_to_str(a);
    ret = bdb_makelsn(obj);
    Data_Get_Struct(ret, struct dblsnst, lsnst);
    data.data = StringValuePtr(a);
    data.size = RSTRING_LEN(a);
#if HAVE_ST_DB_ENV_LG_INFO
    if (!envst->envp->lg_info) {
	rb_raise(bdb_eFatal, "log region not open");
    }
    bdb_test_error(log_put(envst->envp->lg_info, lsnst->lsn, &data, flag));
#elif HAVE_ST_DB_ENV_LOG_PUT
    bdb_test_error(envst->envp->log_put(envst->envp, lsnst->lsn, &data, flag));
#else
    bdb_test_error(log_put(envst->envp, lsnst->lsn, &data, flag));
#endif
    return ret;
}
コード例 #3
0
ファイル: service.c プロジェクト: brianwells/dnssd
static VALUE
dnssd_service_update_record(VALUE self, VALUE _record, VALUE _flags, VALUE _rdata, VALUE _ttl) {

  DNSServiceRef *client;
  DNSRecordRef primaryRec = NULL;
  DNSRecordRef *record;
  DNSServiceFlags flags;
  DNSServiceErrorType e;
  uint16_t rdlen;
  const void *rdata;
  uint32_t ttl;

  _rdata = rb_str_to_str(_rdata);
  flags = (DNSServiceFlags)NUM2ULONG(_flags);
  rdlen = RSTRING_LEN(_rdata);
  rdata = (void *)RSTRING_PTR(_rdata);
  ttl = (uint32_t)NUM2ULONG(_ttl);

  if (!NIL_P(_record)) {
    get(cDNSSDRecord, _record, DNSRecordRef, record);
  } else {
    record = &primaryRec;
  }
  get(cDNSSDService, self, DNSServiceRef, client);

  e = DNSServiceUpdateRecord(*client, *record, flags, rdlen, rdata, ttl);

  dnssd_check_error_code(e);

  return self;
}
コード例 #4
0
ファイル: struct.c プロジェクト: 1nueve/MacRuby
static VALUE
make_struct(VALUE name, VALUE members, VALUE klass)
{
    VALUE nstr;
    ID id;
    long i, len;

    OBJ_FREEZE(members);
    if (NIL_P(name)) {
	nstr = rb_class_new(klass);
#if !WITH_OBJC
	rb_make_metaclass(nstr, RBASIC(klass)->klass);
#endif
	rb_class_inherited(klass, nstr);
    }
    else {
	/* old style: should we warn? */
	name = rb_str_to_str(name);
	id = rb_to_id(name);
	if (!rb_is_const_id(id)) {
	    rb_name_error(id, "identifier %s needs to be constant",
		    StringValuePtr(name));
	}
	if (rb_const_defined_at(klass, id)) {
	    rb_warn("redefining constant Struct::%s", StringValuePtr(name));
	    rb_mod_remove_const(klass, ID2SYM(id));
	}
	nstr = rb_define_class_under(klass, rb_id2name(id), klass);
    }
    rb_ivar_set(nstr, id_members, members);

    rb_objc_define_method(*(VALUE *)nstr, "alloc", struct_alloc, 0);
    rb_objc_define_method(*(VALUE *)nstr, "new", rb_class_new_instance_imp, -1);
    rb_objc_define_method(*(VALUE *)nstr, "[]", rb_class_new_instance_imp, -1);
    rb_objc_define_method(*(VALUE *)nstr, "members", rb_struct_s_members_m, 0);
    len = RARRAY_LEN(members);
    for (i=0; i< len; i++) {
	ID id = SYM2ID(RARRAY_AT(members, i));
	if (rb_is_local_id(id) || rb_is_const_id(id)) {
	    long j = i; /* Needed for block data reference. */
	/* Struct attribute reader */
	rb_objc_define_method(nstr, rb_id2name(id),
		pl_imp_implementationWithBlock(^(VALUE obj) {
		    return RSTRUCT_PTR(obj)[j];
		}), 0);
	/* Struct attribute writer */
	rb_objc_define_method(nstr, rb_id2name(rb_id_attrset(id)),
		pl_imp_implementationWithBlock(^(VALUE obj, VALUE val) {
		    VALUE *ptr = RSTRUCT_PTR(obj);
		    rb_struct_modify(obj);
		    GC_WB(&ptr[i], val);
		    return val;
		}), 1);
コード例 #5
0
static VALUE
make_struct(VALUE name, VALUE members, VALUE klass)
{
    VALUE nstr;
    ID id;
    long i, count;

    OBJ_FREEZE(members);
    if (NIL_P(name)) {
	nstr = rb_class_new(klass);
#if !WITH_OBJC
	rb_make_metaclass(nstr, RBASIC(klass)->klass);
#endif
	rb_class_inherited(klass, nstr);
    }
    else {
	/* old style: should we warn? */
	name = rb_str_to_str(name);
	id = rb_to_id(name);
	if (!rb_is_const_id(id)) {
	    rb_name_error(id, "identifier %s needs to be constant",
		    StringValuePtr(name));
	}
	if (rb_const_defined_at(klass, id)) {
	    rb_warn("redefining constant Struct::%s", StringValuePtr(name));
	    rb_mod_remove_const(klass, ID2SYM(id));
	}
	nstr = rb_define_class_under(klass, rb_id2name(id), klass);
    }
    rb_iv_set(nstr, "__size__", LONG2NUM(RARRAY_LEN(members)));
    rb_iv_set(nstr, "__members__", members);

    rb_objc_define_method(*(VALUE *)nstr, "alloc", struct_alloc, 0);
    rb_objc_define_method(*(VALUE *)nstr, "new", rb_class_new_instance_imp, -1);
    rb_objc_define_method(*(VALUE *)nstr, "[]", rb_class_new_instance_imp, -1);
    rb_objc_define_method(*(VALUE *)nstr, "members", rb_struct_s_members_m, 0);
    for (i = 0, count = RARRAY_LEN(members); i < count; i++) {
	ID id = SYM2ID(RARRAY_AT(members, i));
	if (rb_is_local_id(id) || rb_is_const_id(id)) {
	    if (i < N_REF_FUNC) {
		rb_objc_define_method(nstr, rb_id2name(id), ref_func[i], 0);
	    }
	    else {
		rb_objc_define_method(nstr, rb_id2name(id), rb_struct_ref, 0);
	    }
	    rb_objc_define_method(nstr, rb_id2name(rb_id_attrset(id)),
		    rb_struct_set, 1);
	}
    }

    return nstr;
}
コード例 #6
0
static VALUE
make_struct(VALUE name, VALUE members, VALUE klass)
{
    VALUE nstr, *ptr_members;
    ID id;
    long i, len;

    OBJ_FREEZE(members);
    if (NIL_P(name)) {
	nstr = rb_class_new(klass);
	rb_make_metaclass(nstr, RBASIC(klass)->klass);
	rb_class_inherited(klass, nstr);
    }
    else {
	/* old style: should we warn? */
	name = rb_str_to_str(name);
	id = rb_to_id(name);
	if (!rb_is_const_id(id)) {
	    rb_name_error(id, "identifier %s needs to be constant", StringValuePtr(name));
	}
	if (rb_const_defined_at(klass, id)) {
	    rb_warn("redefining constant Struct::%s", StringValuePtr(name));
	    rb_mod_remove_const(klass, ID2SYM(id));
	}
	nstr = rb_define_class_id_under(klass, id, klass);
    }
    rb_ivar_set(nstr, id_members, members);

    rb_define_alloc_func(nstr, struct_alloc);
    rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1);
    rb_define_singleton_method(nstr, "[]", rb_class_new_instance, -1);
    rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0);
    ptr_members = RARRAY_PTR(members);
    len = RARRAY_LEN(members);
    for (i=0; i< len; i++) {
	ID id = SYM2ID(ptr_members[i]);
	if (rb_is_local_id(id) || rb_is_const_id(id)) {
	    if (i < N_REF_FUNC) {
		rb_define_method_id(nstr, id, ref_func[i], 0);
	    }
	    else {
		rb_define_method_id(nstr, id, rb_struct_ref, 0);
	    }
	    rb_define_method_id(nstr, rb_id_attrset(id), rb_struct_set, 1);
	}
    }

    return nstr;
}
コード例 #7
0
static VALUE
new_struct(VALUE name, VALUE super)
{
    /* old style: should we warn? */
    ID id;
    name = rb_str_to_str(name);
    if (!rb_is_const_name(name)) {
        rb_name_error_str(name, "identifier %"PRIsVALUE" needs to be constant",
                          QUOTE(name));
    }
    id = rb_to_id(name);
    if (rb_const_defined_at(super, id)) {
        rb_warn("redefining constant %"PRIsVALUE"::%"PRIsVALUE, super, name);
        rb_mod_remove_const(super, ID2SYM(id));
    }
    return rb_define_class_id_under(super, id, super);
}
コード例 #8
0
ファイル: rb_u_string_index.c プロジェクト: now/u
/* TODO: Return VALUE. */
long
rb_u_string_index_regexp(VALUE self, const char *begin, VALUE regex, bool reverse)
{
        const struct rb_u_string *string = RVAL2USTRING(self);
        VALUE rbstring = rb_str_to_str(self);

        const char *base = USTRING_STR(string);

        long index = rb_reg_search(regex, rbstring,
                                   rb_reg_adjust_startpos(regex, rbstring,
                                                          begin - base,
                                                          reverse),
                                   reverse);
        if (index == -1)
                return -1;

        return u_pointer_to_offset(base, base + index);
}
コード例 #9
0
ファイル: freetds.c プロジェクト: ebryn/freetds4ruby
static char* value_to_cstr(VALUE value) {
	VALUE str;
	char* result = NULL;
	int max;
	
	if(RTEST(value)) {
		str = value;
		if( TYPE(str) != T_STRING ) {
			str = rb_str_to_str(str);
		}
		str = StringValue(value);
		max = RSTRING(str)->len;
		result = malloc(max+1);
		bzero(result, max+1);
		strncpy(result, STR2CSTR(str), max);		
	}
	
	return result;
}
コード例 #10
0
ファイル: service.c プロジェクト: brianwells/dnssd
static VALUE
dnssd_service_add_record(VALUE self, VALUE _flags, VALUE _rrtype, VALUE _rdata,
    VALUE _ttl) {
  VALUE _record = Qnil;
  DNSServiceRef *client;
  DNSRecordRef *record;
  DNSServiceFlags flags;
  DNSServiceErrorType e;
  uint16_t rrtype;
  uint16_t rdlen;
  const void *rdata;
  uint32_t ttl;

  _rdata = rb_str_to_str(_rdata);
  flags = (DNSServiceFlags)NUM2ULONG(_flags);
  rrtype = NUM2UINT(_rrtype);
  rdlen = RSTRING_LEN(_rdata);
  rdata = (void *)RSTRING_PTR(_rdata);
  ttl = (uint32_t)NUM2ULONG(_ttl);

  get(cDNSSDService, self, DNSServiceRef, client);

  _record = rb_class_new_instance(0, NULL, cDNSSDRecord);

  get(cDNSSDRecord, _record, DNSRecordRef, record);

  e = DNSServiceAddRecord(*client, record, flags, rrtype, rdlen, rdata, ttl);

  dnssd_check_error_code(e);

  /* record will become invalid when this service is destroyed */
  rb_ivar_set(_record, dnssd_iv_service, self);
  rb_ary_push(rb_ivar_get(self, dnssd_iv_records), _record);

  return _record;
}
コード例 #11
0
ファイル: string_spec.c プロジェクト: kstephens/rubyspec
VALUE string_spec_rb_str_to_str(VALUE self, VALUE arg) {
  return rb_str_to_str(arg);
}
コード例 #12
0
ファイル: Pointer.c プロジェクト: bkerley/brainload
/*
 * call-seq: ptr.free
 * @return [self]
 * Free memory pointed by +self+.
 */
static VALUE
ptr_free(VALUE self)
{
    Pointer* ptr;

    Data_Get_Struct(self, Pointer, ptr);

    if (ptr->allocated) {
        if (ptr->storage != NULL) {
            xfree(ptr->storage);
            ptr->storage = NULL;
        }
        ptr->allocated = false;

    } else {
        VALUE caller = rb_funcall(rb_funcall(Qnil, rb_intern("caller"), 0), rb_intern("first"), 0);
        
        rb_warn("calling free on non allocated pointer %s from %s", RSTRING_PTR(ptr_inspect(self)), RSTRING_PTR(rb_str_to_str(caller)));
    }

    return self;
}
コード例 #13
0
ファイル: rb_u_string_gsub.c プロジェクト: now/u
/* @overload gsub(pattern, replacement)
 *
 *   Returns the receiver with all matches of PATTERN replaced by REPLACEMENT,
 *   inheriting any taint and untrust from the receiver and from REPLACEMENT.
 *
 *   The REPLACEMENT is used as a specification for what to replace matches
 *   with:
 *
 *   <table>
 *     <thead>
 *       <tr><th>Specification</th><th>Replacement</th></tr>
 *     </thead>
 *     <tbody>
 *       <tr>
 *         <td><code>\1</code>, <code>\2</code>, …, <code>\</code><em>n</em></td>
 *         <td>Numbered sub-match <em>n</em></td>
 *       </tr>
 *       <tr>
 *         <td><code>\k&lt;</code><em>name</em><code>></code></td>
 *         <td>Named sub-match <em>name</em></td>
 *       </tr>
 *     </tbody>
 *   </table>
 *
 *   The Regexp special variables `$&`, `$'`, <code>$\`</code>, `$1`, `$2`, …,
 *   `$`_n_ are updated accordingly.
 *
 *   @param [Regexp, #to_str] pattern
 *   @param [#to_str] replacement
 *   @return [U::String]
 *
 * @overload gsub(pattern, replacements)
 *
 *   Returns the receiver with all matches of PATTERN replaced by
 *   REPLACEMENTS#[_match_], where _match_ is the matched substring, inheriting
 *   any taint and untrust from the receiver and from the
 *   REPLACEMENTS#[_match_]es, as well as any taint on REPLACEMENTS.
 *
 *   The Regexp special variables `$&`, `$'`, <code>$\`</code>, `$1`, `$2`, …,
 *   `$`_n_ are updated accordingly.
 *
 *   @param [Regexp, #to_str] pattern
 *   @param [#to_hash] replacements
 *   @raise [RuntimeError] If any replacement is the result being constructed
 *   @raise [Exception] Any error raised by REPLACEMENTS#default, if it gets
 *     called
 *   @return [U::String]
 *
 * @overload gsub(pattern){ |match| … }
 *
 *   Returns the receiver with all matches of PATTERN replaced by the results
 *   of the given block, inheriting any taint and untrust from the receiver and
 *   from the results of the given block.
 *
 *   The Regexp special variables `$&`, `$'`, <code>$\`</code>, `$1`, `$2`, …,
 *   `$`_n_ are updated accordingly.
 *
 *   @param [Regexp, #to_str] pattern
 *   @yieldparam [U::String] match
 *   @yieldreturn [#to_str]
 *   @return [U::String]
 *
 * @overload gsub(pattern)
 *
 *   Returns an Enumerator over the matches of PATTERN in the receiver.
 *
 *   The Regexp special variables `$&`, `$'`, <code>$\`</code>, `$1`, `$2`, …,
 *   `$`_n_ will be updated accordingly.
 *
 *   @param [Regexp, #to_str] pattern
 *   @return [Enumerator] */
VALUE
rb_u_string_gsub(int argc, VALUE *argv, VALUE self)
{
        VALUE pattern, replacement;
        VALUE replacements = Qnil;
        bool use_block = false;
        bool tainted = false;

        if (argc == 1) {
                RETURN_ENUMERATOR(self, argc, argv);
                use_block = true;
        }

        if (rb_scan_args(argc, argv, "11", &pattern, &replacement) == 2) {
                replacements = rb_check_convert_type(replacement, T_HASH,
                                                     "Hash", "to_hash");
                if (NIL_P(replacements))
                        StringValue(replacement);
                if (OBJ_TAINTED(replacement))
                        tainted = true;
        }

        pattern = rb_u_pattern_argument(pattern, true);

        VALUE str = rb_str_to_str(self);
        long begin = rb_reg_search(pattern, str, 0, 0);
        if (begin < 0)
                return self;

        const char *base = RSTRING_PTR(str);
        const char *p = base;
        const char *end = RSTRING_END(str);
        VALUE substituted = rb_u_str_buf_new(RSTRING_LEN(str) + 30);
        do {
                VALUE match = rb_backref_get();
                struct re_registers *registers = RMATCH_REGS(match);
                VALUE result;

                if (use_block || !NIL_P(replacements)) {
                        if (use_block) {
                                VALUE ustr = rb_u_string_new_rb(rb_reg_nth_match(0, match));
                                result = rb_u_string_object_as_string(rb_yield(ustr));
                        } else {
                                VALUE ustr = rb_u_string_new_c(self,
                                                               base + registers->beg[0],
                                                               registers->end[0] - registers->beg[0]);
                                result = rb_u_string_object_as_string(rb_hash_aref(replacements, ustr));
                        }

                        if (result == substituted)
                                rb_u_raise(rb_eRuntimeError,
                                           "result of block is string being built; please try not to cheat");
                } else
                        result =
#ifdef HAVE_RB_REG_REGSUB4
                        rb_reg_regsub(replacement, str, registers, pattern);
#else
                        rb_reg_regsub(replacement, str, registers);
#endif

                if (OBJ_TAINTED(result))
                        tainted = true;

                const struct rb_u_string *value = RVAL2USTRING_ANY(result);

                rb_str_buf_cat(substituted, p, registers->beg[0] - (p - base));
                rb_str_buf_cat(substituted, USTRING_STR(value), USTRING_LENGTH(value));
                OBJ_INFECT(substituted, result);

                p = base + registers->end[0];
                if (registers->beg[0] == registers->end[0])
                        p = u_next(p);
                if (p >= end)
                        break;

                begin = rb_reg_search(pattern, str, registers->end[0], 0);
        } while (begin >= 0);

        if (p < end)
                rb_str_buf_cat(substituted, p, end - p);

        rb_reg_search(pattern, str, end - p, 0);

        RBASIC(substituted)->klass = rb_obj_class(str);
        OBJ_INFECT(substituted, str);
        if (tainted)
                OBJ_TAINT(substituted);

        return rb_u_string_new_rb(substituted);
}
コード例 #14
0
ファイル: verse_ext.c プロジェクト: ged/ruby-verse
/*
 *  call-seq:
 *     Verse.host_id = string
 *
 *  Set the server's host id.
 *
 */
static VALUE
rbverse_verse_host_id_eq( VALUE module, VALUE id ) {
	VALUE idstring = rb_str_to_str( id );
	verse_host_id_set( rbverse_str2host_id(idstring) );
	return idstring;
}
コード例 #15
0
ファイル: subtend_string.c プロジェクト: Winstonwzl/rubinius
VALUE ss_str_to_str(VALUE self, VALUE arg) {
  return rb_str_to_str(arg);
}