# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+use warnings;
use strict;
use Carp;
use Data::Dumper;
use Storable;
use IO::File;
+use Time::HiRes qw( gettimeofday );
use Maasha::Config;
use Exporter;
@ISA = qw( Exporter ) ;
+use Inline ( C => <<'END_C', DIRECTORY => $ENV{ "BP_TMP" } );
+
+int index_m( char *str, char *substr, size_t str_len, size_t substr_len, size_t offset, size_t max_mismatch )
+{
+ /* Martin A. Hansen & Selene Fernandez, August 2008 */
+
+ /* Locates a substring within a string starting from offset and allowing for max_mismatch mismatches. */
+ /* The begin position of the substring is returned if found otherwise -1 is returned. */
+
+ int i = 0;
+ int j = 0;
+
+ size_t max_match = substr_len - max_mismatch;
+
+ i = offset;
+
+ while ( i < str_len - ( max_match + max_mismatch ) + 1 )
+ {
+ j = 0;
+
+ while ( j < substr_len - ( max_match + max_mismatch ) + 1 )
+ {
+ if ( match_m( str, substr, str_len, substr_len, i, j, max_match, max_mismatch ) != 0 ) {
+ return i;
+ }
+
+ j++;
+ }
+
+ i++;
+ }
+
+ return -1;
+}
+
+
+int match_m( char *str, char *substr, size_t str_len, size_t substr_len, size_t str_offset, size_t substr_offset, size_t max_match, size_t max_mismatch )
+{
+ /* Martin A. Hansen & Selene Fernandez, August 2008 */
+
+ /* Compares a string and substring starting at speficied string and substring offset */
+ /* positions allowing for a specified number of mismatches. Returns 1 if there is a */
+ /* match otherwise returns 0. */
+
+ size_t match = 0;
+ size_t mismatch = 0;
+
+ while ( str_offset <= str_len && substr_offset <= substr_len )
+ {
+ if ( str[ str_offset ] == substr[ substr_offset ] )
+ {
+ match++;
+
+ if ( match >= max_match ) {
+ return 1;
+ };
+ }
+ else
+ {
+ mismatch++;
+
+ if ( mismatch > max_mismatch ) {
+ return 0;
+ }
+ }
+
+ str_offset++;
+ substr_offset++;
+ }
+
+ return 0;
+}
+
+
+void str_analyze_C( const char *string )
+{
+ /* Martin A. Hansen, July 2009 */
+
+ /* Scans a string incrementing the char count in an array. */
+
+ int count[ 256 ] = { 0 }; /* Integer array spanning the ASCII alphabet */
+ int i;
+
+ for ( i = 0; i < strlen( string ); i++ ) {
+ count[ ( int ) string[ i ] ]++;
+ }
+
+ Inline_Stack_Vars;
+ Inline_Stack_Reset;
+
+ for ( i = 0; i < 256; i++ ) {
+ Inline_Stack_Push( sv_2mortal( newSViv( count[ i ] ) ) );
+ }
+
+ Inline_Stack_Done;
+}
+
+
+END_C
+
+
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$file_max = length "File";
$line_max = length "Line";
- if ( $line =~ /^ at (.+) line (\d+)$/ )
+ if ( $line =~ /^ at (.+) line (\d+)\.?$/ )
{
$file = $1;
$line_no = $2;
$file = $2;
$line_no = $3;
+ $routine =~ s/\(.+\)$/ .../;
+
$routine_max = length $routine if length $routine > $routine_max;
$file_max = length $file if length $file > $file_max;
$line_max = length $line_no if length $line_no > $line_max;
{
# Martin A. Hansen, January 2004.
- # read opens a file and returns a filehandle.
+ # Read opens a file and returns a filehandle.
my ( $path, # full path to file
) = @_;
my ( $fh, $type );
- $type = `file $path` if $path;
+ $type = `file $path` if -f $path;
if ( $type =~ /gzip compressed/ ) {
$fh = new IO::File "zcat $path|" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
}
-sub read_stdin
-{
- # 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 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;
-}
-
-
sub read_args
{
# Martin A. Hansen, December 2006
}
+sub get_time_hires
+{
+ # Martin A. Hansen, May 2008.
+
+ # Get current time in high resolution.
+
+ # Returns a float.
+
+ return gettimeofday();
+}
+
+
sub get_processid
{
# Martin A. Hansen, July 2008.
$path = "$ENV{ 'BP_TMP' }/" . join( "_", $user, $sid, $pid, "bp_tmp" );
- Maasha::Common::dir_create( $path );
+ Maasha::Filesys::dir_create( $path );
return $path;
}
}
-sub file_read
+sub get_fields
{
- # 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 );
+ # Martin A. Hansen, July 2008.
- Maasha::Common::error( qq(Negative length: $len) ) if $len < 0;
+ # Given a filehandle to a file gets the
+ # next line which is split into a list of
+ # fields that is returned.
- sysseek $fh, $beg, 0;
- sysread $fh, $string, $len;
+ my ( $fh, # filehandle
+ $delimiter, # field seperator - OPTIONAL
+ ) = @_;
- return $string;
-}
+ # Returns a list.
+ my ( $line, @fields );
-sub file_size
-{
- # Martin A. Hansen, March 2007
+ $line = <$fh>;
- # returns the file size for a given file
+ return if not defined $line;
- my ( $path, # full path to file
- ) = @_;
+ chomp $line;
- # returns integer
+ $delimiter ||= "\t";
- my $file_size = ( stat ( $path ) )[ 7 ];
+ @fields = split "$delimiter", $line;
- return $file_size;
+ return wantarray ? @fields : \@fields;
}
}
+sub time_stamp_diff
+{
+ # Martin A. Hansen, June 2009.
+
+ # Return the difference between two time stamps in
+ # the time stamp format.
+
+ my ( $t0, # time stamp 0
+ $t1, # time stamp 1
+ ) = @_;
+
+ # Returns a time stamp string.
+
+ my ( $year0, $mon0, $day0, $hour0, $min0, $sec0,
+ $year1, $mon1, $day1, $hour1, $min1, $sec1,
+ $year, $mon, $day, $hour, $min, $sec );
+
+ $t0 =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/;
+ $year0 = $1;
+ $mon0 = $2;
+ $day0 = $3;
+ $hour0 = $4;
+ $min0 = $5;
+ $sec0 = $6;
+
+ $sec0 += $day0 * 24 * 60 * 60;
+ $sec0 += $hour0 * 60 * 60;;
+ $sec0 += $min0 * 60;
+
+ $t1 =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/;
+ $year1 = $1;
+ $mon1 = $2;
+ $day1 = $3;
+ $hour1 = $4;
+ $min1 = $5;
+ $sec1 = $6;
+
+ $sec1 += $day1 * 24 * 60 * 60;
+ $sec1 += $hour1 * 60 * 60;;
+ $sec1 += $min1 * 60;
+
+ $year = $year1 - $year0;
+ $mon = $mon1 - $mon0;
+ $day = $day1 - $day0;
+
+ $sec = $sec1 - $sec0;
+
+ $hour = int( $sec / ( 60 * 60 ) );
+ $sec -= $hour * 60 * 60;
+
+ $min = int( $sec / 60 );
+ $sec -= $min * 60;
+
+ return join( ":", sprintf( "%02d", $hour ), sprintf( "%02d", $min ), sprintf( "%02d", $sec ) );
+}
+
+
sub process_running
{
# Martin A. Hansen, July 2008.
}
+sub str_analyze
+{
+ # Martin A. Hansen, July 2009.
+
+ # Analyzes the string composition of a given string.
+
+ my ( $str, # string to analyze
+ ) = @_;
+
+ # Returns hash
+
+ my ( @composition, %hash, $i );
+
+ @composition = Maasha::Common::str_analyze_C( $str );
+
+ for ( $i = 32; $i <= 126; $i++ ) { # Only include printable chars
+ $hash{ chr $i } = $composition[ $i ]
+ }
+
+ return wantarray ? %hash : \%hash;
+}
+
+
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1;