#Slash-DefaultPath-BinPath-DataPath.pbi

Share your advanced PureBasic knowledge/code with the community.
uweb
User
User
Posts: 98
Joined: Wed Mar 15, 2006 9:40 am
Location: Germany

#Slash-DefaultPath-BinPath-DataPath.pbi

Post by uweb »

Code: Select all

; #Slash-DefaultPath-BinPath-DataPath.pbi
;
; based on this code: http://www.purebasic.fr/german/viewtopic.php?f=8&t=27741
;
; perceives SystemDefaultProgramDataDirectory but also can handle portable binary or binary from IDE (use the SourcePath)
; so it is universal useable - "Use your path here" is not necessary anymore
; perceives "\bin" and use if applicable "..\data" if SystemDefaultProgramDataDirectory is not available
;
; erkennt SystemDefaultProgramDataDirectory; kann aber auch mit portablen Programmen oder Programmen aus der IDE umgehen (nutzt den SourcePath)
; dadurch ist es universell nutzbar- "Use your path here" ist nicht mehr notwendig
; erkennt "\bin" und nutzt gegebenenfalls "..\data" wenn SystemDefaultProgramDataDirectory nicht verfügbar ist
;
EnableExplicit

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    #Slash = "\"
  CompilerDefault
    #Slash = "/"
CompilerEndSelect

Procedure.s GetDefaultDataDirectory()
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      ProcedureReturn GetEnvironmentVariable("APPDATA") + #Slash
    CompilerCase #PB_OS_MacOS
      ProcedureReturn GetHomeDirectory() + "Library/Application Support/"
    CompilerCase #PB_OS_Linux
      ProcedureReturn GetHomeDirectory() +"."
  CompilerEndSelect
EndProcedure

Define.s DataPath, DefaultPath=GetDefaultDataDirectory()
Define.s BinPath = DefaultPath+GetFilePart(ProgramFilename(),#PB_FileSystem_NoExtension)
;Define.s BinPath = DefaultPath+"MyDifferentDirectoryName" ; fix, from Preference, ProgramParameter() or whatever

If Bool(Left(GetFilePart(ProgramFilename()), 9) = "PureBasic")
  BinPath = GetPathPart(ProgramFilename())
Else
  If FileSize(BinPath)=-1
    BinPath = GetPathPart(ProgramFilename())
  EndIf
EndIf

If Right(BinPath,4)="bin"+#Slash
  DataPath = Left(BinPath, Len(BinPath)-4)+"data"+#Slash
  CreateDirectory(DataPath)
Else
  DataPath = BinPath
EndIf

Code: Select all

; for testing
;
EnableExplicit

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    #Slash = "\"
  CompilerDefault
    #Slash = "/"
CompilerEndSelect

Procedure.s GetDefaultDataDirectory()
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      ProcedureReturn GetEnvironmentVariable("APPDATA") + #Slash
    CompilerCase #PB_OS_MacOS
      ProcedureReturn GetHomeDirectory() + "Library/Application Support/"
    CompilerCase #PB_OS_Linux
      ProcedureReturn GetHomeDirectory() +"."
  CompilerEndSelect
EndProcedure

Define.s DataPath, DefaultPath=GetDefaultDataDirectory(), Title
Define.s BinPath = DefaultPath+GetFilePart(ProgramFilename(),#PB_FileSystem_NoExtension)
;Define.s BinPath = DefaultPath+"MyDifferentDirectoryName" ; fix, from Preference, ProgramParameter() or whatever

If Bool(Left(GetFilePart(ProgramFilename()), 9) = "PureBasic")
  Title= "from IDE" ; DataPath and BinPath = SourcePath
  BinPath = GetPathPart(ProgramFilename())
Else
  If FileSize(BinPath)=-1
    Title= "portable"
    BinPath = GetPathPart(ProgramFilename())
  Else
    Title="DefaultProgramDataDirectory"
  EndIf
EndIf

If Right(BinPath,4)="bin"+#Slash
  DataPath = Left(BinPath, Len(BinPath)-4)+"data"+#Slash
  CreateDirectory(DataPath)
Else
  DataPath = BinPath
EndIf

MessageRequester(Title, "BinPath :   "+BinPath+Chr(10)+"DataPath : "+DataPath, #PB_MessageRequester_Ok)
Please pardon my English, my native tongue is German.