Platform Line Termination?

Stefan Matthias Aust sma at 3plus4.de
Sun Feb 6 18:41:18 UTC 2000


I'd suggest to not depend on any encoding.  It might even change within the
file.  All you can depend on is, that CR will prepend LF.  And don't depend
the line end encoding on the platform!  I want to read unix files on
Windows, too.

For one of my parsers I use the following methods

nextLine
  | cr lf ch line |
  cr := Character cr.
  lf := Character lf.
  ch := 0 asCharacter.
  line := WriteStream on: (String new: 255).
  [stream atEnd or: [(ch := stream next) == cr or: [ch == lf]]]
	whileFalse: [line nextPut: ch].
  ch == cr ifTrue: [stream peekFor: lf].
  ^ line contents

previousLine
  | cr lf ch position line |
  cr := Character cr.
  lf := Character lf.
  ch := 0 asCharacter.
  stream skip: -1.
  stream peek == lf ifTrue:
    [ch := lf.
    stream skip: -1].
  stream peek == cr ifTrue:
    [ch := cr.
    stream skip: -1].
  [stream position > 0 and: [stream peek ~= lf and: [stream peek ~= cr]]]
    whileTrue:
      [stream skip: -1].
  position := stream skip: 1; position.
  line := stream upTo: ch.
  stream position: position.
  ^ line

bye
--
Stefan Matthias Aust  //  Bevor wir fallen, fallen wir lieber auf.





More information about the Squeak-dev mailing list