-sub write_stdout
-{
- # Martin A. Hansen, July 2007.
-
- # Returns a filehandle to STDOUT
-
- my ( $fh );
-
- $fh = new IO::File ">&STDOUT" or &Maasha::Common::error( qq(Could not write to STDOUT: $!) );
-
- return $fh;
-}
-
-
-sub file_store
-{
- # Martin A. Hansen, December 2004.
-
- # writes a data structure to file.
-
- my ( $path, # full path to file
- $data, # data structure
- ) = @_;
-
- &Storable::store( $data, $path ) or &Maasha::Common::error( qq(Could not write-open file "$path": $!) );
-}
-
-
-sub file_retrieve
-{
- # Martin A. Hansen, December 2004.
-
- # retrieves hash data structure
- # (this routines needs to test if its a hash, array or else)
-
- my ( $path, # full path to data file
- ) = @_;
-
- my ( $data );
-
- $data = &Storable::retrieve( $path ) or &Maasha::Common::error( qq(Could not read-open file "$path": $!) );
-
- return wantarray ? %{ $data } : $data;
-}
-
-
-sub dir_create
-{
- # Martin A. Hansen, July 2007.
-
- # Creates a directory.
-
- my ( $path, # full path to dir
- ) = @_;
-
- # Returns nothing.
-
- if ( -d $path ) {
- &Maasha::Common::error( qq(Directory already exists "$path": $!) );
- } else {
- mkdir $path or &Maasha::Common::error( qq(Could not create directory "$path": $!) );
- }
-}
-
-
-sub dir_create_if_not_exists
-{
- # Martin A. Hansen, May 2008.
-
- # Creates a directory if it does not already exists.
-
- my ( $path, # full path to dir
- ) = @_;
-
- # Returns nothing.
-
- if ( not -d $path ) {
- mkdir $path or &Maasha::Common::error( qq(Could not create directory "$path": $!) );
- }
-}
-
-
-sub dir_remove
-{
- # Martin A. Hansen, April 2008.
-
- # Removes a directory recursively.
-
- my ( $path, # directory
- ) = @_;
-
- &Maasha::Common::run( "rm", "-rf $path" ) if -d $path;
-}
-
-
-sub ls_dirs
-{
- # Martin A. Hansen, June 2007.
-
- # returns all dirs in a given directory.
-
- my ( $path, # full path to directory
- ) = @_;
-
- # returns a list of filenames.
-
- my ( $dh, @dirs );
-
- $dh = &open_dir( $path );
-
- @dirs = &read_dir( $dh );
- @dirs = grep { -d "$path/$_" } @dirs;
-
- map { $_ = "$path/$_" } @dirs;
-
- close $dh;
-
- return wantarray ? @dirs : \@dirs;
-}
-
-
-sub ls_files
-{
- # Martin A. Hansen, June 2007.
-
- # returns all files in a given directory.
-
- my ( $path, # full path to directory
- ) = @_;
-
- # returns a list of filenames.
-
- my ( $dh, @files );
-
- $dh = &open_dir( $path );
-
- @files = &read_dir( $dh );
- @files = grep { -f "$path/$_" } @files;
-
- map { $_ = "$path/$_" } @files;
-
- close $dh;
-
- return wantarray ? @files : \@files;
-}
-
-
-sub open_dir
-{
- # Martin A. Hansen, June 2007.
-
- # open a directory and returns a directory handle
-
- use IO::Dir;
-
- my ( $path, # full path to directory
- ) = @_;
-
- # returns object
-
- my $dh;
-
- $dh = IO::Dir->new( $path ) or &Maasha::Common::error( qq(Could not open dir "$path": $!) );
-
- return $dh;
-}
-
-
-sub read_dir
-{
- # Martin A. Hansen, June 2007.
-
- # read all files and directories from a directory.
-
- my ( $dh, # directory handle object
- ) = @_;
-
- # returns list
-
- my ( $elem, @elems );
-
- while ( defined( $elem = $dh->read ) ) {
- push @elems, $elem;
- }
-
- return wantarray ? @elems : \@elems;
-}
-
-