996
|
1 package Language::INTERCAL::GenericIO::FILE;
|
|
2
|
|
3 # Write/read data from/to file
|
|
4
|
|
5 # This file is part of CLC-INTERCAL
|
|
6
|
|
7 # Copyright (c) 2006-2008 Claudio Calvelli, all rights reserved.
|
|
8
|
|
9 # CLC-INTERCAL is copyrighted software. However, permission to use, modify,
|
|
10 # and distribute it is granted provided that the conditions set out in the
|
|
11 # licence agreement are met. See files README and COPYING in the distribution.
|
|
12
|
|
13 use strict;
|
|
14 use vars qw($VERSION $PERVERSION);
|
|
15 ($VERSION) = ($PERVERSION = "CLC-INTERCAL/Base INTERCAL/GenericIO/FILE.pm 1.-94.-2") =~ /\s(\S+)$/;
|
|
16
|
|
17 use Carp;
|
|
18 use IO::File;
|
|
19 use Language::INTERCAL::Exporter '1.-94.-2';
|
|
20 use Language::INTERCAL::Splats '1.-94.-2', qw(:SP);
|
|
21 use Language::INTERCAL::GenericIO '1.-94.-2';
|
|
22 use vars qw(@ISA);
|
|
23 @ISA = qw(Language::INTERCAL::GenericIO);
|
|
24
|
|
25 sub _new {
|
|
26 @_ == 3 or croak
|
|
27 "Usage: new Language::INTERCAL::GenericIO::FILE(MODE, NAME)";
|
|
28 my ($object, $mode, $data) = @_;
|
|
29 my $filemode = $mode;
|
|
30 $filemode =~ tr/rw/wr/;
|
|
31 $filemode = 'r+' if $filemode =~ /u/;
|
|
32 $object->{filedata} = {};
|
|
33 my $fh;
|
|
34 if (ref $data && UNIVERSAL::isa($data, 'GLOB')) {
|
|
35 $fh = $data;
|
|
36 } elsif ($data eq '-' || $data eq '-1') {
|
|
37 $fh = $mode =~ /r/ ? \*STDOUT : \*STDIN;
|
|
38 } elsif ($data eq '-2') {
|
|
39 $fh = \*STDERR;
|
|
40 } else {
|
|
41 $fh = new IO::File($data, $filemode) or faint(SP_IOERR, $data, $!);
|
|
42 # $fh->autoflush(1);
|
|
43 $object->{filedata}{to_close} = $fh;
|
|
44 }
|
|
45 $object->{filedata}{handle} = $fh;
|
|
46 }
|
|
47
|
|
48 sub is_terminal {
|
|
49 @_ == 1 or croak "Usage: IO->is_terminal";
|
|
50 my ($object) = @_;
|
|
51 -t $object->{filedata}{handle};
|
|
52 }
|
|
53
|
|
54 sub read_binary {
|
|
55 @_ == 2 or croak "Usage: IO->read_binary(DATA)";
|
|
56 my ($object, $data) = @_;
|
|
57 print { $object->{filedata}{handle} } $data
|
|
58 or faint(SP_IOERR, $object->{data}[0], $!);
|
|
59 }
|
|
60
|
|
61 sub _write_code {
|
|
62 my ($object, $size) = @_;
|
|
63 my $data = '';
|
|
64 read $object->{filedata}{handle}, $data, $size;
|
|
65 $data;
|
|
66 }
|
|
67
|
|
68 sub _write_text_code {
|
|
69 my ($object, $newline) = @_;
|
|
70 local $/ = $newline;
|
|
71 my $fh = $object->{filedata}{handle};
|
|
72 my $data = <$fh>;
|
|
73 defined $data ? $data : '';
|
|
74 }
|
|
75
|
|
76 sub tell {
|
|
77 @_ == 1 or croak "Usage: IO->tell";
|
|
78 my ($object) = @_;
|
|
79 tell($object->{filedata}{handle});
|
|
80 }
|
|
81
|
|
82 sub reset {
|
|
83 @_ == 1 or croak "Usage: IO->reset";
|
|
84 my ($object) = @_;
|
|
85 seek($object->{filedata}{handle}, 0, SEEK_SET);
|
|
86 $object->{buffer} = '';
|
|
87 $object;
|
|
88 }
|
|
89
|
|
90 sub seek {
|
|
91 @_ == 2 || @_ == 3
|
|
92 or croak "Usage: IO->seek(POS [, RELATIVE_TO])";
|
|
93 my ($object, $pos, $rel) = @_;
|
|
94 $rel = SEEK_SET if ! defined $rel;
|
|
95 seek($object->{filedata}{handle}, $pos, $rel);
|
|
96 $object->{buffer} = '';
|
|
97 $object;
|
|
98 }
|
|
99
|
|
100 sub DESTROY {
|
|
101 my ($object) = @_;
|
|
102 $object->{filedata}{to_close} and $object->{filedata}{to_close}->close();
|
|
103 }
|
|
104
|
|
105 1;
|