challenge. Found an interesting situation, with comments after extended
lines.: (most examples I've seen so far will fail on this one)
Code:
FUNCTION OpenFileDialog (BYVAL hWnd AS LONG, _ ' parent window BYVAL Caption AS STRING, _ ' caption Filespec AS STRING, _ ' filename BYVAL InitialDir AS STRING, _ ' start directory BYVAL Filter AS STRING, _ ' filename filter BYVAL DefExtension AS STRING, _ ' default extension Flags AS DWORD _ ' flags ) AS LONG
Code:
'¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ ' Sample of Sub/Function lister for PB code. Parses given file and ' creates a "DECLARE" style result, for easy copy and paste into your ' code. Should handle all aspects, including trailing comments in ' lines, "broken" with an underscore ("_") character. Can be optimized ' for better speed, but pretty fast as it is, so.. ' ' Public Domain, March 2001 by Borje Hagsten ' ..but as always, use at your own responsibility.. [img]http://www.powerbasic.com/support/forums/smile.gif[/img] '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ #COMPILE EXE #INCLUDE "WIN32API.INC" %ID_TEXT = 20 %ID_TEXT2 = 21 GLOBAL TheArray() AS STRING, SubStr() AS STRING, SubCount AS LONG DECLARE CALLBACK FUNCTION DlgProc() AS LONG DECLARE FUNCTION FileToArray(BYVAL TheFile AS STRING) AS LONG DECLARE SUB GetSubs '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ ' Create dialog and controls, etc '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ FUNCTION PBMAIN () AS LONG LOCAL hDlg AS LONG, File AS STRING DIALOG NEW 0, "Sub/Function lister",,, 300, 200, %WS_SYSMENU, 0 TO hDlg CONTROL ADD BUTTON, hDlg, 10, "&Start", 190, 166, 50, 14 CONTROL ADD BUTTON, hDlg, 11, "E&xit", 242, 166, 50, 14 CONTROL ADD TEXTBOX, hDlg, %ID_TEXT, "", 4, 4, 288, 152, _ %WS_CHILD OR %ES_MULTILINE OR %ES_WANTRETURN OR %ES_NOHIDESEL OR _ %WS_HSCROLL OR %ES_AUTOHSCROLL OR %WS_VSCROLL OR %ES_AUTOVSCROLL, _ %WS_EX_CLIENTEDGE CONTROL ADD LABEL, hDlg, -1, "File to scan:", 4, 158, 50, 10 File = "c:\pbdll60\samples\pbnote\pbnote.bas" '<- NOTE - change to proper path.. CONTROL ADD TEXTBOX, hDlg, %ID_TEXT2, File, 4, 168, 178, 12, _ %WS_CHILD OR %ES_AUTOHSCROLL, %WS_EX_CLIENTEDGE DIALOG SHOW MODAL hDlg CALL DlgProc END FUNCTION '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ ' Main callback '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ CALLBACK FUNCTION DlgProc() AS LONG LOCAL I AS LONG, txt AS STRING SELECT CASE CBMSG CASE %WM_COMMAND IF CBCTL = 10 THEN CONTROL GET TEXT CBHNDL, %ID_TEXT2 TO txt IF LEN(txt) = 0 THEN EXIT FUNCTION 'if no length, leave IF (GETATTR(txt) AND %ATTR_DIR) THEN EXIT FUNCTION 'if directory, leave IF DIR$(txt, 55) <> "" THEN 'if file exist, work FileToArray txt 'load file into array GetSubs 'parse out subs/functions txt = "" ARRAY SORT SubStr(), COLLATE UCASE 'sort result FOR I = 0 TO UBOUND(SubStr) txt = txt & "DECLARE " & SubStr(I) & $CRLF 'create declaration string NEXT CONTROL SET TEXT CBHNDL, %ID_TEXT, txt 'show result END IF BEEP ELSEIF CBCTL = 11 THEN DIALOG END CBHNDL END IF END SELECT END FUNCTION '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ ' Load a text file into an array '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ FUNCTION FileToArray(BYVAL TheFile AS STRING) AS LONG LOCAL c AS LONG, NumRecs AS LONG, Buf AS STRING OPEN TheFile FOR BINARY AS #1 GET$ #1, LOF(1), Buf CLOSE #1 Numrecs = TALLY(Buf, $CRLF) '<- line feed count IF Numrecs = 0 THEN EXIT FUNCTION REDIM TheArray(Numrecs) '<- reset arrays REDIM SubStr(0) : SubCount = 0 OPEN TheFile FOR INPUT AS #1 LEN = 32767 FOR c = 0 TO Numrecs - 1 LINE INPUT #1, TheArray(c) '<- read line by line into array NEXT CLOSE #1 FUNCTION = c - 1 '<- if we ever should need it.. END FUNCTION '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ ' Get subs and functions from an array, into another array '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ SUB GetSubs LOCAL I AS LONG, J AS LONG, K AS LONG, sFlag AS LONG, txt AS STRING FOR I = 0 TO UBOUND(TheArray) TheArray(I) = TRIM$(TheArray(I), CHR$(9)) 'remove eventual tabs K = INSTR(TheArray(I), "'") IF K THEN 'if trailing comment txt = LEFT$(TheArray(I), K - 1) 'only use text up to comment txt = TRIM$(txt) 'trim away leading/ending spaces ELSE txt = TRIM$(TheArray(I)) 'trim away leading/ending spaces END IF IF sFlag = 0 THEN 'if we are looking for start IF LEFT$(UCASE$(txt), 9) = "FUNCTION " THEN 'if start is found sFlag = 2 : J = I 'set flag and store I in J ELSEIF LEFT$(UCASE$(txt), 4) = "SUB " THEN sFlag = 1 : J = I ELSEIF LEFT$(UCASE$(txt), 18) = "CALLBACK FUNCTION " THEN sFlag = 2 : J = I END IF ELSE SELECT CASE sFlag 'what kind of end? CASE 1 IF LEFT$(UCASE$(txt), 7) = "END SUB" THEN 'proper end is found REDIM PRESERVE SubStr(SubCount) 'redim target GOSUB GetProperString 'get properly formatted compare string DO WHILE RIGHT$(txt, 1) = "_" 'enter loop if.. SubStr(SubCount) = SubStr(SubCount) + TheArray(J) + $CRLF + SPACE$(8) 'add some for line feed, etc.. INCR J 'next line GOSUB GetProperString 'get properly formatted compare string LOOP SubStr(SubCount) = SubStr(SubCount) + TheArray(J) 'get stored element, J INCR SubCount 'increase counter sFlag = 0 'reset flag and search for next END IF CASE 2 IF LEFT$(UCASE$(txt), 12) = "END FUNCTION" THEN 'same as above, but for functions REDIM PRESERVE SubStr(SubCount) GOSUB GetProperString DO WHILE RIGHT$(txt, 1) = "_" SubStr(SubCount) = SubStr(SubCount) + TheArray(J) + $CRLF + SPACE$(8) INCR J GOSUB GetProperString LOOP SubStr(SubCount) = SubStr(SubCount) + TheArray(J) INCR SubCount sFlag = 0 END IF END SELECT END IF NEXT EXIT SUB GetProperString : K = INSTR(TheArray(J), "'") IF K THEN 'if trailing comment txt = LEFT$(TheArray(J), K - 1) 'only use text up to comment txt = TRIM$(txt) 'trim away leading/ending spaces ELSE txt = TRIM$(TheArray(J)) 'trim away leading/ending spaces END IF RETURN END SUB
------------------
Leave a comment: