home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Examples / AutoDoc / lib / perl5 / Autodoc / FileSupport.pm < prev    next >
Encoding:
Perl POD Document  |  1995-11-02  |  4.8 KB  |  174 lines

  1. package Autodoc::FileSupport;
  2.  
  3. ###############################################################################
  4. ###############################################################################
  5. ##
  6. ##    Written by Adam Swift (c) 1995 by Friday Software and Consulting
  7. ##                           All rights reserved.
  8. ##
  9. ##      This notice may not be removed from this source code.
  10. ##
  11. ##    This program is included in the MiscKit by permission from the author
  12. ##    and its use is governed by the MiscKit license, found in the file
  13. ##    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  14. ##    for a list of all applicable permissions and restrictions.
  15. ##
  16. ##    Because AutoDoc is licensed free of charge, there is no warranty 
  17. ##    for the program.  Copyright holder, Friday Software and Consulting, 
  18. ##    is providing this program "as is" and this program is distributed in 
  19. ##    the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
  20. ##    even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
  21. ##    PARTICULAR PURPOSE.
  22. ##
  23. ###############################################################################
  24. ###############################################################################
  25.  
  26. require 5.000;
  27.  
  28. ##########################
  29. # load required packages #
  30. ##########################
  31. use Exporter;
  32.  
  33. @ISA      = qw(Exporter);
  34. @EXPORT   = qw(file_expandpath
  35.            file_findnewest);
  36.  
  37. ###########################
  38. # Set version information #
  39. ###########################
  40. $module_version = '$Revision: 1.3 $';
  41. $module_version =~ s!(\$\w+: | \$)!!g;
  42. $module_id    = '$Id: FileSupport.pm,v 1.3 1995/10/20 22:16:26 aswift Exp $';
  43. $module_id      =~ s!(\$\w+: | \$)!!g;
  44. $module_name    = $module_id;
  45. $module_name    =~ s!^([^\,]+).*$!$1!;
  46.  
  47.  
  48. ############################################################################
  49. # Purpose: Module that encapsulates some file actions and tests
  50. #
  51. # HISTORY: START
  52. # $Log: FileSupport.pm,v $
  53. # Revision 1.3  1995/10/20  22:16:26  aswift
  54. # Added DevMan style changes Log support
  55. #
  56. #
  57. # HISTORY: END
  58. ############################################################################
  59.  
  60.  
  61.  
  62. #############################################################################
  63. #
  64. # NAME:       module_version
  65. #
  66. # ACTION:     returns the version number of this module
  67. #
  68. # RETURN:     the module version
  69. #
  70. #############################################################################
  71. sub module_version
  72. {
  73.     return $module_version;
  74. }
  75.  
  76. sub module_versionstamp
  77. {
  78.     return "$module_name (rev-$module_version)";
  79. }
  80.  
  81. #############################################################################
  82. #
  83. # NAME:       expand_filepath
  84. #
  85. # ACTION:     Replace the file path '~[user]' '.' or '..' references 
  86. #             with an expanded file path starting with '/'
  87. #
  88. # ARGUMENTS:  A file path
  89. #
  90. # RETURN:     The expanded file path
  91. #
  92. #############################################################################
  93. sub file_expandpath
  94.   {
  95.     local ($filepath, $expanddir);
  96.     $filepath = $_[0];
  97.     
  98.     # If the path starts with '/' no expansion is needed.
  99.     return $filepath if ($filepath =~ m!^/!);
  100.     
  101.     # If the path starts with '~' then user HOME from the user ENV
  102.     if (($filepath =~ m!^~$!) || ($filepath =~ m!^~/!)) {
  103.       $filepath =~ s!^~!$ENV{'HOME'}!;
  104.       return $filepath;
  105.     }
  106.     
  107.     
  108.     # Otherwise, get lazy and make the filesystem to expand it for us
  109.     if ($filepath =~ m!^([^/]+)!) {
  110.       $expanddir = $1;
  111.       
  112.       # Check to see if the path is simply a file name (no directories)
  113.       # if so, add a './' and expand from there.
  114.       if (($expanddir eq $filepath) && !(-d $expanddir)) {
  115.         $filepath = "./$filepath";
  116.         $expanddir = ".";
  117.       }
  118.       
  119.       if ($expanddir ne "") {
  120.         open (EXPDIR, "cd $expanddir; pwd |");
  121.         $expanddir = <EXPDIR>;
  122.         close (EXPDIR);
  123.         chop $expanddir if ($expanddir =~ m!\n$!);
  124.         
  125.         # if it is a directory, replace it in $filepath
  126.         if (($expanddir ne "") && (-d $expanddir)) {
  127.           $filepath =~ s!^[^/]+!$expanddir!;
  128.         }
  129.       }
  130.     }
  131.     
  132.     return $filepath;
  133.   }
  134.  
  135.  
  136. #############################################################################
  137. #
  138. # NAME:       file_findnewest
  139. #
  140. # ACTION:     Finds the newest file of all files passed as arguments and 
  141. #             returns it (the one with the latest time stamp).
  142. #
  143. # ARGUMENTS:  Any number of file paths
  144. #
  145. # RETURN:     The newest file's path
  146. #
  147. #############################################################################
  148. sub file_findnewest
  149.   {
  150.     local ($newest_file, $newest_age, $file, $fileage);
  151.     
  152.     $newest_file = "";
  153.     $newest_age = 0;
  154.     
  155.     foreach $file (@_) {
  156.       if (-r $file) {
  157.         $fileage = -M $file;
  158.         # print "file $file is age $fileage\n";
  159.         if (($newest_age == 0) || ($fileage < $newest_age)) {
  160.           $newest_file = $file;
  161.           $newest_age  = $fileage;
  162.         }
  163.       }
  164.     }
  165.     # print "latest file = $newest_file\n";
  166.  
  167.     return $newest_file;
  168.   }
  169.  
  170.  
  171. 1;
  172.     
  173.