Hi, On Tue, 12 Nov 2013 16:33:41, Dodji Seketeli wrote: > > +/* Reads the next line from FILE into *LINE. If *LINE is too small > + (or NULL) it is allocated (or extended) to have enough space to > + containe the line. *LINE_LENGTH must contain the size of the > + initial*LINE buffer. It's then updated by this function to the > + actual length of the returned line. Note that the returned line > + can contain several zero bytes. Also note that the returned string > + is allocated in static storage that is going to be re-used by > + subsequent invocations of read_line. */ > + > +static bool > +read_next_line (fcache *cache, char ** line, ssize_t *line_len) > +{ > + char *l = NULL; > + ssize_t len = get_next_line (cache, &l); > + > + if (len> 0) > + { > + if (*line == NULL) > { > - string[pos + len - 1] = 0; > - return string; > + *line = XNEWVEC (char, len); > + *line_len = len; > } > - pos += len; > - string = XRESIZEVEC (char, string, string_len * 2); > - string_len *= 2; > + else > + if (*line_len < len) > + *line = XRESIZEVEC (char, *line, len); > + > + memmove (*line, l, len); > + (*line)[len - 1] = '\0'; > + *line_len = --len;
Generally, I would prefer to use memcpy, if it is clear that the memory does not overlap. You copy one char too much and set it to zero? Using -- on a value that goes out of scope looks awkward IMHO. Bernd. > + return true; > } > - > - return pos ? string : NULL; > + > + return false; > +}