Page 1 of 1

Tool: Create Preprocess

Posted: Tue Sep 22, 2015 12:59 pm
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



Re: Tool: Create Preprocess

Posted: Tue Sep 22, 2015 6:28 pm
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)

Re: Tool: Create Preprocess

Posted: Wed Sep 23, 2015 6:01 am
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

Re: Tool: Create Preprocess

Posted: Wed Sep 23, 2015 7:42 pm
by said
Thanks for sharing :D could be very useful