unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* SCM to C string
@ 2002-06-20 20:39 Viktor Pavlenko
  2002-06-20 21:04 ` Brett Viren
  2002-06-20 22:04 ` Christopher Cramer
  0 siblings, 2 replies; 7+ messages in thread
From: Viktor Pavlenko @ 2002-06-20 20:39 UTC (permalink / raw)


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

I browsed the docs and could find anything. Any tips?

Thanks in advance

Viktor



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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SCM to C string
  2002-06-20 20:39 SCM to C string Viktor Pavlenko
@ 2002-06-20 21:04 ` Brett Viren
  2002-06-20 22:10   ` Viktor Pavlenko
  2002-06-20 22:04 ` Christopher Cramer
  1 sibling, 1 reply; 7+ messages in thread
From: Brett Viren @ 2002-06-20 21:04 UTC (permalink / raw)
  Cc: guile-user

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SCM to C string
  2002-06-20 20:39 SCM to C string Viktor Pavlenko
  2002-06-20 21:04 ` Brett Viren
@ 2002-06-20 22:04 ` Christopher Cramer
  2002-06-20 22:09   ` Viktor Pavlenko
  1 sibling, 1 reply; 7+ messages in thread
From: Christopher Cramer @ 2002-06-20 22:04 UTC (permalink / raw)
  Cc: guile-user

On Thu, Jun 20, 2002 at 04:39:21PM -0400, Viktor Pavlenko wrote:
> 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)".

char *objtostr(SCM object)
{
	char *str;
	size_t len;
	SCM s_str = scm_object_to_string(object, SCM_UNDEFINED);
	len = SCM_STRING_LENGTH(s_str);
	str = malloc(len + 1);
	if (!str) abort();
	memcpy(str, SCM_STRING_CHARS(s_str), len);
	str[len] = '\0';
	return str;
}

I haven't tested this, so maybe there are typos or something, but it
should give you the general idea.

-- 
Christopher Cramer <crayc@pyro.net> <http://www.pyro.net/~crayc/>
On résiste à l'invasion des armées; on ne résiste pas à l'invasion
des idées.  -- Victor Hugo

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SCM to C string
  2002-06-20 22:04 ` Christopher Cramer
@ 2002-06-20 22:09   ` Viktor Pavlenko
  2002-06-21  4:17     ` Viktor Pavlenko
  0 siblings, 1 reply; 7+ messages in thread
From: Viktor Pavlenko @ 2002-06-20 22:09 UTC (permalink / raw)
  Cc: guile-user

>>>>> "CC" == Christopher Cramer <crayc@pyro.net> writes:

    CC> On Thu, Jun 20, 2002 at 04:39:21PM -0400, Viktor Pavlenko wrote:

    >> For instance I'd like convert SCM object which contains '("a"
    >> 1) into C string "(\"a\" 1)".

    CC> [...]
    CC> 	SCM s_str = scm_object_to_string(object, SCM_UNDEFINED);

Ah, this is what I missed! Thanks!

Viktor


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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SCM to C string
  2002-06-20 21:04 ` Brett Viren
@ 2002-06-20 22:10   ` Viktor Pavlenko
  0 siblings, 0 replies; 7+ messages in thread
From: Viktor Pavlenko @ 2002-06-20 22:10 UTC (permalink / raw)
  Cc: guile-user

>>>>> "BV" == Brett Viren <bv@bnl.gov> writes:

    BV> Viktor Pavlenko writes:
    >> 
    >> For instance I'd like convert SCM object which contains '("a"
    >> 1) into C string "(\"a\" 1)".

    BV> Why do you need to escape the double quotes?  If you have a
    BV> null terminated string, you should be able to print it (or
    BV> pass it to a Gui or whatever) in C just fine.  Or do I miss
    BV> something.

You don't miss anything. I escaped the quotes to be able to show the
whole string in quotes.

    BV> I asked a similar question some time back, maybe the following
    BV> can help.  It turns a scheme procedure into its coresponding
    BV> source code.  But, I guess it will fail to do what you want
    BV> for compiled procedures as well as arbitrary scheme forms.
    BV> The following is C++, if you are using strict C just return
    BV> the "cptr" and ignore the "string" object.

I use C++. Thanks a lot!

Viktor

    BV> [code]


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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SCM to C string
  2002-06-20 22:09   ` Viktor Pavlenko
@ 2002-06-21  4:17     ` Viktor Pavlenko
  2002-06-21 15:22       ` Rob Browning
  0 siblings, 1 reply; 7+ messages in thread
From: Viktor Pavlenko @ 2002-06-21  4:17 UTC (permalink / raw)


>>>>> "VP" == Viktor Pavlenko <vvp@rogers.com> writes:

>>>>> "CC" == Christopher Cramer <crayc@pyro.net> writes:
    CC> On Thu, Jun 20, 2002 at 04:39:21PM -0400, Viktor Pavlenko wrote:

    >>> For instance I'd like convert SCM object which contains '("a"
    >>> 1) into C string "(\"a\" 1)".

    CC> [...]
    CC> SCM s_str = scm_object_to_string(object, SCM_UNDEFINED);

    VP> Ah, this is what I missed! Thanks!

I hurried with the reply... It _is_ described in the `Guile Reference
Manual' by Mark Galassi but it's not in libguile/*.h headers. It's not
in a snapshot version either. Neither I can find others like
scm_c_symbol2str or scm_symbol2str (I'm trying to use scm_ functions
you see).

Please advise how to deal with this mess^h^h^h^hpuzzle.

tia
Viktor


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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: SCM to C string
  2002-06-21  4:17     ` Viktor Pavlenko
@ 2002-06-21 15:22       ` Rob Browning
  0 siblings, 0 replies; 7+ messages in thread
From: Rob Browning @ 2002-06-21 15:22 UTC (permalink / raw)
  Cc: guile-user

Viktor Pavlenko <vvp@rogers.com> writes:

> I hurried with the reply... It _is_ described in the `Guile Reference
> Manual' by Mark Galassi but it's not in libguile/*.h headers. It's not
> in a snapshot version either. Neither I can find others like
> scm_c_symbol2str or scm_symbol2str (I'm trying to use scm_ functions
> you see).

Functions are being added to the scm_ interface as we prepare to
eventually deprecate the gh_ interface, so those functions may exist
in 1.5.X or CVS HEAD, but not in your version (1.4.X?).

> Please advise how to deal with this mess^h^h^h^hpuzzle.

Well, for now, you can use the equivalent gh_ functions if you're
running 1.4.X, and instead of scm_object_to_string, you could use
scm_simple_format.

Something like the following perhaps (though you may need to translate
some of the scm_ functions to their equivalent gh_ functions).  You
may also need better error checking:

  char *
  objtostr (SCM obj)
  {
    char *result;
    size_t length;

    SCM fmt = scm_makfrom0str("~S");
    SCM scmstr = scm_simple_format(SCM_BOOL_F, fmt, SCM_LIST_1(obj));
    length = SCM_STRING_LENGTH(scmstr);
    result = malloc(length + 1);
    if (!result) die...
    memcpy (result, SCM_STRING_CHARS (scmstr), length)
    result[length] = '\0';
    return result;
  }

-- 
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG=1C58 8B2C FB5E 3F64 EA5C  64AE 78FE E5FE F0CB A0AD

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-06-21 15:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-06-20 20:39 SCM to C string Viktor Pavlenko
2002-06-20 21:04 ` Brett Viren
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

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