Mehrere Codes überprüfen

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
GPI
Beiträge: 1511
Registriert: 29.08.2004 13:18
Kontaktdaten:

Mehrere Codes überprüfen

Beitrag von GPI »

Fürs Codearchiv brauchte ich eine Möglichkeit, schnell mehrere Quellcodes mit den Compiler zu überprüfen. Dabei kam das raus.
Zudem entfernt der er einige Systemabhängige Compileroptionen wie CurrentDirectory, Cursorposition, Folding etc. und kontrolliert, ob EnableUnicode aktiv ist. Damit jeder Benutzer die Sourcecodes sauber öffnen kann, ohne das eine Fehlermeldung wie "Compiler Win PB5.40" nicht gefunden zu bekommen. Zudem konvertiert er sämtliche Sourcecodes in UTF8, falls sie mal in ASCII gespeichert wurden.
Achja, wenn eine "Placeholder.txt" gefunden wird und sie nicht mehr die einzige Datei in Verzeichnis ist, dann wird sie gelöscht.

Code: Alles auswählen

;    Description: Removes Options of the pb and pbi source
;         Author: GPI
;           Date: 05-12-2015
;     PB-Version: 5.40
;             OS: Windows
;  English-Forum: 
;   French-Forum: 
;   German-Forum: 
; -----------------------------------------------------------------------------
EnableExplicit

Global NewMap KillMask()
KillMask("CURRENTDIRECTORY")=#True
KillMask("CURSORPOSITION")=#True
KillMask("FIRSTLINE")=#True
KillMask("FOLDING")=#True
KillMask("COMPILER")=#True
KillMask("ENABLECOMPILECOUNT")=#True
KillMask("ENABLEBUILDCOUNT")=#True
KillMask("ENABLEEXECONSTANT")=#True
KillMask("EXECUTABLE")=#True
KillMask("CONSTANT")

Global NewMap NeedMask()
NeedMask("ENABLEUNICODE")
NeedMask("ENABLEXP")

OpenConsole("CodeCleaner")

Procedure.s CheckSyntax(file.s,EnableThread)
  Protected compiler
  Protected Output$
  Protected do
  Protected ret.s
  Protected a$
  If Left(file,2)=".\"
    file=GetCurrentDirectory()+Mid(file,3)
  EndIf  
  If EnableThread
    a$="--thread "
  Else
    a$=""
  EndIf
  Compiler = RunProgram(#PB_Compiler_Home+"Compilers\pbcompiler.exe", a$+"--check "+Chr(34)+file+Chr(34), #PB_Compiler_Home+"Compilers", #PB_Program_Open | #PB_Program_Read)
  Output$ = ""
  do=#False
  If Compiler
    While ProgramRunning(Compiler)
      If AvailableProgramOutput(Compiler)
        a$=ReadProgramString(Compiler)
        
        If a$="Starting syntax check..."
          do=#True
        ElseIf do And a$<>""         
          Output$ + a$ + Chr(13)
        EndIf
      EndIf
    Wend
    If ProgramExitCode(Compiler)
      ret= "ERROR:"+file+" "+output$
    EndIf
    CloseProgram(Compiler) ; Close the connection to the program
  EndIf
  ProcedureReturn ret
EndProcedure
Procedure CheckFile(file.s)
  Protected in
  Protected check.s
  Protected do
  Protected out
  Protected Format
  Protected Syncheck.s
  Protected EnableThread=#False
  ConsoleTitle("Check "+file)
  
  NewList FLine.s()
  in=ReadFile(#PB_Any,file )
  If in
    Format=ReadStringFormat(in)
    
    While Not Eof(in)
      AddElement(FLine())
      FLine()=ReadString(in,Format)
      If Asc(Left(fline(),1))=65279 ;BOM entfernen!
        fline()=Mid(FLine(),2)
      EndIf
    Wend
    CloseFile(in)
    
    ;ProcedureReturn 0
  EndIf
  
  If format=#PB_Ascii
    PrintN("Convert to UTF8 "+file)
    do=#True
  EndIf
  
  If LastElement(FLine())
    While Left(FLine(),1)=";" And Left(FLine(),15)<>"; IDE Options =" And PreviousElement(FLine())
    Wend
    
    ForEach NeedMask()
      NeedMask()=0
    Next
        
    While NextElement(FLine())
      check=UCase(Trim(Mid(StringField(FLine(),1,"="),2)))
      If KillMask(check)
        DeleteElement(fline())
        do=#True
      EndIf
      If FindMapElement(NeedMask(),check)
        NeedMask()=#True
      EndIf
      If check="ENABLETHREAD"
        EnableThread=#True
      EndIf
    Wend
    
    ForEach NeedMask()
      If NeedMask()=0
        PrintN("Missing "+MapKey(NeedMask())+" "+file)
      EndIf
    Next
    
  EndIf
  
  If do
    PrintN( "ReCreate "+file)
    out=CreateFile(#PB_Any,file,#PB_UTF8)
    
    If out
      WriteStringFormat(out,#PB_UTF8)
      ForEach FLine()
        WriteStringN(out,FLine(),#PB_UTF8)
      Next
    EndIf
    CloseFile(out)
  EndIf
  
  Syncheck=CheckSyntax(file,EnableThread)
  If Syncheck
    PrintN(Syncheck)
  EndIf
  
  
EndProcedure

Procedure dir(Start.s=".\")
  Protected dir
  Protected name.s,ext.s
  Protected placeholder.s
  Protected count
  dir=ExamineDirectory(#PB_Any,Start,"*.*")
  If dir
    While NextDirectoryEntry(dir)
      name.s=DirectoryEntryName(dir)
      If DirectoryEntryType(dir)=#PB_DirectoryEntry_Directory
        If Left(name,1)<>"."
          dir(start+name+"\")
        EndIf
      Else
        If UCase(name)="PLACEHOLDER.TXT"
          placeholder=name
        Else
          count +1          
          ext=UCase(GetExtensionPart(name))
          If (ext="PB" Or ext="PBI") And name<>"CodeCleaner.pb"
            CheckFile(start+name)
            
          EndIf          
          
        EndIf   
        
      EndIf
    Wend
    
    If count And placeholder<>""
      DeleteFile(start+placeholder)
      PrintN( "delete "+start+placeholder)
    EndIf          
    
    FinishDirectory(dir)
  EndIf
EndProcedure


dir()

PrintN("")
PrintN("Press return")
Input()
CloseConsole()
wie immer: Benutzung auf eigene Gefahr.
CodeArchiv Rebirth: Deutsches Forum Github Hilfe ist immer gern gesehen!