[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] INI files in Scheme? (or something similar)
From: |
Mario Domenech Goulart |
Subject: |
Re: [Chicken-users] INI files in Scheme? (or something similar) |
Date: |
03 Mar 2008 22:58:43 -0300 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 |
Hi Hans,
On Mon, 03 Mar 2008 20:22:11 -0500 Hans Nowak <address@hidden> wrote:
> Is there a "standard" way to do INI files (or similar kinds of
> initialization files, ala .bashrc etc) in Scheme? For example,
> something like this:
>
> [config.ini]
> --->8---
> [Keys]
> Developer=INSERT_DEV_ID
> Application=INSERT_APP_ID
> Certificate=INSERT_CERT_ID
>
> [Server]
> ; This is for the development sandbox
> URL=api.sandbox.foobar.com
> Directory=/ws/api.dll
>
> [Authentication]
> ; blah blah blah
> Token=INSERT_USER_TOKEN
> --->8---
>
>
> I understand that this could easily be done in Scheme itself, e.g.
>
> (define developer INSERT_DEV_ID)
> (define url "api.sandbox.foobar.com")
Another approach would be translating the .ini syntax to Scheme, and
having a program to read the conf file as a list:
,----[ config.ini ]
| (keys
| (developer "INSERT_DEV_ID")
| (application "INSERT_APP_ID")
| (certificate "INSERT_CERT_ID"))
|
| (server
| (url "api.sandbox.foobar.com")
| (directory "/ws/api.dll"))
|
| (authentication
| (token "INSERT_USER_TOKEN"))
`----
,----[ config-reader.scm ]
| (define conf-file "config.ini")
|
| (define read-conf
| (let ((data #f))
| (lambda ()
| (unless data
| (set! data (with-input-from-file conf-file read-file)))
| data)))
|
| (define (conf-section section)
| (alist-ref section (read-conf)))
|
| (define (conf-item section item #!optional default)
| (cond ((alist-ref item (conf-section section))
| => (lambda (i) (car i)))
| (else default)))
|
| (print (conf-item 'server 'url))
`----
Or using parameters:
,----[ config.ini ]
| (conf:keys-application "something")
`----
,----[ config-reader.scm ]
| (define conf-file "config.ini")
| (define conf:keys-developer (make-parameter "INSERT_DEV_ID"))
| (define conf:keys-application (make-parameter "INSERT_APP_ID"))
| ;; ... other defaults here
|
| (load conf-file)
|
| (print (conf:keys-application))
`----
Of course, there are many other ways.
Best wishes,
Mario