'=============================================================================
'
' Test Word (Debugging) example for PowerBASIC for Windows
' Copyright (c) 1998-2011 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 On-line help (PBWIN.CHM - "Debugging PowerBASIC Programs") to
' learn about the PowerBasic integrated debugger.
'
'=============================================================================
#COMPILER PBWIN 10
#COMPILE EXE
#IF NOT %DEF(%WINAPI)
DECLARE FUNCTION GetModuleFileName LIB "KERNEL32.DLL" ALIAS "GetModuleFileNameA" (BYVAL hModule AS LONG, lpFileName AS ASCIIZ, BYVAL nSize AS LONG) AS LONG
%MB_YESNO = &H00000004&
%IDNO = 7
#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 a length of 16 characters
' longer words will go into Overlong
DIM WordLength(MaxWordLen) ' the array used to hold the counts
Blank$ = CHR$(32) ' a space marks the end of a word.
FilePath$ = AppPath
IF LEN(FilePath$) THEN
CHDRIVE FilePath$
CHDIR FilePath$
END IF
WHILE InFile$ = ""
InFile$ = INPUTBOX$("Enter the name of the input file: ")
IF InFile$ <= SPACE$(LEN(InFile$)) THEN InFile$=""
IF InFile$ = "" _
AND MSGBOX ("No file name entered! Do you want to try again?", _
%MB_YESNO, _
"TWord input") = %IDNO THEN
EXIT FUNCTION
END IF
WEND
ERRCLEAR
OPEN InFile$ FOR INPUT AS #1
'If the file can't be opened, give the user an error message.
IF ERR THEN
MSGBOX InFile$,,"Unable to open file"
EXIT FUNCTION
END IF
WHILE NOT(EOF(1)) ' read the file until nothing is left
LINE INPUT #1,FirstString$ ' get a line
MSGBOX FirstString$ ' display it
WHILE FirstString$ <> ""
GOSUB GetAWord ' pull a word for FirstString$ and
' 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
MSGBOX "Length Count"
FOR Count% = 1 TO 16
MSGBOX FORMAT$(Count%) + STR$(WordLength(Count%))
NEXT
MSGBOX "Greater" + STR$(OverLong)
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