'==========================================================
'
' Test Word example (TWORD.BAS)
' Copyright (c) 1998-2006 PowerBASIC, Inc.
' All Rights Reserved.
'
' Read a text file and count the number of words of length
' 1, 2, 3, and so on. This program contains intentional
' bugs. Use it in conjunction with the PowerBASIC Users
' Guide to learn the PowerBASIC integrated debugger.
'
'==========================================================
#IF NOT %DEF(%WINAPI)
DECLARE FUNCTION GetModuleFileName LIB "KERNEL32.DLL" _
ALIAS "GetModuleFileNameA" (BYVAL hModule AS DWORD, _
lpFileName AS ASCIIZ, BYVAL nSize AS LONG) AS LONG
#ENDIF
DEFLNG A-Z
FUNCTION AppPath() AS STRING
LOCAL p AS ASCIIZ * 256
LOCAL ix AS LONG
GetModuleFileName 0, p, SIZEOF(p)
FOR ix = LEN(p) TO 1 STEP -1
IF MID$(p, ix, 1) = "\" OR MID$(p, ix, 1) = "/" THEN
FUNCTION = LEFT$(p, ix)
EXIT FUNCTION
END IF
NEXT
FUNCTION = ""
END FUNCTION
FUNCTION PBMAIN() AS LONG
MaxWordLen = 16 'count words up to 16 chars
'longer words go into Overlong
DIM WordLength(MaxWordLen) 'the array used to hold counts
Blank$ = CHR$(32) 'a space marks end of a word
FilePath$ = AppPath
IF LEN(FilePath$) THEN
CHDRIVE FilePath$
CHDIR FilePath$
END IF
PRINT "Warning: This is a program intended for use in a"
PRINT "practice session of the PowerBASIC debugger."
PRINT "If you are NOT running this program in the "
PRINT "integrated debugging environment, press CTRL+BREAK"
PRINT "now!"
PRINT "See the Debugging chapter in the PowerBASIC Users"
PRINT "Guide for details."
WHILE InFile$ = ""
LINE INPUT "Enter the name of the input file: ";InFile$
IF InFile$ <= SPACE$(LEN(InFile$)) THEN InFile$=""
IF InFile$ = "" THEN
BEEP
PRINT "TWORD canceled!"
EXIT FUNCTION
END IF
WEND
OPEN InFile$ FOR INPUT AS #1
'If the file can't be opened, give user an error message.
IF ERR THEN
PRINT "Unable to open file "; InFile$
EXIT FUNCTION
END IF
WHILE NOT (EOF(1)) 'read file until nothing left
LINE INPUT #1,FirstString$ 'get a line
PRINT FirstString$ 'display it
WHILE FirstString$<>""
GOSUB GetAWord 'pull word for FirstString$
'put it in SecondString$
Test = LEN(SecondString$)
IF Test <= 16 THEN
WordLength(Test) = WordLength(Test) + 1
ELSE
Overlong = Overlong + 1
END IF
WEND
WEND
CLOSE 1
PRINT "Length Count"
FOR Count% = 1 TO 16
PRINT Count%, WordLength(Count%)
NEXT Count%
PRINT "Greater"; OverLong
WAITKEY$
EXIT FUNCTION
GetAWord:
position = INSTR(FirstString$, Blank$) 'a word is a
' sequence of characters ended by a blank or
' the end of the line
IF position = 0 THEN
'the word is the remainder of the line
SecondString$ = FirstString$
FirstString$ = ""
ELSE
'pull the word from the line
SecondString$ = LEFT$(FirstString$, position - 1)
END IF
RETURN
END FUNCTION