site/posts/HI.mdwn

68 lines
2.5 KiB
Markdown

<p>Since I have nothing better to say and still want to say something,
here is some nonsense:
</p>
<p>
I've been having an issue with loading configuration and data files.
The problem is that I like the <a href="http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html">XDG Base Directory Specification</a> and am
trying to write my software in such a way that it supports both this
and the usual(/old) folder in <code>$HOME</code> approach.
</p>
<p>
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 <code>$HOME</code> 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.
</p>
<p>
I feel that I may have it now, though&hellip;
</p>
[[!format el """
(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"))
"""]]
<p>
The <code>stored-file</code> function first assumes that we will want to grab it
from the directory in <code>$HOME</code>, when it can't verify that <i>that</i> particular
file exists and knows that the XDG variable isn't empty it will
instead return a reference to the file in that directory.
</p>
<p>
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 <code>$HOME</code> 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.
</p>
[[!meta date="2012-10-03 02:35:41"]]
[[!tag scheme xdg coding]]