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: port-line bug details & patch question.


hjstein@bfr.co.il (Harvey J. Stein) writes:

> Greg Harvey <Greg.Harvey@thezone.net> writes:
> 
>  > "Harvey J. Stein" <hjstein@bfr.co.il> writes:
>  >
>  > > My questions are:
>  > > 
>  > > 1. Where should it be called for line reading?  The options, as far as
>  > > I can tell are:
>  > > 
>  > >    scm_do_read_line
>  > 
>  > It can't be (as easily) done there, because fgets & generic_fgets
>  > munch off the newline (you could check for eof).
> 
> Isn't that a semantic issue?  Is the # of lines supposed to = the number
> of newlines?  Or should it be the number of strings that (read-line)
> returns?  

The first is what scm_getc is doing. It might not be the best way of
looking at it, but it is consistant. I'm not entirely sure that it
matters either way, though. I was thinking that it would be more
convienient to put it in fgets, so that other port objects could
define their own semantics for counting lines. It also has the
advantage that anyone who goes around read-line to use a port's fgets
method in c code will still get the proper line count. 

> I was thinking of the latter, in which case it would be
> incremented when (read-line) reads the last line in a file, even if it
> isn't newline terminated.  It's kind of irrelevant since the next
> thing to be read will be eof-object anyway, but doing it my way would
> allow just putting an increment into scm_do_read_line if it's not
> returning eof-object (& doing the same in fgets & generic_fgets, which
> is marginally more complicated because of how they're written).

It isn't very pretty ;) This is the patch that I sent to Jim Blandy for
scm_fgets (I'm not entirely sure this is correct, either... I don't
seem to be entirely sure about anything today)

1998-10-08  Greg Harvey  <Greg.Harvey@thezone.net>

	* fports.c (scm_fgets): added port line incrementing


*** guile-core/libguile/fports.c	Thu Oct  8 20:30:40 1998
--- guile-core.dev/libguile/fports.c	Fri Oct  9 03:23:50 1998
***************
*** 323,328 ****
--- 323,329 ----
        if (buf[(*len)++] == '\n')
  	{
  	  buf[*len] = '\0';
+ 	  SCM_INCLINE (port);
  	  return buf;
  	}
      }
***************
*** 341,347 ****
  
        pos = ftell (f);
        if (fgets (p, chunk_size, f) == NULL) {
! 	if (*len)
  	  return buf;
  	free (buf);
  	return NULL;
--- 342,348 ----
  
        pos = ftell (f);
        if (fgets (p, chunk_size, f) == NULL) {
!         if (*len)
  	  return buf;
  	free (buf);
  	return NULL;
***************
*** 349,356 ****
        numread = ftell (f) - pos;
        *len += numread;
  
!       if (numread < chunk_size - 1 || buf[limit-2] == '\n')
  	return buf;
  
        buf = (char *) realloc (buf, sizeof(char) * limit * 2);
        limit *= 2;
--- 350,363 ----
        numread = ftell (f) - pos;
        *len += numread;
  
!       if (numread < chunk_size - 1) {
!         if(buf[*len-1] == '\n') SCM_INCLINE(port);
  	return buf;
+       }
+       if(buf[limit-2] == '\n') {
+ 	SCM_INCLINE(port);
+ 	return buf;
+       }
  
        buf = (char *) realloc (buf, sizeof(char) * limit * 2);
        limit *= 2;


-- 
Greg