Subject: Re: Reading scm files
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 2000/05/03
Newsgroups: comp.lang.scheme
Message-ID: <8eo1e8$6fqsc$1@fido.engr.sgi.com>
Houalla, M. R. (Martin) <mhoualla@eccms1.dearborn.ford.com> 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:http://www.altera.gr/dsouflis/tinyscm.html> or SIOD
<URL:http://people.delphi.com/gjc/siod.html> *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 = {")
	    (newline)
	    (let loop ((expr (read)))
	      (if (eof-object? expr)
		'done
		(for-each
		  (lambda (item)
		    (display "    { \"")
		    (display (car item))
		    (display "\", ")
		    (display (cadr item))
		    (display " },")
		    (newline))
		  expr)))
	    (display "};")
	    (newline)))

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

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