Пример #1
0
static VALUE
rb_warn_m(VALUE self, VALUE mesg)
{
    if (!NIL_P(ruby_verbose)) {
	rb_io_write(rb_stderr, mesg);
	rb_io_write(rb_stderr, rb_default_rs);
    }
    return Qnil;
}
Пример #2
0
static VALUE
rb_fairy_xmarshaled_queue_store_raw(VALUE self, VALUE raw)
{
  VALUE tmpbuf;
  VALUE io;
  fairy_xmarshaled_queue_t *mq;

  GetFairyXMarshaledQueuePtr(self, mq);

  if (mq->log_mstore_p) {
    rb_fairy_debug(self, "START M.STORE");
  }
  
  if (NIL_P(mq->buffer_dir)) {
    tmpbuf = rb_funcall(rb_cFairyFastTempfile, id_open, 1,
			rb_str_new2("port-buffer-"));
  }
  else {
    tmpbuf = rb_funcall(rb_cFairyFastTempfile, id_open, 2,
			rb_str_new2("port-buffer-"),
			mq->buffer_dir);
  }
 
  io = rb_funcall(tmpbuf, id_io, 0);
  rb_io_write(io, raw);
  rb_funcall(tmpbuf, id_close, 0);
  
  if (mq->log_mstore_p) {
    rb_fairy_debug(self, "FINISH M.STORE");
  }
  return tmpbuf;
}
Пример #3
0
/*
 * Document-method: encode
 *
 * call-seq: encode(obj[, io[, &block]])
 *
 * +obj+ is the Ruby object to encode to JSON
 *
 * +io+ is an optional IO used to stream the encoded JSON string to.
 * If +io+ isn't specified, this method will return the resulting JSON string. If +io+ is specified, this method returns nil
 *
 * If an optional block is passed, it's called when encoding is complete and passed the resulting JSON string
 *
 * It should be noted that you can reuse an instance of this class to continue encoding multiple JSON
 * to the same stream. Just continue calling this method, passing it the same IO object with new/different
 * ruby objects to encode. This is how streaming is accomplished.
 */
