Announcement

Collapse
No announcement yet.

source code formatter

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • source code formatter

    I have some ancient Pb code not my own that really needs formatting.
    Anyone point me to a Pb source code formatter. Primarily indents. I tried a search but had a difficult time narrowing down search field.

    Thanks,

    James

  • #2
    Formatter

    There are two on this website... just for starters.

    and this in the source code section.... PBSCFormatter
    Last edited by Jim Padgett; 29 Jun 2009, 05:21 PM.
    Warped by the rain, Driven by the snow...

    jimatluv2rescue.com

    Comment


    • #3
      Originally posted by Jim Padgett View Post
      There are two on this website... just for starters.

      and this in the source code section.... PBSCFormatter
      Jim,
      Nice try but .... no cigar.
      Did you even read the description of PBSCFormatter ? NOT
      This is why I asked. I CAN (DID) do a search but it turned up way too much crap.

      I decide to write my own. Thread closed

      James

      Comment


      • #4
        Thread reopened with a down and dirty utility.
        Written specifically for my immediate needs, If I was to expand I would use pcre regex.

        James

        Code:
        'SED_PBWIN
        '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
        'Code Formatter
        '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
        #COMPILE EXE "CF.EXE
        #DIM ALL
        
        DECLARE FUNCTION MessageBox LIB "USER32.DLL" ALIAS "MessageBoxA" ( _
              BYVAL DWORD _                       ' _ _in _opt HWND hWnd
            , BYREF ASCIIZ _                      ' _ _in _opt LPCSTR lpText
            , BYREF ASCIIZ _                      ' _ _in _opt LPCSTR lpCaption
            , BYVAL DWORD _                       ' _ _in UINT uType
            ) AS LONG                             ' int
        
        '==============================================================================
        FUNCTION RemoveExtAndDot (BYVAL sPath AS STRING) AS STRING
            LOCAL Where AS LONG
        
            Where = INSTR(-1,sPath,".")
            IF Where THEN
                DECR Where
                FUNCTION = LEFT$(sPath,Where)
            END IF
        END FUNCTION
        
        '==============================================================================
        FUNCTION ReturnPath (BYVAL S1 AS STRING) AS STRING
            LOCAL bPtr AS BYTE PTR
            LOCAL i     AS LONG
        
            bPtr = STRPTR(S1)
            i = LEN(S1)
            DO
                IF @bPtr[i] = 92 THEN
                    EXIT LOOP
                END IF
                DECR i
                IF i = 0 THEN
                    EXIT LOOP
                END IF
            LOOP
            FUNCTION = LEFT$(S1,i)
        END FUNCTION
        '==============================================================================
        FUNCTION MultiSpaceToOne(BYVAL InString AS STRING) AS STRING
        
            LOCAL i AS LONG
            FOR i = 10 TO 2 STEP -1
                REPLACE REPEAT$(i," ") WITH " " IN InString
            NEXT i
            FUNCTION = InString
        END FUNCTION
        '==============================================================================
        FUNCTION PBMAIN() AS LONG
            LOCAL sLines() AS STRING
            LOCAL LineCount,Indent,ff,hDlg,i,SelectFlag,IterateFlag    AS LONG
            LOCAL sFileOut,sFileIn,sExt,sMsg,StartDir,S1,S2,S3 AS STRING
            LOCAL sDecCount,sIncCount AS STRING
        
            sDecCount="|ELSE|ELSEIF|END|NEXT|WEND|LOOP|"
            sIncCount="|IF|FOR|FUNCTION|SUB|DO|WHILE|SELECT|CASE|"
        
            IF ISFILE(EXE.Path$+"LastDataDir.Txt") THEN
                ff = FREEFILE
                OPEN "LastDataDir.Txt" FOR INPUT AS ff
                LINE INPUT# ff,StartDir
                CLOSE ff
            ELSE
                StartDir = CURDIR$
            END IF
        
            DISPLAY OPENFILE 0,,,"Select PowerBASIC Basic Source File",StartDir,CHR$("Basic File",0," * .BAS"),"","BAS",0 TO sFileIn
        
        
            IF LEN(sFileIn) = 0 THEN
                EXIT FUNCTION
            END IF
        
            StartDir = ReturnPath(sFileIn)
            ff = FREEFILE
            OPEN EXE.PATH$+"LastDataDir.Txt" FOR OUTPUT AS ff
            PRINT# ff,StartDir
            CLOSE ff
        
            ff = FREEFILE
            OPEN sFileIn FOR INPUT AS ff
            FILESCAN ff ,RECORDS TO LineCount
            REDIM sLines(LineCount)
            LINE INPUT# ff,sLines()
            CLOSE ff
        'Append _CF to Input File
            sFileOut = RemoveExtAndDot(sFileIn)+"_CF.BAS"
            ff = FREEFILE
            OPEN sFileOut FOR OUTPUT AS ff
            TRACE ON
            FOR i = 0 TO LineCount-1
        'I need to remove "_" on FUNCTION/SUB LINE and combine the lines
        'so SED can recognize which procedure caret is in
        
                IF IterateFlag THEN
                    IterateFlag = 0
                    ITERATE
                END IF
        
                S1 = TRIM$(UCASE$(sLines(i)),ANY CHR$(32,9))
                S2 = "|"+EXTRACT$(S1,ANY CHR$(32,9))+"|"
                IF INSTR(sIncCount,S2)  THEN
                    IF S2 = "|FUNCTION|" THEN
                        IF ISFALSE(INSTR(S1," = ")) THEN
                            S3 = REMOVE$(S1,ANY "FUNCTION ")
                            IF S3 = "_" THEN
                                IterateFlag = 1
                                S3 = "FUNCTION "+sLines(i+1)
                                S3 = MultiSpaceToOne(S3)
                                PRINT# ff,REPEAT$(Indent,CHR$(9));TRIM$(S3,ANY CHR$(32,9))
                                Indent += 1
                                ITERATE
                            END IF
                            PRINT# ff,REPEAT$(Indent,CHR$(9));TRIM$(sLines(i),ANY CHR$(32,9))
                            Indent += 1
                            ITERATE
                        END IF
                    ELSEIF S2 = "|SUB|" THEN
                            S3 = REMOVE$(S1,ANY "SUB ")
                            IF S3 = "_" THEN
                                IterateFlag = 1
                                S3 = "SUB "+sLines(i+1)
                                S3 = MultiSpaceToOne(S3)
                                PRINT# ff,REPEAT$(Indent,CHR$(9));TRIM$(S3,ANY CHR$(32,9))
                                Indent += 1
                                ITERATE
                            END IF
                            PRINT# ff,REPEAT$(Indent,CHR$(9));TRIM$(sLines(i),ANY CHR$(32,9))
                            Indent += 1
                            ITERATE
                    ELSEIF S2 = "|IF|" THEN
                        S3 = LTRIM$(REMAIN$(S1,"THEN"),CHR$(32,9))
                        'Single Line IF THEN
                        IF LEN(S3) THEN
                            IF LEFT$(S3,1) <> "'" OR LEFT$(S3,3)<> "REM" THEN
                                PRINT# ff, REPEAT$(Indent, CHR$(9));TRIM$(sLines(i),ANY CHR$(32, 9))
                                ITERATE
                            END IF
                        ELSE
                            PRINT# ff, REPEAT$(Indent, CHR$(9));TRIM$(sLines(i),ANY CHR$(32, 9))
                            Indent + = 1
                            ITERATE
                        END IF
                    ELSEIF S2 = "|CASE|" THEN
                        IF ISFALSE(SelectFlag) THEN
                            Indent - = 1
                            PRINT# ff, REPEAT$(Indent, CHR$(9));TRIM$(sLines(i),ANY CHR$(32, 9))
                            Indent + = 1
                            ITERATE
                        ELSE
                            PRINT# ff, REPEAT$(Indent, CHR$(9));TRIM$(sLines(i),ANY CHR$(32, 9))
                            Indent + = 1
                            SelectFlag = 0
                            ITERATE
                        END IF
                    ELSEIF S2 = "|SELECT|" THEN
                            PRINT# ff, REPEAT$(Indent, CHR$(9));TRIM$(sLines(i),ANY CHR$(32, 9))
                            Indent + = 1
                            SelectFlag = 1
                            ITERATE
                    ELSE
                            PRINT# ff, REPEAT$(Indent, CHR$(9));TRIM$(sLines(i),ANY CHR$(32, 9))
                            Indent + = 1
                            ITERATE
                    END IF
                ELSEIF INSTR(sDecCount, S2) THEN
                    IF S2 = "|ELSE|" OR S2 = "|ELSEIF|" THEN
                        Indent - = 1
                        PRINT# ff, REPEAT$(Indent, CHR$(9));TRIM$(sLines(i),ANY CHR$(32, 9))
                        Indent + = 1
                        ITERATE
                    ELSEIF S2 = "|END|" THEN
                        Indent - = 1
                        IF INSTR(S1, "SELECT") THEN
                            Indent - = 1
                        END IF
                        PRINT# ff, REPEAT$(Indent, CHR$(9));TRIM$(sLines(i),ANY CHR$(32, 9))
                        ITERATE
                    ELSE
                        Indent - = 1
                        PRINT# ff, REPEAT$(Indent, CHR$(9));TRIM$(sLines(i),ANY CHR$(32, 9))
                        ITERATE
                    END IF
                END IF
                IF Indent < 0 THEN
                    Indent = 0
                END IF
        
                PRINT# ff, REPEAT$(Indent, CHR$(9));TRIM$(sLines(i),ANY CHR$(32, 9))
            NEXT i
            CLOSE ff
        
            MessageBox hDlg, "All Done", "Display Msg", IIF&(hDlg, 0, %MB_TOPMOST OR %MB_SETFOREGROUND)
        
        
        END FUNCTION
        Attached Files

        Comment


        • #5
          This is really worth checking out. SrcFrmt v2.0, TheirCorp's source code formattter:
          http://www.powerbasic.com/support/pb...d.php?p=280755
          Rgds, Dave

          Comment


          • #6
            I remember PB/DOS coming with a source code formatter in its examples directory. pbsf.bas was the file name, IIRC.

            Comment


            • #7
              Thanks Dave.
              That was the one I was looking for but I found I needed another little tweak.
              I use SED and "FUNCTION" and the function name need to be on the same line for the function to be identified by the editor.
              The sources I need to format use:
              Code:
              FUNCTION _
                  FunctionName ( _
                      BYVAL P1 AS LONG _
                  )AS LONG
              I had 5 or 6 of 20k-40k lines each to do. My method worked fine.

              James


              James
              Last edited by jcfuller; 30 Jun 2009, 09:49 AM.

              Comment


              • #8
                My Bad

                I know I've seen a source code formatter here in the past... durn if I can remember.. I also thought it would do something like inclean as well.. so much for my memory.
                Warped by the rain, Driven by the snow...

                jimatluv2rescue.com

                Comment


                • #9
                  My formatter it the download section works good.

                  I have been lax in keeping it updated on the website for the new commands. My personal copy is up-to-date. I should repost the updates.
                  <b>George W. Bleck</b>
                  <img src='http://www.blecktech.com/myemail.gif'>

                  Comment

                  Working...
                  X