<> <> <> <> <> <> <> <> <> DIRECTORY Ascii USING [Lower], Atom USING [MakeAtom, MakeAtomFromRefText], BasicTime USING [GMT], FileOps USING [comment, endNode, endOfFile, IntBytes, LengthByte, look1, look2, look3, looks, looksFirst, LooksIndex, looksLast, Op, otherNode, otherNodeShort, otherNodeSpecs, otherNodeSpecsShort, prop, PropIndex, propShort, rope, runs, startNode, startNodeFirst, startNodeLast, terminalTextNode, terminalTextNodeFirst, terminalTextNodeLast, ThirdByte, FormatIndex], FileReader USING [FromRope, FromStream, InterDoc, Open, OpenC], FS USING [ComponentPositions, ExpandName, GetName, OpenFile, Position], InterFile USING [FromRope], IO USING [STREAM], NodeProps USING [DoSpecs, PutProp], PGSupport USING [BadIndex, CreatePGF, EnterLooks, EnterProp, EnterFormatName, FreePGF, PGF, RetrieveLooks, RetrieveProp, RetrieveFormatName], PutGet USING [], Rope USING [MaxLen, ROPE, Substr, Translate], RopeEdit USING [MaxLen, Substr], RopeReader USING [Backwards, BumpIndex, Get, GetRope, Position, ReadOffEnd, Ref, SetIndex], RuntimeError USING [UNCAUGHT], SafeStorage USING [SetCollectionInterval], TextEdit USING [], TextLooks USING [BaseRuns, Concat, Looks, LooksBytes, Runs], TextLooksSupport USING [NewBase, Short], TextNode USING [Body, MaxLen, Ref, RefTextNode]; GetFileImpl: CEDAR MONITOR IMPORTS Ascii, Atom, FileReader, FS, InterFile, NodeProps, PGSupport, Rope, RopeEdit, RopeReader, RuntimeError, SafeStorage, TextLooks, TextLooksSupport EXPORTS PutGet SHARES TextLooks = BEGIN OPEN FileOps; ROPE: TYPE = Rope.ROPE; Byte: TYPE = [0..255]; FromFileError: PUBLIC ERROR = CODE; FromFile: PUBLIC PROC [fileName: ROPE, start: INT _ 0, len: INT _ TextNode.MaxLen] RETURNS [root: TextNode.Ref] = { control, comment, stext: RopeReader.Ref; interdoc: ROPE; tiogaFile: BOOL; fh: FS.OpenFile; createDate: BasicTime.GMT; StartRead[]; { ENABLE UNWIND => EndRead[]; [control, comment, stext, tiogaFile, fh, createDate] _ FileReader.Open[fileName, start, len ! FileReader.InterDoc => { interdoc _ doc; RESUME }]; root _ IF interdoc#NIL THEN InterFile.FromRope[interdoc] ELSE Finish[control, comment, stext, tiogaFile]; }; AddFileExtension[root, fh]; AddCreateDate[root, createDate]; EndRead[]; }; FromFileC: PUBLIC PROC [file: FS.OpenFile, start: INT _ 0, len: INT _ TextNode.MaxLen] RETURNS [root: TextNode.Ref] = { control, comment, stext: RopeReader.Ref; interdoc: ROPE; tiogaFile: BOOL; createDate: BasicTime.GMT; StartRead[]; { ENABLE UNWIND => EndRead[]; [control, comment, stext, tiogaFile, createDate] _ FileReader.OpenC[file, start, len ! FileReader.InterDoc => { interdoc _ doc; RESUME }]; root _ IF interdoc#NIL THEN InterFile.FromRope[interdoc] ELSE Finish[control, comment, stext, tiogaFile]; }; AddFileExtension[root, file]; AddCreateDate[root, createDate]; EndRead[]; }; AddFileExtension: PROC [root: TextNode.Ref, file: FS.OpenFile] = { ForceLower: PROC [r: ROPE] RETURNS [ROPE] = { ForceCharLower: PROC [old: CHAR] RETURNS [new: CHAR] = { RETURN [Ascii.Lower[old]]; }; RETURN [Rope.Translate[base: r, translator: ForceCharLower]]; }; name: ROPE; cp: FS.ComponentPositions; [fullFName: name, cp: cp] _ FS.ExpandName[name: FS.GetName[file].fullFName]; NodeProps.PutProp[root, $FileExtension, IF cp.ext.length=0 THEN $null ELSE Atom.MakeAtom[ForceLower[name.Substr[cp.ext.start, cp.ext.length]]]]; }; AddCreateDate: PROC [root: TextNode.Ref, createDate: BasicTime.GMT] = { NodeProps.PutProp[root, $FileCreateDate, NEW[BasicTime.GMT _ createDate]]; }; FromRope: PUBLIC PROC [rope: ROPE, start: INT _ 0, len: INT _ TextNode.MaxLen] RETURNS [root: TextNode.Ref] = { control, comment, stext: RopeReader.Ref; interdoc: ROPE; tiogaFile: BOOL; StartRead[]; { ENABLE UNWIND => EndRead[]; [control, comment, stext, tiogaFile] _ FileReader.FromRope[rope, start, len ! FileReader.InterDoc => { interdoc _ doc; RESUME }]; root _ IF interdoc#NIL THEN InterFile.FromRope[interdoc] ELSE Finish[control, comment, stext, tiogaFile]; }; EndRead[]; }; FromStream: PUBLIC PROC [stream: IO.STREAM, len: INT _ TextNode.MaxLen] RETURNS [root: TextNode.Ref] = { control, comment, stext: RopeReader.Ref; interdoc: ROPE; tiogaFile: BOOL; StartRead[]; { ENABLE UNWIND => EndRead[]; [control, comment, stext, tiogaFile] _ FileReader.FromStream[stream, len ! FileReader.InterDoc => { interdoc _ doc; RESUME }]; root _ IF interdoc#NIL THEN InterFile.FromRope[interdoc] ELSE Finish[control, comment, stext, tiogaFile]; }; EndRead[]; }; collectionInterval: LONG CARDINAL; bigCollectionInterval: LONG CARDINAL = 1000000; startCount: INTEGER _ 0; StartRead: ENTRY PROC = { ENABLE UNWIND => NULL; IF startCount = 0 THEN { collectionInterval _ SafeStorage.SetCollectionInterval[bigCollectionInterval]; }; startCount _ startCount+1; }; EndRead: ENTRY PROC = { -- restore initial collectionInterval when all Get's done ENABLE UNWIND => NULL; IF (startCount _ startCount-1) = 0 THEN [] _ SafeStorage.SetCollectionInterval[collectionInterval]; }; Finish: PROC [control, cmmnt, stext: RopeReader.Ref, tiogaFile: BOOL] RETURNS [root: TextNode.Ref] = { op: Op; parent, node, prev: TextNode.Ref; textNode: TextNode.RefTextNode; terminalNode: BOOL; textLength, runsLength: INT _ 0; formatName: ATOM; propname: ATOM; charProps: REF _ NIL; charSets: REF _ NIL; InsertNode: PROC [node: TextNode.Ref] = { IF prev#NIL THEN prev.next _ node ELSE IF parent#NIL THEN parent.child _ node; prev _ node; }; ReadByte: PROC RETURNS [Byte] = INLINE { RETURN [LOOPHOLE[ReadChar[]]]; }; ReadChar: PROC RETURNS [CHAR] = { RETURN [RopeReader.Get[control]]; }; ReadProp: PROC = { specs: ROPE _ GetControlRope[]; disaster: BOOL _ FALSE; value: REF; value _ NodeProps.DoSpecs[propname, specs ! RuntimeError.UNCAUGHT => {disaster_TRUE} ]; IF disaster THEN ERROR FromFileError; IF value # NIL THEN { <> IF propname = $CharProps THEN charProps _ value ELSE IF propname = $CharSets THEN charSets _ value ELSE NodeProps.PutProp[node, propname, value]; }; }; GetRope: PROC [len: INT, rdr: RopeReader.Ref] RETURNS [ROPE] = { rope: ROPE; pos: INT; [rope, pos] _ RopeReader.Position[rdr]; RopeReader.SetIndex[rdr, pos+len]; RETURN [RopeEdit.Substr[rope, pos, len]]; }; GetControlRope: PROC RETURNS [ROPE] = { RETURN [GetRope[ReadLength[], control]]; }; GetText: PROC = { len: NAT; IF (len _ ReadByte[]) > text.maxLength THEN text _ NEW[TEXT[len]]; FOR i: NAT IN [0..len) DO text[i] _ RopeReader.Get[control]; ENDLOOP; text.length _ len; }; GetAtom: PROC RETURNS [ATOM] = { GetText[]; -- get the print name RETURN [IF text.length = 0 THEN NIL ELSE Atom.MakeAtomFromRefText[text]]; }; ReadLength: PROC RETURNS [INT] = { first, second, fourth: LengthByte; third: ThirdByte; card: IntBytes; first _ LOOPHOLE[ReadByte[]]; card.first _ first.data; IF ~first.others THEN RETURN [LOOPHOLE[card]]; second _ LOOPHOLE[ReadByte[]]; card.second _ second.data; IF ~second.others THEN RETURN [LOOPHOLE[card]]; third _ LOOPHOLE[ReadByte[]]; card.thirdBottom _ third.dataBottom; card.thirdTop _ third.dataTop; IF ~third.others THEN RETURN [LOOPHOLE[card]]; fourth _ LOOPHOLE[ReadByte[]]; card.fourth _ fourth.data; RETURN [LOOPHOLE[card]]; }; NextOp: PROC RETURNS [op: Op] = { op _ RopeReader.Get[control ! RopeReader.ReadOffEnd => { op _ endOfFile; CONTINUE }]; }; <
> pgf: PGSupport.PGF _ PGSupport.CreatePGF[]; text: REF TEXT _ NEW[TEXT[32]]; terminalNode _ FALSE; op _ NextOp[]; DO SELECT op FROM IN [terminalTextNodeFirst..terminalTextNodeLast] => { formatName _ PGSupport.RetrieveFormatName[ LOOPHOLE[op-terminalTextNodeFirst, FormatIndex], pgf ! PGSupport.BadIndex => ERROR FromFileError ]; terminalNode _ TRUE; }; IN [startNodeFirst..startNodeLast] => formatName _ PGSupport.RetrieveFormatName[ LOOPHOLE[op-startNodeFirst, FormatIndex], pgf ! PGSupport.BadIndex => ERROR FromFileError ]; endNode => { IF prev#NIL THEN { prev.last _ TRUE; prev.next _ parent;}; prev _ parent; IF (parent _ parent.next)=NIL THEN EXIT; op _ NextOp[]; LOOP; }; startNode => { formatName _ GetAtom[]; [] _ PGSupport.EnterFormatName[formatName, pgf]; }; terminalTextNode => { formatName _ GetAtom[]; [] _ PGSupport.EnterFormatName[formatName, pgf]; terminalNode _ TRUE; }; rope, comment => { reader: RopeReader.Ref; IF op=rope THEN reader _ stext ELSE { reader _ cmmnt; textNode.comment _ TRUE;}; IF textNode=NIL OR textNode#node THEN ERROR FromFileError; IF (textLength_ReadLength[]) > 0 THEN -- get the rope textNode.rope _ GetRope[textLength, reader]; IF charProps#NIL THEN {NodeProps.PutProp[node,$CharProps,charProps]; charProps_NIL}; IF charSets#NIL THEN {NodeProps.PutProp[node, $CharSets, charSets]; charSets _ NIL}; SELECT runsLength FROM 0 => NULL; -- no runs for this rope textLength => runsLength _ 0; -- correct length ENDCASE => ERROR FromFileError; -- mismatch RopeReader.BumpIndex[reader, 1]; -- skip CR at end of rope op _ NextOp[]; LOOP; }; runs => { -- runs, if any, come before corresponding rope lookRuns: TextLooks.Runs; numRuns: INT; pos: INT _ 0; IF textNode=NIL OR textNode#node THEN ERROR FromFileError; numRuns _ ReadLength[]; -- read number of runs WHILE numRuns > 0 DO num: NAT _ TextLooksSupport.Short[MIN[numRuns, LAST[NAT]]]; baseRuns: TextLooks.BaseRuns _ TextLooksSupport.NewBase[num]; len: INT _ pos; numRuns _ numRuns-num; FOR i:NAT IN [0..num) DO -- read runs for this baseRuns looks: TextLooks.Looks; ReadLookChars: PROC [num: NAT] = { FOR i:NAT IN [0..num) DO looks[ReadChar[]] _ TRUE; ENDLOOP; [] _ PGSupport.EnterLooks[looks, pgf]; }; SELECT op _ NextOp[] FROM IN [looksFirst .. looksLast] => { looks _ PGSupport.RetrieveLooks[ LOOPHOLE[op-looksFirst, LooksIndex], pgf ! PGSupport.BadIndex => ERROR FromFileError ] }; look1 => ReadLookChars[1]; look2 => ReadLookChars[2]; look3 => ReadLookChars[3]; FileOps.looks => { <> lb: TextLooks.LooksBytes; lb.byte0 _ ReadByte[]; lb.byte1 _ ReadByte[]; lb.byte2 _ ReadByte[]; lb.byte3 _ ReadByte[]; [] _ PGSupport.EnterLooks[looks _ LOOPHOLE[lb], pgf]; }; ENDCASE => ERROR FromFileError; baseRuns[i] _ [pos_pos+ReadLength[], looks]; ENDLOOP; lookRuns _ IF lookRuns=NIL THEN baseRuns ELSE TextLooks.Concat[lookRuns, baseRuns, len, pos-len]; ENDLOOP; runsLength _ pos; -- for use in checking rope length textNode.runs _ lookRuns; op _ NextOp[]; LOOP; }; prop => { [] _ PGSupport.EnterProp[propname _ GetAtom[], pgf]; ReadProp; op _ NextOp[]; LOOP; }; propShort => { propname _ PGSupport.RetrieveProp[ LOOPHOLE[ReadByte[], PropIndex], pgf ! PGSupport.BadIndex => ERROR FromFileError ]; ReadProp; op _ NextOp[]; LOOP; }; otherNode, otherNodeShort, otherNodeSpecs, otherNodeSpecsShort => { ERROR FromFileError; }; endOfFile => { IF parent=NIL THEN EXIT; -- have reached the root [] _ RopeReader.Backwards[control]; -- backup so read endOfFile again op _ endNode; LOOP; }; ENDCASE => ERROR FromFileError; <> IF charProps # NIL OR charSets # NIL THEN ERROR FromFileError; InsertNode[node _ textNode _ NEW[TextNode.Body]]; node.formatName _ formatName; IF terminalNode THEN terminalNode _ FALSE ELSE { node.next _ parent; parent _ node; prev _ NIL }; op _ NextOp[]; ENDLOOP; IF (root _ prev)=NIL THEN -- don't have a normal tree IF node=NIL THEN root _ NEW[TextNode.Body] -- null file ELSE root _ node; -- single node in file root.last _ TRUE; NodeProps.PutProp[root, $FromTiogaFile, IF tiogaFile THEN $Yes ELSE $No]; PGSupport.FreePGF[pgf]; }; END.