17 #if defined(HAVE_FCNTL_H) || defined(_WIN32) 19 #elif defined(HAVE_SYS_FCNTL_H) 20 #include <sys/fcntl.h> 35 #define IS_STRIO(obj) (rb_typeddata_is_kind_of((obj), &strio_data_type)) 36 #define error_inval(msg) (rb_syserr_fail(EINVAL, msg)) 37 #define get_enc(ptr) ((ptr)->enc ? (ptr)->enc : rb_enc_get((ptr)->string)) 64 if (--ptr->
count <= 0) {
85 #define check_strio(self) ((struct StringIO*)rb_check_typeddata((self), &strio_data_type)) 113 if (len > rlen) len = rlen;
114 if (len < 0) len = 0;
119 #define StringIO(obj) get_strio(obj) 121 #define STRIO_READABLE FL_USER4 122 #define STRIO_WRITABLE FL_USER5 123 #define STRIO_READWRITE (STRIO_READABLE|STRIO_WRITABLE) 125 #define STRIO_MODE_SET_P(strio, mode) \ 126 ((RBASIC(strio)->flags & STRIO_##mode) && \ 127 ((struct StringIO*)DATA_PTR(strio))->flags & FMODE_##mode) 128 #define CLOSED(strio) (!STRIO_MODE_SET_P(strio, READWRITE)) 129 #define READABLE(strio) STRIO_MODE_SET_P(strio, READABLE) 130 #define WRITABLE(strio) STRIO_MODE_SET_P(strio, WRITABLE) 191 switch (
rb_scan_args(argc, argv,
"02", &
string, &mode)) {
196 trunc = flags & O_TRUNC;
474 if (copy == orig)
return copy;
530 #define strio_fcntl strio_unimpl 532 #define strio_flush strio_self 534 #define strio_fsync strio_0 633 if (amount >
LONG_MAX - offset || amount + offset < 0) {
636 ptr->
pos = amount + offset;
653 #define strio_set_sync strio_first 655 #define strio_tell strio_get_pos 684 rb_warn(
"StringIO#bytes is deprecated; use #each_byte instead");
743 if (pos + len > olen) {
816 if (cl == 0)
return Qnil;
833 long ex = (rest < 0 ? cl-
pos : cl+rest);
837 if (rest < 0)
memmove(s + cl, s + pos, -rest);
846 if (rest > cl) memset(s + len, 0, rest - cl);
908 rb_warn(
"StringIO#chars is deprecated; use #each_char instead");
952 rb_warn(
"StringIO#codepoints is deprecated; use #each_codepoint instead");
964 for (c = 0; c < (1 <<
CHAR_BIT); c++) {
968 skip[(
unsigned char)*pat++] = m;
973 bm_search(
const char *little,
long llen,
const char *big,
long blen,
const long *skip)
981 while (j >= 0 && big[k] == little[j]) {
985 if (j < 0)
return k + 1;
986 i += skip[(
unsigned char)big[i]];
1000 VALUE str, lim, opts;
1003 argc =
rb_scan_args(argc, argv,
"02:", &str, &lim, &opts);
1031 static ID keywords[1];
1045 if (e > s && *--e ==
'\n') {
1046 if (e > s && *--e ==
'\r')
return 2;
1055 const char *s, *e, *p;
1066 if (limit > 0 && (
size_t)limit < (
size_t)(e - s)) {
1077 while (p[(p + 1 < e) && (*p ==
'\r') && 0] ==
'\n') {
1084 while ((p = memchr(p,
'\n', e - p)) && (p != e)) {
1087 w = (arg->
chomp ? 1 : 0);
1090 else if (*p ==
'\r' && p < e && p[1] ==
'\n') {
1092 w = (arg->
chomp ? 2 : 0);
1096 if (!w && arg->
chomp) {
1102 if ((p = memchr(s,
RSTRING_PTR(str)[0], e - s)) != 0) {
1104 w = (arg->
chomp ? (p > s && *(p-1) ==
'\r') + 1 : 0);
1111 for (p = s; p + n <= e; ++p) {
1113 e = p + (arg->
chomp ? 0 : n);
1122 if ((pos =
bm_search(p, n, s, e - s, skip)) >= 0) {
1123 e = s + pos + (arg->
chomp ? 0 : n);
1212 rb_warn(
"StringIO#lines is deprecated; use #each_line instead");
1266 if (enc != enc2 && enc != ascii8bit) {
1270 if (len == 0)
return INT2FIX(0);
1276 if (ptr->
pos == olen) {
1277 if (enc == ascii8bit || enc2 == ascii8bit) {
1302 #define strio_addstr rb_io_addstr 1311 #define strio_print rb_io_print 1319 #define strio_printf rb_io_printf 1351 #define strio_puts rb_io_puts 1376 if (!
NIL_P(argv[0])) {
1391 if (len <= ptr->
pos) {
1411 if (len > rest) len = rest;
1471 #define strio_syswrite rb_io_write 1482 #define strio_isatty strio_false 1484 #define strio_pid strio_nil 1486 #define strio_fileno strio_nil 1499 if (
NIL_P(
string)) {
1572 VALUE ext_enc, int_enc, opt;
1574 argc =
rb_scan_args(argc, argv,
"11:", &ext_enc, &int_enc, &opt);
1576 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)
static VALUE enc_subseq(VALUE str, long pos, long len, rb_encoding *enc)
#define MEMCMP(p1, p2, type, n)
int rb_enc_codelen(int c, rb_encoding *enc)
RUBY_EXTERN VALUE rb_cData
static VALUE strio_get_string(VALUE self)
void rb_enc_copy(VALUE obj1, VALUE obj2)
#define RUBY_TYPED_FREE_IMMEDIATELY
void rb_syserr_fail(int e, const char *mesg)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
static VALUE strio_closed(VALUE self)
static VALUE strio_read_nonblock(int argc, VALUE *argv, VALUE self)
static VALUE sym_exception
#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)
static VALUE strio_codepoints(VALUE self)
VALUE rb_ary_push(VALUE ary, VALUE item)
static VALUE strio_readlines(int argc, VALUE *argv, VALUE self)
void rb_str_set_len(VALUE, long)
static VALUE strio_readline(int argc, VALUE *argv, VALUE self)
int rb_io_modestr_fmode(const char *modestr)
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,...)
char strio_flags_check[(STRIO_READABLE/FMODE_READABLE==STRIO_WRITABLE/FMODE_WRITABLE) *2 - 1]
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 int chomp_newline_width(const char *s, const char *e)
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)
VALUE rb_str_buf_append(VALUE, VALUE)
static VALUE strio_closed_write(VALUE self)
RUBY_EXTERN void * memmove(void *, const void *, size_t)
static VALUE strio_get_pos(VALUE self)
#define RB_TYPE_P(obj, type)
static VALUE strio_syswrite_nonblock(int argc, VALUE *argv, 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)
static VALUE strio_s_allocate(VALUE klass)
int rb_block_given_p(void)
VALUE rb_str_substr(VALUE, long, long)
static VALUE strio_close(VALUE self)
int rb_io_oflags_fmode(int oflags)
VALUE rb_obj_as_string(VALUE)
static struct StringIO * writable(VALUE strio)
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)
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)
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
static VALUE strio_getc(VALUE self)
VALUE rb_str_resize(VALUE, long)
static VALUE strio_readchar(VALUE self)
VALUE rb_str_subseq(VALUE, long, long)
static VALUE strio_each_char(VALUE self)
#define check_strio(self)
static struct getline_arg * prepare_getline_args(struct getline_arg *arg, int argc, VALUE *argv)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
static VALUE strio_each_codepoint(VALUE self)
void rb_str_modify_expand(VALUE, long)
static VALUE strio_first(VALUE self, VALUE arg)
unsigned char buf[MIME_BUF_SIZE]
static VALUE strio_unimpl(int argc, VALUE *argv, VALUE self)
VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
static VALUE strio_unget_bytes(struct StringIO *, const char *, long)
static VALUE strio_init(int, VALUE *, struct StringIO *, VALUE)
static VALUE strio_putc(VALUE self, VALUE ch)
VALUE rb_call_super(int, const VALUE *)
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
static VALUE strio_set_pos(VALUE self, VALUE pos)
static VALUE strio_getline(struct getline_arg *arg, struct StringIO *ptr)
static VALUE strio_seek(int argc, VALUE *argv, VALUE self)
static VALUE strio_bytes(VALUE self)
register unsigned int len
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_chars(VALUE self)
static VALUE strio_rewind(VALUE self)
rb_encoding * rb_enc_get(VALUE obj)
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
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_enumeratorize(VALUE obj, VALUE meth, int argc, const VALUE *argv)
VALUE rb_check_string_type(VALUE)
static VALUE strio_binmode(VALUE self)
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 struct StringIO * readable(VALUE strio)
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)
static VALUE strio_s_new(int argc, VALUE *argv, VALUE klass)
rb_encoding * rb_ascii8bit_encoding(void)
#define rb_intern_const(str)
#define rb_enc_mbcput(c, buf, enc)
static VALUE strio_set_string(VALUE self, VALUE string)
static VALUE strio_get_sync(VALUE self)
#define RB_INTEGER_TYPE_P(obj)
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_lines(int argc, VALUE *argv, VALUE self)
static VALUE strio_nil(VALUE self)
void rb_warn(const char *fmt,...)
VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc)
VALUE rb_str_new(const char *, long)
static VALUE strio_substr(struct StringIO *ptr, long pos, long len)
static struct StringIO * strio_alloc(void)