On 7/3/06, Petr Hlozek <[EMAIL PROTECTED]> wrote:
I'm developing Linux application and I need install some share data. How should I work with these data in program? Can I use direct path to data (/usr/share/myapp/data)?
Well, that depends if you want your app to work on all linuxes and all unixes (and even all windows!), or you just want it to work on a specific linux distribution. For just a specific distribution this will work. This won´t work on some distributions that use different file paths, like GoboLinux, and won´t work on other unixes. I already faced that problem on the magnifier, bellow is a striped version of a code to solve it. The important parts is that it stores a configuration file on the same place as the executable (on win32), or it stores at a place responded by ConfigFilePath := GetAppConfigFile(False); on unixes. The it uses this to get the directory of the executable. DefaultDirectory is a constant equal to '/usr/share/myapp'. You can also see the full source with comments here: http://magnifier.cvs.sourceforge.net/magnifier/magnifierv3/appsettings.pas?revision=1.20&view=markup TConfigurations = class(TObject) private ConfigFilePath: string; public {other settings as fields here} MyDirectory: string; constructor Create; destructor Destroy; override; procedure ReadFromFile(Sender: TObject); procedure Save(Sender: TObject); end; var vConfigurations: TConfigurations; vTranslations: TTranslations; implementation { TConfigurations } constructor TConfigurations.Create; begin {$ifdef win32} ConfigFilePath := ExtractFilePath(Application.EXEName) + 'magnifier.ini'; {$endif} {$ifdef Unix} ConfigFilePath := GetAppConfigFile(False) + '.conf'; {$endif} ReadFromFile(nil); end; destructor TConfigurations.Destroy; begin Save(nil); inherited Destroy; end; procedure TConfigurations.Save(Sender: TObject); var MyFile: TIniFile; begin MyFile := TIniFile.Create(ConfigFilePath); try MyFile.WriteString(SectionUnix, IdentMyDirectory, MyDirectory); finally MyFile.Free; end; end; procedure TConfigurations.ReadFromFile(Sender: TObject); var MyFile: TIniFile; begin MyFile := TIniFile.Create(ConfigFilePath); try {$ifdef Win32} MyDirectory := MyFile.ReadString(SectionUnix, IdentMyDirectory, ExtractFilePath(Application.EXEName)); {$else} MyDirectory := MyFile.ReadString(SectionUnix, IdentMyDirectory, DefaultDirectory); {$endif} finally MyFile.Free; end; end; initialization vConfigurations := TConfigurations.Create; finalization FreeAndNil(vTranslations); end. -- Felipe Monteiro de Carvalho _________________________________________________________________ To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe" as the Subject archives at http://www.lazarus.freepascal.org/mailarchives
