dropping debugging again
[feisty_meow.git] / scripts / files / filename_helper.pl
index 9b8668c2f31070f8d1dbdf63ffc5a8ed63944572..3be75c780d307c2a4f2fb9aa01cf6a941a10a354 100644 (file)
@@ -66,23 +66,30 @@ sub glob_list {
     local(@files_found) = readdir(WHERE);
     closedir WHERE;
     foreach $possible_name (@files_found) {
-      # zoom through the list and see if we need to add it to the ones
-      # matching the passed in patterns.
-#      if ( ($possible_name eq ".") || ($possible_name eq "..") ) { 
-#        # skip the directory entries.
-#        print "skipping dir entries\n";
-#        next;
-#      }
-      # we need to process this a bit; directory patterns are different.
+      # we need to process the pattern a bit; directory patterns are different
+      # from perl regular expression patterns, so we end up massaging any "ls"
+      # wildcards into an equivalent perl-style one below.
       local($match) = $chopped_filename[1];
+#hmmm: would be nice to combine the replacements into a long batch instead of separate commands, but i do not seem to know how to do that yet in perl.
       $match =~ s/\./\\./g;  # replace periods with escaped ones.
       $match =~ s/\*/.*/g;  # replace asterisks with dot star.
       $match =~ s/\+/\\+/g;  # escape plusses.
+      $match =~ s/\?/\\?/g;  # escape question marks.
+      $match =~ s/\|/\\|/g;  # escape pipe char.
+      $match =~ s/\$/\\\$/g;  # escape dollar sign.
+      $match =~ s/\[/\\[/g;  # escape open bracket.
+      $match =~ s/\]/\\]/g;  # escape close bracket.
+      $match =~ s/\(/\\(/g;  # escape open quote.
+      $match =~ s/\)/\\)/g;  # escape close quote.
+      $match =~ s/\{/\\{/g;  # escape open curly bracket.
+      $match =~ s/\}/\\}/g;  # escape close curly bracket.
+
       $match = "^" . $match . "\$";  # make sure the whole thing matches.
-#print "possibname is $possible_name\n";
+#print "possibname is '$possible_name':\n";
       if ($possible_name =~ /$match/) {
         # this one matches so add it.
         push @to_return, $chopped_filename[0] . $possible_name;
+#print "a match on: $chopped_filename\n";
       }
     }
   }
@@ -325,7 +332,7 @@ sub important_filename {
   # at the front, we will match only the whole string.  double slashes are
   # used before periods to ensure we match a real period character.
   local(@junk_files) = ("~", "^\\.#.*", "^\\._.*", "\\.aps", "\\.bak",
-      "^binaries",
+      "^binaries", "^bin.ant", "^bin.eclipse",
       "\\.clw", "^cpdiff_tmp\\.txt", "^\\.ds_store", "^diffs\\.txt",
       "^diff_tmp\\.txt", "\\.dsp", "\\.dsw", "\\.gid", "gmon\\.out", "\\.isr",
       "^isconfig\\.ini", "\\.log", "^manifest.txt", "^obj",
@@ -333,7 +340,7 @@ sub important_filename {
       "\\.sbr", ".*scc", "^Setup\\.dbg", "^Setup\\.inx",
       "^Setup\\.map", "^Setup\\.obs", "^Selenium_.*Login.html",
       "\\.stackdump", "^string1033\\.txt", "\\.suo", "\\.swp",
-      "^thumbs.db", "\\.tmp", "^trans\\.tbl", "\\.user", "_version\\.h",
+      "^thumbs.db", "[a-zA-Z0-9]\\.tmp", "^trans\\.tbl", "\\.user", "_version\\.h",
       "_version\\.rc", "^waste", "\\.ws4", "\\.wsm");
 
   foreach $temp (@junk_files) {
@@ -394,19 +401,97 @@ sub upper {
 # recursively deletes a directory that is passed as the single parameter.
 # from http://developer.novell.com/wiki/index.php/Recursive_Directory_Remove
 sub recursive_delete {
-  my $dir = shift;
-  local *DIR;
-
-  opendir DIR, $dir or die "opendir $dir: $!";
-  my $found = 0;
-  while ($_ = readdir DIR) {
-    next if /^\.{1,2}$/;
-    my $path = "$dir/$_";
-    unlink $path if -f $path;
-    recursive_delete($path) if -d $path;
+  my $dir;
+  foreach $dir (@_) {
+    if ( -f "$dir" ) {
+print "this is not a dir: $dir\nshould whack it here?\n";
+return;
+    }
+
+    local *DIR;
+    # if we can't open the dir, just skip to the next one.
+    opendir DIR, $dir or next;
+    while ($_ = readdir DIR) {
+      next if /^\.{1,2}$/;
+      my $path = "$dir/$_";
+      unlink $path if -f $path;
+      recursive_delete($path) if -d $path;
+    }
+    closedir DIR;
+    rmdir $dir or print "error - $!";
+  }
+}
+
+############################################################################
+
+# finds any directories under the arguments, which can be a list of directories.
+sub find_directories {
+  my @dirs_found = ();
+  my $dir;
+  foreach $dir (@_) {
+    local *DIR;
+    # if we can't open the dir, just skip to the next one.
+    opendir DIR, $dir or next;
+    while ($_ = readdir DIR) {
+      # skip if it's current or parent dir.
+      next if /^\.{1,2}$/;
+      my $path = "$dir/$_";
+      # skip if this entry is not itself a directory.
+      next if ! -d $path;
+      push @dirs_found, $path;
+    }
+    closedir DIR;
+  }
+  return @dirs_found;
+}
+
+############################################################################
+
+# given a list of paths, this returns an array of all the filenames found therein.
+sub find_files {
+  my @files_found = ();
+  my $dir;
+  foreach $dir (@_) {
+    if (-f $dir) {
+      # that's actually just a file, so add it.
+      push @files_found, $dir;
+      next;
+    }
+    local *DIR;
+    # if we can't open the dir, just skip to the next one.
+    opendir DIR, $dir or next;
+    while ($_ = readdir DIR) {
+      # skip if it's current or parent dir.
+      next if /^\.{1,2}$/;
+      my $path = "$dir/$_";
+      # skip if this entry is not a file.
+      next if ! -f $path;
+      push @files_found, $path;
+    }
+    closedir DIR;
   }
-  closedir DIR;
-  rmdir $dir or print "error - $!";
+  return @files_found;
+}
+
+############################################################################
+
+# finds all directories starting at a particular directory and returns them
+# in an array.  does not include the starting directory.
+sub recursive_find_directories {
+  # first find all the directories within the parameters.
+  my @toplevel = find_directories(@_);
+
+  my @to_return;
+  push(@to_return, @toplevel);
+
+  # return the composition of the list we found here plus any directories under those.
+  # we only recurse if there's something to chew on in our directory list.
+  # otherwise, we've hit the bottom of that tree.
+  if (scalar @toplevel > 0) {
+    my @subs_found = recursive_find_directories(@toplevel);
+    push(@to_return, @subs_found);
+  }
+  return @to_return;
 }
 
 ############################################################################