Page 1 of 1

AskPureBasicCompiler - Module

Posted: Sun Feb 15, 2015 5:47 am
by StarBootics
Hello everyone,

I really love the Module, this small module for asking PBCompiler about the current version. Very usefull when you are developping a custom tool and you need to know the current version of the compiler.

Please note, I have only tested this code under Linux x64 but it should work on other OS as well.

Edit #1 : Little modification that reduce the size of the compiled program (Init function), Help() added, and a Reset() command added.
Edit #2 : Since the search for the compiler can be time consuming I have rename the Init() function to Initialize() and make it publically available.

Best regards
StarBootics

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : AskPureBasicCompiler - Module
; File Name : AskPureBasicCompiler - Module.pb
; File version: 1.1.1
; Programming : OK
; Programmed by : StarBootics
; Date : 22-10-2012
; Last Update : 21-02-2015
; PureBasic code : V5.31
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule AskPureBasicCompiler
  
  Declare Initialize()
  Declare.s CurrentVersion()
  Declare.s CurrentOS()
  Declare.s CurrentVersionOS()
  Declare.s Help()
  Declare Reset()
  
EndDeclareModule

Module AskPureBasicCompiler
  
  CompilerSelect #PB_Compiler_OS
      
    CompilerCase #PB_OS_Windows 
      #LineFeed = #CRLF$

    CompilerCase #PB_OS_Linux
      #LineFeed = #LF$

    CompilerCase #PB_OS_MacOS
      #LineFeed = #CR$
      
  CompilerEndSelect
  
  Structure Instance
    
    CompilerProgramFile.s
    CompilerFile.s
    CompilerHome.s
    RunParamVersion.s
    RunParamHelp.s
    CurrentVersion.s
    CurrentOS.s
    CurrentVersionOS.s
    Help.s
    IsInit.b
    
  EndStructure
  
  Global Instance.Instance
  
  Procedure.s FindPureBasicCompiler(Directory.s, FileName.s)
    
    Protected DirectoryHandle.i, EntryName.s, Output.s
    
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows 
      
      If Right(Directory, 1) <> "\"
        Directory + "\"
      EndIf
      
    CompilerElse
      
      If Right(Directory, 1) <> "/"
        Directory + "/"
      EndIf
      
    CompilerEndIf
    
    DirectoryHandle = ExamineDirectory(#PB_Any, Directory, "*.*") 
    
    If DirectoryHandle <> 0
      
      While NextDirectoryEntry(DirectoryHandle) 
        
        EntryName = DirectoryEntryName(DirectoryHandle)
        
        If EntryName <> "." And EntryName <> ".." 
          
          Select DirectoryEntryType(DirectoryHandle)
              
            Case #PB_DirectoryEntry_File
              
              If EntryName = FileName
                Output = Directory
                Break
              Else
                Output = ""
              EndIf
              
            Case #PB_DirectoryEntry_Directory 
              
              Output = FindPureBasicCompiler(Directory + EntryName, FileName)
              
              If Output <> ""
                Break
              EndIf
              
          EndSelect
          
        EndIf
        
      Wend
      
      FinishDirectory(DirectoryHandle)
      
    EndIf
    
    ProcedureReturn Output
  EndProcedure 
  
  Procedure Initialize()
    
    CompilerSelect #PB_Compiler_OS
        
      CompilerCase #PB_OS_Windows 
        Instance\CompilerFile = "pbcompiler.exe"
        Instance\CompilerHome = FindPureBasicCompiler("C:\", Instance\CompilerFile)
        Instance\RunParamVersion = "/VERSION"
        Instance\RunParamHelp = "/HELP"
        
      CompilerCase #PB_OS_Linux
        Instance\CompilerFile =  "pbcompiler"
        Instance\CompilerHome = FindPureBasicCompiler("/bin/", Instance\CompilerFile)
        Instance\RunParamVersion = "-v"
        Instance\RunParamHelp = "-h"
        
        If Instance\CompilerHome = ""
          Instance\CompilerHome = FindPureBasicCompiler("/usr/share/purebasic/", Instance\CompilerFile)
        EndIf
        
        If Instance\CompilerHome = ""
          Instance\CompilerHome = FindPureBasicCompiler(GetHomeDirectory(), Instance\CompilerFile)
        EndIf
        
      CompilerCase #PB_OS_MacOS
        Instance\CompilerFile =  "pbcompiler"
        Instance\CompilerHome = FindPureBasicCompiler("/??????/", Instance\CompilerFile)
        Instance\RunParamVersion = "-v"
        Instance\RunParamHelp = "-h"
        
    CompilerEndSelect
    
    Instance\CompilerProgramFile = Instance\CompilerHome + Instance\CompilerFile
    Instance\IsInit = #True  
    
  EndProcedure

  Procedure.s CurrentVersion()
    
    Protected LIndex.l, RIndex.l, PB_Compiler_Handle.i
    
    If Instance\CurrentVersion = ""
      
      If Instance\IsInit = #False
        Initialize()
      EndIf
      
      PB_Compiler_Handle = RunProgram(Instance\CompilerProgramFile, Instance\RunParamVersion, "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
      
      If PB_Compiler_Handle 
        
        While ProgramRunning(PB_Compiler_Handle)
          If AvailableProgramOutput(PB_Compiler_Handle)
            Instance\CurrentVersion = ReadProgramString(PB_Compiler_Handle)
          EndIf
        Wend
        
        CloseProgram(PB_Compiler_Handle)
        
        LIndex = FindString(Instance\CurrentVersion, "PureBasic", 0) 
        RIndex = FindString(Instance\CurrentVersion, "(", 0) 
        
        If LIndex And RIndex 
          LIndex + Len("PureBasic") 
          Instance\CurrentVersion = Trim(Mid(Instance\CurrentVersion, LIndex, RIndex - LIndex))
        EndIf 
        
      EndIf
      
    EndIf
    
    ProcedureReturn Instance\CurrentVersion
  EndProcedure
  
  Procedure.s CurrentOS()
    
    Protected LIndex.l, RIndex.l, PB_Compiler_Handle.i
    
    If Instance\CurrentOS = ""
      
      If Instance\IsInit = #False
        Initialize()
      EndIf
      
      PB_Compiler_Handle = RunProgram(Instance\CompilerProgramFile, Instance\RunParamVersion, "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
      
      If PB_Compiler_Handle 
        
        While ProgramRunning(PB_Compiler_Handle)
          If AvailableProgramOutput(PB_Compiler_Handle)
            Instance\CurrentOS = ReadProgramString(PB_Compiler_Handle)
          EndIf
        Wend
        
        CloseProgram(PB_Compiler_Handle)
        
        LIndex = FindString(Instance\CurrentOS, "(", 0) + 1 
        RIndex = FindString(Instance\CurrentOS, ")", 0)
        
        If LIndex And RIndex
          Instance\CurrentOS = Trim(Mid(Instance\CurrentOS, LIndex, RIndex - LIndex))
        EndIf 
        
      EndIf
      
    EndIf
    
    ProcedureReturn "(" + Instance\CurrentOS + ")"
  EndProcedure
  
  Procedure.s CurrentVersionOS()
    
    Protected LIndex.l, RIndex.l, PB_Compiler_Handle.i
    
    If Instance\CurrentVersionOS = ""
      
      If Instance\IsInit = #False
        Initialize()
      EndIf

      PB_Compiler_Handle = RunProgram(Instance\CompilerProgramFile, Instance\RunParamVersion, "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
      
      If PB_Compiler_Handle 
        
        While ProgramRunning(PB_Compiler_Handle)
          If AvailableProgramOutput(PB_Compiler_Handle)
            Instance\CurrentVersionOS = ReadProgramString(PB_Compiler_Handle)
          EndIf
        Wend
        
        CloseProgram(PB_Compiler_Handle)
        
        LIndex = FindString(Instance\CurrentVersionOS, "PureBasic", 0)
        RIndex = FindString(Instance\CurrentVersionOS, "- (c)", 0)
        
        If LIndex And RIndex
          Instance\CurrentVersionOS = Trim(Mid(Instance\CurrentVersionOS, LIndex, RIndex - LIndex))
        EndIf 
        
      EndIf
      
    EndIf
    
    ProcedureReturn Instance\CurrentVersionOS
  EndProcedure
  
  Procedure.s Help()
    
    Protected LIndex.l, RIndex.l, PB_Compiler_Handle.i
    
    If Instance\Help = ""
      
      If Instance\IsInit = #False
        Initialize()
      EndIf
      
      PB_Compiler_Handle = RunProgram(Instance\CompilerProgramFile, Instance\RunParamHelp, "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
      
      If PB_Compiler_Handle 
        
        While ProgramRunning(PB_Compiler_Handle)
          If AvailableProgramOutput(PB_Compiler_Handle)
            Instance\Help + ReadProgramString(PB_Compiler_Handle) + #LineFeed
          EndIf
        Wend
        
        CloseProgram(PB_Compiler_Handle)
        
      EndIf
      
    EndIf
    
    ProcedureReturn Instance\Help
  EndProcedure
  
  Procedure Reset()
    
    Instance\CompilerProgramFile = ""
    Instance\CompilerFile = ""
    Instance\CompilerHome = ""
    Instance\RunParamVersion = ""
    Instance\RunParamHelp = ""
    Instance\CurrentVersion = ""
    Instance\CurrentOS = ""
    Instance\CurrentVersionOS = ""
    Instance\Help = ""
    Instance\IsInit = #False
    
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  Debug AskPureBasicCompiler::CurrentVersion()
  Debug AskPureBasicCompiler::CurrentOS()
  Debug AskPureBasicCompiler::CurrentVersionOS()
  Debug AskPureBasicCompiler::Help()
  
  AskPureBasicCompiler::Reset()
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<

Re: AskPureBasicCompiler - Module

Posted: Sun Feb 15, 2015 6:08 am
by netmaestro
Mine's a bit shorter:

Code: Select all

Debug #PB_Compiler_Version

Re: AskPureBasicCompiler - Module

Posted: Sun Feb 15, 2015 6:31 am
by chi
oh c'mon :)

Re: AskPureBasicCompiler - Module

Posted: Sun Feb 15, 2015 8:11 am
by StarBootics
netmaestro wrote:Mine's a bit shorter:

Code: Select all

Debug #PB_Compiler_Version
I'm sorry but this will not work because the value of #PB_Compiler_Version will remain the same. To understand why I have create these instruction, I have create a custom tool with PureBasic V4.20 and I don't recompile it since then.
If I have used the #PB_Compiler_Version my program will use " PureBasic V4.20" instead of "PureBasic V5.31".

As long as Fred don't change the way to communicate with the Compiler, will don't have to recompile my custom tool for many PureBasic version to come.

Best regards
StarBootics

Re: AskPureBasicCompiler - Module

Posted: Sun Feb 15, 2015 10:31 am
by Bisonte
Nice, but with this you MUST compile it in ASCII.... With Unicode it will not work...

And btw. you can't ask the PB Compiler wich OS Version is on system....
it shows only the started compiler versionsstring.

Re: AskPureBasicCompiler - Module

Posted: Sun Feb 15, 2015 3:38 pm
by StarBootics
@Bisonte

Compiling the example above in Unicode and this what I see in the Debugger output :
5.31
(Linux - x64)
5.31 (Linux - x64)
I don't know for you but apparently it work for me !

Best regards
StarBootics