Ruby  1.9.3p551(2014-11-13revision48407)
stringio.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  stringio.c -
4 
5  $Author: nobu $
6  $RoughId: stringio.c,v 1.13 2002/03/14 03:24:18 nobu Exp $
7  created at: Tue Feb 19 04:10:38 JST 2002
8 
9  All the files in this distribution are covered under the Ruby's
10  license (see the file COPYING).
11 
12 **********************************************************************/
13 
14 #include "ruby.h"
15 #include "ruby/io.h"
16 #include "ruby/encoding.h"
17 #if defined(HAVE_FCNTL_H) || defined(_WIN32)
18 #include <fcntl.h>
19 #elif defined(HAVE_SYS_FCNTL_H)
20 #include <sys/fcntl.h>
21 #endif
22 
23 struct StringIO {
25  long pos;
26  long lineno;
27  int flags;
28  int count;
29 };
30 
31 static void strio_init(int, VALUE *, struct StringIO *);
32 
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))
35 
36 static struct StringIO *
38 {
39  struct StringIO *ptr = ALLOC(struct StringIO);
40  ptr->string = Qnil;
41  ptr->pos = 0;
42  ptr->lineno = 0;
43  ptr->flags = 0;
44  ptr->count = 1;
45  return ptr;
46 }
47 
48 static void
49 strio_mark(void *p)
50 {
51  struct StringIO *ptr = p;
52  if (ptr) {
53  rb_gc_mark(ptr->string);
54  }
55 }
56 
57 static void
58 strio_free(void *p)
59 {
60  struct StringIO *ptr = p;
61  if (--ptr->count <= 0) {
62  xfree(ptr);
63  }
64 }
65 
66 static size_t
67 strio_memsize(const void *p)
68 {
69  const struct StringIO *ptr = p;
70  if (!ptr) return 0;
71  return sizeof(struct StringIO);
72 }
73 
75  "strio",
76  {
77  strio_mark,
78  strio_free,
80  },
81 };
82 
83 #define check_strio(self) ((struct StringIO*)rb_check_typeddata((self), &strio_data_type))
84 
85 static struct StringIO*
87 {
88  struct StringIO *ptr = check_strio(rb_io_taint_check(self));
89 
90  if (!ptr) {
91  rb_raise(rb_eIOError, "uninitialized stream");
92  }
93  return ptr;
94 }
95 
96 static VALUE
97 strio_substr(struct StringIO *ptr, long pos, long len)
98 {
99  VALUE str = ptr->string;
100  rb_encoding *enc = rb_enc_get(str);
101  long rlen = RSTRING_LEN(str) - pos;
102 
103  if (len > rlen) len = rlen;
104  if (len < 0) len = 0;
105  return rb_enc_str_new(RSTRING_PTR(str)+pos, len, enc);
106 }
107 
108 #define StringIO(obj) get_strio(obj)
109 
110 #define CLOSED(ptr) (!((ptr)->flags & FMODE_READWRITE))
111 #define READABLE(ptr) ((ptr)->flags & FMODE_READABLE)
112 #define WRITABLE(ptr) ((ptr)->flags & FMODE_WRITABLE)
113 
114 static struct StringIO*
116 {
117  if (!READABLE(ptr)) {
118  rb_raise(rb_eIOError, "not opened for reading");
119  }
120  return ptr;
121 }
122 
123 static struct StringIO*
125 {
126  if (!WRITABLE(ptr)) {
127  rb_raise(rb_eIOError, "not opened for writing");
128  }
129  if (!OBJ_TAINTED(ptr->string)) {
130  rb_secure(4);
131  }
132  return ptr;
133 }
134 
135 static void
137 {
138  if (OBJ_FROZEN(ptr->string)) {
139  rb_raise(rb_eIOError, "not modifiable string");
140  }
141 }
142 
143 static VALUE
145 {
146  return TypedData_Wrap_Struct(klass, &strio_data_type, 0);
147 }
148 
149 /*
150  * call-seq: StringIO.new(string=""[, mode])
151  *
152  * Creates new StringIO instance from with _string_ and _mode_.
153  */
154 static VALUE
156 {
157  struct StringIO *ptr = check_strio(self);
158 
159  if (!ptr) {
160  DATA_PTR(self) = ptr = strio_alloc();
161  }
162  rb_call_super(0, 0);
163  strio_init(argc, argv, ptr);
164  return self;
165 }
166 
167 static void
169 {
170  VALUE string, mode;
171  int trunc = 0;
172 
173  switch (rb_scan_args(argc, argv, "02", &string, &mode)) {
174  case 2:
175  if (FIXNUM_P(mode)) {
176  int flags = FIX2INT(mode);
177  ptr->flags = rb_io_modenum_flags(flags);
178  trunc = flags & O_TRUNC;
179  }
180  else {
181  const char *m = StringValueCStr(mode);
182  ptr->flags = rb_io_mode_flags(m);
183  trunc = *m == 'w';
184  }
185  StringValue(string);
186  if ((ptr->flags & FMODE_WRITABLE) && OBJ_FROZEN(string)) {
187  errno = EACCES;
188  rb_sys_fail(0);
189  }
190  if (trunc) {
191  rb_str_resize(string, 0);
192  }
193  break;
194  case 1:
195  StringValue(string);
196  ptr->flags = OBJ_FROZEN(string) ? FMODE_READABLE : FMODE_READWRITE;
197  break;
198  case 0:
199  string = rb_enc_str_new("", 0, rb_default_external_encoding());
200  ptr->flags = FMODE_READWRITE;
201  break;
202  }
203  ptr->string = string;
204  ptr->pos = 0;
205  ptr->lineno = 0;
206 }
207 
208 static VALUE
210 {
211  struct StringIO *ptr = StringIO(self);
212  ptr->string = Qnil;
213  ptr->flags &= ~FMODE_READWRITE;
214  return self;
215 }
216 
217 /*
218  * call-seq: StringIO.open(string=""[, mode]) {|strio| ...}
219  *
220  * Equivalent to StringIO.new except that when it is called with a block, it
221  * yields with the new instance and closes it, and returns the result which
222  * returned from the block.
223  */
224 static VALUE
226 {
227  VALUE obj = rb_class_new_instance(argc, argv, klass);
228  if (!rb_block_given_p()) return obj;
229  return rb_ensure(rb_yield, obj, strio_finalize, obj);
230 }
231 
232 /*
233  * Returns +false+. Just for compatibility to IO.
234  */
235 static VALUE
237 {
238  StringIO(self);
239  return Qfalse;
240 }
241 
242 /*
243  * Returns +nil+. Just for compatibility to IO.
244  */
245 static VALUE
247 {
248  StringIO(self);
249  return Qnil;
250 }
251 
252 /*
253  * Returns *strio* itself. Just for compatibility to IO.
254  */
255 static VALUE
257 {
258  StringIO(self);
259  return self;
260 }
261 
262 /*
263  * Returns 0. Just for compatibility to IO.
264  */
265 static VALUE
267 {
268  StringIO(self);
269  return INT2FIX(0);
270 }
271 
272 /*
273  * Returns the argument unchanged. Just for compatibility to IO.
274  */
275 static VALUE
277 {
278  StringIO(self);
279  return arg;
280 }
281 
282 /*
283  * Raises NotImplementedError.
284  */
285 static VALUE
287 {
288  StringIO(self);
289  rb_notimplement();
290  return Qnil; /* not reached */
291 }
292 
293 /*
294  * call-seq: strio.string -> string
295  *
296  * Returns underlying String object, the subject of IO.
297  */
298 static VALUE
300 {
301  return StringIO(self)->string;
302 }
303 
304 /*
305  * call-seq:
306  * strio.string = string -> string
307  *
308  * Changes underlying String object, the subject of IO.
309  */
310 static VALUE
312 {
313  struct StringIO *ptr = StringIO(self);
314 
315  rb_io_taint_check(self);
316  ptr->flags &= ~FMODE_READWRITE;
317  StringValue(string);
318  ptr->flags = OBJ_FROZEN(string) ? FMODE_READABLE : FMODE_READWRITE;
319  ptr->pos = 0;
320  ptr->lineno = 0;
321  return ptr->string = string;
322 }
323 
324 /*
325  * call-seq:
326  * strio.close -> nil
327  *
328  * Closes strio. The *strio* is unavailable for any further data
329  * operations; an +IOError+ is raised if such an attempt is made.
330  */
331 static VALUE
333 {
334  struct StringIO *ptr = StringIO(self);
335  if (CLOSED(ptr)) {
336  rb_raise(rb_eIOError, "closed stream");
337  }
338  ptr->flags &= ~FMODE_READWRITE;
339  return Qnil;
340 }
341 
342 /*
343  * call-seq:
344  * strio.close_read -> nil
345  *
346  * Closes the read end of a StringIO. Will raise an +IOError+ if the
347  * *strio* is not readable.
348  */
349 static VALUE
351 {
352  struct StringIO *ptr = StringIO(self);
353  if (!READABLE(ptr)) {
354  rb_raise(rb_eIOError, "closing non-duplex IO for reading");
355  }
356  ptr->flags &= ~FMODE_READABLE;
357  return Qnil;
358 }
359 
360 /*
361  * call-seq:
362  * strio.close_write -> nil
363  *
364  * Closes the write end of a StringIO. Will raise an +IOError+ if the
365  * *strio* is not writeable.
366  */
367 static VALUE
369 {
370  struct StringIO *ptr = StringIO(self);
371  if (!WRITABLE(ptr)) {
372  rb_raise(rb_eIOError, "closing non-duplex IO for writing");
373  }
374  ptr->flags &= ~FMODE_WRITABLE;
375  return Qnil;
376 }
377 
378 /*
379  * call-seq:
380  * strio.closed? -> true or false
381  *
382  * Returns +true+ if *strio* is completely closed, +false+ otherwise.
383  */
384 static VALUE
386 {
387  struct StringIO *ptr = StringIO(self);
388  if (!CLOSED(ptr)) return Qfalse;
389  return Qtrue;
390 }
391 
392 /*
393  * call-seq:
394  * strio.closed_read? -> true or false
395  *
396  * Returns +true+ if *strio* is not readable, +false+ otherwise.
397  */
398 static VALUE
400 {
401  struct StringIO *ptr = StringIO(self);
402  if (READABLE(ptr)) return Qfalse;
403  return Qtrue;
404 }
405 
406 /*
407  * call-seq:
408  * strio.closed_write? -> true or false
409  *
410  * Returns +true+ if *strio* is not writable, +false+ otherwise.
411  */
412 static VALUE
414 {
415  struct StringIO *ptr = StringIO(self);
416  if (WRITABLE(ptr)) return Qfalse;
417  return Qtrue;
418 }
419 
420 /*
421  * call-seq:
422  * strio.eof -> true or false
423  * strio.eof? -> true or false
424  *
425  * Returns true if *strio* is at end of file. The stringio must be
426  * opened for reading or an +IOError+ will be raised.
427  */
428 static VALUE
430 {
431  struct StringIO *ptr = readable(StringIO(self));
432  if (ptr->pos < RSTRING_LEN(ptr->string)) return Qfalse;
433  return Qtrue;
434 }
435 
436 /* :nodoc: */
437 static VALUE
438 strio_copy(VALUE copy, VALUE orig)
439 {
440  struct StringIO *ptr;
441 
442  orig = rb_convert_type(orig, T_DATA, "StringIO", "to_strio");
443  if (copy == orig) return copy;
444  ptr = StringIO(orig);
445  if (check_strio(copy)) {
446  strio_free(DATA_PTR(copy));
447  }
448  DATA_PTR(copy) = ptr;
449  OBJ_INFECT(copy, orig);
450  ++ptr->count;
451  return copy;
452 }
453 
454 /*
455  * call-seq:
456  * strio.lineno -> integer
457  *
458  * Returns the current line number in *strio*. The stringio must be
459  * opened for reading. +lineno+ counts the number of times +gets+ is
460  * called, rather than the number of newlines encountered. The two
461  * values will differ if +gets+ is called with a separator other than
462  * newline. See also the <code>$.</code> variable.
463  */
464 static VALUE
466 {
467  return LONG2NUM(StringIO(self)->lineno);
468 }
469 
470 /*
471  * call-seq:
472  * strio.lineno = integer -> integer
473  *
474  * Manually sets the current line number to the given value.
475  * <code>$.</code> is updated only on the next read.
476  */
477 static VALUE
479 {
480  StringIO(self)->lineno = NUM2LONG(lineno);
481  return lineno;
482 }
483 
484 /* call-seq: strio.binmode -> true */
485 #define strio_binmode strio_self
486 
487 /* call-seq: strio.fcntl */
488 #define strio_fcntl strio_unimpl
489 
490 /* call-seq: strio.flush -> strio */
491 #define strio_flush strio_self
492 
493 /* call-seq: strio.fsync -> 0 */
494 #define strio_fsync strio_0
495 
496 /*
497  * call-seq:
498  * strio.reopen(other_StrIO) -> strio
499  * strio.reopen(string, mode) -> strio
500  *
501  * Reinitializes *strio* with the given <i>other_StrIO</i> or _string_
502  * and _mode_ (see StringIO#new).
503  */
504 static VALUE
506 {
507  rb_io_taint_check(self);
508  if (argc == 1 && TYPE(*argv) != T_STRING) {
509  return strio_copy(self, *argv);
510  }
511  strio_init(argc, argv, StringIO(self));
512  return self;
513 }
514 
515 /*
516  * call-seq:
517  * strio.pos -> integer
518  * strio.tell -> integer
519  *
520  * Returns the current offset (in bytes) of *strio*.
521  */
522 static VALUE
524 {
525  return LONG2NUM(StringIO(self)->pos);
526 }
527 
528 /*
529  * call-seq:
530  * strio.pos = integer -> integer
531  *
532  * Seeks to the given position (in bytes) in *strio*.
533  */
534 static VALUE
536 {
537  struct StringIO *ptr = StringIO(self);
538  long p = NUM2LONG(pos);
539  if (p < 0) {
540  error_inval(0);
541  }
542  ptr->pos = p;
543  return pos;
544 }
545 
546 /*
547  * call-seq:
548  * strio.rewind -> 0
549  *
550  * Positions *strio* to the beginning of input, resetting
551  * +lineno+ to zero.
552  */
553 static VALUE
555 {
556  struct StringIO *ptr = StringIO(self);
557  ptr->pos = 0;
558  ptr->lineno = 0;
559  return INT2FIX(0);
560 }
561 
562 /*
563  * call-seq:
564  * strio.seek(amount, whence=SEEK_SET) -> 0
565  *
566  * Seeks to a given offset _amount_ in the stream according to
567  * the value of _whence_ (see IO#seek).
568  */
569 static VALUE
571 {
572  VALUE whence;
573  struct StringIO *ptr = StringIO(self);
574  long offset;
575 
576  rb_scan_args(argc, argv, "11", NULL, &whence);
577  offset = NUM2LONG(argv[0]);
578  if (CLOSED(ptr)) {
579  rb_raise(rb_eIOError, "closed stream");
580  }
581  switch (NIL_P(whence) ? 0 : NUM2LONG(whence)) {
582  case 0:
583  break;
584  case 1:
585  offset += ptr->pos;
586  break;
587  case 2:
588  offset += RSTRING_LEN(ptr->string);
589  break;
590  default:
591  error_inval("invalid whence");
592  }
593  if (offset < 0) {
594  error_inval(0);
595  }
596  ptr->pos = offset;
597  return INT2FIX(0);
598 }
599 
600 /*
601  * call-seq:
602  * strio.sync -> true
603  *
604  * Returns +true+ always.
605  */
606 static VALUE
608 {
609  StringIO(self);
610  return Qtrue;
611 }
612 
613 /* call-seq: strio.sync = boolean -> boolean */
614 #define strio_set_sync strio_first
615 
616 #define strio_tell strio_get_pos
617 
618 /*
619  * call-seq:
620  * strio.bytes {|byte| block } -> strio
621  * strio.bytes -> anEnumerator
622  *
623  * strio.each_byte {|byte| block } -> strio
624  * strio.each_byte -> anEnumerator
625  *
626  * See IO#each_byte.
627  */
628 static VALUE
630 {
631  struct StringIO *ptr = readable(StringIO(self));
632 
633  RETURN_ENUMERATOR(self, 0, 0);
634 
635  while (ptr->pos < RSTRING_LEN(ptr->string)) {
636  char c = RSTRING_PTR(ptr->string)[ptr->pos++];
637  rb_yield(CHR2FIX(c));
638  }
639  return self;
640 }
641 
642 /*
643  * call-seq:
644  * strio.getc -> string or nil
645  *
646  * See IO#getc.
647  */
648 static VALUE
650 {
651  struct StringIO *ptr = readable(StringIO(self));
652  rb_encoding *enc = rb_enc_get(ptr->string);
653  int len;
654  char *p;
655 
656  if (ptr->pos >= RSTRING_LEN(ptr->string)) {
657  return Qnil;
658  }
659  p = RSTRING_PTR(ptr->string)+ptr->pos;
660  len = rb_enc_mbclen(p, RSTRING_END(ptr->string), enc);
661  ptr->pos += len;
662  return rb_enc_str_new(p, len, rb_enc_get(ptr->string));
663 }
664 
665 /*
666  * call-seq:
667  * strio.getbyte -> fixnum or nil
668  *
669  * See IO#getbyte.
670  */
671 static VALUE
673 {
674  struct StringIO *ptr = readable(StringIO(self));
675  int c;
676  if (ptr->pos >= RSTRING_LEN(ptr->string)) {
677  return Qnil;
678  }
679  c = RSTRING_PTR(ptr->string)[ptr->pos++];
680  return CHR2FIX(c);
681 }
682 
683 static void
684 strio_extend(struct StringIO *ptr, long pos, long len)
685 {
686  long olen;
687 
688  check_modifiable(ptr);
689  olen = RSTRING_LEN(ptr->string);
690  if (pos + len > olen) {
691  rb_str_resize(ptr->string, pos + len);
692  if (pos > olen)
693  MEMZERO(RSTRING_PTR(ptr->string) + olen, char, pos - olen);
694  }
695  else {
696  rb_str_modify(ptr->string);
697  }
698 }
699 
700 /*
701  * call-seq:
702  * strio.ungetc(string) -> nil
703  *
704  * Pushes back one character (passed as a parameter) onto *strio*
705  * such that a subsequent buffered read will return it. There is no
706  * limitation for multiple pushbacks including pushing back behind the
707  * beginning of the buffer string.
708  */
709 static VALUE
711 {
712  struct StringIO *ptr = readable(StringIO(self));
713  long lpos, clen;
714  char *p, *pend;
715  rb_encoding *enc, *enc2;
716 
717  if (NIL_P(c)) return Qnil;
718  if (FIXNUM_P(c)) {
719  int cc = FIX2INT(c);
720  char buf[16];
721 
722  enc = rb_enc_get(ptr->string);
723  rb_enc_mbcput(cc, buf, enc);
724  c = rb_enc_str_new(buf, rb_enc_codelen(cc, enc), enc);
725  }
726  else {
727  SafeStringValue(c);
728  enc = rb_enc_get(ptr->string);
729  enc2 = rb_enc_get(c);
730  if (enc != enc2 && enc != rb_ascii8bit_encoding()) {
731  c = rb_str_conv_enc(c, enc2, enc);
732  }
733  }
734  if (RSTRING_LEN(ptr->string) < ptr->pos) {
735  long len = RSTRING_LEN(ptr->string);
736  rb_str_resize(ptr->string, ptr->pos - 1);
737  memset(RSTRING_PTR(ptr->string) + len, 0, ptr->pos - len - 1);
738  rb_str_concat(ptr->string, c);
739  ptr->pos--;
740  }
741  else {
742  /* get logical position */
743  lpos = 0; p = RSTRING_PTR(ptr->string); pend = p + ptr->pos;
744  for (;;) {
745  clen = rb_enc_mbclen(p, pend, enc);
746  if (p+clen >= pend) break;
747  p += clen;
748  lpos++;
749  }
750  clen = p - RSTRING_PTR(ptr->string);
751  rb_str_update(ptr->string, lpos, ptr->pos ? 1 : 0, c);
752  ptr->pos = clen;
753  }
754 
755  return Qnil;
756 }
757 
758 /*
759  * call-seq:
760  * strio.ungetbyte(fixnum) -> nil
761  *
762  * See IO#ungetbyte
763  */
764 static VALUE
766 {
767  struct StringIO *ptr = readable(StringIO(self));
768  char buf[1], *cp = buf;
769  long pos = ptr->pos, cl = 1;
770  VALUE str = ptr->string;
771 
772  if (NIL_P(c)) return Qnil;
773  if (FIXNUM_P(c)) {
774  buf[0] = (char)FIX2INT(c);
775  }
776  else {
777  SafeStringValue(c);
778  cp = RSTRING_PTR(c);
779  cl = RSTRING_LEN(c);
780  if (cl == 0) return Qnil;
781  }
782  rb_str_modify(str);
783  if (cl > pos) {
784  char *s;
785  long rest = RSTRING_LEN(str) - pos;
786  rb_str_resize(str, rest + cl);
787  s = RSTRING_PTR(str);
788  memmove(s + cl, s + pos, rest);
789  pos = 0;
790  }
791  else {
792  pos -= cl;
793  }
794  memcpy(RSTRING_PTR(str) + pos, cp, cl);
795  ptr->pos = pos;
796  RB_GC_GUARD(c);
797  return Qnil;
798 }
799 
800 /*
801  * call-seq:
802  * strio.readchar -> string
803  *
804  * See IO#readchar.
805  */
806 static VALUE
808 {
809  VALUE c = rb_funcall2(self, rb_intern("getc"), 0, 0);
810  if (NIL_P(c)) rb_eof_error();
811  return c;
812 }
813 
814 /*
815  * call-seq:
816  * strio.readbyte -> fixnum
817  *
818  * See IO#readbyte.
819  */
820 static VALUE
822 {
823  VALUE c = rb_funcall2(self, rb_intern("getbyte"), 0, 0);
824  if (NIL_P(c)) rb_eof_error();
825  return c;
826 }
827 
828 /*
829  * call-seq:
830  * strio.chars {|char| block } -> strio
831  * strio.chars -> anEnumerator
832  *
833  * strio.each_char {|char| block } -> strio
834  * strio.each_char -> anEnumerator
835  *
836  * See IO#each_char.
837  */
838 static VALUE
840 {
841  VALUE c;
842 
843  RETURN_ENUMERATOR(self, 0, 0);
844 
845  while (!NIL_P(c = strio_getc(self))) {
846  rb_yield(c);
847  }
848  return self;
849 }
850 
851 /*
852  * call-seq:
853  * strio.codepoints {|c| block } -> strio
854  * strio.codepoints -> anEnumerator
855  *
856  * strio.each_codepoint {|c| block } -> strio
857  * strio.each_codepoint -> anEnumerator
858  *
859  * See IO#each_codepoint.
860  */
861 static VALUE
863 {
864  struct StringIO *ptr;
865  rb_encoding *enc;
866  unsigned int c;
867  int n;
868 
869  RETURN_ENUMERATOR(self, 0, 0);
870 
871  ptr = readable(StringIO(self));
872  enc = rb_enc_get(ptr->string);
873  for (;;) {
874  if (ptr->pos >= RSTRING_LEN(ptr->string)) {
875  return self;
876  }
877 
879  RSTRING_END(ptr->string), &n, enc);
880  rb_yield(UINT2NUM(c));
881  ptr->pos += n;
882  }
883  return self;
884 }
885 
886 /* Boyer-Moore search: copied from regex.c */
887 static void
888 bm_init_skip(long *skip, const char *pat, long m)
889 {
890  int c;
891 
892  for (c = 0; c < (1 << CHAR_BIT); c++) {
893  skip[c] = m;
894  }
895  while (--m) {
896  skip[(unsigned char)*pat++] = m;
897  }
898 }
899 
900 static long
901 bm_search(const char *little, long llen, const char *big, long blen, const long *skip)
902 {
903  long i, j, k;
904 
905  i = llen - 1;
906  while (i < blen) {
907  k = i;
908  j = llen - 1;
909  while (j >= 0 && big[k] == little[j]) {
910  k--;
911  j--;
912  }
913  if (j < 0) return k + 1;
914  i += skip[(unsigned char)big[i]];
915  }
916  return -1;
917 }
918 
919 static VALUE
921 {
922  const char *s, *e, *p;
923  long n, limit = 0;
924  VALUE str, lim;
925 
926  rb_scan_args(argc, argv, "02", &str, &lim);
927  switch (argc) {
928  case 0:
929  str = rb_rs;
930  break;
931 
932  case 1:
933  if (!NIL_P(str) && TYPE(str) != T_STRING) {
934  VALUE tmp = rb_check_string_type(str);
935  if (NIL_P(tmp)) {
936  limit = NUM2LONG(str);
937  if (limit == 0) return rb_str_new(0,0);
938  str = rb_rs;
939  }
940  else {
941  str = tmp;
942  }
943  }
944  break;
945 
946  case 2:
947  if (!NIL_P(str)) StringValue(str);
948  limit = NUM2LONG(lim);
949  break;
950  }
951 
952  if (ptr->pos >= (n = RSTRING_LEN(ptr->string))) {
953  return Qnil;
954  }
955  s = RSTRING_PTR(ptr->string);
956  e = s + RSTRING_LEN(ptr->string);
957  s += ptr->pos;
958  if (limit > 0 && s + limit < e) {
959  e = rb_enc_right_char_head(s, s + limit, e, rb_enc_get(ptr->string));
960  }
961  if (NIL_P(str)) {
962  str = strio_substr(ptr, ptr->pos, e - s);
963  }
964  else if ((n = RSTRING_LEN(str)) == 0) {
965  p = s;
966  while (*p == '\n') {
967  if (++p == e) {
968  return Qnil;
969  }
970  }
971  s = p;
972  while ((p = memchr(p, '\n', e - p)) && (p != e)) {
973  if (*++p == '\n') {
974  e = p + 1;
975  break;
976  }
977  }
978  str = strio_substr(ptr, s - RSTRING_PTR(ptr->string), e - s);
979  }
980  else if (n == 1) {
981  if ((p = memchr(s, RSTRING_PTR(str)[0], e - s)) != 0) {
982  e = p + 1;
983  }
984  str = strio_substr(ptr, ptr->pos, e - s);
985  }
986  else {
987  if (n < e - s) {
988  if (e - s < 1024) {
989  for (p = s; p + n <= e; ++p) {
990  if (MEMCMP(p, RSTRING_PTR(str), char, n) == 0) {
991  e = p + n;
992  break;
993  }
994  }
995  }
996  else {
997  long skip[1 << CHAR_BIT], pos;
998  p = RSTRING_PTR(str);
999  bm_init_skip(skip, p, n);
1000  if ((pos = bm_search(p, n, s, e - s, skip)) >= 0) {
1001  e = s + pos + n;
1002  }
1003  }
1004  }
1005  str = strio_substr(ptr, ptr->pos, e - s);
1006  }
1007  ptr->pos = e - RSTRING_PTR(ptr->string);
1008  ptr->lineno++;
1009  return str;
1010 }
1011 
1012 /*
1013  * call-seq:
1014  * strio.gets(sep=$/) -> string or nil
1015  * strio.gets(limit) -> string or nil
1016  * strio.gets(sep, limit) -> string or nil
1017  *
1018  * See IO#gets.
1019  */
1020 static VALUE
1022 {
1023  VALUE str = strio_getline(argc, argv, readable(StringIO(self)));
1024 
1025  rb_lastline_set(str);
1026  return str;
1027 }
1028 
1029 /*
1030  * call-seq:
1031  * strio.readline(sep=$/) -> string
1032  * strio.readline(limit) -> string or nil
1033  * strio.readline(sep, limit) -> string or nil
1034  *
1035  * See IO#readline.
1036  */
1037 static VALUE
1039 {
1040  VALUE line = rb_funcall2(self, rb_intern("gets"), argc, argv);
1041  if (NIL_P(line)) rb_eof_error();
1042  return line;
1043 }
1044 
1045 /*
1046  * call-seq:
1047  * strio.each(sep=$/) {|line| block } -> strio
1048  * strio.each(limit) {|line| block } -> strio
1049  * strio.each(sep, limit) {|line| block } -> strio
1050  * strio.each(...) -> anEnumerator
1051  *
1052  * strio.each_line(sep=$/) {|line| block } -> strio
1053  * strio.each_line(limit) {|line| block } -> strio
1054  * strio.each_line(sep,limit) {|line| block } -> strio
1055  * strio.each_line(...) -> anEnumerator
1056  *
1057  * strio.lines(sep=$/) {|line| block } -> strio
1058  * strio.lines(limit) {|line| block } -> strio
1059  * strio.lines(sep,limit) {|line| block } -> strio
1060  * strio.lines(...) -> anEnumerator
1061  *
1062  * See IO#each.
1063  */
1064 static VALUE
1066 {
1067  struct StringIO *ptr = StringIO(self);
1068  VALUE line;
1069 
1070  RETURN_ENUMERATOR(self, argc, argv);
1071 
1072  if (argc > 0 && !NIL_P(argv[argc-1]) && NIL_P(rb_check_string_type(argv[argc-1])) &&
1073  NUM2LONG(argv[argc-1]) == 0) {
1074  rb_raise(rb_eArgError, "invalid limit: 0 for each_line");
1075  }
1076 
1077  while (!NIL_P(line = strio_getline(argc, argv, readable(ptr)))) {
1078  rb_yield(line);
1079  }
1080  return self;
1081 }
1082 
1083 /*
1084  * call-seq:
1085  * strio.readlines(sep=$/) -> array
1086  * strio.readlines(limit) -> array
1087  * strio.readlines(sep,limit) -> array
1088  *
1089  * See IO#readlines.
1090  */
1091 static VALUE
1093 {
1094  struct StringIO *ptr = StringIO(self);
1095  VALUE ary = rb_ary_new(), line;
1096 
1097  if (argc > 0 && !NIL_P(argv[argc-1]) && NIL_P(rb_check_string_type(argv[argc-1])) &&
1098  NUM2LONG(argv[argc-1]) == 0) {
1099  rb_raise(rb_eArgError, "invalid limit: 0 for readlines");
1100  }
1101 
1102  while (!NIL_P(line = strio_getline(argc, argv, readable(ptr)))) {
1103  rb_ary_push(ary, line);
1104  }
1105  return ary;
1106 }
1107 
1108 /*
1109  * call-seq:
1110  * strio.write(string) -> integer
1111  * strio.syswrite(string) -> integer
1112  *
1113  * Appends the given string to the underlying buffer string of *strio*.
1114  * The stream must be opened for writing. If the argument is not a
1115  * string, it will be converted to a string using <code>to_s</code>.
1116  * Returns the number of bytes written. See IO#write.
1117  */
1118 static VALUE
1120 {
1121  struct StringIO *ptr = writable(StringIO(self));
1122  long len, olen;
1123  rb_encoding *enc, *enc2;
1124 
1125  RB_GC_GUARD(str);
1126  if (TYPE(str) != T_STRING)
1127  str = rb_obj_as_string(str);
1128  enc = rb_enc_get(ptr->string);
1129  enc2 = rb_enc_get(str);
1130  if (enc != enc2 && enc != rb_ascii8bit_encoding()) {
1131  str = rb_str_conv_enc(str, enc2, enc);
1132  }
1133  len = RSTRING_LEN(str);
1134  if (len == 0) return INT2FIX(0);
1135  check_modifiable(ptr);
1136  olen = RSTRING_LEN(ptr->string);
1137  if (ptr->flags & FMODE_APPEND) {
1138  ptr->pos = olen;
1139  }
1140  if (ptr->pos == olen) {
1141  rb_str_cat(ptr->string, RSTRING_PTR(str), len);
1142  }
1143  else {
1144  strio_extend(ptr, ptr->pos, len);
1145  memmove(RSTRING_PTR(ptr->string)+ptr->pos, RSTRING_PTR(str), len);
1146  OBJ_INFECT(ptr->string, str);
1147  }
1148  OBJ_INFECT(ptr->string, self);
1149  ptr->pos += len;
1150  return LONG2NUM(len);
1151 }
1152 
1153 /*
1154  * call-seq:
1155  * strio << obj -> strio
1156  *
1157  * See IO#<<.
1158  */
1159 #define strio_addstr rb_io_addstr
1160 
1161 /*
1162  * call-seq:
1163  * strio.print() -> nil
1164  * strio.print(obj, ...) -> nil
1165  *
1166  * See IO#print.
1167  */
1168 #define strio_print rb_io_print
1169 
1170 /*
1171  * call-seq:
1172  * strio.printf(format_string [, obj, ...] ) -> nil
1173  *
1174  * See IO#printf.
1175  */
1176 #define strio_printf rb_io_printf
1177 
1178 /*
1179  * call-seq:
1180  * strio.putc(obj) -> obj
1181  *
1182  * See IO#putc.
1183  */
1184 static VALUE
1186 {
1187  struct StringIO *ptr = writable(StringIO(self));
1188  int c = NUM2CHR(ch);
1189  long olen;
1190 
1191  check_modifiable(ptr);
1192  olen = RSTRING_LEN(ptr->string);
1193  if (ptr->flags & FMODE_APPEND) {
1194  ptr->pos = olen;
1195  }
1196  strio_extend(ptr, ptr->pos, 1);
1197  RSTRING_PTR(ptr->string)[ptr->pos++] = c;
1198  OBJ_INFECT(ptr->string, self);
1199  return ch;
1200 }
1201 
1202 /*
1203  * call-seq:
1204  * strio.puts(obj, ...) -> nil
1205  *
1206  * See IO#puts.
1207  */
1208 #define strio_puts rb_io_puts
1209 
1210 /*
1211  * call-seq:
1212  * strio.read([length [, buffer]]) -> string, buffer, or nil
1213  *
1214  * See IO#read.
1215  */
1216 static VALUE
1218 {
1219  struct StringIO *ptr = readable(StringIO(self));
1220  VALUE str = Qnil;
1221  long len;
1222  int binary = 0;
1223 
1224  switch (argc) {
1225  case 2:
1226  str = argv[1];
1227  if (!NIL_P(str)) {
1228  StringValue(str);
1229  rb_str_modify(str);
1230  }
1231  case 1:
1232  if (!NIL_P(argv[0])) {
1233  len = NUM2LONG(argv[0]);
1234  if (len < 0) {
1235  rb_raise(rb_eArgError, "negative length %ld given", len);
1236  }
1237  if (len > 0 && ptr->pos >= RSTRING_LEN(ptr->string)) {
1238  if (!NIL_P(str)) rb_str_resize(str, 0);
1239  return Qnil;
1240  }
1241  binary = 1;
1242  break;
1243  }
1244  /* fall through */
1245  case 0:
1246  len = RSTRING_LEN(ptr->string);
1247  if (len <= ptr->pos) {
1248  if (NIL_P(str)) {
1249  str = rb_str_new(0, 0);
1250  }
1251  else {
1252  rb_str_resize(str, 0);
1253  }
1254  return str;
1255  }
1256  else {
1257  len -= ptr->pos;
1258  }
1259  break;
1260  default:
1261  rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
1262  }
1263  if (NIL_P(str)) {
1264  str = strio_substr(ptr, ptr->pos, len);
1265  if (binary) rb_enc_associate(str, rb_ascii8bit_encoding());
1266  }
1267  else {
1268  long rest = RSTRING_LEN(ptr->string) - ptr->pos;
1269  if (len > rest) len = rest;
1270  rb_str_resize(str, len);
1271  MEMCPY(RSTRING_PTR(str), RSTRING_PTR(ptr->string) + ptr->pos, char, len);
1272  if (binary)
1274  else
1275  rb_enc_copy(str, ptr->string);
1276  }
1277  ptr->pos += RSTRING_LEN(str);
1278  return str;
1279 }
1280 
1281 /*
1282  * call-seq:
1283  * strio.sysread(integer[, outbuf]) -> string
1284  *
1285  * Similar to #read, but raises +EOFError+ at end of string instead of
1286  * returning +nil+, as well as IO#sysread does.
1287  */
1288 static VALUE
1290 {
1291  VALUE val = rb_funcall2(self, rb_intern("read"), argc, argv);
1292  if (NIL_P(val)) {
1293  rb_eof_error();
1294  }
1295  return val;
1296 }
1297 
1298 #define strio_syswrite rb_io_write
1299 
1300 /*
1301  * call-seq:
1302  * strio.isatty -> nil
1303  * strio.tty? -> nil
1304  *
1305  */
1306 #define strio_isatty strio_false
1307 
1308 /* call-seq: strio.pid -> nil */
1309 #define strio_pid strio_nil
1310 
1311 /* call-seq: strio.fileno -> nil */
1312 #define strio_fileno strio_nil
1313 
1314 /*
1315  * call-seq:
1316  * strio.size -> integer
1317  *
1318  * Returns the size of the buffer string.
1319  */
1320 static VALUE
1322 {
1323  VALUE string = StringIO(self)->string;
1324  if (NIL_P(string)) {
1325  rb_raise(rb_eIOError, "not opened");
1326  }
1327  return ULONG2NUM(RSTRING_LEN(string));
1328 }
1329 
1330 /*
1331  * call-seq:
1332  * strio.truncate(integer) -> 0
1333  *
1334  * Truncates the buffer string to at most _integer_ bytes. The *strio*
1335  * must be opened for writing.
1336  */
1337 static VALUE
1339 {
1340  VALUE string = writable(StringIO(self))->string;
1341  long l = NUM2LONG(len);
1342  long plen = RSTRING_LEN(string);
1343  if (l < 0) {
1344  error_inval("negative legnth");
1345  }
1346  rb_str_resize(string, l);
1347  if (plen < l) {
1348  MEMZERO(RSTRING_PTR(string) + plen, char, l - plen);
1349  }
1350  return len;
1351 }
1352 
1353 /*
1354  * call-seq:
1355  * strio.external_encoding => encoding
1356  *
1357  * Returns the Encoding object that represents the encoding of the file.
1358  * If strio is write mode and no encoding is specified, returns <code>nil</code>.
1359  */
1360 
1361 static VALUE
1363 {
1364  return rb_enc_from_encoding(rb_enc_get(StringIO(self)->string));
1365 }
1366 
1367 /*
1368  * call-seq:
1369  * strio.internal_encoding => encoding
1370  *
1371  * Returns the Encoding of the internal string if conversion is
1372  * specified. Otherwise returns nil.
1373  */
1374 
1375 static VALUE
1377 {
1378  return Qnil;
1379 }
1380 
1381 /*
1382  * call-seq:
1383  * strio.set_encoding(ext_enc, [int_enc[, opt]]) => strio
1384  *
1385  * Specify the encoding of the StringIO as <i>ext_enc</i>.
1386  * Use the default external encoding if <i>ext_enc</i> is nil.
1387  * 2nd argument <i>int_enc</i> and optional hash <i>opt</i> argument
1388  * are ignored; they are for API compatibility to IO.
1389  */
1390 
1391 static VALUE
1393 {
1394  rb_encoding* enc;
1395  VALUE str = StringIO(self)->string;
1396  VALUE ext_enc, int_enc, opt;
1397 
1398  argc = rb_scan_args(argc, argv, "11:", &ext_enc, &int_enc, &opt);
1399 
1400  if (NIL_P(ext_enc)) {
1402  }
1403  else {
1404  enc = rb_to_encoding(ext_enc);
1405  }
1406  rb_enc_associate(str, enc);
1407  return self;
1408 }
1409 
1410 /*
1411  * Pseudo I/O on String object.
1412  */
1413 void
1415 {
1416  VALUE StringIO = rb_define_class("StringIO", rb_cData);
1417 
1418  rb_include_module(StringIO, rb_mEnumerable);
1420  rb_define_singleton_method(StringIO, "open", strio_s_open, -1);
1421  rb_define_method(StringIO, "initialize", strio_initialize, -1);
1422  rb_define_method(StringIO, "initialize_copy", strio_copy, 1);
1423  rb_define_method(StringIO, "reopen", strio_reopen, -1);
1424 
1425  rb_define_method(StringIO, "string", strio_get_string, 0);
1426  rb_define_method(StringIO, "string=", strio_set_string, 1);
1427  rb_define_method(StringIO, "lineno", strio_get_lineno, 0);
1428  rb_define_method(StringIO, "lineno=", strio_set_lineno, 1);
1429 
1430  rb_define_method(StringIO, "binmode", strio_binmode, 0);
1431  rb_define_method(StringIO, "close", strio_close, 0);
1432  rb_define_method(StringIO, "close_read", strio_close_read, 0);
1433  rb_define_method(StringIO, "close_write", strio_close_write, 0);
1434  rb_define_method(StringIO, "closed?", strio_closed, 0);
1435  rb_define_method(StringIO, "closed_read?", strio_closed_read, 0);
1436  rb_define_method(StringIO, "closed_write?", strio_closed_write, 0);
1437  rb_define_method(StringIO, "eof", strio_eof, 0);
1438  rb_define_method(StringIO, "eof?", strio_eof, 0);
1439  rb_define_method(StringIO, "fcntl", strio_fcntl, -1);
1440  rb_define_method(StringIO, "flush", strio_flush, 0);
1441  rb_define_method(StringIO, "fsync", strio_fsync, 0);
1442  rb_define_method(StringIO, "pos", strio_get_pos, 0);
1443  rb_define_method(StringIO, "pos=", strio_set_pos, 1);
1444  rb_define_method(StringIO, "rewind", strio_rewind, 0);
1445  rb_define_method(StringIO, "seek", strio_seek, -1);
1446  rb_define_method(StringIO, "sync", strio_get_sync, 0);
1447  rb_define_method(StringIO, "sync=", strio_set_sync, 1);
1448  rb_define_method(StringIO, "tell", strio_tell, 0);
1449 
1450  rb_define_method(StringIO, "each", strio_each, -1);
1451  rb_define_method(StringIO, "each_line", strio_each, -1);
1452  rb_define_method(StringIO, "lines", strio_each, -1);
1453  rb_define_method(StringIO, "each_byte", strio_each_byte, 0);
1454  rb_define_method(StringIO, "bytes", strio_each_byte, 0);
1455  rb_define_method(StringIO, "each_char", strio_each_char, 0);
1456  rb_define_method(StringIO, "chars", strio_each_char, 0);
1457  rb_define_method(StringIO, "each_codepoint", strio_each_codepoint, 0);
1458  rb_define_method(StringIO, "codepoints", strio_each_codepoint, 0);
1459  rb_define_method(StringIO, "getc", strio_getc, 0);
1460  rb_define_method(StringIO, "ungetc", strio_ungetc, 1);
1461  rb_define_method(StringIO, "ungetbyte", strio_ungetbyte, 1);
1462  rb_define_method(StringIO, "getbyte", strio_getbyte, 0);
1463  rb_define_method(StringIO, "gets", strio_gets, -1);
1464  rb_define_method(StringIO, "readlines", strio_readlines, -1);
1465  rb_define_method(StringIO, "read", strio_read, -1);
1466 
1467  rb_define_method(StringIO, "write", strio_write, 1);
1468  rb_define_method(StringIO, "putc", strio_putc, 1);
1469 
1470  rb_define_method(StringIO, "isatty", strio_isatty, 0);
1471  rb_define_method(StringIO, "tty?", strio_isatty, 0);
1472  rb_define_method(StringIO, "pid", strio_pid, 0);
1473  rb_define_method(StringIO, "fileno", strio_fileno, 0);
1474  rb_define_method(StringIO, "size", strio_size, 0);
1475  rb_define_method(StringIO, "length", strio_size, 0);
1476  rb_define_method(StringIO, "truncate", strio_truncate, 1);
1477 
1478  rb_define_method(StringIO, "external_encoding", strio_external_encoding, 0);
1479  rb_define_method(StringIO, "internal_encoding", strio_internal_encoding, 0);
1480  rb_define_method(StringIO, "set_encoding", strio_set_encoding, -1);
1481 
1482  {
1483  VALUE mReadable = rb_define_module_under(rb_cIO, "readable");
1484  rb_define_method(mReadable, "readchar", strio_readchar, 0);
1485  rb_define_method(mReadable, "readbyte", strio_readbyte, 0);
1486  rb_define_method(mReadable, "readline", strio_readline, -1);
1487  rb_define_method(mReadable, "sysread", strio_sysread, -1);
1488  rb_define_method(mReadable, "readpartial", strio_sysread, -1);
1489  rb_define_method(mReadable, "read_nonblock", strio_sysread, -1);
1490  rb_include_module(StringIO, mReadable);
1491  }
1492  {
1493  VALUE mWritable = rb_define_module_under(rb_cIO, "writable");
1494  rb_define_method(mWritable, "<<", strio_addstr, 1);
1495  rb_define_method(mWritable, "print", strio_print, -1);
1496  rb_define_method(mWritable, "printf", strio_printf, -1);
1497  rb_define_method(mWritable, "puts", strio_puts, -1);
1498  rb_define_method(mWritable, "syswrite", strio_syswrite, 1);
1499  rb_define_method(mWritable, "write_nonblock", strio_syswrite, 1);
1500  rb_include_module(StringIO, mWritable);
1501  }
1502 }
static VALUE strio_closed_read(VALUE self)
Definition: stringio.c:399
static void check_modifiable(struct StringIO *ptr)
Definition: stringio.c:136
static VALUE strio_getbyte(VALUE self)
Definition: stringio.c:672
#define RSTRING_LEN(string)
Definition: generator.h:45
static long NUM2LONG(VALUE x)
Definition: ruby.h:510
#define MEMCMP(p1, p2, type, n)
Definition: ruby.h:1055
int rb_enc_codelen(int c, rb_encoding *enc)
Definition: encoding.c:916
long pos
Definition: stringio.c:25
RUBY_EXTERN VALUE rb_cData
Definition: ruby.h:1253
ssize_t n
Definition: bigdecimal.c:5519
volatile VALUE ary
Definition: tcltklib.c:9700
static VALUE UINT2NUM(unsigned int v)
Definition: ruby.h:992
UChar * pat
Definition: regerror.c:385
static VALUE strio_get_string(VALUE self)
Definition: stringio.c:299
void rb_enc_copy(VALUE obj1, VALUE obj2)
Definition: encoding.c:817
#define FMODE_READWRITE
Definition: io.h:95
gz enc2
Definition: zlib.c:2035
#define strio_print
Definition: stringio.c:1168
parser parser_yylval val
Definition: ripper.c:14289
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1343
#define error_inval(msg)
Definition: stringio.c:34
VALUE rb_str_cat(VALUE, const char *, long)
Definition: string.c:1889
#define LONG2NUM(i)
Definition: cparse.c:72
static VALUE strio_closed(VALUE self)
Definition: stringio.c:385
#define strio_set_sync
Definition: stringio.c:614
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: ruby.h:826
static void strio_free(void *p)
Definition: stringio.c:58
#define FMODE_WRITABLE
Definition: io.h:94
rb_encoding * rb_to_encoding(VALUE enc)
Definition: encoding.c:179
#define FMODE_READABLE
Definition: io.h:93
SYMID SyckParser * p
Definition: yaml2byte.c:119
VALUE rb_enc_from_encoding(rb_encoding *encoding)
Definition: encoding.c:102
static VALUE strio_write(VALUE self, VALUE str)
Definition: stringio.c:1119
unsigned long VALUE
Definition: ruby.h:88
VALUE enc
Definition: tcltklib.c:10402
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:740
static VALUE strio_readlines(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:1092
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:2088
static VALUE strio_readline(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:1038
#define RSTRING_PTR(string)
Definition: generator.h:42
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
Definition: encoding.c:894
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1574
int flags
Definition: stringio.c:27
void rb_str_update(VALUE, long, long, VALUE)
Definition: string.c:3350
return Qtrue
Definition: tcltklib.c:9597
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Definition: encoding.c:727
static VALUE strio_copy(VALUE copy, VALUE orig)
Definition: stringio.c:438
VALUE rb_io_taint_check(VALUE)
Definition: io.c:461
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2079
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
static VALUE strio_close_write(VALUE self)
Definition: stringio.c:368
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
Definition: encoding.c:847
static VALUE strio_getline(int argc, VALUE *argv, struct StringIO *ptr)
Definition: stringio.c:920
return str
Definition: ruby.c:1183
#define FMODE_APPEND
Definition: io.h:100
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:663
static VALUE strio_set_encoding(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:1392
static VALUE strio_truncate(VALUE self, VALUE len)
Definition: stringio.c:1338
#define strio_pid
Definition: stringio.c:1309
#define FIXNUM_P(f)
Definition: ruby.h:338
static VALUE strio_readbyte(VALUE self)
Definition: stringio.c:821
#define strio_isatty
Definition: stringio.c:1306
#define OBJ_TAINTED(x)
Definition: ruby.h:963
#define StringIO(obj)
Definition: stringio.c:108
#define strio_fsync
Definition: stringio.c:494
gz lineno
Definition: zlib.c:2031
#define CLOSED(ptr)
Definition: stringio.c:110
static VALUE strio_closed_write(VALUE self)
Definition: stringio.c:413
RUBY_EXTERN void * memmove(void *, const void *, size_t)
Definition: memmove.c:7
rb_secure(4)
#define strio_flush
Definition: stringio.c:491
static VALUE strio_get_pos(VALUE self)
Definition: stringio.c:523
#define Qnil
Definition: ruby.h:367
#define strio_puts
Definition: stringio.c:1208
#define MEMZERO(p, type, n)
Definition: ruby.h:1052
static VALUE strio_close_read(VALUE self)
Definition: stringio.c:350
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1245
n NULL
Definition: yaml2byte.c:134
memset(y->frac+ix+1, 0,(y->Prec-(ix+1))*sizeof(BDIGIT))
BDIGIT m
Definition: bigdecimal.c:4946
static VALUE strio_gets(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:1021
VALUE rb_class_new_instance(int, VALUE *, VALUE)
Definition: object.c:1639
return Qfalse
Definition: tcltklib.c:6768
static VALUE strio_s_allocate(VALUE klass)
Definition: stringio.c:144
int rb_block_given_p(void)
Definition: eval.c:604
#define rb_io_modenum_flags(oflags)
Definition: io.h:182
static VALUE strio_close(VALUE self)
Definition: stringio.c:332
VALUE string
Definition: stringio.c:24
#define strio_tell
Definition: stringio.c:616
#define RSTRING_END(str)
Definition: ruby.h:680
VALUE rb_obj_as_string(VALUE)
Definition: string.c:854
VALUE rb_ary_new(void)
Definition: array.c:339
static VALUE strio_internal_encoding(VALUE self)
Definition: stringio.c:1376
int flags
Definition: tcltklib.c:3012
#define NIL_P(v)
Definition: ruby.h:374
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:469
static void strio_init(int, VALUE *, struct StringIO *)
Definition: stringio.c:168
static VALUE VALUE obj
Definition: tcltklib.c:3147
static VALUE strio_get_lineno(VALUE self)
Definition: stringio.c:465
static struct StringIO * writable(struct StringIO *ptr)
Definition: stringio.c:124
static VALUE strio_0(VALUE self)
Definition: stringio.c:266
#define strio_binmode
Definition: stringio.c:485
static VALUE strio_set_lineno(VALUE self, VALUE lineno)
Definition: stringio.c:478
void rb_lastline_set(VALUE)
Definition: vm.c:761
#define OBJ_FROZEN(x)
Definition: ruby.h:969
static VALUE strio_self(VALUE self)
Definition: stringio.c:256
#define TYPE(x)
Definition: ruby.h:441
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Definition: string.c:528
static void strio_mark(void *p)
Definition: stringio.c:49
#define WRITABLE(ptr)
Definition: stringio.c:112
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1053
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:3913
static VALUE strio_read(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:1217
static VALUE strio_each(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:1065
#define ALLOC(type)
Definition: ruby.h:1035
static VALUE strio_getc(VALUE self)
Definition: stringio.c:649
VALUE rb_str_resize(VALUE, long)
Definition: string.c:1779
static VALUE strio_readchar(VALUE self)
Definition: stringio.c:807
VALUE * argv
Definition: tcltklib.c:1962
RUBY_EXTERN VALUE rb_cIO
Definition: ruby.h:1262
static VALUE strio_each_char(VALUE self)
Definition: stringio.c:839
memcpy(buf+1, str, len)
VALUE rb_yield(VALUE)
Definition: vm_eval.c:781
int errno
#define T_DATA
Definition: ruby.h:428
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:669
VALUE rb_mEnumerable
Definition: enum.c:17
#define RB_GC_GUARD(object)
Definition: generator.h:50
register char * s
Definition: os2.c:56
#define check_strio(self)
Definition: stringio.c:83
VALUE mode
Definition: tcltklib.c:1655
#define strio_addstr
Definition: stringio.c:1159
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1416
static VALUE strio_each_codepoint(VALUE self)
Definition: stringio.c:862
static VALUE strio_first(VALUE self, VALUE arg)
Definition: stringio.c:276
static VALUE strio_unimpl(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:286
#define READABLE(ptr)
Definition: stringio.c:111
int argc
Definition: tcltklib.c:1961
static VALUE strio_putc(VALUE self, VALUE ch)
Definition: stringio.c:1185
#define FIX2INT(x)
Definition: ruby.h:538
void Init_stringio()
Definition: stringio.c:1414
register unsigned int len
Definition: name2ctype.h:22210
#define xfree
Definition: defines.h:69
VALUE rb_call_super(int, const VALUE *)
Definition: vm_eval.c:189
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:737
RUBY_EXTERN VALUE rb_rs
Definition: intern.h:473
#define rb_io_mode_flags(modestr)
Definition: io.h:181
void rb_sys_fail(const char *mesg)
Definition: error.c:1671
static VALUE strio_set_pos(VALUE self, VALUE pos)
Definition: stringio.c:535
return ptr
Definition: tcltklib.c:780
VpDivd * c
Definition: bigdecimal.c:1163
#define CHAR_BIT
Definition: ruby.h:192
static VALUE strio_seek(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:570
static struct StringIO * readable(struct StringIO *ptr)
Definition: stringio.c:115
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:607
#define StringValueCStr(v)
Definition: ruby.h:468
static size_t strio_memsize(const void *p)
Definition: stringio.c:67
static VALUE strio_sysread(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:1289
#define rb_enc_right_char_head(s, p, e, enc)
Definition: encoding.h:164
arg
Definition: ripper.y:1287
void rb_str_modify(VALUE)
Definition: string.c:1319
static VALUE strio_rewind(VALUE self)
Definition: stringio.c:554
int count
Definition: stringio.c:28
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:733
#define INT2FIX(i)
Definition: ruby.h:225
static VALUE ULONG2NUM(unsigned long v)
Definition: ruby.h:1015
static void strio_extend(struct StringIO *ptr, long pos, long len)
Definition: stringio.c:684
static VALUE strio_initialize(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:155
static VALUE strio_reopen(int argc, VALUE *argv, VALUE self)
Definition: stringio.c:505
#define CHR2FIX(x)
Definition: ruby.h:1032
DATA_PTR(self)
static void bm_init_skip(long *skip, const char *pat, long m)
Definition: stringio.c:888
static VALUE strio_s_open(int argc, VALUE *argv, VALUE klass)
Definition: stringio.c:225
VALUE rb_check_string_type(VALUE)
Definition: string.c:1450
#define T_STRING
Definition: ruby.h:418
klass
Definition: tcltklib.c:3493
char ch
Definition: yaml2byte.c:124
#define OBJ_INFECT(x, s)
Definition: ruby.h:967
static VALUE strio_ungetbyte(VALUE self, VALUE c)
Definition: stringio.c:765
#define strio_printf
Definition: stringio.c:1176
static struct StringIO * get_strio(VALUE self)
Definition: stringio.c:86
void rb_notimplement(void)
Definition: error.c:1598
static VALUE strio_ungetc(VALUE self, VALUE c)
Definition: stringio.c:710
#define RETURN_ENUMERATOR(obj, argc, argv)
Definition: intern.h:210
#define SafeStringValue(v)
Definition: ruby.h:472
static VALUE strio_each_byte(VALUE self)
Definition: stringio.c:629
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
Definition: string.c:424
static VALUE strio_size(VALUE self)
Definition: stringio.c:1321
static VALUE strio_false(VALUE self)
Definition: stringio.c:236
static char NUM2CHR(VALUE x)
Definition: ruby.h:1027
RUBY_EXTERN VALUE rb_eIOError
Definition: ruby.h:1296
static VALUE strio_external_encoding(VALUE self)
Definition: stringio.c:1362
static VALUE strio_finalize(VALUE self)
Definition: stringio.c:209
static long bm_search(const char *little, long llen, const char *big, long blen, const long *skip)
Definition: stringio.c:901
BDIGIT e
Definition: bigdecimal.c:4946
rb_encoding * rb_ascii8bit_encoding(void)
Definition: encoding.c:1110
#define strio_fileno
Definition: stringio.c:1312
ssize_t i
Definition: bigdecimal.c:5519
#define rb_enc_mbcput(c, buf, enc)
Definition: encoding.h:158
static VALUE strio_set_string(VALUE self, VALUE string)
Definition: stringio.c:311
#define rb_intern(str)
static VALUE strio_get_sync(VALUE self)
Definition: stringio.c:607
rb_gc_mark(ptr->aliases)
static VALUE strio_eof(VALUE self)
Definition: stringio.c:429
static const rb_data_type_t strio_data_type
Definition: stringio.c:74
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1210
static VALUE strio_nil(VALUE self)
Definition: stringio.c:246
#define strio_syswrite
Definition: stringio.c:1298
VALUE rb_eArgError
Definition: error.c:468
STATIC void unsigned char * cp
Definition: crypt.c:307
#define StringValue(v)
Definition: ruby.h:466
#define strio_fcntl
Definition: stringio.c:488
long lineno
Definition: stringio.c:26
VALUE rb_str_new(const char *, long)
Definition: string.c:410
static VALUE strio_substr(struct StringIO *ptr, long pos, long len)
Definition: stringio.c:97
static struct StringIO * strio_alloc(void)
Definition: stringio.c:37
void rb_eof_error(void)
Definition: io.c:455