ray 0.0.1 → 0.1.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.yardopts +4 -0
- data/README.md +17 -21
- data/Rakefile +18 -139
- data/VERSION +1 -1
- data/ext/audio.cpp +723 -0
- data/ext/{color.c → color.cpp} +25 -13
- data/ext/drawable.cpp +91 -0
- data/ext/event.cpp +460 -0
- data/ext/extconf.rb +5 -104
- data/ext/font.cpp +190 -0
- data/ext/image.cpp +733 -0
- data/ext/input.cpp +74 -0
- data/ext/ray.cpp +168 -0
- data/ext/ray.hpp +356 -0
- data/ext/{rect.c → rect.cpp} +51 -37
- data/ext/shader.cpp +169 -0
- data/ext/shape.cpp +409 -0
- data/ext/sprite.cpp +306 -0
- data/ext/text.cpp +181 -0
- data/ext/vector.cpp +215 -0
- data/guide.md +619 -0
- data/lib/ray/audio.rb +0 -41
- data/lib/ray/color.rb +32 -10
- data/lib/ray/drawable.rb +16 -0
- data/lib/ray/dsl/event_listener.rb +25 -2
- data/lib/ray/dsl/event_runner.rb +33 -5
- data/lib/ray/dsl/event_translator.rb +66 -30
- data/lib/ray/dsl/handler.rb +3 -2
- data/lib/ray/dsl/matcher.rb +58 -14
- data/lib/ray/font.rb +38 -96
- data/lib/ray/font_set.rb +8 -8
- data/lib/ray/game.rb +87 -66
- data/lib/ray/helper.rb +105 -10
- data/lib/ray/image.rb +150 -24
- data/lib/ray/image_set.rb +3 -1
- data/lib/ray/input.rb +10 -0
- data/lib/ray/music_set.rb +5 -3
- data/lib/ray/ray.rb +21 -9
- data/lib/ray/rect.rb +48 -7
- data/lib/ray/rmagick.rb +41 -0
- data/lib/ray/scene.rb +99 -43
- data/lib/ray/scene_list.rb +67 -0
- data/lib/ray/shape.rb +132 -0
- data/lib/ray/sound_set.rb +4 -2
- data/lib/ray/sprite.rb +49 -111
- data/lib/ray/text.rb +101 -0
- data/lib/ray/text_helper.rb +37 -0
- data/lib/ray/turtle.rb +215 -0
- data/lib/ray/vector.rb +226 -0
- data/samples/audio/spacial.rb +44 -0
- data/samples/hello_world/hello.rb +9 -13
- data/samples/hello_world/hello_dsl.rb +8 -12
- data/samples/hello_world/text.rb +15 -0
- data/samples/opengl/binding.rb +38 -0
- data/samples/opengl/image.rb +32 -0
- data/samples/opengl/opengl.rb +34 -0
- data/samples/opengl/shader.rb +42 -0
- data/samples/pong/pong.rb +14 -10
- data/samples/run_scene.rb +53 -0
- data/samples/shaders/scene.rb +40 -0
- data/samples/shaders/shaders.rb +42 -0
- data/samples/shaders/shape.rb +34 -0
- data/samples/sokoban/sokoban.rb +18 -18
- data/samples/test/actual_scene.rb +41 -0
- data/samples/test/scene_riot.rb +39 -0
- data/samples/test/scene_spec.rb +32 -0
- data/samples/test/scene_test_unit.rb +25 -0
- data/samples/turtle/byzantium.rb +45 -0
- data/samples/turtle/hilbert.rb +48 -0
- data/samples/turtle/koch.rb +55 -0
- data/samples/turtle/mandala.rb +61 -0
- data/samples/turtle/tree.rb +57 -0
- data/test/audio_test.rb +69 -0
- data/test/color_test.rb +77 -0
- data/test/drawable_test.rb +19 -0
- data/test/dsl_test.rb +93 -0
- data/test/font_test.rb +57 -0
- data/test/helpers.rb +94 -0
- data/test/image_test.rb +82 -0
- data/test/ray_test.rb +25 -0
- data/test/rect_test.rb +121 -0
- data/{spec → test}/res/VeraMono.ttf +0 -0
- data/{spec → test}/res/aqua.bmp +0 -0
- data/{spec → test}/res/aqua.png +0 -0
- data/{spec → test}/res/aqua2.bmp +0 -0
- data/{spec → test}/res/not_a_jpeg.jpeg +0 -0
- data/{spec → test}/res/pop.wav +0 -0
- data/test/resource_set_test.rb +99 -0
- data/test/run_all.rb +7 -0
- data/test/shape_test.rb +101 -0
- data/test/sprite_test.rb +89 -0
- data/test/text_test.rb +78 -0
- data/test/turtle_test.rb +176 -0
- data/test/vector_test.rb +111 -0
- data/yard_ext.rb +0 -28
- metadata +95 -139
- data/.gitignore +0 -23
- data/.gitmodules +0 -3
- data/.rspec +0 -3
- data/ext/audio.c +0 -473
- data/ext/event.c +0 -557
- data/ext/font.c +0 -287
- data/ext/image.c +0 -933
- data/ext/joystick.c +0 -145
- data/ext/ray.c +0 -489
- data/ext/ray.h +0 -245
- data/ext/ray_osx.m +0 -161
- data/lib/ray/joystick.rb +0 -30
- data/psp/SDL_psp_main.c +0 -84
- data/psp/bigdecimal/README +0 -60
- data/psp/bigdecimal/bigdecimal.c +0 -4697
- data/psp/bigdecimal/bigdecimal.h +0 -216
- data/psp/bigdecimal/lib/bigdecimal/jacobian.rb +0 -85
- data/psp/bigdecimal/lib/bigdecimal/ludcmp.rb +0 -84
- data/psp/bigdecimal/lib/bigdecimal/math.rb +0 -235
- data/psp/bigdecimal/lib/bigdecimal/newton.rb +0 -77
- data/psp/bigdecimal/lib/bigdecimal/util.rb +0 -65
- data/psp/digest/bubblebabble/bubblebabble.c +0 -142
- data/psp/digest/defs.h +0 -20
- data/psp/digest/digest.c +0 -643
- data/psp/digest/digest.h +0 -32
- data/psp/digest/lib/digest.rb +0 -50
- data/psp/digest/lib/md5.rb +0 -27
- data/psp/digest/lib/sha1.rb +0 -27
- data/psp/digest/md5/md5.c +0 -420
- data/psp/digest/md5/md5.h +0 -80
- data/psp/digest/md5/md5init.c +0 -40
- data/psp/digest/rmd160/rmd160.c +0 -457
- data/psp/digest/rmd160/rmd160.h +0 -56
- data/psp/digest/rmd160/rmd160init.c +0 -40
- data/psp/digest/sha1/sha1.c +0 -269
- data/psp/digest/sha1/sha1.h +0 -39
- data/psp/digest/sha1/sha1init.c +0 -40
- data/psp/digest/sha2/lib/sha2.rb +0 -73
- data/psp/digest/sha2/sha2.c +0 -919
- data/psp/digest/sha2/sha2.h +0 -109
- data/psp/digest/sha2/sha2init.c +0 -52
- data/psp/enumerator/enumerator.c +0 -298
- data/psp/etc/etc.c +0 -559
- data/psp/ext.c +0 -289
- data/psp/fcntl/fcntl.c +0 -187
- data/psp/lib/rbconfig.rb +0 -178
- data/psp/nkf/lib/kconv.rb +0 -367
- data/psp/nkf/nkf-utf8/config.h +0 -88
- data/psp/nkf/nkf-utf8/nkf.c +0 -6040
- data/psp/nkf/nkf-utf8/utf8tbl.c +0 -8500
- data/psp/nkf/nkf-utf8/utf8tbl.h +0 -34
- data/psp/nkf/nkf.c +0 -654
- data/psp/socket/addrinfo.h +0 -173
- data/psp/socket/getaddrinfo.c +0 -676
- data/psp/socket/getnameinfo.c +0 -270
- data/psp/socket/pspsocket.c +0 -71
- data/psp/socket/pspsocket.h +0 -28
- data/psp/socket/socket.c +0 -4662
- data/psp/socket/sockport.h +0 -76
- data/psp/stringio/stringio.c +0 -1306
- data/psp/strscan/strscan.c +0 -1320
- data/psp/syck/bytecode.c +0 -1166
- data/psp/syck/emitter.c +0 -1242
- data/psp/syck/gram.c +0 -1894
- data/psp/syck/gram.h +0 -79
- data/psp/syck/handler.c +0 -174
- data/psp/syck/implicit.c +0 -2990
- data/psp/syck/node.c +0 -408
- data/psp/syck/rubyext.c +0 -2367
- data/psp/syck/syck.c +0 -504
- data/psp/syck/syck.h +0 -456
- data/psp/syck/token.c +0 -2725
- data/psp/syck/yaml2byte.c +0 -257
- data/psp/syck/yamlbyte.h +0 -170
- data/psp/thread/thread.c +0 -1175
- data/psp/zlib/zlib.c +0 -3547
- data/script.rb +0 -10
- data/spec/ray/audio_spec.rb +0 -146
- data/spec/ray/color_spec.rb +0 -57
- data/spec/ray/event_spec.rb +0 -80
- data/spec/ray/font_spec.rb +0 -93
- data/spec/ray/image_set_spec.rb +0 -48
- data/spec/ray/image_spec.rb +0 -162
- data/spec/ray/joystick_spec.rb +0 -21
- data/spec/ray/matcher_spec.rb +0 -50
- data/spec/ray/ray_spec.rb +0 -88
- data/spec/ray/rect_spec.rb +0 -154
- data/spec/ray/resource_set_spec.rb +0 -105
- data/spec/ray/sprite_spec.rb +0 -163
- data/spec/spec.opts +0 -4
- data/spec/spec_helper.rb +0 -8
data/psp/socket/sockport.h
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
/************************************************
|
2
|
-
|
3
|
-
sockport.h -
|
4
|
-
|
5
|
-
$Author: shyouhei $
|
6
|
-
$Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
|
7
|
-
created at: Fri Apr 30 23:19:34 JST 1999
|
8
|
-
|
9
|
-
************************************************/
|
10
|
-
|
11
|
-
#ifndef SOCKPORT_H
|
12
|
-
#define SOCKPORT_H
|
13
|
-
|
14
|
-
#ifndef SA_LEN
|
15
|
-
# ifdef HAVE_SA_LEN
|
16
|
-
# define SA_LEN(sa) (sa)->sa_len
|
17
|
-
# else
|
18
|
-
# ifdef INET6
|
19
|
-
# define SA_LEN(sa) \
|
20
|
-
(((sa)->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) \
|
21
|
-
: sizeof(struct sockaddr))
|
22
|
-
# else
|
23
|
-
/* by tradition, sizeof(struct sockaddr) covers most of the sockaddrs */
|
24
|
-
# define SA_LEN(sa) (sizeof(struct sockaddr))
|
25
|
-
# endif
|
26
|
-
# endif
|
27
|
-
#endif
|
28
|
-
|
29
|
-
#ifdef HAVE_SA_LEN
|
30
|
-
# define SET_SA_LEN(sa, len) (sa)->sa_len = (len)
|
31
|
-
#else
|
32
|
-
# define SET_SA_LEN(sa, len) (len)
|
33
|
-
#endif
|
34
|
-
|
35
|
-
#ifdef HAVE_SIN_LEN
|
36
|
-
# define SIN_LEN(si) (si)->sin_len
|
37
|
-
# define SET_SIN_LEN(si,len) (si)->sin_len = (len)
|
38
|
-
#else
|
39
|
-
# define SIN_LEN(si) sizeof(struct sockaddr_in)
|
40
|
-
# define SET_SIN_LEN(si,len)
|
41
|
-
#endif
|
42
|
-
|
43
|
-
#ifndef IN_MULTICAST
|
44
|
-
# define IN_CLASSD(i) (((long)(i) & 0xf0000000) == 0xe0000000)
|
45
|
-
# define IN_MULTICAST(i) IN_CLASSD(i)
|
46
|
-
#endif
|
47
|
-
|
48
|
-
#ifndef IN_EXPERIMENTAL
|
49
|
-
# define IN_EXPERIMENTAL(i) ((((long)(i)) & 0xe0000000) == 0xe0000000)
|
50
|
-
#endif
|
51
|
-
|
52
|
-
#ifndef IN_CLASSA_NSHIFT
|
53
|
-
# define IN_CLASSA_NSHIFT 24
|
54
|
-
#endif
|
55
|
-
|
56
|
-
#ifndef IN_LOOPBACKNET
|
57
|
-
# define IN_LOOPBACKNET 127
|
58
|
-
#endif
|
59
|
-
|
60
|
-
#ifndef AF_UNSPEC
|
61
|
-
# define AF_UNSPEC 0
|
62
|
-
#endif
|
63
|
-
|
64
|
-
#ifndef PF_UNSPEC
|
65
|
-
# define PF_UNSPEC AF_UNSPEC
|
66
|
-
#endif
|
67
|
-
|
68
|
-
#ifndef PF_INET
|
69
|
-
# define PF_INET AF_INET
|
70
|
-
#endif
|
71
|
-
|
72
|
-
#if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
|
73
|
-
extern int h_errno;
|
74
|
-
#endif
|
75
|
-
|
76
|
-
#endif
|
data/psp/stringio/stringio.c
DELETED
@@ -1,1306 +0,0 @@
|
|
1
|
-
/**********************************************************************
|
2
|
-
|
3
|
-
stringio.c -
|
4
|
-
|
5
|
-
$Author: shyouhei $
|
6
|
-
$Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
|
7
|
-
$RoughId: stringio.c,v 1.13 2002/03/14 03:24:18 nobu Exp $
|
8
|
-
created at: Tue Feb 19 04:10:38 JST 2002
|
9
|
-
|
10
|
-
All the files in this distribution are covered under the Ruby's
|
11
|
-
license (see the file COPYING).
|
12
|
-
|
13
|
-
**********************************************************************/
|
14
|
-
|
15
|
-
#include <ruby/ruby.h>
|
16
|
-
#include <ruby/rubyio.h>
|
17
|
-
|
18
|
-
#include <fcntl.h>
|
19
|
-
|
20
|
-
|
21
|
-
#define STRIO_EOF FMODE_SYNC
|
22
|
-
|
23
|
-
struct StringIO {
|
24
|
-
VALUE string;
|
25
|
-
long pos;
|
26
|
-
long lineno;
|
27
|
-
int flags;
|
28
|
-
int count;
|
29
|
-
};
|
30
|
-
|
31
|
-
static struct StringIO* strio_alloc _((void));
|
32
|
-
static void strio_mark _((struct StringIO *));
|
33
|
-
static void strio_free _((struct StringIO *));
|
34
|
-
static struct StringIO* check_strio _((VALUE));
|
35
|
-
static struct StringIO* get_strio _((VALUE));
|
36
|
-
static struct StringIO* readable _((struct StringIO *));
|
37
|
-
static struct StringIO* writable _((struct StringIO *));
|
38
|
-
static void check_modifiable _((struct StringIO *));
|
39
|
-
|
40
|
-
#define IS_STRIO(obj) (RDATA(obj)->dmark == (RUBY_DATA_FUNC)strio_mark)
|
41
|
-
#define error_inval(msg) (errno = EINVAL, rb_sys_fail(msg))
|
42
|
-
|
43
|
-
static struct StringIO *
|
44
|
-
strio_alloc()
|
45
|
-
{
|
46
|
-
struct StringIO *ptr = ALLOC(struct StringIO);
|
47
|
-
ptr->string = Qnil;
|
48
|
-
ptr->pos = 0;
|
49
|
-
ptr->lineno = 0;
|
50
|
-
ptr->flags = 0;
|
51
|
-
ptr->count = 1;
|
52
|
-
return ptr;
|
53
|
-
}
|
54
|
-
|
55
|
-
static void
|
56
|
-
strio_mark(ptr)
|
57
|
-
struct StringIO *ptr;
|
58
|
-
{
|
59
|
-
if (ptr) {
|
60
|
-
rb_gc_mark(ptr->string);
|
61
|
-
}
|
62
|
-
}
|
63
|
-
|
64
|
-
static void
|
65
|
-
strio_free(ptr)
|
66
|
-
struct StringIO *ptr;
|
67
|
-
{
|
68
|
-
if (--ptr->count <= 0) {
|
69
|
-
xfree(ptr);
|
70
|
-
}
|
71
|
-
}
|
72
|
-
|
73
|
-
static struct StringIO*
|
74
|
-
check_strio(self)
|
75
|
-
VALUE self;
|
76
|
-
{
|
77
|
-
Check_Type(self, T_DATA);
|
78
|
-
if (!IS_STRIO(self)) {
|
79
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected StringIO)",
|
80
|
-
rb_class2name(CLASS_OF(self)));
|
81
|
-
}
|
82
|
-
return DATA_PTR(self);
|
83
|
-
}
|
84
|
-
|
85
|
-
static struct StringIO*
|
86
|
-
get_strio(self)
|
87
|
-
VALUE self;
|
88
|
-
{
|
89
|
-
struct StringIO *ptr = check_strio(self);
|
90
|
-
|
91
|
-
if (!ptr) {
|
92
|
-
rb_raise(rb_eIOError, "uninitialized stream");
|
93
|
-
}
|
94
|
-
return ptr;
|
95
|
-
}
|
96
|
-
|
97
|
-
#define StringIO(obj) get_strio(obj)
|
98
|
-
|
99
|
-
#define CLOSED(ptr) (!((ptr)->flags & FMODE_READWRITE))
|
100
|
-
#define READABLE(ptr) ((ptr)->flags & FMODE_READABLE)
|
101
|
-
#define WRITABLE(ptr) ((ptr)->flags & FMODE_WRITABLE)
|
102
|
-
|
103
|
-
static struct StringIO*
|
104
|
-
readable(ptr)
|
105
|
-
struct StringIO *ptr;
|
106
|
-
{
|
107
|
-
if (!READABLE(ptr)) {
|
108
|
-
rb_raise(rb_eIOError, "not opened for reading");
|
109
|
-
}
|
110
|
-
return ptr;
|
111
|
-
}
|
112
|
-
|
113
|
-
static struct StringIO*
|
114
|
-
writable(ptr)
|
115
|
-
struct StringIO *ptr;
|
116
|
-
{
|
117
|
-
if (!WRITABLE(ptr)) {
|
118
|
-
rb_raise(rb_eIOError, "not opened for writing");
|
119
|
-
}
|
120
|
-
if (!OBJ_TAINTED(ptr->string)) {
|
121
|
-
rb_secure(4);
|
122
|
-
}
|
123
|
-
return ptr;
|
124
|
-
}
|
125
|
-
|
126
|
-
static void
|
127
|
-
check_modifiable(ptr)
|
128
|
-
struct StringIO *ptr;
|
129
|
-
{
|
130
|
-
if (OBJ_FROZEN(ptr->string)) {
|
131
|
-
rb_raise(rb_eIOError, "not modifiable string");
|
132
|
-
}
|
133
|
-
}
|
134
|
-
|
135
|
-
static VALUE strio_s_allocate _((VALUE));
|
136
|
-
static VALUE strio_s_open _((int, VALUE *, VALUE));
|
137
|
-
static VALUE strio_initialize _((int, VALUE *, VALUE));
|
138
|
-
static VALUE strio_finalize _((VALUE));
|
139
|
-
static VALUE strio_self _((VALUE));
|
140
|
-
static VALUE strio_false _((VALUE));
|
141
|
-
static VALUE strio_nil _((VALUE));
|
142
|
-
static VALUE strio_0 _((VALUE));
|
143
|
-
static VALUE strio_first _((VALUE, VALUE));
|
144
|
-
static VALUE strio_unimpl _((int, VALUE *, VALUE));
|
145
|
-
static VALUE strio_get_string _((VALUE));
|
146
|
-
static VALUE strio_set_string _((VALUE, VALUE));
|
147
|
-
static VALUE strio_close _((VALUE));
|
148
|
-
static VALUE strio_close_read _((VALUE));
|
149
|
-
static VALUE strio_close_write _((VALUE));
|
150
|
-
static VALUE strio_closed _((VALUE));
|
151
|
-
static VALUE strio_closed_read _((VALUE));
|
152
|
-
static VALUE strio_closed_write _((VALUE));
|
153
|
-
static VALUE strio_eof _((VALUE));
|
154
|
-
static VALUE strio_get_lineno _((VALUE));
|
155
|
-
static VALUE strio_set_lineno _((VALUE, VALUE));
|
156
|
-
static VALUE strio_get_pos _((VALUE));
|
157
|
-
static VALUE strio_set_pos _((VALUE, VALUE));
|
158
|
-
static VALUE strio_rewind _((VALUE));
|
159
|
-
static VALUE strio_seek _((int, VALUE *, VALUE));
|
160
|
-
static VALUE strio_get_sync _((VALUE));
|
161
|
-
static VALUE strio_each_byte _((VALUE));
|
162
|
-
static VALUE strio_getc _((VALUE));
|
163
|
-
static VALUE strio_ungetc _((VALUE, VALUE));
|
164
|
-
static VALUE strio_readchar _((VALUE));
|
165
|
-
static VALUE strio_getline _((int, VALUE *, struct StringIO *));
|
166
|
-
static VALUE strio_gets _((int, VALUE *, VALUE));
|
167
|
-
static VALUE strio_readline _((int, VALUE *, VALUE));
|
168
|
-
static VALUE strio_each _((int, VALUE *, VALUE));
|
169
|
-
static VALUE strio_readlines _((int, VALUE *, VALUE));
|
170
|
-
static VALUE strio_write _((VALUE, VALUE));
|
171
|
-
static VALUE strio_putc _((VALUE, VALUE));
|
172
|
-
static VALUE strio_read _((int, VALUE *, VALUE));
|
173
|
-
static VALUE strio_size _((VALUE));
|
174
|
-
static VALUE strio_truncate _((VALUE, VALUE));
|
175
|
-
void Init_stringio _((void));
|
176
|
-
|
177
|
-
/* Boyer-Moore search: copied from regex.c */
|
178
|
-
static void bm_init_skip _((long *, const char *, long));
|
179
|
-
static long bm_search _((const char *, long, const char *, long, const long *));
|
180
|
-
|
181
|
-
static VALUE
|
182
|
-
strio_s_allocate(klass)
|
183
|
-
VALUE klass;
|
184
|
-
{
|
185
|
-
return Data_Wrap_Struct(klass, strio_mark, strio_free, 0);
|
186
|
-
}
|
187
|
-
|
188
|
-
/*
|
189
|
-
* call-seq: StringIO.open(string=""[, mode]) {|strio| ...}
|
190
|
-
*
|
191
|
-
* Equivalent to StringIO.new except that when it is called with a block, it
|
192
|
-
* yields with the new instance and closes it, and returns the result which
|
193
|
-
* returned from the block.
|
194
|
-
*/
|
195
|
-
static VALUE
|
196
|
-
strio_s_open(argc, argv, klass)
|
197
|
-
int argc;
|
198
|
-
VALUE *argv;
|
199
|
-
VALUE klass;
|
200
|
-
{
|
201
|
-
VALUE obj = rb_class_new_instance(argc, argv, klass);
|
202
|
-
if (!rb_block_given_p()) return obj;
|
203
|
-
return rb_ensure(rb_yield, obj, strio_finalize, obj);
|
204
|
-
}
|
205
|
-
|
206
|
-
/*
|
207
|
-
* call-seq: StringIO.new(string=""[, mode])
|
208
|
-
*
|
209
|
-
* Creates new StringIO instance from with _string_ and _mode_.
|
210
|
-
*/
|
211
|
-
static VALUE
|
212
|
-
strio_initialize(argc, argv, self)
|
213
|
-
int argc;
|
214
|
-
VALUE *argv;
|
215
|
-
VALUE self;
|
216
|
-
{
|
217
|
-
struct StringIO *ptr = check_strio(self);
|
218
|
-
VALUE string, mode;
|
219
|
-
int trunc = Qfalse;
|
220
|
-
|
221
|
-
if (!ptr) {
|
222
|
-
DATA_PTR(self) = ptr = strio_alloc();
|
223
|
-
}
|
224
|
-
rb_call_super(0, 0);
|
225
|
-
switch (rb_scan_args(argc, argv, "02", &string, &mode)) {
|
226
|
-
case 2:
|
227
|
-
if (FIXNUM_P(mode)) {
|
228
|
-
int flags = FIX2INT(mode);
|
229
|
-
ptr->flags = rb_io_modenum_flags(flags);
|
230
|
-
trunc = flags & O_TRUNC;
|
231
|
-
}
|
232
|
-
else {
|
233
|
-
const char *m = StringValueCStr(mode);
|
234
|
-
ptr->flags = rb_io_mode_flags(m);
|
235
|
-
trunc = *m == 'w';
|
236
|
-
}
|
237
|
-
StringValue(string);
|
238
|
-
if ((ptr->flags & FMODE_WRITABLE) && OBJ_FROZEN(string)) {
|
239
|
-
errno = EACCES;
|
240
|
-
rb_sys_fail(0);
|
241
|
-
}
|
242
|
-
if (trunc) {
|
243
|
-
rb_str_resize(string, 0);
|
244
|
-
}
|
245
|
-
break;
|
246
|
-
case 1:
|
247
|
-
StringValue(string);
|
248
|
-
ptr->flags = OBJ_FROZEN(string) ? FMODE_READABLE : FMODE_READWRITE;
|
249
|
-
break;
|
250
|
-
case 0:
|
251
|
-
string = rb_str_new("", 0);
|
252
|
-
ptr->flags = FMODE_READWRITE;
|
253
|
-
break;
|
254
|
-
}
|
255
|
-
ptr->string = string;
|
256
|
-
return self;
|
257
|
-
}
|
258
|
-
|
259
|
-
static VALUE
|
260
|
-
strio_finalize(self)
|
261
|
-
VALUE self;
|
262
|
-
{
|
263
|
-
struct StringIO *ptr = StringIO(self);
|
264
|
-
ptr->string = Qnil;
|
265
|
-
ptr->flags &= ~FMODE_READWRITE;
|
266
|
-
return self;
|
267
|
-
}
|
268
|
-
|
269
|
-
/*
|
270
|
-
* Returns +false+. Just for compatibility to IO.
|
271
|
-
*/
|
272
|
-
static VALUE
|
273
|
-
strio_false(self)
|
274
|
-
VALUE self;
|
275
|
-
{
|
276
|
-
StringIO(self);
|
277
|
-
return Qfalse;
|
278
|
-
}
|
279
|
-
|
280
|
-
/*
|
281
|
-
* Returns +nil+. Just for compatibility to IO.
|
282
|
-
*/
|
283
|
-
static VALUE
|
284
|
-
strio_nil(self)
|
285
|
-
VALUE self;
|
286
|
-
{
|
287
|
-
StringIO(self);
|
288
|
-
return Qnil;
|
289
|
-
}
|
290
|
-
|
291
|
-
/*
|
292
|
-
* Returns *strio* itself. Just for compatibility to IO.
|
293
|
-
*/
|
294
|
-
static VALUE
|
295
|
-
strio_self(self)
|
296
|
-
VALUE self;
|
297
|
-
{
|
298
|
-
StringIO(self);
|
299
|
-
return self;
|
300
|
-
}
|
301
|
-
|
302
|
-
/*
|
303
|
-
* Returns 0. Just for compatibility to IO.
|
304
|
-
*/
|
305
|
-
static VALUE
|
306
|
-
strio_0(self)
|
307
|
-
VALUE self;
|
308
|
-
{
|
309
|
-
StringIO(self);
|
310
|
-
return INT2FIX(0);
|
311
|
-
}
|
312
|
-
|
313
|
-
/*
|
314
|
-
* Returns the argument unchanged. Just for compatibility to IO.
|
315
|
-
*/
|
316
|
-
static VALUE
|
317
|
-
strio_first(self, arg)
|
318
|
-
VALUE self, arg;
|
319
|
-
{
|
320
|
-
StringIO(self);
|
321
|
-
return arg;
|
322
|
-
}
|
323
|
-
|
324
|
-
/*
|
325
|
-
* Raises NotImplementedError.
|
326
|
-
*/
|
327
|
-
static VALUE
|
328
|
-
strio_unimpl(argc, argv, self)
|
329
|
-
int argc;
|
330
|
-
VALUE *argv;
|
331
|
-
VALUE self;
|
332
|
-
{
|
333
|
-
StringIO(self);
|
334
|
-
rb_notimplement();
|
335
|
-
return Qnil; /* not reached */
|
336
|
-
}
|
337
|
-
|
338
|
-
/*
|
339
|
-
* call-seq: strio.string -> string
|
340
|
-
*
|
341
|
-
* Returns underlying String object, the subject of IO.
|
342
|
-
*/
|
343
|
-
static VALUE
|
344
|
-
strio_get_string(self)
|
345
|
-
VALUE self;
|
346
|
-
{
|
347
|
-
return StringIO(self)->string;
|
348
|
-
}
|
349
|
-
|
350
|
-
/*
|
351
|
-
* call-seq:
|
352
|
-
* strio.string = string -> string
|
353
|
-
*
|
354
|
-
* Changes underlying String object, the subject of IO.
|
355
|
-
*/
|
356
|
-
static VALUE
|
357
|
-
strio_set_string(self, string)
|
358
|
-
VALUE self, string;
|
359
|
-
{
|
360
|
-
struct StringIO *ptr = StringIO(self);
|
361
|
-
|
362
|
-
if (!OBJ_TAINTED(self)) rb_secure(4);
|
363
|
-
ptr->flags &= ~FMODE_READWRITE;
|
364
|
-
StringValue(string);
|
365
|
-
ptr->flags = OBJ_FROZEN(string) ? FMODE_READABLE : FMODE_READWRITE;
|
366
|
-
ptr->pos = 0;
|
367
|
-
ptr->lineno = 0;
|
368
|
-
return ptr->string = string;
|
369
|
-
}
|
370
|
-
|
371
|
-
/*
|
372
|
-
* call-seq:
|
373
|
-
* strio.close -> nil
|
374
|
-
*
|
375
|
-
* Closes strio. The *strio* is unavailable for any further data
|
376
|
-
* operations; an +IOError+ is raised if such an attempt is made.
|
377
|
-
*/
|
378
|
-
static VALUE
|
379
|
-
strio_close(self)
|
380
|
-
VALUE self;
|
381
|
-
{
|
382
|
-
struct StringIO *ptr = StringIO(self);
|
383
|
-
if (CLOSED(ptr)) {
|
384
|
-
rb_raise(rb_eIOError, "closed stream");
|
385
|
-
}
|
386
|
-
ptr->flags &= ~FMODE_READWRITE;
|
387
|
-
return Qnil;
|
388
|
-
}
|
389
|
-
|
390
|
-
/*
|
391
|
-
* call-seq:
|
392
|
-
* strio.close_read -> nil
|
393
|
-
*
|
394
|
-
* Closes the read end of a StringIO. Will raise an +IOError+ if the
|
395
|
-
* *strio* is not readable.
|
396
|
-
*/
|
397
|
-
static VALUE
|
398
|
-
strio_close_read(self)
|
399
|
-
VALUE self;
|
400
|
-
{
|
401
|
-
struct StringIO *ptr = StringIO(self);
|
402
|
-
if (!READABLE(ptr)) {
|
403
|
-
rb_raise(rb_eIOError, "closing non-duplex IO for reading");
|
404
|
-
}
|
405
|
-
ptr->flags &= ~FMODE_READABLE;
|
406
|
-
return Qnil;
|
407
|
-
}
|
408
|
-
|
409
|
-
/*
|
410
|
-
* call-seq:
|
411
|
-
* strio.close_write -> nil
|
412
|
-
*
|
413
|
-
* Closes the write end of a StringIO. Will raise an +IOError+ if the
|
414
|
-
* *strio* is not writeable.
|
415
|
-
*/
|
416
|
-
static VALUE
|
417
|
-
strio_close_write(self)
|
418
|
-
VALUE self;
|
419
|
-
{
|
420
|
-
struct StringIO *ptr = StringIO(self);
|
421
|
-
if (!WRITABLE(ptr)) {
|
422
|
-
rb_raise(rb_eIOError, "closing non-duplex IO for writing");
|
423
|
-
}
|
424
|
-
ptr->flags &= ~FMODE_WRITABLE;
|
425
|
-
return Qnil;
|
426
|
-
}
|
427
|
-
|
428
|
-
/*
|
429
|
-
* call-seq:
|
430
|
-
* strio.closed? -> true or false
|
431
|
-
*
|
432
|
-
* Returns +true+ if *strio* is completely closed, +false+ otherwise.
|
433
|
-
*/
|
434
|
-
static VALUE
|
435
|
-
strio_closed(self)
|
436
|
-
VALUE self;
|
437
|
-
{
|
438
|
-
struct StringIO *ptr = StringIO(self);
|
439
|
-
if (!CLOSED(ptr)) return Qfalse;
|
440
|
-
return Qtrue;
|
441
|
-
}
|
442
|
-
|
443
|
-
/*
|
444
|
-
* call-seq:
|
445
|
-
* strio.closed_read? -> true or false
|
446
|
-
*
|
447
|
-
* Returns +true+ if *strio* is not readable, +false+ otherwise.
|
448
|
-
*/
|
449
|
-
static VALUE
|
450
|
-
strio_closed_read(self)
|
451
|
-
VALUE self;
|
452
|
-
{
|
453
|
-
struct StringIO *ptr = StringIO(self);
|
454
|
-
if (READABLE(ptr)) return Qfalse;
|
455
|
-
return Qtrue;
|
456
|
-
}
|
457
|
-
|
458
|
-
/*
|
459
|
-
* call-seq:
|
460
|
-
* strio.closed_write? -> true or false
|
461
|
-
*
|
462
|
-
* Returns +true+ if *strio* is not writable, +false+ otherwise.
|
463
|
-
*/
|
464
|
-
static VALUE
|
465
|
-
strio_closed_write(self)
|
466
|
-
VALUE self;
|
467
|
-
{
|
468
|
-
struct StringIO *ptr = StringIO(self);
|
469
|
-
if (WRITABLE(ptr)) return Qfalse;
|
470
|
-
return Qtrue;
|
471
|
-
}
|
472
|
-
|
473
|
-
/*
|
474
|
-
* call-seq:
|
475
|
-
* strio.eof -> true or false
|
476
|
-
* strio.eof? -> true or false
|
477
|
-
*
|
478
|
-
* Returns true if *strio* is at end of file. The stringio must be
|
479
|
-
* opened for reading or an +IOError+ will be raised.
|
480
|
-
*/
|
481
|
-
static VALUE
|
482
|
-
strio_eof(self)
|
483
|
-
VALUE self;
|
484
|
-
{
|
485
|
-
struct StringIO *ptr = readable(StringIO(self));
|
486
|
-
if (ptr->pos < RSTRING(ptr->string)->len) return Qfalse;
|
487
|
-
return Qtrue;
|
488
|
-
}
|
489
|
-
|
490
|
-
/* :nodoc: */
|
491
|
-
static VALUE
|
492
|
-
strio_copy(copy, orig)
|
493
|
-
VALUE copy, orig;
|
494
|
-
{
|
495
|
-
struct StringIO *ptr;
|
496
|
-
|
497
|
-
orig = rb_convert_type(orig, T_DATA, "StringIO", "to_strio");
|
498
|
-
if (copy == orig) return copy;
|
499
|
-
ptr = StringIO(orig);
|
500
|
-
if (check_strio(copy)) {
|
501
|
-
strio_free(DATA_PTR(copy));
|
502
|
-
}
|
503
|
-
DATA_PTR(copy) = ptr;
|
504
|
-
OBJ_INFECT(copy, orig);
|
505
|
-
++ptr->count;
|
506
|
-
return copy;
|
507
|
-
}
|
508
|
-
|
509
|
-
/*
|
510
|
-
* call-seq:
|
511
|
-
* strio.lineno -> integer
|
512
|
-
*
|
513
|
-
* Returns the current line number in *strio*. The stringio must be
|
514
|
-
* opened for reading. +lineno+ counts the number of times +gets+ is
|
515
|
-
* called, rather than the number of newlines encountered. The two
|
516
|
-
* values will differ if +gets+ is called with a separator other than
|
517
|
-
* newline. See also the <code>$.</code> variable.
|
518
|
-
*/
|
519
|
-
static VALUE
|
520
|
-
strio_get_lineno(self)
|
521
|
-
VALUE self;
|
522
|
-
{
|
523
|
-
return LONG2NUM(StringIO(self)->lineno);
|
524
|
-
}
|
525
|
-
|
526
|
-
/*
|
527
|
-
* call-seq:
|
528
|
-
* strio.lineno = integer -> integer
|
529
|
-
*
|
530
|
-
* Manually sets the current line number to the given value.
|
531
|
-
* <code>$.</code> is updated only on the next read.
|
532
|
-
*/
|
533
|
-
static VALUE
|
534
|
-
strio_set_lineno(self, lineno)
|
535
|
-
VALUE self, lineno;
|
536
|
-
{
|
537
|
-
StringIO(self)->lineno = NUM2LONG(lineno);
|
538
|
-
return lineno;
|
539
|
-
}
|
540
|
-
|
541
|
-
/* call-seq: strio.binmode -> true */
|
542
|
-
#define strio_binmode strio_self
|
543
|
-
|
544
|
-
/* call-seq: strio.fcntl */
|
545
|
-
#define strio_fcntl strio_unimpl
|
546
|
-
|
547
|
-
/* call-seq: strio.flush -> strio */
|
548
|
-
#define strio_flush strio_self
|
549
|
-
|
550
|
-
/* call-seq: strio.fsync -> 0 */
|
551
|
-
#define strio_fsync strio_0
|
552
|
-
|
553
|
-
/*
|
554
|
-
* call-seq:
|
555
|
-
* strio.reopen(other_StrIO) -> strio
|
556
|
-
* strio.reopen(string, mode) -> strio
|
557
|
-
*
|
558
|
-
* Reinitializes *strio* with the given <i>other_StrIO</i> or _string_
|
559
|
-
* and _mode_ (see StringIO#new).
|
560
|
-
*/
|
561
|
-
static VALUE
|
562
|
-
strio_reopen(argc, argv, self)
|
563
|
-
int argc;
|
564
|
-
VALUE *argv;
|
565
|
-
VALUE self;
|
566
|
-
{
|
567
|
-
if (!OBJ_TAINTED(self)) rb_secure(4);
|
568
|
-
if (argc == 1 && TYPE(*argv) != T_STRING) {
|
569
|
-
return strio_copy(self, *argv);
|
570
|
-
}
|
571
|
-
return strio_initialize(argc, argv, self);
|
572
|
-
}
|
573
|
-
|
574
|
-
/*
|
575
|
-
* call-seq:
|
576
|
-
* strio.pos -> integer
|
577
|
-
* strio.tell -> integer
|
578
|
-
*
|
579
|
-
* Returns the current offset (in bytes) of *strio*.
|
580
|
-
*/
|
581
|
-
static VALUE
|
582
|
-
strio_get_pos(self)
|
583
|
-
VALUE self;
|
584
|
-
{
|
585
|
-
return LONG2NUM(StringIO(self)->pos);
|
586
|
-
}
|
587
|
-
|
588
|
-
/*
|
589
|
-
* call-seq:
|
590
|
-
* strio.pos = integer -> integer
|
591
|
-
*
|
592
|
-
* Seeks to the given position (in bytes) in *strio*.
|
593
|
-
*/
|
594
|
-
static VALUE
|
595
|
-
strio_set_pos(self, pos)
|
596
|
-
VALUE self;
|
597
|
-
VALUE pos;
|
598
|
-
{
|
599
|
-
struct StringIO *ptr = StringIO(self);
|
600
|
-
long p = NUM2LONG(pos);
|
601
|
-
if (p < 0) {
|
602
|
-
error_inval(0);
|
603
|
-
}
|
604
|
-
ptr->pos = p;
|
605
|
-
ptr->flags &= ~STRIO_EOF;
|
606
|
-
return pos;
|
607
|
-
}
|
608
|
-
|
609
|
-
/*
|
610
|
-
* call-seq:
|
611
|
-
* strio.rewind -> 0
|
612
|
-
*
|
613
|
-
* Positions *strio* to the beginning of input, resetting
|
614
|
-
* +lineno+ to zero.
|
615
|
-
*/
|
616
|
-
static VALUE
|
617
|
-
strio_rewind(self)
|
618
|
-
VALUE self;
|
619
|
-
{
|
620
|
-
struct StringIO *ptr = StringIO(self);
|
621
|
-
ptr->pos = 0;
|
622
|
-
ptr->lineno = 0;
|
623
|
-
ptr->flags &= ~STRIO_EOF;
|
624
|
-
return INT2FIX(0);
|
625
|
-
}
|
626
|
-
|
627
|
-
/*
|
628
|
-
* call-seq:
|
629
|
-
* strio.seek(amount, whence=SEEK_SET) -> 0
|
630
|
-
*
|
631
|
-
* Seeks to a given offset _amount_ in the stream according to
|
632
|
-
* the value of _whence_ (see IO#seek).
|
633
|
-
*/
|
634
|
-
static VALUE
|
635
|
-
strio_seek(argc, argv, self)
|
636
|
-
int argc;
|
637
|
-
VALUE *argv;
|
638
|
-
VALUE self;
|
639
|
-
{
|
640
|
-
VALUE whence;
|
641
|
-
struct StringIO *ptr = StringIO(self);
|
642
|
-
long offset;
|
643
|
-
|
644
|
-
rb_scan_args(argc, argv, "11", NULL, &whence);
|
645
|
-
offset = NUM2LONG(argv[0]);
|
646
|
-
switch (NIL_P(whence) ? 0 : NUM2LONG(whence)) {
|
647
|
-
case 0:
|
648
|
-
break;
|
649
|
-
case 1:
|
650
|
-
offset += ptr->pos;
|
651
|
-
break;
|
652
|
-
case 2:
|
653
|
-
offset += RSTRING(ptr->string)->len;
|
654
|
-
break;
|
655
|
-
default:
|
656
|
-
rb_raise(rb_eArgError, "invalid whence %ld", NUM2LONG(whence));
|
657
|
-
}
|
658
|
-
if (offset < 0) {
|
659
|
-
error_inval(0);
|
660
|
-
}
|
661
|
-
ptr->pos = offset;
|
662
|
-
ptr->flags &= ~STRIO_EOF;
|
663
|
-
return INT2FIX(0);
|
664
|
-
}
|
665
|
-
|
666
|
-
/*
|
667
|
-
* call-seq:
|
668
|
-
* strio.sync -> true
|
669
|
-
*
|
670
|
-
* Returns +true+ always.
|
671
|
-
*/
|
672
|
-
static VALUE
|
673
|
-
strio_get_sync(self)
|
674
|
-
VALUE self;
|
675
|
-
{
|
676
|
-
StringIO(self);
|
677
|
-
return Qtrue;
|
678
|
-
}
|
679
|
-
|
680
|
-
/* call-seq: strio.sync = boolean -> boolean */
|
681
|
-
#define strio_set_sync strio_first
|
682
|
-
|
683
|
-
#define strio_tell strio_get_pos
|
684
|
-
|
685
|
-
/*
|
686
|
-
* call-seq:
|
687
|
-
* strio.each_byte {|byte| block } -> strio
|
688
|
-
*
|
689
|
-
* See IO#each_byte.
|
690
|
-
*/
|
691
|
-
static VALUE
|
692
|
-
strio_each_byte(self)
|
693
|
-
VALUE self;
|
694
|
-
{
|
695
|
-
struct StringIO *ptr = readable(StringIO(self));
|
696
|
-
while (ptr->pos < RSTRING(ptr->string)->len) {
|
697
|
-
char c = RSTRING(ptr->string)->ptr[ptr->pos++];
|
698
|
-
rb_yield(CHR2FIX(c));
|
699
|
-
}
|
700
|
-
return Qnil;
|
701
|
-
}
|
702
|
-
|
703
|
-
/*
|
704
|
-
* call-seq:
|
705
|
-
* strio.getc -> fixnum or nil
|
706
|
-
*
|
707
|
-
* See IO#getc.
|
708
|
-
*/
|
709
|
-
static VALUE
|
710
|
-
strio_getc(self)
|
711
|
-
VALUE self;
|
712
|
-
{
|
713
|
-
struct StringIO *ptr = readable(StringIO(self));
|
714
|
-
int c;
|
715
|
-
if (ptr->pos >= RSTRING(ptr->string)->len) {
|
716
|
-
ptr->flags |= STRIO_EOF;
|
717
|
-
return Qnil;
|
718
|
-
}
|
719
|
-
c = RSTRING(ptr->string)->ptr[ptr->pos++];
|
720
|
-
return CHR2FIX(c);
|
721
|
-
}
|
722
|
-
|
723
|
-
static void
|
724
|
-
strio_extend(ptr, pos, len)
|
725
|
-
struct StringIO *ptr;
|
726
|
-
long pos, len;
|
727
|
-
{
|
728
|
-
long olen;
|
729
|
-
|
730
|
-
check_modifiable(ptr);
|
731
|
-
olen = RSTRING(ptr->string)->len;
|
732
|
-
if (pos + len > olen) {
|
733
|
-
rb_str_resize(ptr->string, pos + len);
|
734
|
-
if (pos > olen)
|
735
|
-
MEMZERO(RSTRING(ptr->string)->ptr + olen, char, pos - olen);
|
736
|
-
}
|
737
|
-
else {
|
738
|
-
rb_str_modify(ptr->string);
|
739
|
-
}
|
740
|
-
}
|
741
|
-
|
742
|
-
/*
|
743
|
-
* call-seq:
|
744
|
-
* strio.ungetc(integer) -> nil
|
745
|
-
*
|
746
|
-
* Pushes back one character (passed as a parameter) onto *strio*
|
747
|
-
* such that a subsequent buffered read will return it. Pushing back
|
748
|
-
* behind the beginning of the buffer string is not possible. Nothing
|
749
|
-
* will be done if such an attempt is made.
|
750
|
-
* In other case, there is no limitation for multiple pushbacks.
|
751
|
-
*/
|
752
|
-
static VALUE
|
753
|
-
strio_ungetc(self, ch)
|
754
|
-
VALUE self, ch;
|
755
|
-
{
|
756
|
-
struct StringIO *ptr = readable(StringIO(self));
|
757
|
-
int cc = NUM2INT(ch);
|
758
|
-
long len, pos = ptr->pos;
|
759
|
-
|
760
|
-
if (cc != EOF && pos > 0) {
|
761
|
-
if ((len = RSTRING(ptr->string)->len) < pos-- ||
|
762
|
-
(unsigned char)RSTRING(ptr->string)->ptr[pos] !=
|
763
|
-
(unsigned char)cc) {
|
764
|
-
strio_extend(ptr, pos, 1);
|
765
|
-
RSTRING(ptr->string)->ptr[pos] = cc;
|
766
|
-
OBJ_INFECT(ptr->string, self);
|
767
|
-
}
|
768
|
-
--ptr->pos;
|
769
|
-
ptr->flags &= ~STRIO_EOF;
|
770
|
-
}
|
771
|
-
return Qnil;
|
772
|
-
}
|
773
|
-
|
774
|
-
/*
|
775
|
-
* call-seq:
|
776
|
-
* strio.readchar -> fixnum
|
777
|
-
*
|
778
|
-
* See IO#readchar.
|
779
|
-
*/
|
780
|
-
static VALUE
|
781
|
-
strio_readchar(self)
|
782
|
-
VALUE self;
|
783
|
-
{
|
784
|
-
VALUE c = strio_getc(self);
|
785
|
-
if (NIL_P(c)) rb_eof_error();
|
786
|
-
return c;
|
787
|
-
}
|
788
|
-
|
789
|
-
static void
|
790
|
-
bm_init_skip(skip, pat, m)
|
791
|
-
long *skip;
|
792
|
-
const char *pat;
|
793
|
-
long m;
|
794
|
-
{
|
795
|
-
int c;
|
796
|
-
|
797
|
-
for (c = 0; c < (1 << CHAR_BIT); c++) {
|
798
|
-
skip[c] = m;
|
799
|
-
}
|
800
|
-
while (--m) {
|
801
|
-
skip[(unsigned char)*pat++] = m;
|
802
|
-
}
|
803
|
-
}
|
804
|
-
|
805
|
-
static long
|
806
|
-
bm_search(little, llen, big, blen, skip)
|
807
|
-
const char *little;
|
808
|
-
long llen;
|
809
|
-
const char *big;
|
810
|
-
long blen;
|
811
|
-
const long *skip;
|
812
|
-
{
|
813
|
-
long i, j, k;
|
814
|
-
|
815
|
-
i = llen - 1;
|
816
|
-
while (i < blen) {
|
817
|
-
k = i;
|
818
|
-
j = llen - 1;
|
819
|
-
while (j >= 0 && big[k] == little[j]) {
|
820
|
-
k--;
|
821
|
-
j--;
|
822
|
-
}
|
823
|
-
if (j < 0) return k + 1;
|
824
|
-
i += skip[(unsigned char)big[i]];
|
825
|
-
}
|
826
|
-
return -1;
|
827
|
-
}
|
828
|
-
|
829
|
-
static VALUE
|
830
|
-
strio_getline(argc, argv, ptr)
|
831
|
-
int argc;
|
832
|
-
VALUE *argv;
|
833
|
-
struct StringIO *ptr;
|
834
|
-
{
|
835
|
-
const char *s, *e, *p;
|
836
|
-
long n;
|
837
|
-
VALUE str;
|
838
|
-
|
839
|
-
if (argc == 0) {
|
840
|
-
str = rb_rs;
|
841
|
-
}
|
842
|
-
else {
|
843
|
-
rb_scan_args(argc, argv, "1", &str);
|
844
|
-
if (!NIL_P(str)) StringValue(str);
|
845
|
-
}
|
846
|
-
|
847
|
-
if (ptr->pos >= (n = RSTRING(ptr->string)->len)) {
|
848
|
-
ptr->flags |= STRIO_EOF;
|
849
|
-
return Qnil;
|
850
|
-
}
|
851
|
-
s = RSTRING(ptr->string)->ptr;
|
852
|
-
e = s + RSTRING(ptr->string)->len;
|
853
|
-
s += ptr->pos;
|
854
|
-
if (NIL_P(str)) {
|
855
|
-
str = rb_str_substr(ptr->string, ptr->pos, e - s);
|
856
|
-
}
|
857
|
-
else if ((n = RSTRING(str)->len) == 0) {
|
858
|
-
p = s;
|
859
|
-
while (*p == '\n') {
|
860
|
-
if (++p == e) {
|
861
|
-
ptr->flags |= STRIO_EOF;
|
862
|
-
return Qnil;
|
863
|
-
}
|
864
|
-
}
|
865
|
-
s = p;
|
866
|
-
while ((p = memchr(p, '\n', e - p)) && (p != e)) {
|
867
|
-
if (*++p == '\n') {
|
868
|
-
e = p;
|
869
|
-
break;
|
870
|
-
}
|
871
|
-
}
|
872
|
-
str = rb_str_substr(ptr->string, s - RSTRING(ptr->string)->ptr, e - s);
|
873
|
-
}
|
874
|
-
else if (n == 1) {
|
875
|
-
if ((p = memchr(s, RSTRING(str)->ptr[0], e - s)) != 0) {
|
876
|
-
e = p + 1;
|
877
|
-
}
|
878
|
-
str = rb_str_substr(ptr->string, ptr->pos, e - s);
|
879
|
-
}
|
880
|
-
else {
|
881
|
-
if (n < e - s) {
|
882
|
-
if (e - s < 1024) {
|
883
|
-
for (p = s; p + n <= e; ++p) {
|
884
|
-
if (MEMCMP(p, RSTRING(str)->ptr, char, n) == 0) {
|
885
|
-
e = p + n;
|
886
|
-
break;
|
887
|
-
}
|
888
|
-
}
|
889
|
-
}
|
890
|
-
else {
|
891
|
-
long skip[1 << CHAR_BIT], pos;
|
892
|
-
p = RSTRING(str)->ptr;
|
893
|
-
bm_init_skip(skip, p, n);
|
894
|
-
if ((pos = bm_search(p, n, s, e - s, skip)) >= 0) {
|
895
|
-
e = s + pos + n;
|
896
|
-
}
|
897
|
-
}
|
898
|
-
}
|
899
|
-
str = rb_str_substr(ptr->string, ptr->pos, e - s);
|
900
|
-
}
|
901
|
-
ptr->pos = e - RSTRING(ptr->string)->ptr;
|
902
|
-
ptr->lineno++;
|
903
|
-
return str;
|
904
|
-
}
|
905
|
-
|
906
|
-
/*
|
907
|
-
* call-seq:
|
908
|
-
* strio.gets(sep_string=$/) -> string or nil
|
909
|
-
*
|
910
|
-
* See IO#gets.
|
911
|
-
*/
|
912
|
-
static VALUE
|
913
|
-
strio_gets(argc, argv, self)
|
914
|
-
int argc;
|
915
|
-
VALUE *argv;
|
916
|
-
VALUE self;
|
917
|
-
{
|
918
|
-
VALUE str = strio_getline(argc, argv, readable(StringIO(self)));
|
919
|
-
|
920
|
-
rb_lastline_set(str);
|
921
|
-
return str;
|
922
|
-
}
|
923
|
-
|
924
|
-
/*
|
925
|
-
* call-seq:
|
926
|
-
* strio.readline(sep_string=$/) -> string
|
927
|
-
*
|
928
|
-
* See IO#readline.
|
929
|
-
*/
|
930
|
-
static VALUE
|
931
|
-
strio_readline(argc, argv, self)
|
932
|
-
int argc;
|
933
|
-
VALUE *argv;
|
934
|
-
VALUE self;
|
935
|
-
{
|
936
|
-
VALUE line = strio_getline(argc, argv, readable(StringIO(self)));
|
937
|
-
if (NIL_P(line)) rb_eof_error();
|
938
|
-
return line;
|
939
|
-
}
|
940
|
-
|
941
|
-
/*
|
942
|
-
* call-seq:
|
943
|
-
* strio.each(sep_string=$/) {|line| block } -> strio
|
944
|
-
* strio.each_line(sep_string=$/) {|line| block } -> strio
|
945
|
-
*
|
946
|
-
* See IO#each.
|
947
|
-
*/
|
948
|
-
static VALUE
|
949
|
-
strio_each(argc, argv, self)
|
950
|
-
int argc;
|
951
|
-
VALUE *argv;
|
952
|
-
VALUE self;
|
953
|
-
{
|
954
|
-
struct StringIO *ptr = StringIO(self);
|
955
|
-
VALUE line;
|
956
|
-
|
957
|
-
while (!NIL_P(line = strio_getline(argc, argv, readable(ptr)))) {
|
958
|
-
rb_yield(line);
|
959
|
-
}
|
960
|
-
return self;
|
961
|
-
}
|
962
|
-
|
963
|
-
/*
|
964
|
-
* call-seq:
|
965
|
-
* strio.readlines(sep_string=$/) -> array
|
966
|
-
*
|
967
|
-
* See IO#readlines.
|
968
|
-
*/
|
969
|
-
static VALUE
|
970
|
-
strio_readlines(argc, argv, self)
|
971
|
-
int argc;
|
972
|
-
VALUE *argv;
|
973
|
-
VALUE self;
|
974
|
-
{
|
975
|
-
struct StringIO *ptr = StringIO(self);
|
976
|
-
VALUE ary = rb_ary_new(), line;
|
977
|
-
while (!NIL_P(line = strio_getline(argc, argv, readable(ptr)))) {
|
978
|
-
rb_ary_push(ary, line);
|
979
|
-
}
|
980
|
-
return ary;
|
981
|
-
}
|
982
|
-
|
983
|
-
/*
|
984
|
-
* call-seq:
|
985
|
-
* strio.write(string) -> integer
|
986
|
-
* strio.syswrite(string) -> integer
|
987
|
-
*
|
988
|
-
* Appends the given string to the underlying buffer string of *strio*.
|
989
|
-
* The stream must be opened for writing. If the argument is not a
|
990
|
-
* string, it will be converted to a string using <code>to_s</code>.
|
991
|
-
* Returns the number of bytes written. See IO#write.
|
992
|
-
*/
|
993
|
-
static VALUE
|
994
|
-
strio_write(self, str)
|
995
|
-
VALUE self, str;
|
996
|
-
{
|
997
|
-
struct StringIO *ptr = writable(StringIO(self));
|
998
|
-
long len, olen;
|
999
|
-
|
1000
|
-
if (TYPE(str) != T_STRING)
|
1001
|
-
str = rb_obj_as_string(str);
|
1002
|
-
len = RSTRING(str)->len;
|
1003
|
-
if (!len) return INT2FIX(0);
|
1004
|
-
check_modifiable(ptr);
|
1005
|
-
olen = RSTRING(ptr->string)->len;
|
1006
|
-
if (ptr->flags & FMODE_APPEND) {
|
1007
|
-
ptr->pos = olen;
|
1008
|
-
}
|
1009
|
-
if (ptr->pos == olen) {
|
1010
|
-
rb_str_cat(ptr->string, RSTRING(str)->ptr, len);
|
1011
|
-
}
|
1012
|
-
else {
|
1013
|
-
strio_extend(ptr, ptr->pos, len);
|
1014
|
-
rb_str_update(ptr->string, ptr->pos, len, str);
|
1015
|
-
}
|
1016
|
-
OBJ_INFECT(ptr->string, self);
|
1017
|
-
ptr->pos += len;
|
1018
|
-
return LONG2NUM(len);
|
1019
|
-
}
|
1020
|
-
|
1021
|
-
/*
|
1022
|
-
* call-seq:
|
1023
|
-
* strio << obj -> strio
|
1024
|
-
*
|
1025
|
-
* See IO#<<.
|
1026
|
-
*/
|
1027
|
-
#define strio_addstr rb_io_addstr
|
1028
|
-
|
1029
|
-
/*
|
1030
|
-
* call-seq:
|
1031
|
-
* strio.print() -> nil
|
1032
|
-
* strio.print(obj, ...) -> nil
|
1033
|
-
*
|
1034
|
-
* See IO#print.
|
1035
|
-
*/
|
1036
|
-
#define strio_print rb_io_print
|
1037
|
-
|
1038
|
-
/*
|
1039
|
-
* call-seq:
|
1040
|
-
* strio.printf(format_string [, obj, ...] ) -> nil
|
1041
|
-
*
|
1042
|
-
* See IO#printf.
|
1043
|
-
*/
|
1044
|
-
#define strio_printf rb_io_printf
|
1045
|
-
|
1046
|
-
/*
|
1047
|
-
* call-seq:
|
1048
|
-
* strio.putc(obj) -> obj
|
1049
|
-
*
|
1050
|
-
* See IO#putc.
|
1051
|
-
*/
|
1052
|
-
static VALUE
|
1053
|
-
strio_putc(self, ch)
|
1054
|
-
VALUE self, ch;
|
1055
|
-
{
|
1056
|
-
struct StringIO *ptr = writable(StringIO(self));
|
1057
|
-
int c = NUM2CHR(ch);
|
1058
|
-
long olen;
|
1059
|
-
|
1060
|
-
check_modifiable(ptr);
|
1061
|
-
olen = RSTRING(ptr->string)->len;
|
1062
|
-
if (ptr->flags & FMODE_APPEND) {
|
1063
|
-
ptr->pos = olen;
|
1064
|
-
}
|
1065
|
-
strio_extend(ptr, ptr->pos, 1);
|
1066
|
-
RSTRING(ptr->string)->ptr[ptr->pos++] = c;
|
1067
|
-
OBJ_INFECT(ptr->string, self);
|
1068
|
-
return ch;
|
1069
|
-
}
|
1070
|
-
|
1071
|
-
/*
|
1072
|
-
* call-seq:
|
1073
|
-
* strio.puts(obj, ...) -> nil
|
1074
|
-
*
|
1075
|
-
* See IO#puts.
|
1076
|
-
*/
|
1077
|
-
#define strio_puts rb_io_puts
|
1078
|
-
|
1079
|
-
/*
|
1080
|
-
* call-seq:
|
1081
|
-
* strio.read([length [, buffer]]) -> string, buffer, or nil
|
1082
|
-
*
|
1083
|
-
* See IO#read.
|
1084
|
-
*/
|
1085
|
-
static VALUE
|
1086
|
-
strio_read(argc, argv, self)
|
1087
|
-
int argc;
|
1088
|
-
VALUE *argv;
|
1089
|
-
VALUE self;
|
1090
|
-
{
|
1091
|
-
struct StringIO *ptr = readable(StringIO(self));
|
1092
|
-
VALUE str = Qnil;
|
1093
|
-
long len, olen;
|
1094
|
-
|
1095
|
-
switch (argc) {
|
1096
|
-
case 2:
|
1097
|
-
str = argv[1];
|
1098
|
-
StringValue(str);
|
1099
|
-
rb_str_modify(str);
|
1100
|
-
case 1:
|
1101
|
-
if (!NIL_P(argv[0])) {
|
1102
|
-
len = olen = NUM2LONG(argv[0]);
|
1103
|
-
if (len < 0) {
|
1104
|
-
rb_raise(rb_eArgError, "negative length %ld given", len);
|
1105
|
-
}
|
1106
|
-
if (len > 0 && ptr->pos >= RSTRING(ptr->string)->len) {
|
1107
|
-
ptr->flags |= STRIO_EOF;
|
1108
|
-
if (!NIL_P(str)) rb_str_resize(str, 0);
|
1109
|
-
return Qnil;
|
1110
|
-
}
|
1111
|
-
else if (ptr->flags & STRIO_EOF) {
|
1112
|
-
if (!NIL_P(str)) rb_str_resize(str, 0);
|
1113
|
-
return Qnil;
|
1114
|
-
}
|
1115
|
-
break;
|
1116
|
-
}
|
1117
|
-
/* fall through */
|
1118
|
-
case 0:
|
1119
|
-
olen = -1;
|
1120
|
-
len = RSTRING(ptr->string)->len;
|
1121
|
-
if (len <= ptr->pos) {
|
1122
|
-
ptr->flags |= STRIO_EOF;
|
1123
|
-
if (NIL_P(str)) {
|
1124
|
-
str = rb_str_new(0, 0);
|
1125
|
-
}
|
1126
|
-
else {
|
1127
|
-
rb_str_resize(str, 0);
|
1128
|
-
}
|
1129
|
-
return str;
|
1130
|
-
}
|
1131
|
-
else {
|
1132
|
-
len -= ptr->pos;
|
1133
|
-
}
|
1134
|
-
break;
|
1135
|
-
default:
|
1136
|
-
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
|
1137
|
-
}
|
1138
|
-
if (NIL_P(str)) {
|
1139
|
-
str = rb_str_substr(ptr->string, ptr->pos, len);
|
1140
|
-
}
|
1141
|
-
else {
|
1142
|
-
long rest = RSTRING(ptr->string)->len - ptr->pos;
|
1143
|
-
if (len > rest) len = rest;
|
1144
|
-
rb_str_resize(str, len);
|
1145
|
-
MEMCPY(RSTRING(str)->ptr, RSTRING(ptr->string)->ptr + ptr->pos, char, len);
|
1146
|
-
}
|
1147
|
-
if (NIL_P(str)) {
|
1148
|
-
if (!(ptr->flags & STRIO_EOF)) str = rb_str_new(0, 0);
|
1149
|
-
len = 0;
|
1150
|
-
}
|
1151
|
-
else {
|
1152
|
-
ptr->pos += len = RSTRING(str)->len;
|
1153
|
-
}
|
1154
|
-
if (olen < 0 || olen > len) ptr->flags |= STRIO_EOF;
|
1155
|
-
return str;
|
1156
|
-
}
|
1157
|
-
|
1158
|
-
/*
|
1159
|
-
* call-seq:
|
1160
|
-
* strio.sysread(integer[, outbuf]) -> string
|
1161
|
-
*
|
1162
|
-
* Similar to #read, but raises +EOFError+ at end of string instead of
|
1163
|
-
* returning +nil+, as well as IO#sysread does.
|
1164
|
-
*/
|
1165
|
-
static VALUE
|
1166
|
-
strio_sysread(argc, argv, self)
|
1167
|
-
int argc;
|
1168
|
-
VALUE *argv;
|
1169
|
-
VALUE self;
|
1170
|
-
{
|
1171
|
-
VALUE val = strio_read(argc, argv, self);
|
1172
|
-
if (NIL_P(val) || RSTRING(val)->len == 0) {
|
1173
|
-
rb_eof_error();
|
1174
|
-
}
|
1175
|
-
return val;
|
1176
|
-
}
|
1177
|
-
|
1178
|
-
#define strio_syswrite strio_write
|
1179
|
-
|
1180
|
-
/* call-seq: strio.path -> nil */
|
1181
|
-
#define strio_path strio_nil
|
1182
|
-
|
1183
|
-
/*
|
1184
|
-
* call-seq:
|
1185
|
-
* strio.isatty -> nil
|
1186
|
-
* strio.tty? -> nil
|
1187
|
-
*
|
1188
|
-
*/
|
1189
|
-
#define strio_isatty strio_false
|
1190
|
-
|
1191
|
-
/* call-seq: strio.pid -> nil */
|
1192
|
-
#define strio_pid strio_nil
|
1193
|
-
|
1194
|
-
/* call-seq: strio.fileno -> nil */
|
1195
|
-
#define strio_fileno strio_nil
|
1196
|
-
|
1197
|
-
/*
|
1198
|
-
* call-seq:
|
1199
|
-
* strio.size -> integer
|
1200
|
-
*
|
1201
|
-
* Returns the size of the buffer string.
|
1202
|
-
*/
|
1203
|
-
static VALUE
|
1204
|
-
strio_size(self)
|
1205
|
-
VALUE self;
|
1206
|
-
{
|
1207
|
-
VALUE string = StringIO(self)->string;
|
1208
|
-
if (NIL_P(string)) {
|
1209
|
-
rb_raise(rb_eIOError, "not opened");
|
1210
|
-
}
|
1211
|
-
return ULONG2NUM(RSTRING(string)->len);
|
1212
|
-
}
|
1213
|
-
|
1214
|
-
/*
|
1215
|
-
* call-seq:
|
1216
|
-
* strio.truncate(integer) -> 0
|
1217
|
-
*
|
1218
|
-
* Truncates the buffer string to at most _integer_ bytes. The *strio*
|
1219
|
-
* must be opened for writing.
|
1220
|
-
*/
|
1221
|
-
static VALUE
|
1222
|
-
strio_truncate(self, len)
|
1223
|
-
VALUE self, len;
|
1224
|
-
{
|
1225
|
-
VALUE string = writable(StringIO(self))->string;
|
1226
|
-
long l = NUM2LONG(len);
|
1227
|
-
long plen = RSTRING(string)->len;
|
1228
|
-
if (l < 0) {
|
1229
|
-
error_inval("negative legnth");
|
1230
|
-
}
|
1231
|
-
rb_str_resize(string, l);
|
1232
|
-
if (plen < l) {
|
1233
|
-
MEMZERO(RSTRING(string)->ptr + plen, char, l - plen);
|
1234
|
-
}
|
1235
|
-
return len;
|
1236
|
-
}
|
1237
|
-
|
1238
|
-
/*
|
1239
|
-
* Pseudo I/O on String object.
|
1240
|
-
*/
|
1241
|
-
void
|
1242
|
-
Init_stringio()
|
1243
|
-
{
|
1244
|
-
VALUE StringIO = rb_define_class("StringIO", rb_cData);
|
1245
|
-
|
1246
|
-
rb_include_module(StringIO, rb_mEnumerable);
|
1247
|
-
rb_define_alloc_func(StringIO, strio_s_allocate);
|
1248
|
-
rb_define_singleton_method(StringIO, "open", strio_s_open, -1);
|
1249
|
-
rb_define_method(StringIO, "initialize", strio_initialize, -1);
|
1250
|
-
rb_define_method(StringIO, "initialize_copy", strio_copy, 1);
|
1251
|
-
rb_define_method(StringIO, "reopen", strio_reopen, -1);
|
1252
|
-
|
1253
|
-
rb_define_method(StringIO, "string", strio_get_string, 0);
|
1254
|
-
rb_define_method(StringIO, "string=", strio_set_string, 1);
|
1255
|
-
rb_define_method(StringIO, "lineno", strio_get_lineno, 0);
|
1256
|
-
rb_define_method(StringIO, "lineno=", strio_set_lineno, 1);
|
1257
|
-
|
1258
|
-
rb_define_method(StringIO, "binmode", strio_binmode, 0);
|
1259
|
-
rb_define_method(StringIO, "close", strio_close, 0);
|
1260
|
-
rb_define_method(StringIO, "close_read", strio_close_read, 0);
|
1261
|
-
rb_define_method(StringIO, "close_write", strio_close_write, 0);
|
1262
|
-
rb_define_method(StringIO, "closed?", strio_closed, 0);
|
1263
|
-
rb_define_method(StringIO, "closed_read?", strio_closed_read, 0);
|
1264
|
-
rb_define_method(StringIO, "closed_write?", strio_closed_write, 0);
|
1265
|
-
rb_define_method(StringIO, "eof", strio_eof, 0);
|
1266
|
-
rb_define_method(StringIO, "eof?", strio_eof, 0);
|
1267
|
-
rb_define_method(StringIO, "fcntl", strio_fcntl, -1);
|
1268
|
-
rb_define_method(StringIO, "flush", strio_flush, 0);
|
1269
|
-
rb_define_method(StringIO, "fsync", strio_fsync, 0);
|
1270
|
-
rb_define_method(StringIO, "pos", strio_get_pos, 0);
|
1271
|
-
rb_define_method(StringIO, "pos=", strio_set_pos, 1);
|
1272
|
-
rb_define_method(StringIO, "rewind", strio_rewind, 0);
|
1273
|
-
rb_define_method(StringIO, "seek", strio_seek, -1);
|
1274
|
-
rb_define_method(StringIO, "sync", strio_get_sync, 0);
|
1275
|
-
rb_define_method(StringIO, "sync=", strio_set_sync, 1);
|
1276
|
-
rb_define_method(StringIO, "tell", strio_tell, 0);
|
1277
|
-
rb_define_method(StringIO, "path", strio_path, 0);
|
1278
|
-
|
1279
|
-
rb_define_method(StringIO, "each", strio_each, -1);
|
1280
|
-
rb_define_method(StringIO, "each_byte", strio_each_byte, 0);
|
1281
|
-
rb_define_method(StringIO, "each_line", strio_each, -1);
|
1282
|
-
rb_define_method(StringIO, "getc", strio_getc, 0);
|
1283
|
-
rb_define_method(StringIO, "ungetc", strio_ungetc, 1);
|
1284
|
-
rb_define_method(StringIO, "readchar", strio_readchar, 0);
|
1285
|
-
rb_define_method(StringIO, "gets", strio_gets, -1);
|
1286
|
-
rb_define_method(StringIO, "readline", strio_readline, -1);
|
1287
|
-
rb_define_method(StringIO, "readlines", strio_readlines, -1);
|
1288
|
-
rb_define_method(StringIO, "read", strio_read, -1);
|
1289
|
-
rb_define_method(StringIO, "sysread", strio_sysread, -1);
|
1290
|
-
|
1291
|
-
rb_define_method(StringIO, "write", strio_write, 1);
|
1292
|
-
rb_define_method(StringIO, "<<", strio_addstr, 1);
|
1293
|
-
rb_define_method(StringIO, "print", strio_print, -1);
|
1294
|
-
rb_define_method(StringIO, "printf", strio_printf, -1);
|
1295
|
-
rb_define_method(StringIO, "putc", strio_putc, 1);
|
1296
|
-
rb_define_method(StringIO, "puts", strio_puts, -1);
|
1297
|
-
rb_define_method(StringIO, "syswrite", strio_syswrite, 1);
|
1298
|
-
|
1299
|
-
rb_define_method(StringIO, "isatty", strio_isatty, 0);
|
1300
|
-
rb_define_method(StringIO, "tty?", strio_isatty, 0);
|
1301
|
-
rb_define_method(StringIO, "pid", strio_pid, 0);
|
1302
|
-
rb_define_method(StringIO, "fileno", strio_fileno, 0);
|
1303
|
-
rb_define_method(StringIO, "size", strio_size, 0);
|
1304
|
-
rb_define_method(StringIO, "length", strio_size, 0);
|
1305
|
-
rb_define_method(StringIO, "truncate", strio_truncate, 1);
|
1306
|
-
}
|