unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Brett Viren <bv@bnl.gov>
Cc: guile-user@gnu.org
Subject: Re: SCM to C string
Date: Thu, 20 Jun 2002 17:04:32 -0400	[thread overview]
Message-ID: <15634.17248.994413.539036@minos.phy.bnl.gov> (raw)
In-Reply-To: <15634.15737.829121.207133@mriya.ua>

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....


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


  reply	other threads:[~2002-06-20 21:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-20 20:39 SCM to C string Viktor Pavlenko
2002-06-20 21:04 ` Brett Viren [this message]
2002-06-20 22:10   ` Viktor Pavlenko
2002-06-20 22:04 ` Christopher Cramer
2002-06-20 22:09   ` Viktor Pavlenko
2002-06-21  4:17     ` Viktor Pavlenko
2002-06-21 15:22       ` Rob Browning

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15634.17248.994413.539036@minos.phy.bnl.gov \
    --to=bv@bnl.gov \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).