1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/* -*- c-file-style: "gnu"; indent-tabs-mode: nil -*- */
/*
* Ruby Cairo Binding
*
* Copyright 2005-2021 Sutou Kouhei <kou@cozmixng.org>
*
* This file is made available under the same terms as Ruby
*
*/
#include "rb_cairo.h"
#include "rb_cairo_private.h"
#include "rb_cairo_io.h"
ID rb_cairo__io_id_read;
ID rb_cairo__io_id_write;
ID rb_cairo__io_id_output;
ID rb_cairo__io_id_to_io;
ID rb_cairo__io_id_to_path;
/* read/write callback */
rb_cairo__io_callback_closure_t *
rb_cairo__io_closure_new (VALUE target)
{
rb_cairo__io_callback_closure_t *closure;
closure = ALLOC (rb_cairo__io_callback_closure_t);
closure->target = target;
closure->error = Qnil;
return closure;
}
void
rb_cairo__io_closure_destroy (rb_cairo__io_callback_closure_t *closure)
{
xfree (closure);
}
void
rb_cairo__io_closure_free (void *closure)
{
rb_cairo__io_closure_destroy ((rb_cairo__io_callback_closure_t *) closure);
}
static VALUE
rb_cairo__io_func_rescue (VALUE io_closure, VALUE error)
{
rb_cairo__io_callback_closure_t *closure;
closure = (rb_cairo__io_callback_closure_t *)io_closure;
closure->error = error;
return Qnil;
}
static VALUE
rb_cairo__io_func_invoke (VALUE user_data)
{
rb_cairo__io_invoke_data_t *data;
data = (rb_cairo__io_invoke_data_t *)user_data;
return rb_rescue2 (data->func, data->data,
rb_cairo__io_func_rescue, data->data, rb_eException,
(VALUE)0);
}
/* write callback */
static VALUE
rb_cairo__io_write_func_invoke (VALUE write_closure)
{
VALUE output, data;
long written_bytes;
rb_cairo__io_callback_closure_t *closure;
long length;
closure = (rb_cairo__io_callback_closure_t *)write_closure;
output = closure->target;
data = rb_str_new ((const char *)closure->data, closure->length);
length = RSTRING_LEN (data);
while (length != 0)
{
VALUE rb_written_bytes = rb_funcall (output,
rb_cairo__io_id_write, 1, data);
written_bytes = NUM2LONG (rb_written_bytes);
data = rb_str_substr (data, written_bytes,
RSTRING_LEN (data) - written_bytes);
length -= written_bytes;
}
return Qnil;
}
cairo_status_t
rb_cairo__io_write_func (void *write_closure,
const unsigned char *data, unsigned int length)
{
rb_cairo__io_callback_closure_t *closure;
rb_cairo__io_invoke_data_t invoke_data;
closure = (rb_cairo__io_callback_closure_t *)write_closure;
closure->data = (unsigned char *)data;
closure->length = length;
invoke_data.func = rb_cairo__io_write_func_invoke;
invoke_data.data = (VALUE)closure;
rb_cairo__invoke_callback (rb_cairo__io_func_invoke, (VALUE)&invoke_data);
if (NIL_P (closure->error))
return CAIRO_STATUS_SUCCESS;
else
return CAIRO_STATUS_WRITE_ERROR;
}
/* read callback */
static VALUE
rb_cairo__io_read_func_invoke (VALUE read_closure)
{
VALUE input, result;
rb_cairo__io_callback_closure_t *closure;
long length, rest;
closure = (rb_cairo__io_callback_closure_t *)read_closure;
input = closure->target;
length = closure->length;
result = rb_str_new2 ("");
for (rest = length; rest != 0; rest = length - RSTRING_LEN (result))
{
rb_str_concat (result,
rb_funcall (input,
rb_cairo__io_id_read, 1, LONG2NUM (rest)));
}
memcpy ((void *)closure->data, (const void *) StringValuePtr (result), length);
return Qnil;
}
cairo_status_t
rb_cairo__io_read_func (void *read_closure,
unsigned char *data, unsigned int length)
{
rb_cairo__io_callback_closure_t *closure;
rb_cairo__io_invoke_data_t invoke_data;
closure = (rb_cairo__io_callback_closure_t *)read_closure;
closure->data = data;
closure->length = length;
invoke_data.func = rb_cairo__io_read_func_invoke;
invoke_data.data = (VALUE)closure;
rb_cairo__invoke_callback (rb_cairo__io_func_invoke, (VALUE)&invoke_data);
if (NIL_P (closure->error))
return CAIRO_STATUS_SUCCESS;
else
return CAIRO_STATUS_READ_ERROR;
}
void
Init_cairo_io (void)
{
rb_cairo__io_id_read = rb_intern ("read");
rb_cairo__io_id_write = rb_intern ("write");
rb_cairo__io_id_output = rb_intern ("output");
rb_cairo__io_id_to_io = rb_intern ("to_io");
rb_cairo__io_id_to_path = rb_intern ("to_path");
}
|