Tool: Create Preprocess

Share your advanced PureBasic knowledge/code with the community.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Tool: Create Preprocess

Post by GPI »

When you work with macros, you maybe get the problem, that an error is in your code and PureBasic only mark the line of the call of the macro.

This tool call the compiler with the "/preprocess"-command combined with "/command" and remove unnecassary commands and macros. The result is one big file with all macros exand, compilerif removed and all file included.

Add the execute under "tools\configurate tools..." with "%FILE" as parameter (with "-Quote!)

Code: Select all

;"%FILE"

Procedure FindCommend(*pos.character)
  Protected *start=*pos
  Protected isDQuote
  Protected isQuote
  While *pos\c>0
    If isDQuote
      If *pos\c='"'
        isDQuote=#False
      EndIf
    ElseIf isQuote
      If *pos\c=39 ;'
        isQuote=#False
      EndIf
    Else
      Select *pos\c
        Case '"'
          isDQuote=#True
        Case 39;'
          isQuote=#True
        Case ';'
          Break
      EndSelect
    EndIf
    *pos+SizeOf(character)
  Wend
  ProcedureReturn (*pos-*start )/SizeOf(character)
EndProcedure


file$=ProgramParameter()
pb$=GetEnvironmentVariable("PB_TOOL_Compiler")
If file$ And FileSize(file$)>0
  dir$ = GetPathPart(file$)
  outfile$=file$+".pre.pb"
  DeleteFile(dir$+outfile$)
  OpenConsole()  
  Compiler = RunProgram(pb$, Chr(34)+file$+Chr(34)+" /COMMENTED /PREPROCESS "+Chr(34)+outfile$+Chr(34), dir$,#PB_Program_Wait)
  
  
  NewList file.s()
  
  Define in,str.s,lastcom.s
  Macro output(a)
    AddElement(file())
    file()=a
  EndMacro
  PrintN("")
  Print("Remove unnecassary comments...")
  
  in=ReadFile(#PB_Any,outfile$)
  If in
    While Not Eof(in)
      line+1
      str=ReadString(in)
      If Left(str,1)=";"
        If lastcom<>""
          If Trim(Left(lastcom,FindCommend(@lastcom)))=""
            output("  "+lastcom)
          Else
            output("; "+lastcom)
          EndIf
        EndIf      
        lastcom=Right(str,Len(str)-2)
      Else
        If Left(Trim(str),6)="Macro "
          isMacro=#True
          str=""
        ElseIf Left(Trim(str),8)="EndMacro"
          isMacro=#False
          str=""
        ElseIf isMacro
          str=""
        EndIf      
        If lastcom<>""
          x=FindCommend(@lastcom)
          If str<>Left(lastcom,x)
            
            output( "; "+lastcom)
            If str<>""
              output( str)
            EndIf
            
          Else
            output(lastcom)
          EndIf
          lastcom=""
        ElseIf str<>""
          output(str)
        EndIf
        
      EndIf
      
      
    Wend
    
    CloseFile(in)
  EndIf
  PrintN("done")
  PrintN("Removed lines:"+Str(line-ListSize(file())))
  Print("Write File ...")
  out=CreateFile(#PB_Any,outfile$)
  If out
    ForEach file()
      WriteStringN(out,file())
    Next
    CloseFile(out)
  EndIf
  PrintN("done")
  PrintN("")  
  PrintN("Press [Return]")
  
  Input()
  RunProgram(outfile$)
EndIf


User avatar
skywalk
Addict
Addict
Posts: 4219
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Tool: Create Preprocess

Post by skywalk »

Thanks for posting. :)
It is useful to check resulting syntax of many Macro's.
However, if I use my compiler settings for a particular app, the flattened file does not compile normally.
Specifically, the DataSection's are garbled.
Read.s see's evil "??"s, but I compiled with Unicode ON?
Is there an order required to the command line options?
I cannot think what else is missing?
I also made sure the resulting files are all UTF-8 with BOM.

Code: Select all

rsrc$ = "C:\myapp\myapp.rc"
exe_ico$ = "C:\myapp\myapp.ico"
Compiler = RunProgram(pb$, Chr(34)+file$+Chr(34)+" /UNICODE /THREAD /XP /RESOURCE " + rsrc$ + " /ICON " + exe_ico$ + " /COMMENTED /PREPROCESS "+Chr(34)+outfile$+Chr(34), dir$,#PB_Program_Wait)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Tool: Create Preprocess

Post by ts-soft »

Here is a modified version, without console-support but crossplattform:

Code: Select all

EnableExplicit

Procedure FindCommend(*pos.character)
  Protected *start = *pos
  Protected isDQuote
  Protected isQuote
  While *pos\c > 0
    If isDQuote
      If *pos\c = '"'
        isDQuote = #False
      EndIf
    ElseIf isQuote
      If *pos\c = 39 ;'
        isQuote = #False
      EndIf
    Else
      Select *pos\c
        Case '"'
          isDQuote = #True
        Case 39;'
          isQuote = #True
        Case ';'
          Break
      EndSelect
    EndIf
    *pos + SizeOf(character)
  Wend
  ProcedureReturn (*pos-*start ) / SizeOf(character)
EndProcedure


Define.s file$, outfile$, compiler$, parameter$
Define.i line, isMacro, x, out

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    parameter$ = " /COMMENTED /PREPROCESS "
  CompilerDefault
    parameter$ = " --commented --preprocess "
CompilerEndSelect

compiler$ = GetEnvironmentVariable("PB_TOOL_Compiler")
file$ = ProgramParameter()
outfile$ = file$ + ".pre.pb"

If RunProgram(compiler$, #DQUOTE$ + file$ + #DQUOTE$ + parameter$ + #DQUOTE$ + outfile$ + #DQUOTE$, GetPathPart(file$), #PB_Program_Wait)
  NewList file.s()
  Define in, str.s, lastcom.s
  
  Macro output(a)
    AddElement(file())
    file() = a
  EndMacro
  
  in = ReadFile(#PB_Any, outfile$)
  If in
    While Not Eof(in)
      line + 1
      str = ReadString(in)
      If Left(str,1) = ";"
        If lastcom <> ""
          If Trim(Left(lastcom, FindCommend(@lastcom))) = ""
            output("  " + lastcom)
          Else
            output("; " + lastcom)
          EndIf
        EndIf     
        lastcom = Right(str, Len(str) - 2)
      Else
        If Left(Trim(str), 6) = "Macro "
          isMacro = #True
          str = ""
        ElseIf Left(Trim(str), 8) = "EndMacro"
          isMacro = #False
          str = ""
        ElseIf isMacro
          str = ""
        EndIf     
        If lastcom <> ""
          x = FindCommend(@lastcom)
          If str <> Left(lastcom, x)
            output( "; " + lastcom)
            If str <> ""
              output(str)
            EndIf
          Else
            output(lastcom)
          EndIf
          lastcom = ""
        ElseIf str <> ""
          output(str)
        EndIf
      EndIf
    Wend
    CloseFile(in)  
  EndIf
  out = CreateFile(#PB_Any, outfile$)
  If out
    ForEach file()
      WriteStringN(out,file())
    Next
    CloseFile(out)
  EndIf
  RunProgram(GetEnvironmentVariable("PB_TOOL_IDE"), #DQUOTE$ + outfile$ + #DQUOTE$, "")  
EndIf
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Tool: Create Preprocess

Post by said »

Thanks for sharing :D could be very useful
Post Reply