| 1 | package Sidef::Types::Glob::FileHandle; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | |||
| 6 | use parent qw( | ||
| 7 | Sidef::Object::Object | ||
| 8 | ); | ||
| 9 | |||
| 10 | use Sidef::Types::Bool::Bool; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my (undef, $fh, $file) = @_; | ||
| 14 | |||
| 15 | bless { | ||
| 16 | fh => $fh, | ||
| 17 | parent => $file, | ||
| 18 | }, | ||
| 19 | __PACKAGE__; | ||
| 20 | } | ||
| 21 | |||
| 22 | *call = \&new; | ||
| 23 | |||
| 24 | sub get_value { | ||
| 25 | $_[0]{fh}; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub parent { | ||
| 29 | $_[0]{parent}; | ||
| 30 | } | ||
| 31 | |||
| 32 | *file = \&parent; | ||
| 33 | |||
| 34 | sub new_buf { | ||
| 35 | my ($self, $mode, $initial_string) = @_; | ||
| 36 | |||
| 37 | $mode //= 'raw'; | ||
| 38 | $initial_string //= ''; | ||
| 39 | $mode = "$mode" if (ref($mode) ne ''); | ||
| 40 | |||
| 41 | my $str = "$initial_string"; | ||
| 42 | |||
| 43 | if ($mode ne 'raw' and $str ne '') { | ||
| 44 | require Encode; | ||
| 45 | $str = Encode::encode($mode, $str); | ||
| 46 | } | ||
| 47 | |||
| 48 | CORE::open(my $fh, "+<:$mode", \$str) or return undef; | ||
| 49 | CORE::seek($fh, CORE::length($str), 0) if ($str ne ''); | ||
| 50 | my $str_obj = bless(\$str, 'Sidef::Types::String::String'); | ||
| 51 | my $fh_obj = __PACKAGE__->new($fh, $str_obj); | ||
| 52 | wantarray ? ($fh_obj, $str_obj) : $fh_obj; | ||
| 53 | } | ||
| 54 | |||
| 55 | *new_buffer = \&new_buf; | ||
| 56 | |||
| 57 | sub is_on_tty { | ||
| 58 | (-t $_[0]{fh}) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 59 | } | ||
| 60 | |||
| 61 | *isatty = \&is_on_tty; | ||
| 62 | |||
| 63 | sub stdout { | ||
| 64 | __PACKAGE__->new(\*STDOUT); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub stderr { | ||
| 68 | __PACKAGE__->new(\*STDERR); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub stdin { | ||
| 72 | __PACKAGE__->new(\*STDIN); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub autoflush { | ||
| 76 | my ($self, $bool) = @_; | ||
| 77 | my $fh = $self->{fh}; | ||
| 78 | my $old_fh = CORE::select($fh); | ||
| 79 | local $| = ($bool ? 1 : 0); | ||
| 80 | CORE::select($old_fh); | ||
| 81 | $self; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub flush { | ||
| 85 | my ($self) = @_; | ||
| 86 | my $fh = $self->{fh}; | ||
| 87 | my $old_fh = CORE::select($fh); | ||
| 88 | local $| = 1; | ||
| 89 | print $fh ''; | ||
| 90 | CORE::select($old_fh); | ||
| 91 | $self; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub binmode { | ||
| 95 | my ($self, $encoding) = @_; | ||
| 96 | CORE::binmode($self->{fh}, "$encoding"); | ||
| 97 | $self; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub syswrite { | ||
| 101 | my ($self, $scalar, $length, $offset) = @_; | ||
| 102 | ( | ||
| 103 | defined($offset) ? (CORE::syswrite $self->{fh}, $scalar, $length, $offset) | ||
| 104 | : defined($length) ? (CORE::syswrite $self->{fh}, $scalar, $length) | ||
| 105 | : (CORE::syswrite $self->{fh}, $scalar) | ||
| 106 | ) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub print { | ||
| 110 | my ($self, @args) = @_; | ||
| 111 | (CORE::print {$self->{fh}} @args) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 112 | } | ||
| 113 | |||
| 114 | *write = \&print; | ||
| 115 | *spurt = \&print; | ||
| 116 | |||
| 117 | sub println { | ||
| 118 | my ($self, @args) = @_; | ||
| 119 | (CORE::say {$self->{fh}} @args) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 120 | } | ||
| 121 | |||
| 122 | *say = \&println; | ||
| 123 | |||
| 124 | sub printf { | ||
| 125 | my ($self, @args) = @_; | ||
| 126 | (CORE::printf {$self->{fh}} @args) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub printlnf { | ||
| 130 | my ($self, $format, @args) = @_; | ||
| 131 | $self->printf("$format\n", @args); | ||
| 132 | } | ||
| 133 | |||
| 134 | *sayf = \&printlnf; | ||
| 135 | |||
| 136 | sub iter { | ||
| 137 | my ($self) = @_; | ||
| 138 | Sidef::Types::Block::Block->new( | ||
| 139 | code => sub { | ||
| 140 | my $line = CORE::readline($self->{fh}) // return undef; | ||
| 141 | chomp($line); | ||
| 142 | Sidef::Types::String::String->new($line); | ||
| 143 | } | ||
| 144 | ); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub fcntl { | ||
| 148 | my ($self, $func, $flags) = @_; | ||
| 149 | CORE::fcntl($self->{fh}, CORE::int($func), CORE::int($flags)) | ||
| 150 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 151 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub read { | ||
| 155 | my ($self, $var_ref, $length, $offset) = @_; | ||
| 156 | |||
| 157 | my $chunk = "$$var_ref"; | ||
| 158 | my $size = Sidef::Types::Number::Number::_set_int( | ||
| 159 | defined($offset) | ||
| 160 | ? CORE::read($self->{fh}, $chunk, $length, $offset) | ||
| 161 | : CORE::read($self->{fh}, $chunk, $length) | ||
| 162 | ); | ||
| 163 | |||
| 164 | $$var_ref = Sidef::Types::String::String->new($chunk); | ||
| 165 | |||
| 166 | return $size; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub sysread { | ||
| 170 | my ($self, $var_ref, $length, $offset) = @_; | ||
| 171 | |||
| 172 | my $chunk = "$$var_ref"; | ||
| 173 | my $size = Sidef::Types::Number::Number::_set_int( | ||
| 174 | defined($offset) | ||
| 175 | ? CORE::sysread($self->{fh}, $chunk, $length, $offset) | ||
| 176 | : CORE::sysread($self->{fh}, $chunk, $length) | ||
| 177 | ); | ||
| 178 | |||
| 179 | $$var_ref = Sidef::Types::String::String->new($chunk); | ||
| 180 | |||
| 181 | return $size; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub slurp { | ||
| 185 | my ($self) = @_; | ||
| 186 | Sidef::Types::String::String->new( | ||
| 187 | do { | ||
| 188 | local $/; | ||
| 189 | CORE::readline($self->{fh}); | ||
| 190 | } | ||
| 191 | ); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub read_line { | ||
| 195 | my ($self, $var_ref) = @_; | ||
| 196 | |||
| 197 | my $line = CORE::readline($self->{fh}); | ||
| 198 | |||
| 199 | if (defined $var_ref) { | ||
| 200 | $line // return Sidef::Types::Bool::Bool::FALSE; | ||
| 201 | chomp($line); | ||
| 202 | $$var_ref = Sidef::Types::String::String->new($line); | ||
| 203 | return Sidef::Types::Bool::Bool::TRUE; | ||
| 204 | } | ||
| 205 | |||
| 206 | $line // return undef; | ||
| 207 | chomp($line); | ||
| 208 | Sidef::Types::String::String->new($line); | ||
| 209 | } | ||
| 210 | |||
| 211 | *readln = \&read_line; | ||
| 212 | *readline = \&read_line; | ||
| 213 | *get = \&read_line; | ||
| 214 | *line = \&read_line; | ||
| 215 | |||
| 216 | sub read_char { | ||
| 217 | my ($self, $var_ref) = @_; | ||
| 218 | |||
| 219 | my $char = CORE::getc($self->{fh}); | ||
| 220 | |||
| 221 | if (defined $var_ref) { | ||
| 222 | $$var_ref = Sidef::Types::String::String->new($char // return Sidef::Types::Bool::Bool::FALSE); | ||
| 223 | return Sidef::Types::Bool::Bool::TRUE; | ||
| 224 | } | ||
| 225 | |||
| 226 | Sidef::Types::String::String->new($char // return undef); | ||
| 227 | } | ||
| 228 | |||
| 229 | *char = \&read_char; | ||
| 230 | *getc = \&read_char; | ||
| 231 | |||
| 232 | sub peek_char { | ||
| 233 | my ($self) = @_; | ||
| 234 | my $pos = CORE::tell($self->{fh}); | ||
| 235 | my $char = CORE::getc($self->{fh}); | ||
| 236 | defined($char) or return undef; | ||
| 237 | CORE::seek($self->{fh}, $pos, 0); | ||
| 238 | Sidef::Types::String::String->new($char); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub read_byte { | ||
| 242 | my ($self, $var_ref) = @_; | ||
| 243 | |||
| 244 | my $char = CORE::getc($self->{fh}); | ||
| 245 | my $byte = defined($char) ? CORE::ord($char) : undef; | ||
| 246 | |||
| 247 | if (defined $var_ref) { | ||
| 248 | $$var_ref = Sidef::Types::Number::Number::_set_int($byte // return Sidef::Types::Bool::Bool::FALSE); | ||
| 249 | return Sidef::Types::Bool::Bool::TRUE; | ||
| 250 | } | ||
| 251 | |||
| 252 | Sidef::Types::Number::Number::_set_int($byte // return undef); | ||
| 253 | } | ||
| 254 | |||
| 255 | *getb = \&read_byte; | ||
| 256 | *byte = \&read_byte; | ||
| 257 | |||
| 258 | sub read_lines { | ||
| 259 | my ($self) = @_; | ||
| 260 | Sidef::Types::Array::Array->new([map { chomp($_); Sidef::Types::String::String->new($_) } CORE::readline($self->{fh})]); | ||
| 261 | } | ||
| 262 | |||
| 263 | *readlines = \&read_lines; | ||
| 264 | *lines = \&read_lines; | ||
| 265 | |||
| 266 | sub grep { | ||
| 267 | my ($self, $obj) = @_; | ||
| 268 | |||
| 269 | my @array; | ||
| 270 | |||
| 271 | while (defined(my $line = CORE::readline($self->{fh}))) { | ||
| 272 | chomp($line); | ||
| 273 | my $string = Sidef::Types::String::String->new($line); | ||
| 274 | push @array, $string if $obj->run($string); | ||
| 275 | } | ||
| 276 | |||
| 277 | Sidef::Types::Array::Array->new(\@array); | ||
| 278 | } | ||
| 279 | |||
| 280 | *select = \&grep; | ||
| 281 | |||
| 282 | sub map { | ||
| 283 | my ($self, $block) = @_; | ||
| 284 | |||
| 285 | my @array; | ||
| 286 | while (defined(my $line = CORE::readline($self->{fh}))) { | ||
| 287 | chomp($line); | ||
| 288 | push @array, $block->run(Sidef::Types::String::String->new($line)); | ||
| 289 | } | ||
| 290 | Sidef::Types::Array::Array->new(\@array); | ||
| 291 | } | ||
| 292 | |||
| 293 | *collect = \↦ | ||
| 294 | |||
| 295 | sub words { | ||
| 296 | my ($self) = @_; | ||
| 297 | Sidef::Types::Array::Array->new( | ||
| 298 | [ | ||
| 299 | map { | ||
| 300 | map { Sidef::Types::String::String->new($_) } | ||
| 301 | grep { $_ ne '' } | ||
| 302 | split(' ', $_) | ||
| 303 | } CORE::readline($self->{fh}) | ||
| 304 | ] | ||
| 305 | ); | ||
| 306 | } | ||
| 307 | |||
| 308 | sub chars { | ||
| 309 | my ($self) = @_; | ||
| 310 | Sidef::Types::Array::Array->new( | ||
| 311 | [ | ||
| 312 | map { Sidef::Types::String::String->new($_) } do { | ||
| 313 | local $/; | ||
| 314 | split(//, scalar CORE::readline($self->{fh})); | ||
| 315 | } | ||
| 316 | ] | ||
| 317 | ); | ||
| 318 | } | ||
| 319 | |||
| 320 | sub each { | ||
| 321 | my ($self, $code) = @_; | ||
| 322 | |||
| 323 | while (defined(my $line = CORE::readline($self->{fh}))) { | ||
| 324 | chomp($line); | ||
| 325 | $code->run(Sidef::Types::String::String->new($line)); | ||
| 326 | } | ||
| 327 | |||
| 328 | $self; | ||
| 329 | } | ||
| 330 | |||
| 331 | *each_line = \&each; | ||
| 332 | |||
| 333 | sub each_kv { | ||
| 334 | my ($self, $code) = @_; | ||
| 335 | my $i = 0; | ||
| 336 | while (defined(my $line = CORE::readline($self->{fh}))) { | ||
| 337 | chomp($line); | ||
| 338 | $code->run(Sidef::Types::Number::Number::_set_int($i++), Sidef::Types::String::String->new($line)); | ||
| 339 | } | ||
| 340 | $self; | ||
| 341 | } | ||
| 342 | |||
| 343 | sub each_char { | ||
| 344 | my ($self, $code) = @_; | ||
| 345 | |||
| 346 | while (defined(my $char = CORE::getc($self->{fh}))) { | ||
| 347 | $code->run(Sidef::Types::String::String->new($char)); | ||
| 348 | } | ||
| 349 | |||
| 350 | $self; | ||
| 351 | } | ||
| 352 | |||
| 353 | sub eof { | ||
| 354 | my ($self) = @_; | ||
| 355 | CORE::eof($self->{fh}) | ||
| 356 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 357 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 358 | } | ||
| 359 | |||
| 360 | sub tell { | ||
| 361 | my ($self) = @_; | ||
| 362 | Sidef::Types::Number::Number::_set_int(CORE::tell($self->{fh})); | ||
| 363 | } | ||
| 364 | |||
| 365 | sub rewind { | ||
| 366 | my ($self) = @_; | ||
| 367 | CORE::seek($self->{fh}, 0, 0) | ||
| 368 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 369 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 370 | } | ||
| 371 | |||
| 372 | sub seek { | ||
| 373 | my ($self, $pos, $whence) = @_; | ||
| 374 | CORE::seek($self->{fh}, $pos, $whence) | ||
| 375 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 376 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 377 | } | ||
| 378 | |||
| 379 | sub sysseek { | ||
| 380 | my ($self, $pos, $whence) = @_; | ||
| 381 | CORE::sysseek($self->{fh}, $pos, $whence) | ||
| 382 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 383 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 384 | } | ||
| 385 | |||
| 386 | sub fileno { | ||
| 387 | my ($self) = @_; | ||
| 388 | Sidef::Types::Number::Number::_set_int(CORE::fileno($self->{fh})); | ||
| 389 | } | ||
| 390 | |||
| 391 | sub lock { | ||
| 392 | my ($self) = @_; | ||
| 393 | |||
| 394 | state $x = require Fcntl; | ||
| 395 | $self->flock(&Fcntl::LOCK_EX); | ||
| 396 | } | ||
| 397 | |||
| 398 | sub unlock { | ||
| 399 | my ($self) = @_; | ||
| 400 | |||
| 401 | state $x = require Fcntl; | ||
| 402 | $self->flock(&Fcntl::LOCK_UN); | ||
| 403 | } | ||
| 404 | |||
| 405 | sub flock { | ||
| 406 | my ($self, $mode) = @_; | ||
| 407 | CORE::flock($self->{fh}, $mode) | ||
| 408 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 409 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 410 | } | ||
| 411 | |||
| 412 | sub close { | ||
| 413 | my ($self) = @_; | ||
| 414 | CORE::close($self->{fh}) | ||
| 415 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 416 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 417 | } | ||
| 418 | |||
| 419 | sub stat { | ||
| 420 | my ($self) = @_; | ||
| 421 | Sidef::Types::Glob::Stat->stat($self->{fh}, $self); | ||
| 422 | } | ||
| 423 | |||
| 424 | sub lstat { | ||
| 425 | my ($self) = @_; | ||
| 426 | Sidef::Types::Glob::Stat->lstat($self->{fh}, $self); | ||
| 427 | } | ||
| 428 | |||
| 429 | sub truncate { | ||
| 430 | my ($self, $length) = @_; | ||
| 431 | CORE::truncate($self->{fh}, $length // 0) | ||
| 432 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 433 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 434 | } | ||
| 435 | |||
| 436 | sub read_to { | ||
| 437 | my ($self, $var_ref) = @_; | ||
| 438 | |||
| 439 | my $line = CORE::readline($self->{fh}); | ||
| 440 | |||
| 441 | if (defined($line)) { | ||
| 442 | chomp($line); | ||
| 443 | $$var_ref = Sidef::Types::String::String->new($line); | ||
| 444 | } | ||
| 445 | else { | ||
| 446 | undef $$var_ref; | ||
| 447 | } | ||
| 448 | |||
| 449 | $self; | ||
| 450 | } | ||
| 451 | |||
| 452 | sub write_from { | ||
| 453 | my ($self, @args) = @_; | ||
| 454 | CORE::print {$self->{fh}} @args; | ||
| 455 | $self; | ||
| 456 | } | ||
| 457 | |||
| 458 | sub copy { | ||
| 459 | my ($self, $other) = @_; | ||
| 460 | |||
| 461 | state $x = require File::Copy; | ||
| 462 | |||
| 463 | if (ref($other) eq __PACKAGE__) { | ||
| 464 | return ( | ||
| 465 | File::Copy::copy($self->{fh}, $other->{fh}) | ||
| 466 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 467 | : (Sidef::Types::Bool::Bool::FALSE) | ||
| 468 | ); | ||
| 469 | } | ||
| 470 | |||
| 471 | File::Copy::copy($self->{fh}, "$other") | ||
| 472 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 473 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 474 | } | ||
| 475 | |||
| 476 | *cp = \© | ||
| 477 | |||
| 478 | { | ||
| 479 | no strict 'refs'; | ||
| 480 | *{__PACKAGE__ . '::' . '>>'} = \&read_to; | ||
| 481 | *{__PACKAGE__ . '::' . '»'} = \&read_to; | ||
| 482 | *{__PACKAGE__ . '::' . '<<'} = \&write_from; | ||
| 483 | *{__PACKAGE__ . '::' . '«'} = \&write_from; | ||
| 484 | } | ||
| 485 | |||
| 486 | 1 |