[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: SCM to C string
From: |
Brett Viren |
Subject: |
Re: SCM to C string |
Date: |
Thu, 20 Jun 2002 17:04:32 -0400 |
Viktor Pavlenko writes:
> Hi All,
>
> I've started playing with guile recently and am very excited about
> it:)
>
> My program parses a scheme config file. If configuration is not valid
> from the application point of view (but is a valid scheme expression)
> I signal error. What I would like to do is to dump the offending SCM
> object to C-style string so that I can display it as debugging
> information.
>
> For instance I'd like convert SCM object which contains '("a" 1) into
> C string "(\"a\" 1)".
Why do you need to escape the double quotes? If you have a null
terminated string, you should be able to print it (or pass it to a Gui
or whatever) in C just fine. Or do I miss something.
I asked a similar question some time back, maybe the following can
help. It turns a scheme procedure into its coresponding source code.
But, I guess it will fail to do what you want for compiled procedures
as well as arbitrary scheme forms. The following is C++, if you are
using strict C just return the "cptr" and ignore the "string" object.
string EOS::Guile::ScmSource(SCM proc)
{
static SCM result = SCM_UNDEFINED;
if (result == SCM_UNDEFINED) {
result = gh_eval_str("(lambda (proc) (call-with-output-string (lambda
(p) (display (cddr (procedure-source proc)) p))))");
scm_protect_object(result);
}
SCM src = gh_call1(result,proc);
char* cptr = gh_scm2newstr(src,0);
cptr[strlen(cptr)-1] = '\0';
string s(cptr+1);
cptr[0] = '0';
free (cptr);
return s;
}
-Brett.
PS: looking at this old code now and relating to another recent
thread, I should probably be calling "scm_unprotect_object(result)"
after coping out the string....
Hmm, also, I'm not sure why I am truncating that last character....