home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / lines.ex < prev    next >
Text File  |  1994-01-21  |  2KB  |  63 lines

  1. -- lines.e
  2. -- show how many lines and characters there are in a file or bunch of files
  3. -- usage: lines.bat files...
  4. -- example: lines *.e
  5.  
  6. constant SCREEN = 1
  7. constant TRUE = 1
  8.  
  9. function scan(integer fileNum) 
  10. -- count lines, non-blank lines, characters
  11.     object line
  12.     integer lines, nb_lines, chars
  13.  
  14.     lines = 0
  15.     nb_lines = 0
  16.     chars = 0
  17.     while TRUE do
  18.     line = gets(fileNum)
  19.     if atom(line) then
  20.         return {nb_lines, lines, chars} -- end of file
  21.     else
  22.         lines = lines + 1
  23.         chars = chars + length(line) + 1 -- add 1 to match dir cmd
  24.         if find(TRUE, line != ' ' and line != '\t' and line != '\n') then
  25.         nb_lines = nb_lines + 1
  26.         end if
  27.     end if
  28.     end while
  29. end function
  30.  
  31. include getnames.e
  32.  
  33. procedure lines()
  34. -- process all files 
  35.     integer fileNum
  36.     sequence count, total_count
  37.     sequence file_names
  38.  
  39.     -- gather file names
  40.     file_names = get_names()
  41.     if length(file_names) = 0 then
  42.     return
  43.     end if
  44.     -- process all files    
  45.     total_count = {0, 0, 0}
  46.     puts(SCREEN, "non-blank-lines   lines   chars\n")
  47.     for i = 1 to length(file_names) do
  48.         fileNum = open(file_names[i], "r")   
  49.         if fileNum = -1 then
  50.             printf(SCREEN, "cannot open %s\n", {file_names[i]})
  51.         else
  52.         count = scan(fileNum)
  53.         total_count = total_count + count
  54.         printf(SCREEN, "       %8d%8d%8d %s\n", count & {file_names[i]})
  55.         close(fileNum)
  56.         end if
  57.     end for
  58.     printf(SCREEN, "       %8d%8d%8d total\n", total_count)
  59. end procedure
  60.  
  61. lines()
  62.  
  63.