On Mon, 1 Dec 2003, Dirk Laurie wrote:

> Don Simons skryf:
> >
> > It was the d*#@ line-ending characters! Why doesn't everyone just use
> > Windows? :-)
> >
> One might also ask the question: why can't all compilers produce code
> that will accept any of CR, LF, CR+LF or LF+CR as end-of-line?

This was part of ANSI C standard (where you had the choice between text
and binary files). The gcc compiler (i.e. its standard io library), as
being POSIX conform, accepts still the mode option b/t in fopen for
compatibility reasons, but alas, it has no effect any more and this
helpful feature is broken now. The situation might be similar for other
compilers.

The only clean solution: write your own input routine which is capable of
handling the different line end codings.

--snip-- without guarantee

char *my_fgets(char *s, int size, FILE *stream) {
  int c, no_line_end = 1, pos = 0;

  if (--size < 0) return NULL;

  while (no_line_end && (pos < size)) {
    no_line_end = 0;
    switch (c = fgetc(stream)) {
      case EOF:
        if (!pos) return NULL; // error
        break;

      case LF:
        if ((c = fgetc(stream)) != CR) ungetc(c, stream);
        s[pos++] = LOCAL_LINE_END;
        break;

      case CR:
        if ((c = fgetc(stream)) != LF) ungetc(c, stream);
        s[pos++] = LOCAL_LINE_END;
        break;

      default:
        no_line_end = 1;
        s[pos++] = c;
    }
  }

  s[pos] = 0;
  return s;
}


static char last_character = 0;
size_t freadtext(void *ptr, size_t count, FILE *stream) {
// ignores all subsequent LF after a CR and vice versa
  size_t bytes_read = 0;
  int c;

  while (bytes_read < count)
    switch (c = fgetc(stream)) {
      case EOF:
        goto LoopEnd;

      case LF:
        if (last_character == CR) break;
        ((char*)ptr)[bytes_read++] = LOCAL_LINE_END;
        last_character = LF;
        break;

      case CR:
        if (last_character == LF) break;
        ((char*)ptr)[bytes_read++] = LOCAL_LINE_END;
        last_character = CR;
        break;

      default:
        last_character = ((char*)ptr)[bytes_read++] = c;
    }
LoopEnd:
  return bytes_read;
}

--snap--

(you might also ask the question why DOS did not use what was already
established)

regards
Bernhard

_______________________________________________
Tex-music mailing list
[EMAIL PROTECTED]
http://icking-music-archive.org/mailman/listinfo/tex-music

Reply via email to