/* ----------------------------------------------------------------------------- Copyright 2019-2020 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] define ReadIterator { @value ReadPosition<#x> reader @value Int position fromReadPosition (reader) { return fromReadPositionAt<#y>(reader,0) } fromReadPositionAt (reader,position) { Int position2 <- position if (position < 0) { position2 <- -1 } if (position >= reader.readSize() || reader.readSize() == 0) { position2 <- reader.readSize() } return ReadIterator<#y>{ reader, position } } readCurrent () { if (pastForwardEnd() || pastReverseEnd()) { ~ LazyStream$new() .append("Position ") .append(position) .append(" is out of bounds") .writeTo(SimpleOutput$error()) } return reader.readPosition(position) } forward () { return fromReadPositionAt<#x>(reader,position+1) } pastForwardEnd () { return position >= reader.readSize() } reverse () { return fromReadPositionAt<#x>(reader,position-1) } pastReverseEnd () { return position < 0 || reader.readSize() == 0 } } define LazyStream { @value optional #x value @value optional LazyStream<#x> next new () { return LazyStream<#x>{ empty, empty } } append (value2) { return LazyStream<#x>{ value2, self } } @value recursive (Writer<#x>) -> () recursive (writer) { if (present(next)) { ~ require(next).recursive(writer) } if (present(value)) { ~ writer.write(require(value)) } } writeTo (writer) { ~ recursive(writer) ~ writer.flush() } } define TextReader { @value BlockReader reader @value String buffer fromBlockReader (reader) { return TextReader{ reader, "" } } readNextLine () { while (true) { Int newline <- findNewline() if (newline < 0) { if (reader.pastEnd()) { cleanup { buffer <- "" } in return buffer } else { // TODO: Maybe the block size should be a factory argument. buffer <- buffer+reader.readBlock(\x100) } } else { return takeLine(newline) } } return "" } pastEnd () { return buffer.readSize() == 0 && reader.pastEnd() } @value findNewline () -> (Int) findNewline () { scoped { Int position <- 0 } in while (position < buffer.readSize()) { Char c <- buffer.readPosition(position) // TODO: Maybe the line separator should be a factory argument. if (c == '\n' || c == '\r') { return position } } update { position <- position+1 } return -1 } @value takeLine (Int) -> (String) takeLine (position) { String data <- buffer.subSequence(0,position) buffer <- buffer.subSequence(position+1,buffer.readSize()-position-1) return data } }