home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / dos / tools / aurora21 / countlin.aml < prev    next >
Text File  |  1995-08-10  |  5KB  |  154 lines

  1. /* ------------------------------------------------------------------ */
  2. /* Macro:        COUNTLIN.AML                                         */
  3. /* Written by:   nuText Systems                                       */
  4. /*                                                                    */
  5. /* Description:  This macro counts the total number of lines and      */
  6. /*               blank lines in files matching a user-specified       */
  7. /*               filespec, and displays them in a popup menu.         */
  8. /*               This macro can be useful for counting the total      */
  9. /*               lines of source code in a directory or filespec.     */
  10. /*                                                                    */
  11. /* Usage:        Select this macro from the Macro List (on the Macro  */
  12. /*               menu), or run it from the macro picklist <shift f12> */
  13. /*               Select the first line of the popup menu to edit the  */
  14. /*               contents of the menu.                                */
  15. /* ------------------------------------------------------------------ */
  16.  
  17.   // compile time macros and function definitions
  18.   include  bootpath "define.aml"
  19.  
  20.   // disable 'onloading' in EXT.AML while this macro is running
  21.   function onloading
  22.   end
  23.  
  24.   // function to show progress while counting lines
  25.   function  oncounting (filespec file status)
  26.  
  27.     // create the progress window
  28.     if not window? 'count' then
  29.  
  30.       createwindow 'count'
  31.       setwinobj
  32.       setframe ">b"
  33.       setcolor  border_color   color white on gray
  34.       setcolor  text_color     color black on gray
  35.       settitle "Counting lines in '" + filespec + "'" 'c'
  36.       setborder "1i"
  37.       setshadow 2 1
  38.  
  39.       // center the window
  40.       width = 70
  41.       height = 16
  42.       ox = (getvidcols - width) / 2
  43.       oy = (getvidrows - height) / 2
  44.       sizewindow ox oy ox + width oy + height "ad"
  45.  
  46.       // write header line
  47.       writestr "Total lines   Blank lines" (color black on gray) (getcoord 'x1') - 25
  48.  
  49.     // display line count for each file
  50.     elseif file then
  51.       writeline
  52.       writestr file
  53.       writestr status (color black on gray) (getcoord 'x1') - 21
  54.  
  55.     else
  56.       destroywindow
  57.     end
  58.  
  59.     display
  60.   end
  61.  
  62.  
  63.   // get filespec to count
  64.   filespec = ask "Enter filespec where lines should be counted"
  65.  
  66.   // <esc> was pressed
  67.   if not filespec then
  68.     return
  69.   end
  70.  
  71.   // <enter> was pressed
  72.   if filespec == ' ' then
  73.     filespec = "*.*"
  74.   end
  75.  
  76.   filespec = qualify filespec (getbufname)
  77.  
  78.   // initialize total lines and blank lines to zero
  79.   totallines = 0
  80.   totalblank = 0
  81.  
  82.   // create a buffer to hold the results and initialize the first line
  83.   resultbuf = createbuf
  84.   ovltext "≡≡≡≡≡≡ Select this line to edit line counts ≡≡≡≡≡≡"
  85.  
  86.   // get a directory listing
  87.   if (loadbuf filespec '' '' 'h') and getlinelen then
  88.  
  89.     // sort by name
  90.     sendobject "fmgr" "fsort" 'n'
  91.  
  92.     // create progress window
  93.     oncounting filespec
  94.  
  95.     // count lines for all files in the directory
  96.     repeat
  97.       file = sendobject "fmgr" "getffile"
  98.       if loadbuf file then
  99.         lines = getlines
  100.         totallines = totallines + lines
  101.         blanklines = find "^ *$" "x*a"
  102.         totalblank = totalblank + blanklines
  103.         status = (pad (thousands lines) 7) +
  104.                  (pad (thousands blanklines) 14)
  105.         oncounting filespec file status
  106.         addline (pad file 52 'l') + status '' '' resultbuf
  107.         destroybuf
  108.       end
  109.     until not down
  110.  
  111.     // destroy the directory
  112.     destroybuf
  113.  
  114.     // destroy the progress window
  115.     oncounting
  116.  
  117.     currbuf resultbuf
  118.     if getlines > 1 then
  119.  
  120.       // insert line count totals into the result buffer
  121.       insline '' '' 1
  122.       insline "Total lines:            " + (thousands totallines) '' 2
  123.       insline "Total non-blank lines:  " +
  124.               (thousands totallines - totalblank) '' 3
  125.       insline "Blank lines:            " +
  126.               (totalblank * 100) / totallines + '%'  '' 4
  127.       insline '' '' 5
  128.       insline "File" + (copystr " " 44) + "Total lines   Blank lines" '' 6
  129.       insline "----" + (copystr " " 44) + "-----------   -----------" '' 7
  130.  
  131.       // display the results in a popup menu
  132.       file = popup resultbuf "Line counts for '" + filespec + "'" 74
  133.       if file then
  134.  
  135.         // edit linecounts
  136.         if file [1] == '≡' then
  137.           delline 1 1 resultbuf
  138.           setbufname (qualify "LINES.TXT" (getbufname (getwinbuf))) resultbuf
  139.           openbuf resultbuf
  140.  
  141.         // open the selected file
  142.         elseif file [2] == ':' then
  143.           open file [1 : (pos ' ' file) - 1]
  144.         end
  145.       end
  146.     end
  147.  
  148.   else
  149.     destroybuf resultbuf
  150.     display
  151.     say filespec + " not found" 'b'
  152.   end
  153.  
  154.