This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

scheme-string to C


Hi,

I'am trying to display some guile expressions in a gtk window. The
application is writen in C.
My first try is to install a softport that calls my C functions from
within guile. The guile manual explains that guile's strings may
contain arbitrary binary data, but if I pass a pointer to an integer
to the function gh_scm2newstr I should get the "real" length of an
string. Anything works fine as long I only try to display normal
strings. Passing my code a list for example, garbles my console
window. A lot of binary data will be displayed. So why?

Here is I did it:

void scheme_init(void)
{
    char cmd[] =
        "(set-current-output-port"
        " (make-soft-port"
        "  (vector"
        "   (lambda (c) (guiguile-write-char c))"
        "   (lambda (s) (guiguile-write-string s))"
        "   (lambda () (guiguile-flush-output))"
        "   (lambda () ('#f))"
        "   (lambda () ('#f)))"
        "  \"w\"))";
    
    scheme_register_soft_port();
    gh_eval_str(cmd);
}

static void scheme_register_soft_port(void)
{
    gh_new_procedure0_1("guiguile-write-char", scheme_write_char);
    gh_new_procedure0_1("guiguile-write-string", scheme_write_string);
    gh_new_procedure0_0("guiguile-flush-output", scheme_flush_output);
}

static SCM scheme_write_char(SCM val)
{
    char c;

    assert(gh_char_p(val));
    c = gh_scm2char(val);
    printf("scheme_write_char():\t%c\n", c);
    return SCM_TRUE();
}

static SCM scheme_write_string(SCM val)
{
    char *str;
    int  len;

    gh_defer_ints();
    str = gh_scm2newstr(val, &len);
    if (len < 0)
        err_msg("got scheme-string with length: %d\n", len);
    printf("scheme_write_string() with len: %d\t%s\n", len, str);
	free(str);
    gh_allow_ints();
    return SCM_TRUE();
}

static SCM scheme_flush_output(void)
{
    printf("scheme_flush_output()\n");
    fflush(stdout);
    return SCM_TRUE();
}

Marcus
-- 
Marcus.Geiger@bigfoot.com
http://www.informatik.fh-muenchen.de/~ifw98070

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]