]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_perl/Maasha/Filesys.pm
rewrite begin
[biopieces.git] / code_perl / Maasha / Filesys.pm
index 229c7c303f68508a027a7f5ff0bfec8cc2884d99..cb27c95ff62d4544f6c0c607d085e76ce5aca875 100644 (file)
@@ -109,6 +109,59 @@ sub file_append_open
 }
 
 
+sub stdin_read
+{
+    # Martin A. Hansen, July 2007.
+
+    # Returns a filehandle to STDIN
+
+    my ( $fh );
+
+    $fh = new IO::File "<&STDIN" or Maasha::Common::error( qq(Could not read from STDIN: $!) );
+
+    return $fh;
+}
+
+
+sub stdout_write
+{
+    # 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_read
+{
+    # Martin A. Hansen, December 2004.
+
+    # given a file, a seek beg position and
+    # length, returns the corresponding string.
+    
+    my ( $fh,     # file handle to file
+         $beg,    # read start in file
+         $len,    # read length of block
+        ) = @_;
+
+    # returns string
+
+    my ( $string );
+
+    Maasha::Common::error( qq(Negative length: $len) ) if $len < 0;
+
+    sysseek $fh, $beg, 0;
+    sysread $fh, $string, $len;
+
+    return $string;
+}
+
+
 sub file_copy
 {
     # Martin A. Hansen, November 2008.
@@ -181,6 +234,152 @@ sub file_size
 }
 
 
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DIRECTORIES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+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;
+}
+
+
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<