3 October 2012 2:35 AM (scheme | xdg | coding)
Since I have nothing better to say and still want to say something, here is some nonsense:
I've been having an issue with loading configuration and data files.
The problem is that I like the XDG Base Directory Specification and am
trying to write my software in such a way that it supports both this
and the usual(/old) folder in $HOME
approach.
This doesn't sound all that difficult, nor is it, but still I was
making a lot of mistakes with it, because I don't just want to support
something, I don't want to force whomever (nobody but me, really) uses
my software to be forced in to anything. So first I need to check if
the XDG environment variables exist, if they do then I have to see if
the file I want to load exists in that directory, if it doesn't and
one does exist in $HOME
I want that one, but if the one in home
doesn't exist either, I want the one in the XDG folder again. It gets
messy.
I feel that I may have it now, though…
(define (stored-file xdg-env file) "Try to get FILE stored either in XDG-ENV or the home directory." (let ((xdg (getenv xdg-env)) (stored-file (string-append (getenv "HOME") "/.project/" file))) (unless (or (file-exists? stored-file) (not xdg)) (set! stored-file (string-append xdg "/project/" file))) stored-file)) (define (config-file file) (stored-file "XDG_CONFIG_HOME")) (define (data-file file) (stored-file "XDG_DATA_HOME")) (define (rc-file) (config-file "rc.scm")) (define (list-file) (data-file "list.scm"))
The stored-file
function first assumes that we will want to grab it
from the directory in $HOME
, when it can't verify that that particular
file exists and knows that the XDG variable isn't empty it will
instead return a reference to the file in that directory.
All the other functions are just there to make it clear and convenient
to load these files and not specifically necessary. The good part of
this function, in my opinion, is that is separates configuration and
data files from each other in code, but will just grab it all from the
same directory when working with the $HOME
directory. This, of
course, can create naming conflicts, but if you have naming conflicts
this way you might get confused by what you're doing here or there
anyway.