This is the mail archive of the guile@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]

gh_ conversion between C strings and Scheme objects



I've been looking into using Guile as the method for selecting atoms in a
small molecular graphics program I'm writing. I was thinking I would
provide a window with a very simple text editor for entering and
evaluating Guile expressions. I wrote the text editor in C so I need to be
able to "convert" C strings to SCM objects and vice versa. I could use the
following functions but I'm wondering if there are either gh_ functions
which do the same thing or there's another better way?

Thanks,
Westley

SCM string2scm(const char * string, int length) {
  char * tmp_str;
  SCM object;

  tmp_str = malloc(length + 1);
  strncpy(tmp_str, string, length);
  tmp_str[length] = 0;

  object = gh_eval_str(tmp_str);

  free(tmp_str);

  return object;
}

char * scm2string(SCM object) {
  SCM stringify;
  char * string;

  stringify = gh_eval_str("(lambda(object)"
			  " (with-output-to-string"
			  "  (lambda() (write object))))");

  string = gh_scm2newstr(gh_call1(stringify, object), 0);

  return string;
}