17 #if defined(HAVE_FCNTL_H) || defined(_WIN32)
19 #elif defined(HAVE_SYS_FCNTL_H)
20 #include <sys/fcntl.h>
33 #define IS_STRIO(obj) (rb_typeddata_is_kind_of((obj), &strio_data_type))
34 #define error_inval(msg) (errno = EINVAL, rb_sys_fail(msg))
61 if (--ptr->
count <= 0) {
83 #define check_strio(self) ((struct StringIO*)rb_check_typeddata((self), &strio_data_type))
103 if (len > rlen) len = rlen;
104 if (len < 0) len = 0;
108 #define StringIO(obj) get_strio(obj)
110 #define CLOSED(ptr) (!((ptr)->flags & FMODE_READWRITE))
111 #define READABLE(ptr) ((ptr)->flags & FMODE_READABLE)
112 #define WRITABLE(ptr) ((ptr)->flags & FMODE_WRITABLE)
173 switch (
rb_scan_args(argc, argv,
"02", &
string, &mode)) {
178 trunc = flags & O_TRUNC;
443 if (copy == orig)
return copy;
485 #define strio_binmode strio_self
488 #define strio_fcntl strio_unimpl
491 #define strio_flush strio_self
494 #define strio_fsync strio_0
614 #define strio_set_sync strio_first
616 #define strio_tell strio_get_pos
690 if (pos + len > olen) {
746 if (p+clen >= pend)
break;
769 long pos = ptr->
pos, cl = 1;
780 if (cl == 0)
return Qnil;
788 memmove(s + cl, s + pos, rest);
892 for (c = 0; c < (1 <<
CHAR_BIT); c++) {
896 skip[(
unsigned char)*pat++] = m;
901 bm_search(
const char *little,
long llen,
const char *big,
long blen,
const long *skip)
909 while (j >= 0 && big[k] == little[j]) {
913 if (j < 0)
return k + 1;
914 i += skip[(
unsigned char)big[i]];
922 const char *s, *e, *
p;
958 if (limit > 0 && s + limit < e) {
972 while ((p = memchr(p,
'\n', e - p)) && (p != e)) {
981 if ((p = memchr(s,
RSTRING_PTR(str)[0], e - s)) != 0) {
989 for (p = s; p + n <= e; ++
p) {
1000 if ((pos =
bm_search(p, n, s, e - s, skip)) >= 0) {
1134 if (len == 0)
return INT2FIX(0);
1140 if (ptr->
pos == olen) {
1159 #define strio_addstr rb_io_addstr
1168 #define strio_print rb_io_print
1176 #define strio_printf rb_io_printf
1208 #define strio_puts rb_io_puts
1232 if (!
NIL_P(argv[0])) {
1247 if (len <= ptr->
pos) {
1269 if (len > rest) len = rest;
1298 #define strio_syswrite rb_io_write
1306 #define strio_isatty strio_false
1309 #define strio_pid strio_nil
1312 #define strio_fileno strio_nil
1324 if (
NIL_P(
string)) {
1396 VALUE ext_enc, int_enc, opt;
1398 argc =
rb_scan_args(argc, argv,
"11:", &ext_enc, &int_enc, &opt);
1400 if (
NIL_P(ext_enc)) {
static VALUE strio_closed_read(VALUE self)
static void check_modifiable(struct StringIO *ptr)
static VALUE strio_getbyte(VALUE self)
#define RSTRING_LEN(string)
static long NUM2LONG(VALUE x)
#define MEMCMP(p1, p2, type, n)
int rb_enc_codelen(int c, rb_encoding *enc)
RUBY_EXTERN VALUE rb_cData
static VALUE UINT2NUM(unsigned int v)
static VALUE strio_get_string(VALUE self)
void rb_enc_copy(VALUE obj1, VALUE obj2)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
VALUE rb_str_cat(VALUE, const char *, long)
static VALUE strio_closed(VALUE self)
#define TypedData_Wrap_Struct(klass, data_type, sval)
static void strio_free(void *p)
rb_encoding * rb_to_encoding(VALUE enc)
VALUE rb_enc_from_encoding(rb_encoding *encoding)
static VALUE strio_write(VALUE self, VALUE str)
VALUE rb_ary_push(VALUE ary, VALUE item)
static VALUE strio_readlines(int argc, VALUE *argv, VALUE self)
VALUE rb_str_concat(VALUE, VALUE)
static VALUE strio_readline(int argc, VALUE *argv, VALUE self)
#define RSTRING_PTR(string)
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
void rb_raise(VALUE exc, const char *fmt,...)
void rb_str_update(VALUE, long, long, VALUE)
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
static VALUE strio_copy(VALUE copy, VALUE orig)
VALUE rb_io_taint_check(VALUE)
VALUE rb_convert_type(VALUE, int, const char *, const char *)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
static VALUE strio_close_write(VALUE self)
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
static VALUE strio_getline(int argc, VALUE *argv, struct StringIO *ptr)
void rb_include_module(VALUE klass, VALUE module)
void rb_gc_mark(VALUE ptr)
static VALUE strio_set_encoding(int argc, VALUE *argv, VALUE self)
static VALUE strio_truncate(VALUE self, VALUE len)
static VALUE strio_readbyte(VALUE self)
static VALUE strio_closed_write(VALUE self)
RUBY_EXTERN void * memmove(void *, const void *, size_t)
static VALUE strio_get_pos(VALUE self)
#define MEMZERO(p, type, n)
static VALUE strio_close_read(VALUE self)
rb_encoding * rb_default_external_encoding(void)
static VALUE strio_gets(int argc, VALUE *argv, VALUE self)
VALUE rb_class_new_instance(int, VALUE *, VALUE)
static VALUE strio_s_allocate(VALUE klass)
int rb_block_given_p(void)
#define rb_io_modenum_flags(oflags)
static VALUE strio_close(VALUE self)
VALUE rb_obj_as_string(VALUE)
static VALUE strio_internal_encoding(VALUE self)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
static void strio_init(int, VALUE *, struct StringIO *)
static VALUE strio_get_lineno(VALUE self)
static struct StringIO * writable(struct StringIO *ptr)
static VALUE strio_0(VALUE self)
static VALUE strio_set_lineno(VALUE self, VALUE lineno)
void rb_lastline_set(VALUE)
static VALUE strio_self(VALUE self)
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
static void strio_mark(void *p)
#define MEMCPY(p1, p2, type, n)
static VALUE strio_read(int argc, VALUE *argv, VALUE self)
static VALUE strio_each(int argc, VALUE *argv, VALUE self)
static VALUE strio_getc(VALUE self)
VALUE rb_str_resize(VALUE, long)
static VALUE strio_readchar(VALUE self)
static VALUE strio_each_char(VALUE self)
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
#define RB_GC_GUARD(object)
#define check_strio(self)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
static VALUE strio_each_codepoint(VALUE self)
static VALUE strio_first(VALUE self, VALUE arg)
unsigned char buf[MIME_BUF_SIZE]
static VALUE strio_unimpl(int argc, VALUE *argv, VALUE self)
static VALUE strio_putc(VALUE self, VALUE ch)
register unsigned int len
VALUE rb_call_super(int, const VALUE *)
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
#define rb_io_mode_flags(modestr)
void rb_sys_fail(const char *mesg)
static VALUE strio_set_pos(VALUE self, VALUE pos)
static VALUE strio_seek(int argc, VALUE *argv, VALUE self)
static struct StringIO * readable(struct StringIO *ptr)
VALUE rb_define_module_under(VALUE outer, const char *name)
#define StringValueCStr(v)
static size_t strio_memsize(const void *p)
static VALUE strio_sysread(int argc, VALUE *argv, VALUE self)
#define rb_enc_right_char_head(s, p, e, enc)
void rb_str_modify(VALUE)
static VALUE strio_rewind(VALUE self)
rb_encoding * rb_enc_get(VALUE obj)
static VALUE ULONG2NUM(unsigned long v)
static void strio_extend(struct StringIO *ptr, long pos, long len)
static VALUE strio_initialize(int argc, VALUE *argv, VALUE self)
static VALUE strio_reopen(int argc, VALUE *argv, VALUE self)
static void bm_init_skip(long *skip, const char *pat, long m)
static VALUE strio_s_open(int argc, VALUE *argv, VALUE klass)
VALUE rb_check_string_type(VALUE)
static VALUE strio_ungetbyte(VALUE self, VALUE c)
static struct StringIO * get_strio(VALUE self)
void rb_notimplement(void)
static VALUE strio_ungetc(VALUE self, VALUE c)
#define RETURN_ENUMERATOR(obj, argc, argv)
#define SafeStringValue(v)
static VALUE strio_each_byte(VALUE self)
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
static VALUE strio_size(VALUE self)
static VALUE strio_false(VALUE self)
static char NUM2CHR(VALUE x)
RUBY_EXTERN VALUE rb_eIOError
static VALUE strio_external_encoding(VALUE self)
static VALUE strio_finalize(VALUE self)
static long bm_search(const char *little, long llen, const char *big, long blen, const long *skip)
rb_encoding * rb_ascii8bit_encoding(void)
#define rb_enc_mbcput(c, buf, enc)
static VALUE strio_set_string(VALUE self, VALUE string)
static VALUE strio_get_sync(VALUE self)
static VALUE strio_eof(VALUE self)
static const rb_data_type_t strio_data_type
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
static VALUE strio_nil(VALUE self)
VALUE rb_str_new(const char *, long)
static VALUE strio_substr(struct StringIO *ptr, long pos, long len)
static struct StringIO * strio_alloc(void)