DIRECTORY
MessageWindow USING [Append, Blink],
Rope USING [ROPE],
RuntimeError USING [UNCAUGHT],
TiogaAccess USING [Buffer, BufferSize, CharWithLooks, CopyNode, Create, DoneWith, EndOf, Fetch, FromFile, FromSelection, Get, GetBuffer, GetNodeInfo, NodeInfo, ObtainBuffer, PeekRope, Put, PutBuffer, PutNodeInfo, Reader, ReleaseBuffer, Store, WriteFile, Writer, WriteSelection],
TiogaOps USING [CallWithLocks, Ref];
TiogaAccessExample:
CEDAR PROGRAM
IMPORTS MessageWindow, RuntimeError, TiogaAccess, TiogaOps
~ BEGIN
SaveSelection:
PROC [fileName:
ROPE] ~ {
out: TiogaAccess.Writer ← TiogaAccess.Create[];
in: TiogaAccess.Reader ← TiogaAccess.FromSelection[];
UNTIL TiogaAccess.EndOf[in]
DO
nodeInfo: TiogaAccess.NodeInfo ← TiogaAccess.GetNodeInfo[in];
TiogaAccess.PutNodeInfo[out, nodeInfo];
DO
candl: TiogaAccess.CharWithLooks ← TiogaAccess.Get[in];
TiogaAccess.Put[out, candl];
IF candl.endOfNode THEN EXIT;
ENDLOOP;
ENDLOOP;
TiogaAccess.DoneWith[in];
TiogaAccess.WriteFile[out, fileName];
};
GetSelection:
PROC ~ {
selection: TiogaAccess.Reader ← TiogaAccess.FromSelection[];
fileName: ROPE ← TiogaAccess.PeekRope[selection];
buffer: TiogaAccess.Buffer ← TiogaAccess.ObtainBuffer[];
out: TiogaAccess.Writer ← TiogaAccess.Create[];
in: TiogaAccess.Reader ← TiogaAccess.FromFile[fileName];
Could catch FS.Error here for bad name or missing file.
TiogaAccess.DoneWith[selection];
IF
NOT TiogaAccess.EndOf[in]
THEN [] ← TiogaAccess.Get[in];
Discard root; failure to do this will cause the WriteSelection to fail.
UNTIL TiogaAccess.EndOf[in]
DO
[] ← TiogaAccess.CopyNode[out, in]
ENDLOOP;
TiogaAccess.ReleaseBuffer[buffer];
TiogaAccess.DoneWith[in];
TiogaAccess.WriteSelection[out];
};
ReverseBuffer:
PROC [buffer: TiogaAccess.Buffer] ~ {
size:
INT ← buffer.BufferSize-1;
Do not include the end-of-node mark.
FOR i:
INT
IN [0..size/2)
DO
t: TiogaAccess.CharWithLooks ← buffer.Fetch[i];
buffer.Store[i, buffer.Fetch[size-1-i]];
buffer.Store[size-1-i, t]
ENDLOOP;
};
ReverseSelection:
PROC ~ {
buffer: TiogaAccess.Buffer ← TiogaAccess.ObtainBuffer[];
out: TiogaAccess.Writer ← TiogaAccess.Create[];
in: TiogaAccess.Reader ← TiogaAccess.FromSelection[];
UNTIL TiogaAccess.EndOf[in]
DO
nodeInfo: TiogaAccess.NodeInfo ← TiogaAccess.GetNodeInfo[in];
nodeEnd: BOOLEAN ← TiogaAccess.GetBuffer[in, buffer];
IF NOT nodeEnd THEN ERROR;
TiogaAccess.PutNodeInfo[out, nodeInfo];
ReverseBuffer[buffer];
TiogaAccess.PutBuffer[out, buffer];
ENDLOOP;
TiogaAccess.ReleaseBuffer[buffer];
TiogaAccess.DoneWith[in];
TiogaAccess.WriteSelection[out];
};
LockedReverseSelection:
PROC ~ {
action: PROC [ref: TiogaOps.Ref] ~ {ReverseSelection[]};
TiogaOps.CallWithLocks[action ! RuntimeError.
UNCAUGHT => {
MessageWindow.Append["Unknown error in ReverseSelection", TRUE];
MessageWindow.Blink[];
CONTINUE
}];
};
END.
CreateButton S ← TiogaAccessExample.SaveSelection["Save"]
CreateButton G ← TiogaAccessExample.GetSelection[]
CreateButton R ← TiogaAccessExample.ReverseSelection[]
CreateButton L ← TiogaAccessExample.LockedReverseSelection[]
CreateButton P ← TiogaAccess.GetPosition[TiogaAccess.FromSelection[]]