Subject: Re: Reading scm files
From: (Rob Warnock)
Date: 2000/05/03
Newsgroups: comp.lang.scheme
Message-ID: <8eo1e8$6fqsc$>
Houalla, M. R. (Martin) <> wrote:
| I have a couple of scm data files (list files). Is there a quick way
| to read them in a c program without having to read a line by line and
| parse the line fields?

Well... You could always link one of the small free Schemes (such as
"tinyscm" <URL:> or SIOD
<URL:> *with* your C program,
and let the Scheme code read it and return you a tree (or list of trees,
if the files have more than one S-expr per file).

Or you could "popen()" a Scheme script that would read them for you
and print them on stdout in whatever format you wanted [also see below].

Or if the data files don't change very often, write a Scheme script that
reads them in and writes out C *source* code, which you then compile with
your program. E.g., give a data file "test.dat" containing this:

	((foo 123) (bar 456) ("baz7253!@#" 789))

this Scheme program [caution: almost no error-checking!]:

	(with-input-from-file "test.dat"
	  (lambda ()
	    (display "struct mytest_s {char *str; int num;} mydat = {")
	    (let loop ((expr (read)))
	      (if (eof-object? expr)
		  (lambda (item)
		    (display "    { \"")
		    (display (car item))
		    (display "\", ")
		    (display (cadr item))
		    (display " },")
	    (display "};")

would produce this output:

	struct mytest_s {char *str; int num;} mydat = {
	    { "foo", 123 },
	    { "bar", 456 },
	    { "baz7253!@#", 789 },

[I'll leave it to you to improve the formatting...]


Rob Warnock, 41L-955
Applied Networking
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		PP-ASEL-IA
Mountain View, CA  94043