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 STRIO_READABLE FL_USER4
111 #define STRIO_WRITABLE FL_USER5
112 #define STRIO_READWRITE (STRIO_READABLE|STRIO_WRITABLE)
114 #define STRIO_MODE_SET_P(strio, mode) \
115 ((RBASIC(strio)->flags & STRIO_##mode) && \
116 ((struct StringIO*)DATA_PTR(strio))->flags & FMODE_##mode)
117 #define CLOSED(strio) (!STRIO_MODE_SET_P(strio, READWRITE))
118 #define READABLE(strio) STRIO_MODE_SET_P(strio, READABLE)
119 #define WRITABLE(strio) STRIO_MODE_SET_P(strio, WRITABLE)
182 switch (
rb_scan_args(argc, argv,
"02", &
string, &mode)) {
187 trunc = flags & O_TRUNC;
454 if (copy == orig)
return copy;
498 #define strio_binmode strio_self
501 #define strio_fcntl strio_unimpl
504 #define strio_flush strio_self
507 #define strio_fsync strio_0
627 #define strio_set_sync strio_first
629 #define strio_tell strio_get_pos
658 rb_warn(
"StringIO#bytes is deprecated; use #each_byte instead");
712 if (pos + len > olen) {
769 if (p+clen >= pend)
break;
792 long pos = ptr->
pos, cl = 1;
803 if (cl == 0)
return Qnil;
812 memmove(s + cl, s + pos, rest);
878 rb_warn(
"StringIO#chars is deprecated; use #each_char instead");
922 rb_warn(
"StringIO#codepoints is deprecated; use #each_codepoint instead");
934 for (c = 0; c < (1 <<
CHAR_BIT); c++) {
938 skip[(
unsigned char)*pat++] = m;
943 bm_search(
const char *little,
long llen,
const char *big,
long blen,
const long *skip)
951 while (j >= 0 && big[k] == little[j]) {
955 if (j < 0)
return k + 1;
956 i += skip[(
unsigned char)big[i]];
964 const char *
s, *
e, *
p;
1000 if (limit > 0 && s + limit < e) {
1008 while (*p ==
'\n') {
1014 while ((p = memchr(p,
'\n', e - p)) && (p != e)) {
1023 if ((p = memchr(s,
RSTRING_PTR(str)[0], e - s)) != 0) {
1031 for (p = s; p + n <=
e; ++
p) {
1042 if ((pos =
bm_search(p, n, s, e - s, skip)) >= 0) {
1126 rb_warn(
"StringIO#lines is deprecated; use #each_line instead");
1184 if (len == 0)
return INT2FIX(0);
1190 if (ptr->
pos == olen) {
1209 #define strio_addstr rb_io_addstr
1218 #define strio_print rb_io_print
1226 #define strio_printf rb_io_printf
1258 #define strio_puts rb_io_puts
1282 if (!
NIL_P(argv[0])) {
1297 if (len <= ptr->
pos) {
1319 if (len > rest) len = rest;
1348 #define strio_syswrite rb_io_write
1356 #define strio_isatty strio_false
1359 #define strio_pid strio_nil
1362 #define strio_fileno strio_nil
1374 if (
NIL_P(
string)) {
1446 VALUE ext_enc, int_enc, opt;
1448 argc =
rb_scan_args(argc, argv,
"11:", &ext_enc, &int_enc, &opt);
1450 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 RB_TYPE_P(obj, type)
int rb_enc_codelen(int c, rb_encoding *enc)
static VALUE strio_get_string(VALUE self)
void rb_enc_copy(VALUE obj1, VALUE obj2)
VALUE rb_class_new_instance(int, VALUE *, VALUE)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
static VALUE strio_closed(VALUE self)
RUBY_EXTERN void * memmove(void *, const void *, size_t)
static void strio_free(void *p)
#define rb_enc_right_char_head(s, p, e, enc)
rb_encoding * rb_to_encoding(VALUE enc)
VALUE rb_enc_from_encoding(rb_encoding *encoding)
static VALUE strio_write(VALUE self, VALUE str)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
static VALUE strio_codepoints(VALUE self)
VALUE rb_ary_push(VALUE ary, VALUE item)
static VALUE strio_readlines(int argc, VALUE *argv, VALUE self)
char strio_flags_check[(STRIO_READABLE/FMODE_READABLE==STRIO_WRITABLE/FMODE_WRITABLE)*2-1]
void rb_lastline_set(VALUE)
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
static VALUE strio_readline(int argc, VALUE *argv, VALUE self)
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,...)
#define MEMCMP(p1, p2, type, n)
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
static VALUE strio_copy(VALUE copy, VALUE orig)
#define RETURN_ENUMERATOR(obj, argc, argv)
VALUE rb_io_taint_check(VALUE)
static VALUE strio_close_write(VALUE self)
#define SafeStringValue(v)
RUBY_EXTERN VALUE rb_eIOError
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)
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)
VALUE rb_str_concat(VALUE, VALUE)
static VALUE strio_closed_write(VALUE self)
static VALUE strio_get_pos(VALUE self)
#define MEMZERO(p, type, n)
VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv)
void rb_str_update(VALUE, long, long, VALUE)
static VALUE strio_close_read(VALUE self)
rb_encoding * rb_default_external_encoding(void)
memset(y->frac+ix+1, 0,(y->Prec-(ix+1))*sizeof(BDIGIT))
static VALUE strio_gets(int argc, VALUE *argv, VALUE self)
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)
static struct StringIO * writable(VALUE strio)
#define StringValueCStr(v)
static VALUE strio_internal_encoding(VALUE self)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
static VALUE strio_get_lineno(VALUE self)
static VALUE strio_0(VALUE self)
static VALUE strio_set_lineno(VALUE self, VALUE lineno)
static VALUE strio_self(VALUE self)
static void strio_mark(void *p)
VALUE rb_call_super(int, const VALUE *)
#define TypedData_Wrap_Struct(klass, data_type, sval)
unsigned char buf[MIME_BUF_SIZE]
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)
static VALUE strio_readchar(VALUE self)
VALUE rb_obj_as_string(VALUE)
VALUE rb_str_resize(VALUE, long)
static VALUE strio_each_char(VALUE self)
static void strio_init(int, VALUE *, struct StringIO *, VALUE)
#define rb_enc_mbcput(c, buf, enc)
#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)
static VALUE strio_unimpl(int argc, VALUE *argv, VALUE self)
static VALUE strio_putc(VALUE self, VALUE ch)
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 VALUE strio_bytes(VALUE self)
#define MEMCPY(p1, p2, type, n)
VALUE rb_define_module_under(VALUE outer, const char *name)
static size_t strio_memsize(const void *p)
static VALUE strio_sysread(int argc, VALUE *argv, VALUE self)
void rb_str_modify(VALUE)
static VALUE strio_chars(VALUE self)
VALUE rb_str_cat(VALUE, const char *, long)
static VALUE strio_rewind(VALUE self)
rb_encoding * rb_enc_get(VALUE obj)
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
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)
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)
static VALUE strio_each_byte(VALUE self)
VALUE rb_str_new(const char *, long)
static VALUE strio_size(VALUE self)
static VALUE strio_false(VALUE self)
static struct StringIO * readable(VALUE strio)
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)
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
rb_encoding * rb_ascii8bit_encoding(void)
static VALUE strio_set_string(VALUE self, VALUE string)
static VALUE strio_get_sync(VALUE self)
RUBY_EXTERN VALUE rb_cData
static VALUE strio_eof(VALUE self)
VALUE rb_check_string_type(VALUE)
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_lines(int argc, VALUE *argv, VALUE self)
static VALUE strio_nil(VALUE self)
void rb_warn(const char *fmt,...)
VALUE rb_convert_type(VALUE, int, const char *, const char *)
STATIC void unsigned char * cp
static VALUE strio_substr(struct StringIO *ptr, long pos, long len)
static struct StringIO * strio_alloc(void)