home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 283 / Shell5SourceCode.sis / wc (.txt) < prev   
Encoding:
EPOC OPL Source  |  1998-08-30  |  3.5 KB  |  114 lines

  1.  
  2.  
  3. Rem   wc, an addon command for Shell5
  4. Rem   Display the number of lines, words and character in a file
  5. Rem
  6. Rem   Copyright (C) 1998  Nick Murray
  7. Rem
  8. Rem   This program is free software; you can redistribute it and/or
  9. Rem   modify it under the terms of the GNU General Public License
  10. Rem   as published by the Free Software Foundation; either version 2
  11. Rem   of the License, or (at your option) any later version.
  12. Rem
  13. Rem   This program is distributed in the hope that it will be useful,
  14. Rem   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. Rem   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. Rem   See the GNU General Public License for more details.
  17. Rem
  18. Rem   You should have received a copy of the GNU General Public License
  19. Rem   along with this program; if not, write to the Free Software Foundation,
  20. Rem   Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21. Rem
  22. PROC wc%:(n%)
  23. LOCAL i%,ret%,d$(255),handle%,txt$(255),p%
  24. LOCAL lines&,words&,chars&,size&
  25.     ONERR ErrTrap::
  26.     IF n%<2 AND _in%=0    Rem no args and no redirected input
  27.         PRINT "Usage: wc <filename>"
  28.         RETURN
  29.     ENDIF
  30.     i%=2        Rem argv&(2) is the FIRST argument after the comman d
  31.     IF _in%                Rem input is redirected, so set the file
  32.         handle%=_in%    Rem handle to SHin% and jump over the
  33.         GOTO loop::        Rem file handling routines.
  34.     ENDIF
  35.     WHILE i%<=n%        Rem whilst we have more arguments
  36.         Rem parse the i% th argument to check if it's a valid file
  37.         ret%=Fparse%:(ADDR(d$),PEEK$(argv&(i%)))
  38.         IF ret%<0                    Rem error opening this file
  39.             _Err:(i%,ret%)
  40.         ELSEIF ret% AND 16    Rem this is a directory
  41.             _Err:(i%,3)
  42.         ELSE
  43.             ret%=IOOPEN(handle%,d$,$0600)        Rem random access opening first
  44.             IF ret%
  45.                 _Err:(i%,ret%)
  46.             ENDIF
  47.             IOSEEK(handle%,2,size&)                                Rem get size
  48.             IOCLOSE(handle%)
  49.             handle%=0
  50.             REM open=$0000, text=$0020, share=$0400
  51.             ret%=IOOPEN(handle%,d$,$0420)
  52.             IF ret%
  53.                 _Err:(i%,ret%)
  54.             ELSE
  55.                 lines&=0            Rem initialize counters
  56.                 words&=0
  57.                 chars&=0
  58. loop::        WHILE 1
  59.                     IOYIELD
  60.                     IF _stat%<>-46
  61.                         IF _key%(1)=27
  62.                             GOTO Quit::
  63.                         ELSE
  64.                             KEYA(_stat%,_key%())
  65.                         ENDIF
  66.                     ENDIF
  67.                     Rem read the next line of input, up to 254 chars
  68.                     ret%=IOREAD(handle%,ADDR(txt$)+1,255)
  69.                     IF ret%<0                    Rem an error of some sort
  70.                         IF ret%<>-36        Rem -36 merely indicates EOF
  71.                             _Err:(i%,ret%)    Rem another error, display
  72.                         ENDIF
  73.                         GOTO Endfile::
  74.                     ENDIF
  75.                     POKEB ADDR(txt$),ret%        Rem set length of input
  76.                     chars&=chars&+ret%+2        Rem + 0D + 0A
  77.                     lines&=lines&+1
  78.                     WHILE LEN(txt$)
  79.                         p%=LOC(txt$," ")
  80.                         IF p%>1    Rem not blank first
  81.                             words&=words&+1
  82.                         ELSEIF p%=0 AND LEN(txt$)
  83.                             words&=words&+1
  84.                             BREAK
  85.                         ENDIF
  86.                         txt$=RIGHT$(txt$,LEN(txt$)-p%)
  87.                     ENDWH
  88.                 ENDWH
  89. EndFile::    txt$=num$(lines&,10)+" line(s), "+NUM$(words&,10)+" word(s), "
  90.                 IF handle%<>_in%                Rem only close input file
  91.                     ret%=IOCLOSE(handle%)        Rem if it's not SHin%
  92.                     txt$=txt$+NUM$(size&,10)+" chars  "+PrPATH$:(d$)
  93.                 ELSE
  94.                     txt$=txt$+NUM$(chars&,10)+" chars"
  95.                 ENDIF
  96.                 fprint%:(txt$)        Rem use fprint% so output can be
  97.             ENDIF                        Rem redirected
  98.         ENDIF
  99.         i%=i%+1
  100.     ENDWH
  101.     RETURN
  102. ErrTrap::
  103.     ONERR off
  104.     PRINT err$:(ERR)
  105. quit::    Rem called on escape key, also on an error
  106.     IF handle% AND handle%<>_in%
  107.         IOCLOSE(handle%)
  108.     ENDIF
  109.     RETURN
  110. ENDP
  111.  
  112.  
  113.  
  114.