Meta

 view release on metacpan or  search on metacpan

Meta/Utils/Utils.pm  view on Meta::CPAN

	return($dir."/".$base);
	#the old code which is bad since it does not handle files
	#which have "." in the directory part.
	#$file=~s/\..*/$suff/;
}

sub remove_suffix($) {
	my($file)=@_;
	return(replace_suffix($file,""));
}

sub remove_suf($$) {
	my($full,$partial)=@_;
	if(substr($full,length($full)-length($partial),length($partial)) eq $partial) {
		return(substr($full,0,length($full)-length($partial)));
	} else {
		throw Meta::Error::Simple("partial is [".$partial."] and full is [".$full."]\n");
	}
}

sub basename($) {
	my($file)=@_;
	my(@arra)=split('/',$file);
	my($last)=$arra[$#arra];
	return(remove_suffix($last));
#	my($basename)=($file=~/\/([.-\/]*)\.[.-\/]*$/);
#	return($basename);
}

sub get_suffix($) {
	my($file)=@_;
	my($suff)=($file=~/^.*\.(.*)$/);
	return($suff);
}

sub is_prefix($$) {
	my($stri,$pref)=@_;
	#$pref=quotemeta($pref);
	#Meta::Utils::Output::print("pref is [".$pref."]\n");
	#return($stri=~/^$pref/);
	my($sub)=substr($stri,0,length($pref));
	return($sub eq $pref);
}

sub is_suffix($$) {
	my($stri,$suff)=@_;
	#$suff=quotemeta($suff);
	#Meta::Utils::Output::print("suff is [".$suff."]\n");
	#return($stri=~/$suff$/);
	my($sub)=substr($stri,-length($suff));
	return($sub eq $suff);
}

sub cuid() {
	return(POSIX::getuid());
#	return($>);
}

sub cuname() {
	my($uid)=POSIX::getuid();
	return((POSIX::getpwuid($uid))[0]);
}

sub cgid() {
	return(POSIX::getegid());
}

sub get_home_dir() {
	my($uid)=POSIX::getuid();
	return((POSIX::getpwuid($uid))[7]);
	#my($user)=POSIX::getpwnam();
	#return(get_user_home_dir($user));
#	return(Meta::Utils::Env::get("HOME"));
}

sub get_user_home_dir($) {
	my($user)=@_;
	my($resu)=((POSIX::getpwnam($user))[7]);
	if(!defined($resu)) {
		throw Meta::Error::Simple("user [".$user."] unknown");
	}
	return($resu);
}

sub remove_comments($) {
	my($text)=@_;
	$text=~s/\/\/*.*\*\///;
	return($text);
}

sub cat($$$) {
	my($f1,$f2,$out)=@_;
	if($f1 eq $out || $f2 eq $out) {
		throw Meta::Error::Simple("bad files given");
	}
	my($io_out)=Meta::IO::File->new_writer($out);
	my($io_f1)=Meta::IO::File->new_reader($f1);
	while(!$io_f1->eof()) {
		my($line)=$io_f1->getline();
		print $io_out $line;
	}
	$io_f1->close();
	my($io_f2)=Meta::IO::File->new_reader($f2);
	while(!$io_f2->eof()) {
		my($line)=$io_f2->getline();
		print $io_out $line;
	}
	$io_f2->close();
	$io_out->close();
	#older implementation which is much less efficient
	#my($text_f1,$text_f2);
	#Meta::Utils::File::File::load($f1,\$text_f1);
	#Meta::Utils::File::File::load($f2,\$text_f2);
	#my($text_out)=$text_f1.$text_f2;
	#Meta::Utils::File::File::save($out,$text_out);
}

sub is_absolute($) {
	my($fn)=@_;
	return($fn=~/^\//);
}

sub is_relative($) {
	my($fn)=@_;
	return($fn!~/^\//);
}

sub to_absolute($) {
	my($fn)=@_;
	if(is_absolute($fn)) {

Meta/Utils/Utils.pm  view on Meta::CPAN


=item B<get_temp_dir()>

This gives you a temporary directory where you can store temporary files
to your hearts content. Currently this just returns "/tmp" which is ok for
UNIX type systems.

=item B<get_temp_dire()>

This method will give you a directory it created in a temporary location.
Currently it iterates on names until it manages to create the directory.

=item B<get_temp_file()>

This gives you a temporary file name using the POSIX tmpnam function.

=item B<replace_suffix($$)>

This replaces the strings suffix with another one.

=item B<remove_suffix($)>

This removes a suffix from the string argument given it.
This just substitues the suffix of the string with nothing...:)

=item B<remove_suf($$)>

This method removes a suffix from a string.
The suffix is an explicit string passed to it.

=item B<is_prefix($$)>

This routine receives a string and a prefix and returns whether the
prefix is a prefix for that string

=item B<is_suffix($$)>

This routine receives a string and a suffix and returns whether the
suffix is a suffix for that string

=item B<cuid()>

This routine returns the numerical value of the current user (uid).

=item B<cuname()>

This routine returns the current user name (uname).

=item B<cgid()>

This routine returns the numerical value of the current group (gid).
I don't think there is a cleaner way to do this.

=item B<get_home_dir()>

This routine returns the current users home directory.
The implementation used to work with the environment and getting the
HOME variable but this is very unrobust and works for less platforms
and situations. Currently this uses POSIX which is much more robust
to find the uid of the current user and then the home directory from
the password file using getpwuid. The reason that this does not use
the get_user_home_dir method from this same module is that there is
no convinient way to get the current user name (it would take
another function to convert uid to uname). The implementation marked
out using POSIX::getpwnam does not work.

=item B<get_user_home_dir($)>

This routine returns the home dir of the user that is given to it as the
argument.

=item B<remove_comments($)>

This routine will receive a text and will remove all comments from it.
The idea here is C/C++ style comments : /* sdfdaf */

=item B<cat($$$)>

This function receives the names of two files and write the content of the
two fles into the third one. If one of the input files is the output file
then this function will throw an exception. In the future the function may
be able to deal with such cases by moving the info through an intermediate
file.

=item B<is_absolute($)>

This function will return whether the file name it received is an absolute file name.

=item B<is_relative($)>

This function will return whether the file name it received is a relative file name.

=item B<to_absolute($)>

This function will convert a relative file name to an absolute one. It use
Meta::Utils::Chdir::pwd to do it's thing.

=item B<TEST($)>

Test suite for this module.

=back

=head1 SUPER CLASSES

None.

=head1 BUGS

None.

=head1 AUTHOR

	Name: Mark Veltzer
	Email: mailto:veltzer@cpan.org
	WWW: http://www.veltzer.org
	CPAN id: VELTZER

=head1 HISTORY

	0.00 MV initial code brought in



( run in 2.950 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )