One of my most important procedures: ProgramFilePath()

Share your advanced PureBasic knowledge/code with the community.
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

One of my most important procedures: ProgramFilePath()

Post by Axolotl »

Good Day,
I want to share one of my most important selfmade procedures which is called: ProgramFilePath()
The procedure took into consideration that I do not always use "Create temporary executable in the source directory".

Because I made the implementation in a include file I have to do the following in every main source file.

Code: Select all

; Internal use in INCLUDE files according to the respective main source file 
; 
#PB_Compiler_MainFilePath      = #PB_Compiler_FilePath     ; this kept the file path          of the main source
#PB_Compiler_MainFilename      = #PB_Compiler_Filename     ;  -"-          file name           -"-
#PB_Compiler_MainFile          = #PB_Compiler_File         ;  -"-          file path and name  -"-
In my include file
; <PathToMySourceCodes>\SharedCodes.pbi

Code: Select all

; ---------------------------------------------------------------------------------------------------------------------
;   ProgramFilePath() 
;
;     returns always (in all situations) the correct path to the executable program file (<name>.exe) 
;

Procedure.s ProgramFilePath()  ; returns file path -- of the executable (or the source file if in development) 
  Protected result.s  

  CompilerIf #PB_Editor_CreateExecutable       ; .. at runtime (easy) 
    result = GetPathPart(ProgramFilename()) 

  CompilerElse                                 ; .. at development time 
    CompilerIf Defined(PB_Compiler_MainFile, #PB_Constant) 
      If #PB_Compiler_MainFilename = "PB_EditorOutput.pb"   ; set constant in main file, than this works in include files) 
        Debug "● HINT: " + #PB_Compiler_Procedure + "() with unsaved main file returns ''" 
        result = ""   ; no valid program path  
      Else 
        result = #PB_Compiler_MainFilePath 
      EndIf 
    CompilerElse ; Defined(PB_Compiler_MainFile, #PB_Constant) 
      Debug "● HINT: " + #PB_Compiler_Procedure + "() "
      Debug "; The following constants are missing in main source code file: "   
      Debug "#PB_Compiler_MainFilePath = #PB_Compiler_FilePath     ; this kept the file path          of the main source " 
      Debug "#PB_Compiler_MainFilename = #PB_Compiler_Filename     ;               file name          " 
      Debug "#PB_Compiler_MainFile     = #PB_Compiler_File         ;               file path and name " 
      Debug "" 
    CompilerEndIf 
  CompilerEndIf 
  ProcedureReturn result 
EndProcedure 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).