## Some Cool some routines I have written over time. Eventually ## I will post them on my website, then I get to them from anywhere! ## Hmmm, I could have my website link to this file then. ## Copyright (c) 2000 Eric Hanson 3/8/2000 ## Note, I don't care if you use this for free, credit is nice ## If used for profit, contact me first ##################################### ## THE SUBROUTINES #6/14/2000 ## This is a sub that will get the data in a particular format ## from a preference file ## The format it is expecting is tab , and ## itself may be tab seperated. When this is run, ## it reads the preference file from the path passed in, ## and stores the values in a local hash which ## it returns. Or -1 if the file doesn't exist. ## IGNORES: lines beging with "#" or "whitespace" ## ARGS: Path and name of preference file ## ASSUMES: A global hash structure: GLBL_data_hash ## At this time I dont want to mess with passing back an array to ## become a hash, maybe later. -EH # Check 6-14 sub get_pref_data { # VARS PASSED my($prefs) = $_[0]; # Path to prefrence file # VARS not passed my($line); # Basically a temp var my($key); # The key for the hash my(@data); # The array for the hash my($temp); # a forache var. my(%LOCAL_data_hash); ## Check that prefs file exist and opens open(FILE, $prefs) || return(-1); ## get data and fill up varaibles ## GEt rid of comments while( $line= ){ # check if not a comment line if( $line=~m/^(\#|\s)/ ){next;} # End of if else{ # We have a data line (We hope) # clean up chomp($line); # Get the key and rest of data ($key, @data) = split("\t", $line); # Clean up data foreach $temp (@data) { # This removes whitespace at the begining # and end of an entry. Won't mess up numbers then. # But leavees whites space between words, numbers, etc. $temp=~ s/^\s*|\s*$//g; } # End of forach temp # Enter into GLBL_data_hash $LOCAL_data_hash{$key}= (join("\t", @data)); } # End of else statement } # End of while file return %LOCAL_data_hash; } # End of sub, get pref data # 6/5/2000 -EH # This is a sub that takes a string and an array as arguments, # Then searches through the array for the string returning true or false # ARGS1: A string that may or may not be in ARGS2 # ARGS2: An array that my or may not contain ARGS1 # RETURNS: 0 (False) or # (Index + 1) sub does_match { # 2 argument, a string, an array, my($string); # The string to look for my(@array); # The array to search through ($string, @array) = @_; # Getting passed in vars my($elem); # Foreach var. my($count) = 0; #Counter variable foreach $elem (@array) { if($string eq $elem) { return $count+1; } # End of if $count++; } # end of foreach return 0; } # end of sub # 3/22/2000 -EH #This sub will take a path to a directory and return an array of the files #Args1: A base path to the dir you want to get the list of files from ## NOTE: that the default is the current directory if no ARG is given #Returns: an array of dirs in the current dir sub make_list_of_files { my($path)= ""; # The variable for the argument my(@fil_list); # Keep a list of directories in this directory my($ls_input); # Current data from ls ## Testing if anything is given as argument if ( defined @_ ) { $path = $_[0]; } ## OPENING a ls process to read in the contents of a dir open( LS, "ls $path | sort|"); ## USING while to look at each entry and check if it is a dir while($ls_input = ) { chomp($ls_input); # testing if it is a file, if you want to check if it is a # symlink you should also use -l if( -f "$path$ls_input" ){ @fil_list = (@fil_list,"$ls_input"); }# end of if -d $_ }# End of while() #closing the input pipe of the ls command close(LS); return @fil_list; } # end of sub # 3/9/2000 -EH #This sub will take a path and a file name and return true of false # depending on if the file exists. Note that the path can include # the trailing slash or not, i.e. /home/ or /home # Also note this can be a absolute or relative path # ARG: A file path, a file name # Pleas pass in "." for the current directory # NOTE: The wildcard operator IS valid, i.e. *.rpm, min*.txt, etc. # RETURNS: boolean, checking for file existance sub file_check { my($file_path) = $_[0]; # The path to the file my($file_name) = $_[1]; # the file name itself my($whole_thing) = "$file_path/$file_name"; my(@files) = glob($whole_thing); if(defined($files[0]) && -e $files[0]) { return 1; }else { return 0; } } #End of sub # 3/8/00 -EH #This subroutine will take a path to a directory and then return a # list of directories in that directory. Note that if no argument # is given that is will simply use the current direcory. This will # ignore any files. #Args: A base path to the dir you want to get the list of dirs from ## NOTE: that the default is the current directory if no ARG is given #Returns: an array of dirs in the current dir sub make_list_of_dirs { my($path)= ""; # The variable for the argument my(@dir_list); # Keep a list of directories in this directory my($ls_input); # Current data from ls ## Testing if anything is given as argument if ( defined @_ ) { $path = $_[0]; } ## OPENING a ls process to read in the contents of a dir open( LS, "ls $path | sort|"); ## USING while to look at each entry and check if it is a dir while($ls_input = ) { chomp($ls_input); # testing if it is a dir, if you want to check if it is a # symlink you should also use -l if( -d "$path$ls_input" ){ @dir_list = (@dir_list,"$ls_input"); }# end of if -d $_ }# End of while() #closing the input pipe of the ls command close(LS); return @dir_list; } # end of sub #Args: An array of strings. #REturns: an array of pretty strings, no "_"'s and each word capped sub make_names_pretty { my(@dir_names) = @_; my($org_dir_name); #single name of dir at time my($new_dir_name); #single name of dir at time my($part); # single word of a dir name my(@new_names); #list of new 'good' names my(@name_parts); #list of names with _'s foreach $org_dir_name (@dir_names) { # Replacing "_" w/ " " @name_parts = split( /_/, $org_dir_name ); $org_dir_name = ""; # Capatilizing each word in the dir name (org_dir_name) foreach $part (@name_parts) { $part = ucfirst($part); # Appending the name_parts back to the variable $org_dir_name = $org_dir_name . $part ." "; } # End of foreach part # Adding the "Tests" ending to the string DONT NEED HERE #$org_dir_name = $org_dir_name . "Tests"; } # End of foreach org_dir_name return @dir_names; }# End of sub #Args: an array of strings. #REturns: an array of rpms in the current dir sub make_array_of_rpms { # 1 argument, an array my($the_input); #input form stdin open(IN, "ls *.rpm | sort|") || die "Couldn't open!\n- make_array_of_rpms\n- $!"; while($the_input = ){ $the_input =~ s/(.*?)\-[0-9].*/$1/; #print; @_ = (@_,"$the_input"); } close IN; return @_; } # end of sub ## ADD shelltrap and simplescript subs here!