-
Notifications
You must be signed in to change notification settings - Fork 138
/
stringhandle.pmc
489 lines (322 loc) · 11.1 KB
/
stringhandle.pmc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
Copyright (C) 2008-2011, Parrot Foundation.
=head1 NAME
src/pmc/stringhandle.pmc - StringHandle PMC
=head1 DESCRIPTION
The StringHandle PMC performs I/O operations, but on an internal string rather
than an external file. Commonly used as a mock FileHandle for testing.
=cut
*/
#include "../src/io/io_private.h"
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_INLINE
static int encoding_is_utf8(PARROT_INTERP, ARGIN_NULLOK(const STRING *s))
__attribute__nonnull__(1);
#define ASSERT_ARGS_encoding_is_utf8 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=head2 Internal Functions
=over 4
=item C<static int encoding_is_utf8(PARROT_INTERP, const STRING *s)>
Helper function for internal usage. Return 1 if the string argument is
not null and has utf8 encoding, 0 otherwise.
=back
=cut
*/
PARROT_INLINE
static int
encoding_is_utf8(PARROT_INTERP, ARGIN_NULLOK(const STRING *s))
{
ASSERT_ARGS(encoding_is_utf8)
if (STRING_IS_NULL(s))
return 0;
else
return STRING_equal(interp, s, CONST_STRING(interp, "utf8"));
}
pmclass StringHandle extends Handle auto_attrs {
ATTR INTVAL flags; /* Filehandle flags */
ATTR STRING *stringhandle; /* The string data */
ATTR STRING *mode; /* The mode string used in open */
ATTR STRING *encoding; /* The encoding for read/write */
ATTR STRING *filename; /* A mock path and filename */
ATTR INTVAL read_offset; /* Position, for reading bytes */
/*
=head2 Vtable Functions
=over 4
=item C<void init()>
Initializes a newly created StringHandle object.
=cut
*/
VTABLE void init() {
Parrot_StringHandle_attributes * const data_struct =
(Parrot_StringHandle_attributes *) PMC_data(SELF);
data_struct->flags = 0;
data_struct->stringhandle = NULL;
data_struct->mode = NULL;
data_struct->encoding = NULL;
data_struct->filename = NULL;
data_struct->read_offset = 0;
PObj_custom_mark_SET(SELF);
}
/*
=item C<PMC *clone()>
Create a copy of the stringhandle.
=cut
*/
VTABLE PMC *clone() {
const Parrot_StringHandle_attributes * const old_struct = PARROT_STRINGHANDLE(SELF);
PMC * const copy = Parrot_pmc_new(INTERP, enum_class_StringHandle);
Parrot_StringHandle_attributes * const data_struct = PARROT_STRINGHANDLE(copy);
data_struct->stringhandle = old_struct->stringhandle;
data_struct->mode = old_struct->mode;
data_struct->encoding = old_struct->encoding;
data_struct->flags = old_struct->flags;
return copy;
}
/*
=item C<void mark()>
Mark active stringhandle data as live.
=cut
*/
VTABLE void mark() {
Parrot_StringHandle_attributes * const data_struct = PARROT_STRINGHANDLE(SELF);
Parrot_gc_mark_STRING_alive(INTERP, data_struct->stringhandle);
Parrot_gc_mark_STRING_alive(INTERP, data_struct->mode);
Parrot_gc_mark_STRING_alive(INTERP, data_struct->encoding);
Parrot_gc_mark_STRING_alive(INTERP, data_struct->filename);
}
/*
=item C<INTVAL get_bool()>
Returns whether the StringHandle has reached the end of the file.
=cut
*/
VTABLE INTVAL get_bool() {
STRING *stringhandle;
GET_ATTR_stringhandle(INTERP, SELF, stringhandle);
if (STRING_IS_NULL(stringhandle))
return 0;
return 1;
}
/*
=back
=head2 Methods
=over 4
=item C<METHOD open(STRING *filename :optional, STRING *mode :optional)>
Opens a string handle with the given mode. The filename is not used, but is
stored for mocking.
=cut
*/
METHOD open(STRING *filename :optional, INTVAL got_filename :opt_flag,
STRING *mode :optional, INTVAL got_mode :opt_flag) {
STRING *open_mode, *old_string;
INTVAL flags;
if (got_mode && !STRING_IS_NULL(mode))
SET_ATTR_mode(INTERP, SELF, mode);
if (got_filename && !STRING_IS_NULL(filename))
SET_ATTR_filename(INTERP, SELF, filename);
/* If StringHandle hasn't already been initialized, create a new string. */
GET_ATTR_stringhandle(INTERP, SELF, old_string);
if (STRING_IS_NULL(old_string)) {
STRING *new_string;
STRING *encoding;
GET_ATTR_encoding(INTERP, SELF, encoding);
if (encoding_is_utf8(INTERP, encoding))
new_string = Parrot_str_new_init(INTERP, "", 0,
Parrot_utf8_encoding_ptr, 0);
else
new_string = CONST_STRING(INTERP, "");
SET_ATTR_stringhandle(INTERP, SELF, new_string);
}
/* Set a default mode of read-only. */
GET_ATTR_mode(INTERP, SELF, open_mode);
if (STRING_IS_NULL(open_mode)) {
open_mode = CONST_STRING(INTERP, "r");
SET_ATTR_mode(INTERP, SELF, open_mode);
}
flags = Parrot_io_parse_open_flags(INTERP, open_mode);
SET_ATTR_flags(INTERP, SELF, flags);
RETURN(PMC *SELF);
}
/*
=item C<METHOD is_closed()>
Check if the StringHandle is open.
=cut
*/
METHOD is_closed() {
STRING *stringhandle;
INTVAL status;
GET_ATTR_stringhandle(INTERP, SELF, stringhandle);
status = STRING_IS_NULL(stringhandle);
RETURN(INTVAL status);
}
/*
=item METHOD readall(STRING *name);
Read the entire contents of the StringHandle into a Parrot string. On a
StringHandle object that isn't opened yet, returns an empty string.
=cut
*/
METHOD readall(STRING *name :optional, INTVAL got_name :opt_flag) {
STRING *string_result;
GET_ATTR_stringhandle(INTERP, SELF, string_result);
if (STRING_IS_NULL(string_result)) {
STRING *encoding;
GET_ATTR_encoding(INTERP, SELF, encoding);
if (encoding_is_utf8(INTERP, encoding))
string_result = Parrot_str_new_init(INTERP, "", 0,
Parrot_utf8_encoding_ptr, 0);
else
string_result = CONST_STRING(INTERP, "");
}
RETURN(STRING *string_result);
}
/*
=item C<METHOD flush()>
Clear the StringHandle by resetting it to a null value.
=cut
*/
METHOD flush() {
Parrot_io_flush_handle(INTERP, SELF);
}
/*
=item C<METHOD print([INTVAL|FLOATVAL|STRING *|PMC*] value)>
Print the passed in integer, number, string, or PMC to the stringhandle.
(Integers, numbers, and strings are auto-boxed as PMCs.)
=cut
*/
METHOD print(PMC *to_print) {
STRING * const string_to_print = VTABLE_get_string(INTERP, to_print);
Parrot_io_putps(INTERP, SELF, string_to_print);
}
/*
=item C<METHOD puts(STRING *value)>
Print the string to the stringhandle.
=cut
*/
METHOD puts(STRING *to_print) {
INTVAL flags, status;
STRING *old_string, *new_string;
GET_ATTR_stringhandle(INTERP, SELF, old_string);
if (STRING_IS_NULL(old_string))
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_PIO_ERROR,
"Cannot write to a closed filehandle");
GET_ATTR_flags(INTERP, SELF, flags);
if (!(flags & PIO_F_WRITE))
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_PIO_ERROR,
"Cannot write to a filehandle not opened for write");
new_string = Parrot_str_concat(INTERP, old_string, to_print);
SET_ATTR_stringhandle(INTERP, SELF, new_string);
status = Parrot_str_byte_length(INTERP, new_string);
RETURN(INTVAL status);
}
/*
=item C<METHOD buffer_type(STRING *new_type :optional)>
Set or retrieve the buffering attribute for the stringhandle. This attribute is
ignored, but stored for mocking.
=cut
*/
METHOD buffer_type(STRING *new_type :optional, INTVAL got_type :opt_flag) {
INTVAL flags;
STRING * const nobuffer_string = CONST_STRING(INTERP, "unbuffered");
STRING * const linebuffer_string = CONST_STRING(INTERP, "line-buffered");
STRING * const fullbuffer_string = CONST_STRING(INTERP, "full-buffered");
GET_ATTR_flags(INTERP, SELF, flags);
if (got_type) {
if (STRING_equal(INTERP, new_type, nobuffer_string)) {
flags &= ~ PIO_F_LINEBUF;
flags &= ~ PIO_F_BLKBUF;
}
else if (STRING_equal(INTERP, new_type, linebuffer_string)) {
flags |= PIO_F_LINEBUF;
flags &= ~ PIO_F_BLKBUF;
}
else if (STRING_equal(INTERP, new_type, fullbuffer_string)) {
flags &= ~ PIO_F_LINEBUF;
flags |= PIO_F_BLKBUF;
}
SET_ATTR_flags(INTERP, SELF, flags);
}
if (flags & PIO_F_LINEBUF)
RETURN(STRING *linebuffer_string);
else if (flags & PIO_F_BLKBUF)
RETURN(STRING *fullbuffer_string);
RETURN(STRING *nobuffer_string);
}
/*
=item C<METHOD buffer_size(INTVAL new_size :optional)>
Returns the current size of the stringhandle.
=cut
*/
METHOD buffer_size(INTVAL new_size :optional, INTVAL got_size :opt_flag) {
INTVAL buffer_size;
STRING *stringhandle;
GET_ATTR_stringhandle(INTERP, SELF, stringhandle);
if (STRING_IS_NULL(stringhandle))
RETURN(INTVAL 0);
buffer_size = Parrot_str_byte_length(INTERP, stringhandle);
RETURN(INTVAL buffer_size);
}
/*
=item C<METHOD mode()>
Retrieve the read mode string for the stringhandle.
=cut
*/
METHOD mode() {
STRING *mode;
GET_ATTR_mode(INTERP, SELF, mode);
RETURN(STRING *mode);
}
/*
=item C<METHOD encoding(STRING *new_encoding)>
Set or retrieve the encoding attribute (a string name of the selected encoding
scheme) for the stringhandle.
=cut
*/
METHOD encoding(STRING *new_encoding :optional, INTVAL got_encoding :opt_flag) {
STRING *encoding;
if (got_encoding) {
SET_ATTR_encoding(INTERP, SELF, new_encoding);
RETURN(STRING *new_encoding);
}
GET_ATTR_encoding(INTERP, SELF, encoding);
RETURN(STRING *encoding);
}
/*
=item C<METHOD eof()>
Check if the StringHandle is at end-of-file (if it has read to the end of the
string data).
=cut
*/
METHOD eof() {
STRING *stringhandle;
UINTVAL offset;
GET_ATTR_stringhandle(INTERP, SELF, stringhandle);
GET_ATTR_read_offset(INTERP, SELF, offset);
if (offset >= Parrot_str_byte_length(INTERP, stringhandle))
RETURN(INTVAL 1);
RETURN(INTVAL 0);
}
/*
=item C<METHOD get_fd()>
StringHandles do not use integer file descriptors, so always returns an error
value.
=cut
*/
METHOD get_fd() {
UNUSED(INTERP);
RETURN(INTVAL -1);
}
/*
=back
=cut
*/
} /* end pmclass */
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/