home *** CD-ROM | disk | FTP | other *** search
Wrap
'******************************* FXSTATIC.MST ********************************* 'Demonstrates: This sample will search through an MSTest script and insure that ' the STATIC keyword appears at the end of each SUB or FUNCTION, ' and that INPUT statements are using the LINE INPUT syntax. ' ' The above syntax requirements will ensure maximum compatibility ' with future versions of MSTest and other Microsoft BASIC ' BASIC products. ' 'Required Files: COMMDLG.DLL, MSTEST.INC ' 'Uses: ' 'Complexity Level: INTERMEDIATE ' 'Notes: Uses the COMMDLG FileOpen Dialog to get filename(s) from user of ' files to convert. Adds "STATIC" keyword to all SUB and FUNCTIONs => ' STATIC SUB FOO. Adds "LINE" to all INPUT # statements => LINE INPUT # ' '****************************************************************************** '****************************************************************************** ' INCLUDE SECTION - WINCMDLG.INC is provided as a sample include file, and ' should have been installed in the INCLUDE sub-directory. '****************************************************************************** '$INCLUDE: 'MSTest.INC' '$INCLUDE: 'WINCMDLG.INC' '****************************************************************************** ' CONSTANTS '****************************************************************************** Dim NIL As String NIL = Chr$(0) '****************************************************************************** ' Parts needed from WINUSER.INC '****************************************************************************** ' Dialog Box Command IDs Const IDYES = 6 Const IDNO = 7 ' MessageBox() Flags Const MB_YESNO = &H4 Const MB_ICONQUESTION = &H20 Const MB_TASKMODAL = &H2000 Declare Function MessageBox Lib "User" ( hWnd%, lpText$, lpCaption$, wType%) As Integer Declare Sub MessageBeep Lib "User" ( wType%) '****************************************************************************** ' SUB and FUNCTION Declarations... '****************************************************************************** Declare SUB AddKeywords (MSTFile AS STRING) '****************************************************************************** ' CREATE Buffers for FileOpen structure '****************************************************************************** Dim OFN As tagOFN Allocate OFN.lpstrFilter, 1 Allocate OFN.lpstrCustomFilter, 1 Allocate OFN.lpstrFile, 1 Allocate OFN.lpstrFileTitle, 1 Allocate OFN.lpstrInitialDir, 1 Allocate OFN.lpstrTitle, 1 Allocate OFN.lpstrDefExt, 1 Allocate OFN.lpTemplateName, 1 '****************************************************************************** ' MAIN PROGRAM '****************************************************************************** ViewPort OFF ViewPort Clear ViewPort ON '* Fill in structure for Common Dialog OFN.lStructSize = OFN_StructSize OFN.hWndOwner = 0 OFN.hInstance = 0 OFN.lpstrFilter[0] = "Script Files" + NIL + "*.MST" + NIL + "Include files" + NIL + "*.INC" + NIL + "Script AND Include Files" + NIL + "*.MST;*.INC" + String$(2, 0) OFN.lpstrCustomFilter[0] = String$(2,0) OFN.nMaxCustFilter = Len(OFN.lpstrCustomFilter[0]) OFN.nFilterIndex = 3 OFN.nMaxFile = Len(OFN.lpstrFile[0]) OFN.lpstrFileTitle = NULL OFN.nMaxFileTitle = 0 OFN.lpstrInitialDir[0] = CurDir$ + NIL OFN.lpstrTitle[0] = "Add ""STATIC"" keyword to script(s)..." + NIL OFN.Flags = OFN_ALLOWMULTISELECT Or OFN_HIDEREADONLY Or OFN_FILEMUSTEXIST OFN.lpstrDefExt[0] = "MST" + NIL ' Beep for Messagebox MessageBeep 0 While MessageBox(0,"Convert a script, or list of scripts, to add ""STATIC"" to Sub/Function definitions, and ""LINE"" to INPUT statements?","Convert Script(s)???",MB_ICONQUESTION Or MB_YESNO Or MB_TASKMODAL)=IDYES OFN.lpstrFile[0] = NIL IF GetOpenFileName(OFN) THEN NILPos% = InStr(OFN.lpstrFile[0], NIL) IF NilPos% THEN Buffer$ = RTrim$(Mid$(OFN.lpstrFile[0],1,NILPos% - 1)) ELSE Buffer$ = RTrim$(OFN.lpstrFile[0]) END IF _Pos% = InStr(Buffer$, " ") 'See if Spaces in filename, indicating multiple filenames If _Pos% THEN 'MULTIPLE FILES SELECTED Path$ = Mid$(Buffer$, 1, _Pos% - 1) 'Extract PathSpec IF Mid$(Path$, Len(Path$), 1) <> "\" THEN 'Append backslash to path if not present Path$ = Path$ + "\" END IF Buffer$ = Mid$(Buffer$, _Pos% + 1) 'Remove Path from buffer$ While Len(Buffer$) 'While buffer$ still contains filenames _Pos% = InStr(Buffer$, " ") IF _Pos% THEN 'IF Multiple files left... FName$ = Mid$(Buffer$, 1, _Pos% - 1) 'Extract First in line Buffer$ = Mid$(Buffer$, _Pos% + 1) 'And Remove from Buffer ELSE 'ELSE One file left... FName$ = Buffer$ 'Extract Last file Buffer$ = "" 'And Remove from Buffer END IF AddKeywords Path$ + FName$ 'CONVERT FILE WEND ELSE 'SINGLE FILE SELECTED AddKeywords Buffer$ 'CONVERT FILE ENDIF ELSE 'NO FILES SELECTED Exit While 'DONE ENDIF MessageBeep 0 'BEEP FOR MessageBOX WEND '****************************************************************************** ' Subs and Function section '****************************************************************************** '****************************************************************************** ' AddKeywords: Add the STATIC key word to every SUB or FUNCTION heading ' in a given file. This subprogram looks specifically for ' the following when it determines where to add the STATIC. ' ' Pre: The file name must be the name of a file in the current directory. ' ' Post: The following changes will be made in the file. ' SUB -- STATIC is added before the comment, ' but after the last char on ' a SUB line, which will usually ' be the closing paren ')'. ' ' FUNCTION -- STATIC is added before a comment, ' before the AS <type> clause, ' but after right paren ')' or type ' specifier (if one exists). ' ' ' ' The original file will be renamed to <filename>.BAK. The new file will be the ' name of the original file. ' ' Comments will should be preserved at the ends of the lines when STATIC is added. ' STATIC should not be added to a line if it is already there (in case this ' sub was used twice on the same file. (But we could do some syntax checking to ' make sure that if it's already there, it's in the right place). ' ' No provisions are made for lines that will exceed the 252 char limit after the ' STATIC keyword is added. The Editor will warn you of those when you load the ' file to run it, at which time you'll have to fix these rare cases '****************************************************************************** SUB AddKeywords (MSTFile AS STRING) STATIC DIM FileLine As String ' The text line read from the file. Dim TempLine As String ' Temporary buffer for analyzing the line. DIM Comment AS STRING ' Comment extracted from the line. DIM AsClause AS STRING ' Holds the AS clause for a FUNCTION. DIM FName As String, Ext AS STRING DIM SubPos, FuncPos As INTEGER, AsPos As INTEGER DIM ComPos AS INTEGER, StaticPos AS INTEGER DIM _Function$, _Sub$, _Input$, _Comment$, _As$, _Static$, _Line$, _RParen$, _Space$, _Empty$ _Function = "FUNCTION " _Sub = "SUB " _Input = "INPUT" _Comment = "'" _As = "AS " _Static = "STATIC" _Line = "LINE" _RParen = ")" _Space = " " _Empty = "" 'LET USER KNOW WHAT's HAPPENING Print "*******************************************************************************" Print "Processing File: " + """" + MSTFile + """" Count = 0 ' Backup the file. SplitPath MSTFile, szDrive$, szDir$, FName, Ext FName = szDrive$ + szDir$ + FName + ".BAK" IF EXISTS(FName) THEN KILL FName END IF NAME MSTFile AS FName ' Rename the file to *.BAK OPEN FName FOR INPUT AS #1 ' Read from the .BAK file and OPEN MSTFile FOR OUTPUT AS #2 ' write to the original file name. WHILE NOT EOF(1) AsClause = "" 'INITIALIZE STRINGS... Comment = "" Line Input #1, FileLine ' Get the next line of the file. TempLine = UCase$(LTrim$(RTrim$(FileLine))) ' Clean up the text for searching SubPos = InStr(1, TempLine, _Sub) ' FIND Upper case "SUB". FuncPos = InStr(1, TempLine, _Function) ' FIND Upper case "FUNCTION". InputPos = InStr(1, TempLine, _Input) ' FIND Upper case "INPUT". ComPos = Instr(1, UCase$(FileLine), _Comment) ' Look for comment. StaticPos = Instr(1, UCase$(FileLine), _Static) ' Look for STATIC keyword already existing ' Only process if it's a SUB or FUNCTION header, OR an INPUT # statement... ' If SUB or FUNCTION is the first word on the line. IF (FuncPos = 1) OR (SubPos = 1) THEN 'IF STATIC FOUND, VALIDATE IT... IF StaticPos THEN 'IF STATIC FOUND IN COMMENT, IT's INVALID IF (ComPos > 0) AND (StaticPos > ComPos) THEN StaticPos = 0 'RESET TO INDICATE NOT FOUND ELSE 'KEEP CHECKING If StaticPos THEN StaticPrevChar$ = Mid$(FileLine, StaticPos - 1, 1) 'GET CHAR BEFORE STATICPOS 'CAN'T BE BEGINNING OF LINE, BECAUSE "Sub" OR "Function" IS END IF 'IF PREV CHAR IS A RIGHT PAREN OR A SPACE OR BEGINNING OF LINE, KEEP CHECKING IF (StaticPrevChar$ = _RParen) OR (StaticPrevChar$ = _Space) THEN StaticTrailPos = StaticPos + Len(_Static) 'IF STATIC NOT LAST THING ON LINE...KEEP CHECKING IF StaticTrailPos <= Len(FileLine) THEN 'IF NOT FOLLOWED BY A SPACE Or COMMENT, IT's INVALID StaticTrailChar$ = Mid$(FileLine, StaticTrailPos, 1) IF (StaticTrailChar$ <> _Space) And (StaticTrailChar$ <> _Comment) THEN StaticPos = 0 'RESET TO INDICATE NOT FOUND END IF 'OTHERWISE, IT'S FOLLOWED BY A SPACE, THEREFOR IT'S VALID END IF 'OTHERWISE, IT'S LAST THING ON LINE, THEREFOR IT's VALID ELSE 'OTHERWISE, IT'S NOT PRECEDED BY ")" or " " SO IT's INVALID StaticPos = 0 'RESET TO INDICATE NOT FOUND END IF END IF END IF 'IF STATIC FOUND 'If STATIC not found, ADD IT IF (StaticPos = 0) THEN FileLine = _Static + _Space + FileLine ' INSERT "STATIC" TO BEGINNING OF LINE IF Count = 0 THEN Print "-------------------------------------------------------------------------------" END IF Print FileLine Count = Count + 1 END IF 'NO STATIC FOUND 'ELSE IF "INPUT" IS FIRST WORD ON LINE... ELSEIF (InputPos = 1) THEN 'VERIFY IT's FOLLOWED BY "#" or _Space InputTrailPos = InputPos + Len(_Input) InputTrailChar$ = Mid$(FileLine, InputTrailPos, 1) If (InputTrailChar = _Space) Or (InputTrailChar = "#") THEN FileLine = _Line + _Space + FileLine ' INSERT "LINE" TO BEGINNING OF LINE IF Count = 0 THEN Print "-------------------------------------------------------------------------------" END IF Print FileLine Count = Count + 1 END IF END IF 'SUB or FUNCTION Definition line, ELSEIF INPUT line PRINT #2, FileLine WEND IF Count THEN Print "-------------------------------------------------------------------------------" END IF Print "FINISHED w/File: """ + MSTFile + """" Print "*******************************************************************************" CLOSE END SUB