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
|
#-*- perl -*-
#
# Copyright (C) 2001,2002,2003 Ken'ichi Fukamachi
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Array.pm,v 1.30 2003/02/11 11:37:47 fukachan Exp $
#
package IO::Adapter::Array;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
use IO::Adapter::ErrorStatus qw(error_set error error_clear);
=head1 NAME
IO::Adapter::Array - base class for IO emulation for ARRAY_REF.
=head1 SYNOPSIS
use IO::Adapter::Array;
$map = [ 'rudo', 'kenken', 'hitomi' ];
$obj = new IO::Adapter::Array $map;
$obj->open;
while ($x = $obj->get_next_key) { print $x;}
$obj->close;
=head1 DESCRIPTION
emulate IO operation for the ARRAY_REF on memory.
One array is similar to a set of primary keys without optional values
such as a file:
rudo
kenken
hitomi
...
=head1 METHODS
=item C<new()>
constructor.
=cut
# Descriptions: constructor
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: OBJ
sub new
{
my ($self) = @_;
my ($type) = ref($self) || $self;
my $me = {};
return bless $me, $type;
}
=head2
=item C<open($args)>
open IO for the array ARRAY_REF. $args is a hash reference.
The option follows:
$args = {
flag => $flag
_array_reference => ARRAY_REFERENCE
}
$flag is "r" only (read only) now.
=cut
# Descriptions: open() emulation
# Arguments: OBJ($self) HASH_REF($args)
# $args = {
# flag => $flag
# _array_reference => ARRAY_REFERENCE
# }
# Side Effects: malloc @elements array
# Return Value: ARRAY_REF
sub open
{
my ($self, $args) = @_;
my $flag = $args->{ flag } || 'r';
my $r_array = $self->{ _array_reference };
if ($flag ne 'r') {
$self->error_set("Error: type=$self->{_type} is read only.");
return undef;
}
# malloc()
my @elements = @$r_array;
$self->{_elements} = $r_array;
$self->{_num_elements} = $#elements;
$self->{_counter} = 0;
return( @elements ? \@elements : undef );
}
=head2
=item C<getline()>
should not use this ?
=item C<get_next_key()>
return the next element of the array.
=cut
# Descriptions: forwarded to get_next_key()
# XXX getline() == get_next_key() is valid in this case.
# XXX since this map has only key and no value.
# Arguments: OBJ($self) HASH_REF($args)
# Side Effects: increment the counter in the object
# Return Value: STR(the next element)
sub getline { get_next_key(@_);}
# Descriptions: return the next element of the array
# Arguments: OBJ($self) HASH_REF($args)
# Side Effects: increment the counter in the object
# Return Value: STR(the next element)
sub get_next_key
{
my ($self, $args) = @_;
my $i = $self->{_counter}++;
my $ra = $self->{_elements};
return( defined $$ra[ $i ] ? $$ra[ $i ] : undef );
}
=head2 C<getpos()>
return the current position in the array.
=head2 C<setpos($pos)>
set the current position to $pos -th element.
=cut
# Descriptions: return the current position in the array, that is,
# which element in the array
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM(the current number of element)
sub getpos
{
my ($self) = @_;
return $self->{_counter};
}
# Descriptions: set the postion in the array
# Arguments: OBJ($self) NUM($pos)
# $pos is the integer number.
# Side Effects: reset counter in the object
# Return Value: NUM(update position)
sub setpos
{
my ($self, $pos) = @_;
$self->{_counter} = $pos;
}
=head2 C<eof()>
whether the current position reaches the end of the array or not.
If it already reaches the end, return 1.
=head2 C<close()>
end of IO operation. It is a dummy.
=cut
# Descriptions: whether end of the array is not now
# Arguments: OJB($self)
# Side Effects: none
# Return Value: 1 or 0.
# return 1 if the element reaches the end of the array.
sub eof
{
my ($self) = @_;
$self->{_counter} > $self->{_num_elements} ? 1 : 0;
}
# Descriptions: close() is a fake.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub close
{
my ($self) = @_;
}
=head1 CODING STYLE
See C<http://www.fml.org/software/FNF/> on fml coding style guide.
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2001,2002,2003 Ken'ichi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
IO::Adapter::Array first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.
=cut
1;
|