package utils::util; #Author:Fernando A. P. Gomes #Email: fapg@eurotux.com #Package utils #Version 0.4.6 # Copyright (C) 2001-2002 Fernando Gomes # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA use strict; use vars qw(@ISA @EXPORT); require Exporter; @ISA = qw(Exporter); @EXPORT = qw(min_array max_array cat getfile); # retorna o elemento menor de um dado array # retuns min array element sub min_array { my $min = 0; map {$_>$min and $min=$_} @_; return $min; } # retorna o elemento maior de um dado array # retuns max array element sub max_array { my $max = 0; map {$_>$max and $max=$_} @_; return $max; } #faz um cat para o stdout de um ficheiro #do the same thing as a cat command sub cat { my $file; foreach $file (@_) { if (open(FD, "<$file")) { while () { print; } close(FD); } else { print STDERR "$file: $!\n"; } } return 0; } #faz um cat para uma variavel de um ficheiro #prints a contents of a file into a var sub getfile { my $file; my @files; foreach $file (@_) { if (open(FD, "<$file")) { push @files, ; close(FD); } else { push @files, "$file: $!\n"; } } return @files; } # cp expect, copia o que esta num determinado ficheiro # para outro, mas tirando um determinado match nesse ficheiro # grep -v ola origem > destino # ex:cpex("ola", origem, destino); sub cpex { my ($str, $org, $dst) = @_; if ($dst =~ /(.*)/) { $dst = $1; } if (! defined $str || ! defined $org || !defined $dst) { return 1; } if (!open(OR, "<$org")) { print "$org: $!\n"; return 1; } if (!open(DS, ">$dst")) { print "$dst: $!\n"; close(OR); return 1; } while () { if (index($_, $str) < 0) { print DS $_; } } close(OR); close(DS); return 0; } # conta o numero de matches num determinado ficheiro # tipo grep -c ola fich fich1 # ex: cgrep ("ola", @ficheiros) sub cgrep { my ($str, @files) = @_; my $f; my $i; if (! defined $str) { return 0; } $i = 0; foreach $f (@files) { if (open(FD, "<$f")) { while () { chomp; if ($_ eq $str) { $i++; } } close(FD); } } return $i; } # retorna os matches de um detrminado ficheiro # igual ao grep ola fich1 fich2 # ex: mygrep("ola", @files) sub mygrep { my ($str, @files) = @_; my $f; my @res = (); my $i; if (! defined $str) { return @res; } $i = 0; foreach $f (@files) { if (open(FD, "<$f")) { while () { chomp; if (index($_, $str) >= 0) { $res[$i++] = $_; } } close(FD); } } return @res; } # copia um ficheiro para outro # o mesmo que o cp origem destino # ex: cp("origem", "destino") sub cp { my ($orig, $dest) = @_; if (!open(ORIG, "<$orig")) { return 0; } if (!open(DEST, ">$dest")) { close(ORIG); return 0; } while () { print DEST; } close(ORIG); close(DEST); return 1; } # o mesmo que o shift mas repetido varias vezes # ex: my_shift(2, @array); sub my_shift { my ($number, @array) = @_; for (my $i=0; $i <= $number; $i++) { shift @array; } return @array; } 1;