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]

Re: schemeports and Filepointers


| What will be the new way to convert Scheme Portobjects to Filepointers
| and back on the C/libguile level, after the reformation of Houston's?
| In order to add SCREEN types to my curses stubs, it will be necessary
| to use FILE*<->port conversion , as the newterm call in curses takes
| FILE* s as arguments. On Scheme level that will mean assigning a port
| to the terminal.

Something like this should work with both formulations:

SCM_PROC (s_fputs, "fputs", 2, 0, 0, scm_fputs);
SCM
scm_fputs (SCM string, SCM port)
{
  int fdes;
  int fdes2;
  FILE *file;

  port = SCM_COERCE_OUTPORT (port);
  fdes = SCM_INUM (scm_fileno (port));
  SCM_ASSERT (SCM_NIMP (string) && SCM_STRINGP (string), string, SCM_ARG2,
	      s_fputs);
  fdes2 = dup (fdes);
  if (fdes2 == -1)
    scm_syserror (s_fputs);
  file = fdopen (fdes2, "w");
  if (file == NULL)
    scm_syserror (s_fputs);
  setvbuf (file, NULL, _IONBF, 0);
  if (fputs (SCM_CHARS (string), file) == EOF)
    scm_syserror (s_fputs);
  if (fclose (file) == EOF)
    scm_syserror (s_fputs);
  return SCM_UNSPECIFIED;
}

Creating a new FILE each call seems like a lot of overhead. 
Maybe the port should have fields for "user-data" so that the FILE
could conveniently be made persistent.