class Coolio::Buffer

Constants

MAX_SIZE

Public Class Methods

Coolio::Buffer.default_node_size → 4096 click to toggle source

Retrieves the current value of the default node size.

static VALUE
Coolio_Buffer_default_node_size(VALUE klass)
{
    return UINT2NUM(default_node_size);
}
Coolio::Buffer.default_node_size = 16384 click to toggle source

Sets the default node size for calling Coolio::Buffer.new with no arguments.

static VALUE
Coolio_Buffer_set_default_node_size(VALUE klass, VALUE size)
{
    default_node_size = convert_node_size(size);

    return size;
}
Coolio::Buffer.new(size = Coolio::Buffer.default_node_size) → Coolio::Buffer click to toggle source

Create a new Coolio::Buffer with linked segments of the given size

static VALUE
Coolio_Buffer_initialize(int argc, VALUE * argv, VALUE self)
{
    VALUE           node_size_obj;
    struct buffer *buf;

    if (rb_scan_args(argc, argv, "01", &node_size_obj) == 1) {
        TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);

        /*
         * Make sure we're not changing the buffer size after data
         * has been allocated
         */
        assert(!buf->head);
        assert(!buf->pool_head);

        buf->node_size = convert_node_size(node_size_obj);
    }
    return Qnil;
}

Public Instance Methods

Coolio::Buffer#append(data) → String click to toggle source

Append the given data to the end of the buffer

static VALUE
Coolio_Buffer_append(VALUE self, VALUE data)
{
    struct buffer *buf;
    TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);

    /* Is this needed?  Never seen anyone else do it... */
    data = rb_convert_type(data, T_STRING, "String", "to_str");
    buffer_append(buf, RSTRING_PTR(data), RSTRING_LEN(data));

    return data;
}
Also aliased as: append, write
Coolio::Buffer#append(data) → String

Append the given data to the end of the buffer

Alias for: <<
Coolio::Buffer#clear → nil click to toggle source

Clear all data from the Coolio::Buffer

static VALUE
Coolio_Buffer_clear(VALUE self)
{
    struct buffer *buf;
    TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);

    buffer_clear(buf);

    return Qnil;
}
Coolio::Buffer#empty? → Boolean click to toggle source

Is the buffer empty?

static VALUE
Coolio_Buffer_empty(VALUE self)
{
    struct buffer *buf;
    TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);

    return buf->size > 0 ? Qfalse : Qtrue;
}
Coolio::Buffer#prepend(data) → String click to toggle source

Prepend the given data to the beginning of the buffer

static VALUE
Coolio_Buffer_prepend(VALUE self, VALUE data)
{
    struct buffer *buf;
    TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);

    data = rb_convert_type(data, T_STRING, "String", "to_str");
    buffer_prepend(buf, RSTRING_PTR(data), RSTRING_LEN(data));

    return data;
}
Coolio::Buffer#read(length = nil) → String click to toggle source

Read the specified abount of data from the buffer. If no value is given the entire contents of the buffer are returned. Any data read from the buffer is cleared. The given length must be greater than 0 or an exception would raise. If the buffer size is zero then an empty string is returned (regardless the given length).

static VALUE
Coolio_Buffer_read(int argc, VALUE * argv, VALUE self)
{
    VALUE  length_obj, str;
    int    length;
    struct buffer *buf;

    TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);

    if (rb_scan_args(argc, argv, "01", &length_obj) == 1) {
        length = NUM2INT(length_obj);
        if(length < 1)
          rb_raise(rb_eArgError, "length must be greater than zero");
        if(length > buf->size)
          length = buf->size;
    } else
        length = buf->size;

    if(buf->size == 0)
        return rb_str_new2("");

    str = rb_str_new(0, length);
    buffer_read(buf, RSTRING_PTR(str), length);

    return str;
}
Coolio::Buffer#read_frame(str, mark) → boolean click to toggle source

Read up to and including the given frame marker (expressed a a Fixnum 0-255) byte, copying into the supplied string object. If the mark is not encountered before the end of the buffer, false is returned but data is still copied into str. True is returned if the end of a frame is reached.

static VALUE
Coolio_Buffer_read_frame(VALUE self, VALUE data, VALUE mark)
{
    char mark_c = (char) NUM2INT(mark);
    struct buffer *buf;

    TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);

    if (buffer_read_frame(buf, data, mark_c)) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}
Coolio::Buffer#read_from(io) → Integer click to toggle source

Perform a nonblocking read of the the given IO object and fill the buffer with any data received. The call will read as much data as it can until the read would block.

static VALUE
Coolio_Buffer_read_from(VALUE self, VALUE io)
{
    struct buffer  *buf;
    int             ret;
#if defined(HAVE_RB_IO_T) || defined(HAVE_RB_IO_DESCRIPTOR)
    rb_io_t        *fptr;
#else
    OpenFile       *fptr;
#endif

    TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);
    io = rb_convert_type(io, T_FILE, "IO", "to_io");
    GetOpenFile(io, fptr);
    rb_io_set_nonblock(fptr);

#ifdef HAVE_RB_IO_DESCRIPTOR
    ret = buffer_read_from(buf, rb_io_descriptor(io));
#else
    ret = buffer_read_from(buf, FPTR_TO_FD(fptr));
#endif
    return ret == -1 ? Qnil : INT2NUM(ret);
}
Coolio::Buffer#size → Integer click to toggle source

Return the size of the buffer in bytes

static VALUE
Coolio_Buffer_size(VALUE self)
{
    struct buffer *buf;
    TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);

    return INT2NUM(buf->size);
}
Coolio::Buffer#to_str → String click to toggle source

Convert the Buffer to a String. The original buffer is unmodified.

static VALUE
Coolio_Buffer_to_str(VALUE self)
{
    VALUE          str;
    struct buffer *buf;

    TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);

    str = rb_str_new(0, buf->size);
    buffer_copy(buf, RSTRING_PTR(str), buf->size);

    return str;
}
Coolio::Buffer#append(data) → String

Append the given data to the end of the buffer

Alias for: <<
Coolio::Buffer#write_to(io) → Integer click to toggle source

Perform a nonblocking write of the buffer to the given IO object. As much data as possible is written until the call would block. Any data which is written is removed from the buffer.

static VALUE
Coolio_Buffer_write_to(VALUE self, VALUE io)
{
    struct buffer  *buf;
#if defined(HAVE_RB_IO_T) || defined(HAVE_RB_IO_DESCRIPTOR)
    rb_io_t        *fptr;
#else
    OpenFile       *fptr;
#endif

    TypedData_Get_Struct(self, struct buffer, &Coolio_Buffer_type, buf);
    io = rb_convert_type(io, T_FILE, "IO", "to_io");
    GetOpenFile(io, fptr);
    rb_io_set_nonblock(fptr);

#ifdef HAVE_RB_IO_DESCRIPTOR
    return INT2NUM(buffer_write_to(buf, rb_io_descriptor(io)));
#else
    return INT2NUM(buffer_write_to(buf, FPTR_TO_FD(fptr)));
#endif
}