static VALUE rb_yajl_encoder_encode(int argc, VALUE * argv, VALUE self) {
    yajl_encoder_wrapper * wrapper;
    const unsigned char * buffer;
    unsigned int len;
    VALUE obj, io, blk, outBuff;

    GetEncoder(self, wrapper);

    rb_scan_args(argc, argv, "11&", &obj, &io, &blk);

    if (blk != Qnil) {
        wrapper->on_progress_callback = blk;
    }

    /* begin encode process */
    yajl_encode_part(wrapper, obj, io);

    /* just make sure we output the remaining buffer */
    yajl_gen_get_buf(wrapper->encoder, &buffer, &len);
    outBuff = rb_str_new((const char *)buffer, len);
#ifdef HAVE_RUBY_ENCODING_H
    rb_enc_associate(outBuff, utf8Encoding);
#endif
    yajl_gen_clear(wrapper->encoder);

    if (io != Qnil) {
        rb_io_write(io, outBuff);
        if (wrapper->terminator != 0 && wrapper->terminator != Qnil) {
            rb_io_write(io, wrapper->terminator);
        }
        return Qnil;
    } else if (blk != Qnil) {
        rb_funcall(blk, intern_call, 1, outBuff);
        if (wrapper->terminator != 0) {
            rb_funcall(blk, intern_call, 1, wrapper->terminator);
        }
        return Qnil;
    } else {
        if (wrapper->terminator != 0 && wrapper->terminator != Qnil) {
            rb_str_concat(outBuff, wrapper->terminator);
        }
        return outBuff;
    }
    return Qnil;
}
Пример #4
0
static void
prompt(int argc, VALUE *argv, VALUE io)
{
    if (argc > 0 && !NIL_P(argv[0])) {
	VALUE str = argv[0];
	StringValueCStr(str);
	rb_check_safe_obj(str);
	rb_io_write(io, str);
    }
}
Пример #5
0
static void header_done(void *data, const char *at, size_t length)
{
    VALUE req = (VALUE)data;
    VALUE temp = Qnil;
    VALUE ctype = Qnil;
    VALUE clen = Qnil;
    VALUE body = Qnil;
    char *colon = NULL;

    clen = rb_hash_aref(req, global_http_content_length);
    if(clen != Qnil) {
        rb_hash_aset(req, global_content_length, clen);
        rb_hash_delete(req, global_http_content_length);
    }

    ctype = rb_hash_aref(req, global_http_content_type);
    if(ctype != Qnil) {
        rb_hash_aset(req, global_content_type, ctype);
        rb_hash_delete(req, global_http_content_type);
    }

    rb_hash_aset(req, global_gateway_interface, global_gateway_interface_value);
    if((temp = rb_hash_aref(req, global_http_host)) != Qnil) {
        /* ruby better close strings off with a '\0' dammit */
        colon = strchr(RSTRING_PTR(temp), ':');
        if(colon != NULL) {
            rb_hash_aset(req, global_server_name, rb_str_substr(temp, 0, colon - RSTRING_PTR(temp)));
            rb_hash_aset(req, global_server_port,
                         rb_str_substr(temp, colon - RSTRING_PTR(temp)+1,
                                       RSTRING_LEN(temp)));
        } else {
            rb_hash_aset(req, global_server_name, temp);
            rb_hash_aset(req, global_server_port, global_port_80);
        }
    }

    /* grab the initial body and stuff it into the hash */
    if(length > 0) {
        body = rb_hash_aref(req, global_http_body);
        rb_io_write(body, rb_str_new(at, length));
    }

    /* according to Rack specs, query string must be empty string if none */
    if (rb_hash_aref(req, global_query_string) == Qnil) {
        rb_hash_aset(req, global_query_string, global_empty);
    }
    if (rb_hash_aref(req, global_path_info) == Qnil) {
        rb_hash_aset(req, global_path_info, global_empty);
    }

    /* set some constants */
    rb_hash_aset(req, global_server_protocol, global_server_protocol_value);
    rb_hash_aset(req, global_url_scheme, global_url_scheme_value);
    rb_hash_aset(req, global_script_name, global_empty);
}
Пример #6
0
static void
w_nbyte(const char *s, int n, struct dump_arg *arg)
{
    VALUE buf = arg->str;
    rb_str_buf_cat(buf, s, n);
    if (arg->dest && RSTRING_LEN(buf) >= BUFSIZ) {
	if (arg->taint) OBJ_TAINT(buf);
	rb_io_write(arg->dest, buf);
	rb_str_resize(buf, 0);
    }
}
Пример #7
0
static void
w_nbyte(const char *s, long n, struct dump_arg *arg)
{
    VALUE buf = arg->str;
    rb_str_buf_cat(buf, s, n);
    RBASIC(buf)->flags |= arg->infection;
    if (arg->dest && RSTRING_LEN(buf) >= BUFSIZ) {
	rb_io_write(arg->dest, buf);
	rb_str_resize(buf, 0);
    }
}
Пример #8
0
static void
oldbt_print_to(void *data, VALUE file, int lineno, VALUE name)
{
    VALUE output = (VALUE)data;
    VALUE str = rb_sprintf("\tfrom %"PRIsVALUE":%d:in ", file, lineno);

    if (NIL_P(name)) {
	rb_str_cat2(str, "unknown method\n");
    }
    else {
	rb_str_catf(str, " `%"PRIsVALUE"'\n", name);
    }
    rb_io_write(output, str);
}
Пример #9
0
static void
w_nbyte(const char *s, int n, struct dump_arg *arg)
{
    VALUE buf = arg->str;
    rb_bstr_concat(buf, (const uint8_t *)s, n);
#if 0 // unused
    if (arg->dest && RSTRING_LEN(buf) >= BUFSIZ) {
	if (arg->taint) {
	    OBJ_TAINT(buf);
	}
	rb_io_write(arg->dest, buf);
	rb_str_resize(buf, 0);
    }
#endif
}
Пример #10
0
void wankelEncoder_flush(yajl_gen g, VALUE io, int write_buffer_size) {
	VALUE rbBuffer;
    yajl_gen_status status;
    const unsigned char * buffer;
    size_t len;
	
	if (io != Qnil) {
		status = yajl_gen_get_buf(g, &buffer, &len);
		yajl_helper_check_gen_status(status);
		
		if (len >= (size_t)write_buffer_size) {
			rbBuffer = rb_str_new((const char *)buffer, len);
			rb_enc_associate(rbBuffer, rb_utf8_encoding());
			rb_io_write(io, rbBuffer);
			yajl_gen_clear(g);
		}
	}
}
Пример #11
0
VALUE io_spec_rb_io_write(VALUE self, VALUE io, VALUE str) {
  return rb_io_write(io, str);
}
Пример #12
0
void yajl_encode_part(void * wrapper, VALUE obj, VALUE io) {
    VALUE str, outBuff, otherObj;
    yajl_encoder_wrapper * w = wrapper;
    yajl_gen_status status;
    int idx = 0;
    const unsigned char * buffer;
    const char * cptr;
    unsigned int len;

    if (io != Qnil || w->on_progress_callback != Qnil) {
        status = yajl_gen_get_buf(w->encoder, &buffer, &len);
        if (len >= WRITE_BUFSIZE) {
            outBuff = rb_str_new((const char *)buffer, len);
            if (io != Qnil) {
                rb_io_write(io, outBuff);
            } else if (w->on_progress_callback != Qnil) {
                rb_funcall(w->on_progress_callback, intern_call, 1, outBuff);
            }
            yajl_gen_clear(w->encoder);
        }
    }

    switch (TYPE(obj)) {
        case T_HASH:
            status = yajl_gen_map_open(w->encoder);

            /* TODO: itterate through keys in the hash */
            VALUE keys = rb_funcall(obj, intern_keys, 0);
            VALUE entry, keyStr;
            for(idx=0; idx<RARRAY_LEN(keys); idx++) {
                entry = rb_ary_entry(keys, idx);
                keyStr = rb_funcall(entry, intern_to_s, 0); /* key must be a string */
                /* the key */
                yajl_encode_part(w, keyStr, io);
                /* the value */
                yajl_encode_part(w, rb_hash_aref(obj, entry), io);
            }

            status = yajl_gen_map_close(w->encoder);
            break;
        case T_ARRAY:
            status = yajl_gen_array_open(w->encoder);
            for(idx=0; idx<RARRAY_LEN(obj); idx++) {
                otherObj = rb_ary_entry(obj, idx);
                yajl_encode_part(w, otherObj, io);
            }
            status = yajl_gen_array_close(w->encoder);
            break;
        case T_NIL:
            status = yajl_gen_null(w->encoder);
            break;
        case T_TRUE:
            status = yajl_gen_bool(w->encoder, 1);
            break;
        case T_FALSE:
            status = yajl_gen_bool(w->encoder, 0);
            break;
        case T_FIXNUM:
        case T_FLOAT:
        case T_BIGNUM:
            str = rb_funcall(obj, intern_to_s, 0);
            cptr = RSTRING_PTR(str);
            len = RSTRING_LEN(str);
            if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) {
                rb_raise(cEncodeError, "'%s' is an invalid number", cptr);
            }
            status = yajl_gen_number(w->encoder, cptr, len);
            break;
        case T_STRING:
            cptr = RSTRING_PTR(obj);
            len = RSTRING_LEN(obj);
            status = yajl_gen_string(w->encoder, (const unsigned char *)cptr, len);
            break;
        default:
            if (rb_respond_to(obj, intern_to_json)) {
                str = rb_funcall(obj, intern_to_json, 0);
                cptr = RSTRING_PTR(str);
                len = RSTRING_LEN(str);
                status = yajl_gen_number(w->encoder, cptr, len);
            } else {
                str = rb_funcall(obj, intern_to_s, 0);
                cptr = RSTRING_PTR(str);
                len = RSTRING_LEN(str);
                status = yajl_gen_string(w->encoder, (const unsigned char *)cptr, len);
            }
            break;
    }
}
Пример #13
0
static void write_chars_to_file (ABSTRACT_WRITER* OutputFile, const char* begin, const char* endnext) {
    rb_io_write((VALUE)OutputFile, rb_str_new(begin, endnext - begin));
}
Пример #14
0
static VALUE
puts_call(VALUE io)
{
    return rb_io_write(io, rb_default_rs);
}