RopeFromImpl.Mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
written by Bill Paxton, February 1981
Bill Paxton, July 2, 1982 1:21 pm
Maxwell, January 5, 1983 12:08 pm
Russ Atkinson, July 22, 1983 4:19 pm
Paul Rovner, August 10, 1983 4:23 pm
Doug Wyatt, January 17, 1984 11:51:56 am PST
Michael Plass, February 18, 1985 1:24:52 pm PST
DIRECTORY
Basics, FS, IO, Rope, RopeFile, RopeFrom
;
RopeFromImpl:
CEDAR
PROGRAM
IMPORTS FS, IO, Rope, RopeFile
EXPORTS RopeFrom = {OPEN RopeFrom;
ROPE: TYPE = Rope.ROPE;
Editing Operations
FlatReplaceByChar:
PUBLIC
PROC [base:
ROPE, char:
CHAR, start:
INT, len, baseSize:
INT, tryAppend:
BOOLEAN ←
TRUE]
RETURNS [new:
ROPE] = {
IF baseSize # MaxLen THEN base ← Rope.Substr[base, 0, baseSize];
IF start >= Rope.InlineSize[base] THEN new ← Rope.Concat[base: base, rest: Character[char]]
ELSE new ← Rope.Replace[base: base, start: start, len: len, with: Character[char]]
};
TryAppendReplaceByChar:
PUBLIC
PROC [base:
ROPE, char:
CHAR,
start:
INT, len, baseSize:
INT]
RETURNS [
ROPE] = {
returns NIL if cannot do a flat replace
RETURN [NIL]
};
FlatReplaceByString:
PUBLIC
PROC [base:
ROPE, string:
REF
READONLY
TEXT, stringStart:
NAT, stringNum:
NAT, start:
INT, len, baseSize:
INT, tryAppend:
BOOLEAN ←
TRUE]
RETURNS [new:
ROPE] = {
with: ROPE ~ String[string, stringStart, stringNum];
IF baseSize # MaxLen THEN base ← Rope.Substr[base, 0, baseSize];
IF start >= Rope.InlineSize[base] THEN new ← Rope.Concat[base: base, rest: with]
ELSE new ← Rope.Replace[base: base, start: start, len: len, with: with];
};
TryAppendReplaceByString:
PUBLIC
PROC [base:
ROPE, string:
REF
READONLY
TEXT,
stringStart:
NAT, stringNum:
NAT,
start:
INT, len, baseSize:
INT]
RETURNS [
ROPE] = {
RETURN [NIL]
};
String:
PUBLIC
PROC [string:
REF
READONLY
TEXT, start:
NAT, len:
NAT]
RETURNS [
ROPE] = {
copies len characters from string starting at start
Chars:
PROC
RETURNS [
CHAR] ~
{c: CHAR ← string[i]; i ← i + 1; RETURN [c]};
i: NAT ← start ← MIN[start, string.length];
len ← MAX[MIN[len, string.length-start], 0];
RETURN [Rope.FromProc[len: len, p: Chars]];
};
Character:
PUBLIC
PROC [char:
CHAR]
RETURNS [
ROPE] = {
RETURN [Rope.FromChar[char]]
};
FlatSubstr:
PUBLIC
PROC [rope:
ROPE, start:
INT, len:
INT]
RETURNS [
ROPE] = {
IF start >= Rope.InlineSize[rope] THEN RETURN [NIL];
RETURN [Rope.Substr[rope, start, len]];
};
FlatConcat:
PUBLIC
PROC [base, rest:
ROPE, baseSize:
INT, restSize:
INT, tryAppend:
BOOLEAN]
RETURNS [new:
ROPE] = {
IF baseSize # MaxLen THEN base ← Rope.Substr[base, 0, baseSize];
IF restSize # MaxLen THEN rest ← Rope.Substr[rest, 0, restSize];
new ← Rope.Concat[base, rest];
};
TryAppendConcat:
PUBLIC
PROC [base, rest:
ROPE, baseSize:
INT, restSize:
INT]
RETURNS [
ROPE] = {
RETURN [NIL]
};
FlatReplace:
PUBLIC
PROC [base:
ROPE, start:
INT, len:
INT, replace:
ROPE, size, baseSize:
INT, repSize:
INT]
RETURNS [
ROPE] = {
IF baseSize # MaxLen THEN base ← Rope.Substr[base, 0, baseSize];
IF repSize # MaxLen THEN replace ← Rope.Substr[replace, 0, repSize];
IF start >= Rope.InlineSize[base] THEN RETURN [Rope.Concat[base: base, rest: replace]]
ELSE RETURN [Rope.Replace[base: base, start: start, len: len, with: replace]];
};
FlatCopy:
PUBLIC
PROC [dest:
ROPE, destLoc:
INT, source:
ROPE, start:
INT, len:
INT, destSize:
INT]
RETURNS [
ROPE] = {
IF destSize # MaxLen THEN dest ← Rope.Substr[dest, 0, destSize];
IF start # 0 OR len # MaxLen THEN source ← Rope.Substr[source, start, len];
IF destLoc >= Rope.InlineSize[dest] THEN RETURN [Rope.Concat[base: dest, rest: source]]
ELSE RETURN [Rope.Replace[base: dest, start: destLoc, len: 0, with: source]];
};
***** File Operations
File:
PUBLIC
PROC [
file: FS.OpenFile,
start:
INT,
byte address in file where rope will start
length: INT,
defaults to rest of file beyond start
activate: BOOLEAN ← FALSE,
Remove activate from interface.
fileLen: INT
]
RETURNS [rope: ROPE] = {
stream: IO.STREAM ← NIL;
options: FS.StreamOptions ← FS.defaultStreamOptions;
options[tiogaRead] ← FALSE; -- include the formatting information
options[closeFSOpenFileOnClose] ← FALSE; -- don't close the openFile
stream ←
FS.StreamFromOpenFile[
openFile: file, accessRights: $read, streamOptions: options,
streamBufferParms: [vmPagesPerBuffer: 2, nBuffers: 1]];
RETURN[RopeFile.FromStream[stream: stream, start: start, len: length]];
};
Stream:
PUBLIC
PROC [stream:
IO.
STREAM, length:
INT]
RETURNS [rope: ROPE] = TRUSTED {
ros: IO.STREAM ← IO.ROS[];
bufSize: INT ~ 200;
buf: PACKED ARRAY [0..bufSize) OF CHAR;
UNTIL
IO.EndOf[stream]
OR length = 0
DO
bytesRead: INT ← IO.UnsafeGetBlock[stream, [base: @buf, startIndex: 0, count: MIN[length, bufSize]]];
IO.UnsafePutBlock[ros, [base: @buf, startIndex: 0, count: bytesRead]];
length ← length - bytesRead;
ENDLOOP;
rope ← IO.RopeFromROS[ros];
IO.Close[ros];
};
StartRopeFrom, StartRopeFromEdit, StartRopeFromFile: PUBLIC PROC = {};
}.
Plass, February 15, 1985 4:08:45 pm PST
Merged in RopeFromEditImpl, RopeFromFileImpl, RopeEditReplaceImpl
Added TSubstr and TConcat to localize dependence upon Rope.RopeRep