<> <> <> <> <> <> <> DIRECTORY Ascii USING [Lower], Atom USING [MakeAtom], 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, TypeIndex], FileReader USING [FromRope, FromStream, InterDoc, Offset, Open, OpenC], FS USING [ComponentPositions, ExpandName, GetName, OpenFile, Position], GetFile USING [], InterFile USING [FromRope], IO USING [STREAM], NameSymbolTable USING [MakeName, Name], NodeProps USING [DoSpecs, PutProp], OtherNode USING [DoSpecs], PGSupport USING [CreatePGF, EnterLooks, EnterProp, EnterTypeName, FreePGF, PGF, RetrieveLooks, RetrieveProp, RetrieveTypeName], PutGet USING [], Rope USING [FromRefText, MaxLen, ROPE, Substr, Translate], RopeEdit USING [MaxLen, Offset, Substr], RopeReader USING [Backwards, BumpIndex, Get, GetRope, GetString, Position, ReadOffEnd, Ref, SetIndex], SafeStorage USING [SetCollectionInterval], TextEdit USING [], TextLooks USING [BaseRuns, Concat, Looks, LooksBytes, Offset, Runs], TextLooksSupport USING [NewBase, Short], TextNode USING [MaxLen, NodeProps, Offset, OtherBody, pZone, qZone, Ref, RefOtherNode, RefTextNode, TextBody, TypeName]; GetFileImpl: CEDAR MONITOR IMPORTS Ascii, Atom, FileReader, FS, InterFile, NameSymbolTable, npI:NodeProps, OtherNode, PGSupport, Rope, RopeEdit, RopeReader, SafeStorage, TextLooks, TextLooksSupport, TextNode EXPORTS PutGet, GetFile SHARES TextLooks = BEGIN OPEN TextNode; ROPE: TYPE = Rope.ROPE; Byte: TYPE = [0..255]; FromFileError: PUBLIC ERROR = CODE; FromFile: PUBLIC PROC [fileName: ROPE, start: Offset _ 0, len: Offset _ MaxLen] RETURNS [root: 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: Offset _ 0, len: Offset _ MaxLen] RETURNS [root: 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: 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]; npI.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: Ref, createDate: BasicTime.GMT] = { npI.PutProp[root, $FileCreateDate, TextNode.pZone.NEW[BasicTime.GMT _ createDate]]; }; FromRope: PUBLIC PROC [rope: ROPE, start: Offset _ 0, len: Offset _ MaxLen] RETURNS [root: 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: Offset _ MaxLen] RETURNS [root: 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: Ref] = { OPEN FileOps; op: Op; parent, node, prev: Ref; textNode: RefTextNode; othrNode: RefOtherNode; terminalNode: BOOLEAN; textLength, runsLength: Offset _ 0; typename: TextNode.TypeName; propname: ATOM; InsertNode: PROC [node: 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 [CHARACTER] = { RETURN [RopeReader.Get[control]] }; MakeOtherNode: PROC = { InsertNode[node _ othrNode _ qZone.NEW[OtherBody]]; node.typename _ typename; node.next _ parent; parent _ node; prev _ NIL }; OtherNodeSpecs: PROC = { IF othrNode=NIL OR othrNode#node THEN ERROR FromFileError; othrNode.variety _ propname; OtherNode.DoSpecs[othrNode,GetControlRope[]] }; ReadProp: PROC = { specs: ROPE _ GetControlRope[]; value: REF _ npI.DoSpecs[propname,specs]; IF value # NIL THEN npI.PutProp[node,propname,value] }; GetRope: PROC [len: Offset, rdr: RopeReader.Ref] RETURNS [ROPE] = { rope: ROPE; pos: Offset; [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 _ pZone.NEW[TEXT[len]]; text.length _ 0; text.length _ RopeReader.GetString[control, text, len] }; GetAtom: PROC RETURNS [ATOM] = { GetText[]; -- get the print name RETURN [Atom.MakeAtom[Rope.FromRefText[text]]] }; GetName: PROC RETURNS [NameSymbolTable.Name] = { GetText[]; RETURN [NameSymbolTable.MakeName[text]] }; ReadLength: PROC RETURNS [Offset] = { 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] = { OPEN RopeReader; op _ Get[control ! ReadOffEnd => { op _ endOfFile; CONTINUE }] }; <<-- MAIN PROGRAM>> pgf: PGSupport.PGF _ PGSupport.CreatePGF[]; text: REF TEXT _ pZone.NEW[TEXT[32]]; terminalNode _ FALSE; op _ NextOp[]; DO SELECT op FROM IN [terminalTextNodeFirst..terminalTextNodeLast] => { typename _ PGSupport.RetrieveTypeName[ LOOPHOLE[op-terminalTextNodeFirst, TypeIndex], pgf]; terminalNode _ TRUE }; IN [startNodeFirst..startNodeLast] => typename _ PGSupport.RetrieveTypeName[ LOOPHOLE[op-startNodeFirst, TypeIndex], pgf]; endNode => { IF prev#NIL THEN { prev.last _ TRUE; prev.next _ parent }; prev _ parent; IF (parent _ parent.next)=NIL THEN EXIT; op _ NextOp[]; LOOP }; startNode => { typename _ GetName[]; [] _ PGSupport.EnterTypeName[typename,pgf] }; terminalTextNode => { typename _ GetName[]; [] _ PGSupport.EnterTypeName[typename,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]; 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: Offset; pos: Offset _ 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: Offset _ 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]; FileOps.look1 => ReadLookChars[1]; FileOps.look2 => ReadLookChars[2]; FileOps.look3 => ReadLookChars[3]; FileOps.looks => { <<-- read 4 bytes of looks from control stream>> 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]; ReadProp; op _ NextOp[]; LOOP }; otherNode => { typename _ GetName[]; [] _ PGSupport.EnterTypeName[typename, pgf]; MakeOtherNode; op _ NextOp[]; LOOP }; otherNodeShort => { typename _ PGSupport.RetrieveTypeName[ LOOPHOLE[ReadByte[],TypeIndex], pgf]; MakeOtherNode; op _ NextOp[]; LOOP }; otherNodeSpecs => { [] _ PGSupport.EnterProp[propname _ GetAtom[], pgf]; OtherNodeSpecs; op _ NextOp[]; LOOP }; otherNodeSpecsShort => { propname _ PGSupport.RetrieveProp [LOOPHOLE[ReadByte[],PropIndex], pgf]; OtherNodeSpecs; op _ NextOp[]; LOOP }; 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 reach here, then want to start a new text node>> InsertNode[node _ textNode _ qZone.NEW[TextBody]]; node.typename _ typename; 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 _ qZone.NEW[TextBody] -- null file ELSE root _ node; -- single node in file root.last _ TRUE; npI.PutProp[root, $FromTiogaFile, IF tiogaFile THEN $Yes ELSE $No]; PGSupport.FreePGF[pgf]; }; StartGetFile: PUBLIC PROC = { }; END.