/
stringhandle.pmc
571 lines (375 loc) · 12.6 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
Copyright (C) 2008, Parrot Foundation.
$Id$
=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"
PARROT_INLINE
static int encoding_is_utf8(PARROT_INTERP, ARGIN_NULLOK(STRING *s));
/*
=head2 Internal Functions
=over 4
=item C<static int encoding_is_utf8()>
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(STRING *s))
{
if (STRING_IS_NULL(s))
return 0;
else
return Parrot_str_equal(interp, s, CONST_STRING(interp, "utf8"));
}
pmclass StringHandle extends Handle need_ext {
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 *data_struct =
mem_allocate_typed(Parrot_StringHandle_attributes);
PMC_data(SELF) = data_struct;
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);
PObj_active_destroy_SET(SELF);
}
/*
=item C<PMC *clone()>
Create a copy of the stringhandle.
=cut
*/
VTABLE PMC *clone() {
Parrot_StringHandle_attributes * const old_struct = PARROT_STRINGHANDLE(SELF);
PMC * const copy = pmc_new(INTERP, enum_class_StringHandle);
Parrot_StringHandle_attributes * const data_struct = PARROT_STRINGHANDLE(copy);
if (old_struct->stringhandle != NULL)
data_struct->stringhandle = Parrot_str_copy(INTERP, old_struct->stringhandle);
if (old_struct->mode != NULL)
data_struct->mode = Parrot_str_copy(INTERP, old_struct->mode);
if (old_struct->encoding != NULL)
data_struct->encoding = Parrot_str_copy(INTERP, 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);
if (data_struct->stringhandle)
Parrot_gc_mark_PObj_alive(interp, (PObj *)data_struct->stringhandle);
if (data_struct->mode)
Parrot_gc_mark_PObj_alive(interp, (PObj *)data_struct->mode);
if (data_struct->encoding)
Parrot_gc_mark_PObj_alive(interp, (PObj *)data_struct->encoding);
if (data_struct->filename)
Parrot_gc_mark_PObj_alive(interp, (PObj *)data_struct->filename);
}
/*
=item C<void destroy()>
Free structures.
=cut
*/
VTABLE void destroy() {
if (PARROT_STRINGHANDLE(SELF)) {
mem_sys_free(PARROT_STRINGHANDLE(SELF));
PMC_data(SELF) = NULL;
}
}
/*
=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, Parrot_str_copy(INTERP, mode));
if (got_filename && !STRING_IS_NULL(filename))
SET_ATTR_filename(INTERP, SELF, Parrot_str_copy(INTERP, 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 = string_make(INTERP, "", 0, "unicode", 0);
else
new_string = Parrot_str_new(INTERP, "", 0);
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 = Parrot_str_new_constant(INTERP, "r");
SET_ATTR_mode(INTERP, SELF, open_mode);
}
Parrot_io_open(INTERP, SELF, filename, open_mode);
RETURN(PMC *SELF);
}
/*
=item C<METHOD is_tty()>
StringHandles are never tty's, returns false.
=cut
*/
METHOD is_tty() {
RETURN(INTVAL 0);
}
/*
=item C<METHOD close()>
Reset some core data for the StringHandle, but don't delete the string data, as
it may be wanted later (for capturing the results).
=cut
*/
METHOD close() {
SET_ATTR_read_offset(INTERP, SELF, 0);
RETURN(INTVAL 0);
}
/*
=item C<METHOD is_closed()>
Check if the StringHandle is open.
=cut
*/
METHOD is_closed() {
INTVAL status;
status = Parrot_io_is_closed(INTERP, SELF);
RETURN(INTVAL status);
}
/*
=item C<METHOD read(INTVAL bytes)>
Read the entire contents of the stringhandle and return it in a string. The
C<bytes> argument is currently ignored.
=cut
*/
METHOD read(INTVAL length) {
STRING *string_result;
string_result = Parrot_io_reads(INTERP, SELF, length);
RETURN(STRING *string_result);
}
/*
=item C<METHOD readline()>
Read a line from the stringhandle and return it in a string. (Currently only
responds to "\n" newlines.)
=cut
*/
METHOD readline() {
STRING *string_result;
string_result = Parrot_io_readline(INTERP, SELF);
RETURN(STRING *string_result);
}
/*
=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 = string_make(INTERP, "", 0, "unicode", 0);
else
string_result = Parrot_str_new_constant(INTERP, "");
}
else
string_result = Parrot_str_copy(INTERP, string_result);
RETURN(STRING *string_result);
}
/*
=item C<METHOD flush()>
Clear the StringHandle by resetting it to a null value.
=cut
*/
METHOD flush() {
Parrot_io_flush(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_append(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 (Parrot_str_equal(INTERP, new_type, nobuffer_string)) {
flags &= ~ PIO_F_LINEBUF;
flags &= ~ PIO_F_BLKBUF;
}
else if (Parrot_str_equal(INTERP, new_type, linebuffer_string)) {
flags |= PIO_F_LINEBUF;
flags &= ~ PIO_F_BLKBUF;
}
else if (Parrot_str_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, *mode_copy;
GET_ATTR_mode(INTERP, SELF, mode);
mode_copy = Parrot_str_copy(INTERP, mode);
RETURN(STRING *mode_copy);
}
/*
=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;
STRING *encoding_copy = NULL;
if (got_encoding) {
if (!STRING_IS_NULL(new_encoding))
encoding_copy = Parrot_str_copy(INTERP, new_encoding);
SET_ATTR_encoding(INTERP, SELF, encoding_copy);
RETURN(STRING *new_encoding);
}
GET_ATTR_encoding(INTERP, SELF, encoding);
if (!STRING_IS_NULL(encoding))
encoding_copy = Parrot_str_copy(INTERP, encoding);
RETURN(STRING *encoding_copy);
}
/*
=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() {
RETURN(INTVAL -1);
}
/*
=back
=cut
*/
} /* end pmclass */
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/