home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / d / d009_2 / 1.ddi / SAMPLES / GENERAL / FXSTATIC.MS$ / FXSTATIC.bin
Encoding:
Text File  |  1992-02-03  |  12.6 KB  |  314 lines

  1. '******************************* FXSTATIC.MST *********************************
  2. 'Demonstrates: This sample will search through an MSTest script and insure that
  3. '              the STATIC keyword appears at the end of each SUB or FUNCTION,
  4. '              and that INPUT statements are using the  LINE INPUT syntax.
  5. '
  6. '              The above syntax requirements will ensure maximum compatibility
  7. '              with future versions of MSTest and other Microsoft BASIC
  8. '              BASIC products.
  9. '
  10. 'Required Files: COMMDLG.DLL, MSTEST.INC
  11. '
  12. 'Uses:
  13. '
  14. 'Complexity Level: INTERMEDIATE
  15. '
  16. 'Notes: Uses the COMMDLG FileOpen Dialog to get filename(s) from user of
  17. '       files to convert. Adds "STATIC" keyword to all SUB and FUNCTIONs =>
  18. '       STATIC SUB FOO.  Adds "LINE" to all INPUT # statements => LINE INPUT #
  19. '
  20. '******************************************************************************
  21.  
  22.  
  23. '******************************************************************************
  24. '    INCLUDE SECTION - WINCMDLG.INC is provided as a sample include file, and 
  25. '                      should have been installed in the INCLUDE sub-directory.
  26. '******************************************************************************
  27.     '$INCLUDE: 'MSTest.INC'
  28.     '$INCLUDE: 'WINCMDLG.INC'
  29.  
  30.  
  31. '******************************************************************************
  32. '    CONSTANTS
  33. '******************************************************************************
  34.     Dim NIL As String
  35.     NIL = Chr$(0)
  36.  
  37. '******************************************************************************
  38. '     Parts needed from WINUSER.INC
  39. '******************************************************************************
  40.     '  Dialog Box Command IDs
  41.     Const IDYES = 6
  42.     Const IDNO = 7
  43.  
  44.     '  MessageBox() Flags
  45.     Const MB_YESNO = &H4
  46.     Const MB_ICONQUESTION = &H20
  47.  
  48.     Const MB_TASKMODAL = &H2000
  49.  
  50.     Declare Function MessageBox Lib "User" ( hWnd%,  lpText$,  lpCaption$,  wType%) As Integer
  51.     Declare Sub MessageBeep Lib "User" ( wType%)
  52.  
  53. '******************************************************************************
  54. '     SUB and FUNCTION Declarations...
  55. '******************************************************************************
  56.     Declare SUB AddKeywords (MSTFile AS STRING)
  57.  
  58.  
  59. '******************************************************************************
  60. '     CREATE Buffers for FileOpen structure
  61. '******************************************************************************
  62.     Dim OFN As tagOFN
  63.  
  64.     Allocate OFN.lpstrFilter, 1
  65.     Allocate OFN.lpstrCustomFilter, 1
  66.     Allocate OFN.lpstrFile, 1
  67.     Allocate OFN.lpstrFileTitle, 1
  68.     Allocate OFN.lpstrInitialDir, 1
  69.     Allocate OFN.lpstrTitle, 1
  70.     Allocate OFN.lpstrDefExt, 1
  71.     Allocate OFN.lpTemplateName, 1
  72.  
  73.  
  74. '******************************************************************************
  75. '     MAIN PROGRAM
  76. '******************************************************************************
  77. ViewPort OFF
  78. ViewPort Clear
  79. ViewPort ON
  80.  
  81. '* Fill in structure for Common Dialog
  82. OFN.lStructSize = OFN_StructSize
  83. OFN.hWndOwner = 0
  84. OFN.hInstance = 0
  85. OFN.lpstrFilter[0] = "Script Files" + NIL + "*.MST" + NIL + "Include files" + NIL + "*.INC" + NIL + "Script AND Include Files" + NIL + "*.MST;*.INC" + String$(2, 0)
  86. OFN.lpstrCustomFilter[0] = String$(2,0)
  87. OFN.nMaxCustFilter = Len(OFN.lpstrCustomFilter[0])
  88. OFN.nFilterIndex = 3
  89. OFN.nMaxFile = Len(OFN.lpstrFile[0])
  90. OFN.lpstrFileTitle = NULL
  91. OFN.nMaxFileTitle = 0
  92. OFN.lpstrInitialDir[0] = CurDir$ + NIL
  93. OFN.lpstrTitle[0] = "Add ""STATIC"" keyword to script(s)..." + NIL
  94. OFN.Flags = OFN_ALLOWMULTISELECT Or OFN_HIDEREADONLY Or OFN_FILEMUSTEXIST
  95. OFN.lpstrDefExt[0] = "MST" + NIL
  96.  
  97. ' Beep for Messagebox
  98. MessageBeep 0
  99.  
  100. 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
  101.    OFN.lpstrFile[0] = NIL
  102.    IF GetOpenFileName(OFN) THEN
  103.       NILPos% = InStr(OFN.lpstrFile[0], NIL)
  104.       IF NilPos% THEN
  105.          Buffer$ = RTrim$(Mid$(OFN.lpstrFile[0],1,NILPos% - 1))
  106.       ELSE
  107.          Buffer$ = RTrim$(OFN.lpstrFile[0])
  108.       END IF
  109.  
  110.       _Pos% = InStr(Buffer$, " ")             'See if Spaces in filename, indicating multiple filenames
  111.       If _Pos% THEN                           'MULTIPLE FILES SELECTED
  112.  
  113.          Path$ = Mid$(Buffer$, 1, _Pos% - 1)         'Extract PathSpec
  114.          IF Mid$(Path$, Len(Path$), 1) <> "\" THEN   'Append backslash to path if not present
  115.             Path$ = Path$ + "\"
  116.          END IF
  117.  
  118.          Buffer$ = Mid$(Buffer$, _Pos% + 1)     'Remove Path from buffer$
  119.  
  120.          While Len(Buffer$)                     'While buffer$ still contains filenames
  121.  
  122.             _Pos% = InStr(Buffer$, " ")
  123.             IF _Pos% THEN                            'IF Multiple files left...
  124.                FName$ = Mid$(Buffer$, 1, _Pos% - 1)      'Extract First in line
  125.                Buffer$ = Mid$(Buffer$, _Pos% + 1)        'And Remove from Buffer
  126.             ELSE                                     'ELSE One file left...
  127.                FName$ = Buffer$                          'Extract Last file
  128.                Buffer$ = ""                              'And Remove from Buffer
  129.             END IF
  130.  
  131.             AddKeywords Path$ + FName$            'CONVERT FILE
  132.          WEND
  133.       ELSE                                    'SINGLE FILE SELECTED
  134.          AddKeywords Buffer$                      'CONVERT FILE
  135.       ENDIF
  136.  
  137.    ELSE            'NO FILES SELECTED
  138.       Exit While      'DONE
  139.    ENDIF
  140.  
  141.    MessageBeep 0   'BEEP FOR MessageBOX
  142. WEND
  143.  
  144.  
  145. '******************************************************************************
  146. '      Subs and Function section
  147. '******************************************************************************
  148.  
  149.  
  150. '******************************************************************************
  151. ' AddKeywords:  Add the STATIC key word to every SUB or FUNCTION heading
  152. '             in a given file.  This subprogram looks specifically for
  153. '             the following when it determines where to add the STATIC.
  154. '
  155. ' Pre:  The file name must be the name of a file in the current directory.
  156. '
  157. ' Post: The following changes will be made in the file.
  158. '     SUB   --    STATIC is added before the comment,
  159. '                 but after the last char on
  160. '                 a SUB line, which will usually
  161. '                 be the closing paren ')'.
  162. '
  163. '     FUNCTION -- STATIC is added before a comment,
  164. '                 before the AS <type> clause,
  165. '                 but after right paren ')' or type
  166. '                 specifier (if one exists).
  167. '
  168. '
  169. '
  170. ' The original file will be renamed to <filename>.BAK.  The new file will be the
  171. ' name of the original file.
  172. '
  173. ' Comments will should be preserved at the ends of the lines when STATIC is added.
  174. ' STATIC should not be added to a line if it is already there (in case this
  175. ' sub was used twice on the same file.  (But we could do some syntax checking to
  176. ' make sure that if it's already there, it's in the right place).
  177. '
  178. ' No provisions are made for lines that will exceed the 252 char limit after the
  179. ' STATIC keyword is added.  The Editor will warn you of those when you load the
  180. ' file to run it, at which time you'll have to fix these rare cases
  181. '******************************************************************************
  182.  
  183. SUB AddKeywords (MSTFile AS STRING) STATIC
  184.    DIM FileLine As String ' The text line read from the file.
  185.    Dim TempLine As String ' Temporary buffer for analyzing the line.
  186.    DIM Comment AS STRING  ' Comment extracted from the line.
  187.    DIM AsClause AS STRING ' Holds the AS clause for a FUNCTION.
  188.    DIM FName As String, Ext AS STRING
  189.  
  190.    DIM SubPos, FuncPos As INTEGER, AsPos As INTEGER
  191.    DIM ComPos AS INTEGER, StaticPos AS INTEGER
  192.    DIM _Function$, _Sub$, _Input$, _Comment$, _As$, _Static$, _Line$, _RParen$, _Space$, _Empty$
  193.  
  194.    _Function   = "FUNCTION "
  195.    _Sub        = "SUB "
  196.    _Input      = "INPUT"
  197.    _Comment    = "'"
  198.    _As         = "AS "
  199.    _Static     = "STATIC"
  200.    _Line       = "LINE"
  201.    _RParen     = ")"
  202.    _Space      = " "
  203.    _Empty      = ""
  204.  
  205.    'LET USER KNOW WHAT's HAPPENING
  206.    Print "*******************************************************************************"
  207.    Print "Processing File: " + """" + MSTFile + """"
  208.    Count = 0
  209.  
  210.    ' Backup the file.
  211.    SplitPath MSTFile, szDrive$, szDir$, FName, Ext
  212.    FName = szDrive$ + szDir$ + FName + ".BAK"
  213.    IF EXISTS(FName) THEN
  214.       KILL FName
  215.    END IF
  216.  
  217.    NAME MSTFile AS FName                    ' Rename the file to *.BAK
  218.  
  219.    OPEN FName FOR INPUT AS #1               ' Read from the .BAK file and
  220.    OPEN MSTFile FOR OUTPUT AS #2            ' write to the original file name.
  221.  
  222.    WHILE NOT EOF(1)
  223.       AsClause = ""  'INITIALIZE STRINGS...
  224.       Comment  = ""
  225.  
  226.       Line Input #1, FileLine                    ' Get the next line of the file.
  227.  
  228.       TempLine = UCase$(LTrim$(RTrim$(FileLine)))  ' Clean up the text for searching
  229.       SubPos  = InStr(1, TempLine, _Sub)           ' FIND Upper case "SUB".
  230.       FuncPos = InStr(1, TempLine, _Function)      ' FIND Upper case "FUNCTION".
  231.       InputPos = InStr(1, TempLine, _Input)        ' FIND Upper case "INPUT".
  232.       ComPos  = Instr(1, UCase$(FileLine), _Comment)    ' Look for comment.
  233.       StaticPos = Instr(1, UCase$(FileLine), _Static)   ' Look for STATIC keyword already existing
  234.  
  235.       ' Only process if it's a SUB or FUNCTION header, OR an INPUT # statement...
  236.       ' If SUB or FUNCTION is the first word on the line.
  237.       IF (FuncPos = 1) OR (SubPos = 1) THEN
  238.  
  239.          'IF STATIC FOUND, VALIDATE IT...
  240.          IF StaticPos THEN
  241.  
  242.             'IF STATIC FOUND IN COMMENT, IT's INVALID
  243.             IF (ComPos > 0) AND (StaticPos > ComPos) THEN
  244.                StaticPos = 0     'RESET TO INDICATE NOT FOUND
  245.  
  246.             ELSE  'KEEP CHECKING
  247.                If StaticPos THEN
  248.                   StaticPrevChar$ = Mid$(FileLine, StaticPos - 1, 1)   'GET CHAR BEFORE STATICPOS
  249.                   'CAN'T BE BEGINNING OF LINE, BECAUSE "Sub" OR "Function" IS
  250.                END IF
  251.  
  252.                'IF PREV CHAR IS A RIGHT PAREN OR A SPACE OR BEGINNING OF LINE, KEEP CHECKING
  253.                IF (StaticPrevChar$ = _RParen) OR (StaticPrevChar$ = _Space) THEN
  254.                   StaticTrailPos = StaticPos + Len(_Static)
  255.  
  256.                   'IF STATIC NOT LAST THING ON LINE...KEEP CHECKING
  257.                   IF StaticTrailPos <= Len(FileLine) THEN
  258.  
  259.                      'IF NOT FOLLOWED BY A SPACE Or COMMENT, IT's INVALID
  260.                      StaticTrailChar$ = Mid$(FileLine, StaticTrailPos, 1)
  261.                      IF (StaticTrailChar$ <> _Space) And (StaticTrailChar$ <> _Comment) THEN
  262.                         StaticPos = 0     'RESET TO INDICATE NOT FOUND
  263.                      END IF  'OTHERWISE, IT'S FOLLOWED BY A SPACE, THEREFOR IT'S VALID
  264.  
  265.                   END IF  'OTHERWISE, IT'S LAST THING ON LINE, THEREFOR IT's VALID
  266.  
  267.                ELSE   'OTHERWISE, IT'S NOT PRECEDED BY ")" or " " SO IT's INVALID
  268.                   StaticPos = 0     'RESET TO INDICATE NOT FOUND
  269.                END IF
  270.             END IF
  271.  
  272.          END IF   'IF STATIC FOUND
  273.  
  274.          'If STATIC not found, ADD IT
  275.          IF (StaticPos = 0) THEN
  276.             FileLine = _Static + _Space + FileLine   ' INSERT "STATIC" TO BEGINNING OF LINE
  277.             IF Count = 0 THEN
  278.                Print "-------------------------------------------------------------------------------"
  279.             END IF
  280.             Print FileLine
  281.             Count = Count + 1
  282.          END IF   'NO STATIC FOUND
  283.  
  284.       'ELSE IF "INPUT" IS FIRST WORD ON LINE...
  285.       ELSEIF (InputPos = 1) THEN
  286.  
  287.          'VERIFY IT's FOLLOWED BY "#" or _Space
  288.          InputTrailPos = InputPos + Len(_Input)
  289.          InputTrailChar$ = Mid$(FileLine, InputTrailPos, 1)
  290.          If (InputTrailChar = _Space) Or (InputTrailChar = "#") THEN
  291.             FileLine = _Line + _Space + FileLine   ' INSERT "LINE" TO BEGINNING OF LINE
  292.             IF Count = 0 THEN
  293.                Print "-------------------------------------------------------------------------------"
  294.             END IF
  295.             Print FileLine
  296.             Count = Count + 1
  297.          END IF
  298.       END IF   'SUB or FUNCTION Definition line, ELSEIF INPUT line
  299.  
  300.       PRINT #2, FileLine
  301.  
  302.  
  303.    WEND
  304.    IF Count THEN
  305.       Print "-------------------------------------------------------------------------------"
  306.    END IF
  307.    Print "FINISHED w/File: """ + MSTFile + """"
  308.    Print "*******************************************************************************"
  309.    CLOSE
  310.  
  311. END SUB
  312.  
  313.  
  314.