Access a common resource directory for all programs?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Access a common resource directory for all programs?

Post by Fangbeast »

Trying to setup a common directory for all my apps where they store all their databases, pictures, icons, other resources in etc.

All my resources get stored in /username/development/dev-current add each program has it's own /store directory off that where the resources are stored.

This works fine for each separate program

Code: Select all

currentDirectory.s  = GetPathPart(ProgramFilename())
databasedirectory.s = currentdirectory  + "store\database\"
picturedirectory.s  = currentdirectory  + "store\pictures\"
I now want to store all resources in a common directory to ALL my programs that is one directory back. Is this the right way to do it? Is there a better way?

This will save me nearly 1.6 gigabytes of duplicates.

Code: Select all

currentDirectory.s  = GetPathPart(ProgramFilename())
databasedirectory.s = currentdirectory  + "..\mystore\database\"
picturedirectory.s  = currentdirectory  + "..\mystore\pictures\"
Haven't even thought about common code re-use yet.
Amateur Radio, D-STAR/VK3HAF
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Access a common resource directory for all programs?

Post by Mistrel »

That's generally how I do it. Windows is seems consistently tolerant of this, in my experience. I don't know if it will pose a problem on other platforms.

Note that there is a 255 character limit on some API calls for paths on Win32 (such as with CreateFile()) but this is largely alleviated by Unicode being default, which instead has a limit of 32,767 characters.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Access a common resource directory for all programs?

Post by Little John »

Fangbeast wrote:I now want to store all resources in a common directory to ALL my programs that is one directory back. Is this the right way to do it? Is there a better way?

This will save me nearly 1.6 gigabytes of duplicates.
Another advantage will be: When you change such a common resource, all programs which use that resource automatically will use the new version. No need anymore to copy the new version to several other directories (which is easy to forget).

However, this can also be a disadvantage: The programs will not be independent from each other anymore. That is, when you change such a common resource, all programs which use that resource automatically will use the new version, even if that is not intended.

After this disadvantage happened to me several times (with common code), I personally now save all my programs independently from each other.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Access a common resource directory for all programs?

Post by mk-soft »

I think this is the right way...

Update

Code: Select all

;-TOP

#CommonDeveloper = "mk-soft"

Procedure.s GetProgramDataPath(Program.s = "")
  Protected path.s
  
  If Program = ""
    Program = GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension)
  EndIf
  path = GetUserDirectory(#PB_Directory_ProgramData) + #CommonDeveloper + #PS$
  If FileSize(path) <> -2
    If Not CreateDirectory(path)
      ProcedureReturn ""
    EndIf
  EndIf
  path = path + Program + #PS$
  If FileSize(path) <> -2
    If Not CreateDirectory(path)
      ProcedureReturn ""
    EndIf
  EndIf
  
  ProcedureReturn path
EndProcedure

Procedure.s GetAllUserDataPath(Program.s = "")
  Protected path.s
  
  If Program = ""
    Program = GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension)
  EndIf
  path = GetUserDirectory(#PB_Directory_AllUserData) + #CommonDeveloper + #PS$
  If FileSize(path) <> -2
    If Not CreateDirectory(path)
      ProcedureReturn ""
    EndIf
  EndIf
  path = path + Program + #PS$
  If FileSize(path) <> -2
    If Not CreateDirectory(path)
      ProcedureReturn ""
    EndIf
  EndIf
  
  ProcedureReturn path
EndProcedure

path_data.s = GetProgramDataPath("MyAPP")
If path_data = ""
  Debug "Error Data Path " + path_data
  End
EndIf

Debug path_data

path_alluser_data.s = GetAllUserDataPath("MyAPP")
If path_alluser_data = ""
  Debug "Error Data Path " + path_alluser_data
  End
EndIf

Debug path_alluser_data

path_alluser_data.s = GetAllUserDataPath("CommonData")
If path_alluser_data = ""
  Debug "Error Data Path " + path_alluser_data
  End
EndIf

Debug path_alluser_data
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Access a common resource directory for all programs?

Post by Fangbeast »

Thanks for all the replies and especially Little John's point.

I plan to update at least 20 of my old programs (should I live that long, the kitten is trying to eat me!!) so they will all be using the current version of the common code. That's what I intend anyhow.

Little things like test blocks of code that I do a lot of to prove that my ideas work won't need resource directories, just the odd config file or two so that should be no problem.

Thanks for the ideas all.
Amateur Radio, D-STAR/VK3HAF
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Access a common resource directory for all programs?

Post by Mistrel »

I think I understand now what you're trying to do to save disk space from duplicate files. I use symbolic links for this kind of thing religiously. Unfortunately, this is not supported well by Windows but it does work with caveats.

You can also use junctions for this kind of behavior but they have more limitations such as only working on directories and being unable to span volumes.

Check out Link Shell Extension for a useful tool to manage these kind of links.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Access a common resource directory for all programs?

Post by Fangbeast »

Mistrel wrote:I think I understand now what you're trying to do to save disk space from duplicate files. I use symbolic links for this kind of thing religiously. Unfortunately, this is not supported well by Windows but it does work with caveats.

You can also use junctions for this kind of behavior but they have more limitations such as only working on directories and being unable to span volumes.

Check out Link Shell Extension for a useful tool to manage these kind of links.
Thanks for the idea but I think I will reduce space for graphic (and other resources) as I mentioned above and as per Little John's warning, I will leave the actual code alone.

The 1.6 gig of duplicate files is the graphics alone so that will be a huge saving. But thanks for the idea as it's always good to have more than one option for other projects.
Amateur Radio, D-STAR/VK3HAF
Post Reply