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

  1. package Autodoc::ScanFile;
  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. use Autodoc::LogDebug;
  33.  
  34. @ISA      = qw(Exporter);
  35. @EXPORT   = qw(scan_line
  36.                scan_rescanline
  37.                scan_eoffound
  38.                scan_closefile
  39.                scan_openfile
  40.                scan_skipCStyleComments
  41.                scan_keepADCNewlines
  42.                scan_postprocess);
  43.  
  44. $module_version = '$Revision: 1.2 $';
  45. $module_version =~ s!(\$\w+: | \$)!!g;
  46. $module_id        = '$Id: ScanFile.pm,v 1.2 1995/10/20 22:16:29 aswift Exp $';
  47. $module_id      =~ s!(\$\w+: | \$)!!g;
  48. $module_name    = $module_id;
  49. $module_name    =~ s!^([^\,]+).*$!$1!;
  50.  
  51. ############################################################################
  52. # Purpose: Module that encapsulates scanning text out of files, specifically
  53. #          designed to scan entire autodoc style comments that span multiple
  54. #          lines.
  55. #
  56. # HISTORY: START
  57. # $Log: ScanFile.pm,v $
  58. # Revision 1.2  1995/10/20  22:16:29  aswift
  59. # Added DevMan style changes Log support
  60. #
  61. #
  62. # HISTORY: END
  63. ############################################################################
  64.  
  65. #############################################################################
  66. #
  67. # NAME:       module_version
  68. #
  69. # ACTION:     returns the version number of this module
  70. #
  71. # RETURN:     the module version
  72. #
  73. #############################################################################
  74. sub module_version
  75. {
  76.     return $module_version;
  77. }
  78.  
  79. sub module_versionstamp
  80. {
  81.     return "$module_name (rev-$module_version)";
  82. }
  83.  
  84. #############################################################################
  85. #
  86. # NAME:       scan_rescanline
  87. #
  88. # ACTION:     Sets a global variable so that scan_line will return the last
  89. #             line it returned - if no argument is passed, the current value
  90. #             will be returned
  91. #
  92. # GLOBALS:    $scan_repeatlast
  93. #
  94. # ARGUMENTS:  An argument may be passed to set $scan_skipCStyleComments
  95. #
  96. # RETURN:     1 if the next call to scan_line will return the previous line
  97. #             scanned, 0 if the next call will 
  98. #
  99. #############################################################################
  100. sub scan_rescanline
  101. {
  102.     $scan_repeatlast = 0 if (!defined($scan_repeatlast));
  103.     $scan_repeatlast = $_[0] if (scalar(@_) > 0);
  104.     return $scan_repeatlast;
  105. }
  106.  
  107.  
  108. #############################################################################
  109. #
  110. # NAME:       scan_eoffound
  111. #
  112. # ACTION:     Returns 1 if there is a file open and the end of file has
  113. #             been encountered, 0 otherwise
  114. #
  115. # GLOBALS:    $scan_eoffound - YES if the end of file has been detected
  116. #             $scan_fileopen - YES if there is a file open
  117. #
  118. # ARGUMENTS:  none
  119. #
  120. #############################################################################
  121. sub scan_eoffound
  122. {
  123.     if (defined($scan_fileopen) && ($scan_fileopen == 1)) {
  124.     $scan_eoffound = eof(SCANFILE);
  125.     return $scan_eoffound;
  126.     } else {
  127.     return 0;
  128.     }
  129. }
  130.  
  131.  
  132. #############################################################################
  133. #
  134. # NAME:       scan_closefile
  135. #
  136. # ACTION:     Closes the file currently open - if any.
  137. #
  138. # GLOBALS:    $scan_eoffound - YES if the end of file has been detected
  139. #             $scan_fileopen - YES if there is a file open
  140. #
  141. # ARGUMENTS:  none
  142. #
  143. #############################################################################
  144. sub scan_closefile
  145. {
  146.     if (defined ($scan_fileopen) && ($scan_fileopen == 1)) {
  147.     close (SCANFILE);
  148.     }
  149.     $scan_fileopen = 0;
  150.     $scan_eoffound = 0;
  151. }
  152.  
  153.  
  154. #############################################################################
  155. #
  156. # NAME:       scan_openfile
  157. #
  158. # ACTION:     Opens the file passed as the argument and sets the relevent 
  159. #             globals appropriatly
  160. #
  161. # GLOBALS:    $scan_eoffound - YES if the end of file has been detected
  162. #             $scan_fileopen - YES if there is a file open
  163. #
  164. # ARGUMENTS:  The path of the file to be opened
  165. #
  166. # RETURN:     Returns 0 if it fails, 1 if it succeeds
  167. #
  168. #############################################################################
  169. sub scan_openfile
  170. {
  171.     local ($filename);
  172.  
  173.     $filename = $_[0];
  174.     scan_closefile();
  175.  
  176.     if (!(open (SCANFILE, $filename))) {
  177.     warn ("Unable to open file: $filename\n");
  178.     return 0;
  179.     } else {
  180.     $scan_fileopen = 1;
  181.     dblog (1, "Scanning from file:\t$filename");
  182.     return 1;
  183.     }
  184. }
  185.  
  186.  
  187. #############################################################################
  188. #
  189. # NAME:       scan_skipCStyleComments
  190. #
  191. # ACTION:     Sets a global variable so that scan_line will keep or discard 
  192. #             c-style comments in its return - if no argument is passed, 
  193. #             the current value will be returned
  194. #
  195. # GLOBALS:    $scan_skipCStyleComments
  196. #
  197. # ARGUMENTS:  An argument may be passed to set $scan_skipCStyleComments
  198. #
  199. # RETURN:     1 if it is skipping c-style comments, 0 otherwise
  200. #
  201. #############################################################################
  202. sub scan_skipCStyleComments
  203. {
  204.     $scan_skipCStyleComments = 0 if (!defined($scan_skipCStyleComments));
  205.     $scan_skipCStyleComments = $_[0] if (scalar(@_) > 0);
  206.     return $scan_skipCStyleComments;
  207. }
  208.  
  209.  
  210. #############################################################################
  211. #
  212. # NAME:       scan_keepADCNewlines
  213. #
  214. # ACTION:     Sets a global variable so that scan_line will keep or discard
  215. #             newlines from autodoc comments - if no argument is passed, 
  216. #             the current value will be returned
  217. #
  218. # GLOBALS:    $scan_keepADCNewlines
  219. #
  220. # ARGUMENTS:  An argument may be passed to set $scan_keepADCNewlines
  221. #
  222. # RETURN:     1 if it is keeping newlines, 0 otherwise
  223. #
  224. #############################################################################
  225. sub scan_keepADCNewlines
  226. {
  227.     $scan_keepADCNewlines = 1 if (!defined($scan_keepADCNewlines));
  228.     $scan_keepADCNewlines = $_[0] if (scalar(@_) > 0);
  229.     return $scan_keepADCNewlines;
  230. }
  231.  
  232.  
  233. #############################################################################
  234. #
  235. # NAME:       scan_line
  236. #
  237. # ACTION:     Read and return a line from the passed file handle.  
  238. #             Ignore blank lines.  Always scan up to the AutoDoc comment 
  239. #             end if we encounter an AutoDoc comment start on a line.  
  240. #             If the $scan_keepADCnewlines is true, encode the newlines into 
  241. #             the string, since they affect paragraph formatting
  242. #             in the documentation.
  243. #
  244. #             If $scan_repeatlast is TRUE return the last line scanned in.  
  245. #             $scan_ok records whether the eof has been enountered
  246. #
  247. # GLOBALS     $scan_repeatlast - YES if the previous line should be returned
  248. #             $scan_lastline - the last line that was scanned by scan line
  249. #             $scan_fileopen - 1 if a scan file is open (SCANFILE is valid)
  250. #
  251. # ARGUMENTS:  none
  252. #
  253. # RETURN:     A string containing the last line scanned from the file
  254. #
  255. #############################################################################
  256. sub scan_line
  257. {
  258.     local ($in, $inputline, $findADCend, $findCSCend, $rescan);
  259.  
  260.     use Autodoc::LogDebug;
  261.  
  262.     # return the last line scanned if $scan_lastline is 1
  263.     if (scan_rescanline()) {
  264.     scan_rescanline(0);
  265.     if (defined($scan_lastline)) {
  266.         dblog (4, "Repeating last scan_line: [$scan_lastline]");
  267.         return $scan_lastline;    # return last line scanned
  268.     } else {
  269.         dblog (4, "Repeating last scan_line: --no last scan_line--");
  270.         return "";
  271.     }
  272.     }
  273.     $in          = "";
  274.     $inputline   = "";
  275.     $findADCend  = 0;
  276.     $rescan      = 0;
  277.  
  278.   SCANLINE:            
  279.     while (1) {
  280.  
  281.     if ($rescan == 0) {
  282.         # scan in the next line for processing
  283.         if ((!defined ($scan_fileopen)) || ($scan_fileopen == 0)) {
  284.         dblog (1, "No scan file open, scan_line failed.\n");
  285.         return "";        # return FALSE
  286.         }
  287.         if (scan_eoffound()) {
  288.         dblog (1, "EOF reached, scan_line couldn\'t read line.");
  289.         return "";        # return FALSE
  290.         }
  291.         $in = <SCANFILE>;
  292.         chop ($in) if ($in =~ m!\n$!);
  293.     } else {
  294.         # process whatever is in $in again
  295.         $rescan = 0;
  296.     }
  297.  
  298.     if (scan_skipCStyleComments()) {
  299.         if ($findCSCend) {
  300.         if ($in =~ m!\*+/!) {
  301.             $findCSCend = 0;
  302.             $in = $';    # continue processing with the remaining text
  303.         } else {
  304.             next SCANLINE; # scan a new line and look for the CSC end
  305.         }
  306.         }
  307.  
  308.         # ignore anything after a '//' found on a line
  309.         $in = $` if ($in =~ m!//!);
  310.  
  311.         if (($in =~ m!/\*!) && !($in =~ m!/\*+\"!)) {
  312.         # check for a single line comment
  313.         if ($in =~ m!/\*+[^\"].*\*+/!) {
  314.             $in = "$`$'";
  315.             $rescan = 1;
  316.             next SCANLINE; # make sure there aren't more comments
  317.         } else {
  318.             $inputline .= $`;
  319.             $findCSCend = 1;
  320.             next SCANLINE;
  321.         }
  322.         }
  323.     }
  324.  
  325.     # scan the whole AutoDoc style comment into the $inputline
  326.     if ($findADCend) {
  327.  
  328.         # kill any '*'s that appear on the beginning of the line 
  329.         $in = $' if ($in =~ m!^\s*\*+!);
  330.  
  331.         if (scan_keepADCNewlines() && !($in =~ m/\S+/)) {
  332.         $inputline .= "\\n"; # blank lines make newlines in docs
  333.         } else {
  334.         $inputline .= " ";
  335.         }
  336.         if (!($in =~ m!\*+/!)) {
  337.  
  338.         $inputline .= $in;
  339.         next SCANLINE;    # Keep looking...
  340.         } else {        # Found the ADC end
  341.         $findADCend = 0;
  342.         }
  343.     } else {
  344.         # read to end of autodoc comment if we are inside one
  345.         if (($in =~ m!/\*+\"!) && !($in =~ m!\*+/!)) {
  346.         $inputline .= $in;
  347.         $findADCend = 1;
  348.         next SCANLINE;
  349.         }
  350.     }
  351.  
  352.     $inputline .= $in;
  353.     if ($inputline =~ m/\S+/) {
  354.         last SCANLINE;    # found data on a line, so return it
  355.     }             
  356.     }
  357.     
  358.     $scan_lastline = &scan_postprocess($inputline);
  359.  
  360.     dblog (4, "scanned[$scan_lastline]");
  361.     return $scan_lastline;        # return new $scan_lastline
  362. }
  363.  
  364.  
  365. #############################################################################
  366. #
  367. # NAME:       scan_postprocess
  368. #
  369. # ACTION:     Removes whitespace from the beginning and end of the string, 
  370. #             and compresses all whitespace runs into a single space
  371. #
  372. # ARGUMENTS:  The string to be processed
  373. #
  374. # RETURN:     Returns the processed version of the line
  375. #
  376. #############################################################################
  377. sub scan_postprocess
  378. {
  379.     local ($line);
  380.  
  381.     $line = $_[0];
  382.  
  383.     $line =~ s!\s*$!!;        # remove whitespace from the end of the line
  384.     $line =~ s!^\s*!!;            # and the beginning of the line
  385.     $line =~ s/\s+/ /g;        # turn all whitespace into a single space
  386.  
  387.     return $line
  388. }
  389.  
  390. 1;
  391.     
  392.