-- NewSTPImpl.Mesa, last edit February 9, 1983 3:14 pm

DIRECTORY
ConvertUnsafe: TYPE USING[ToRope],
Heap: TYPE USING[systemZone],
NewSTP,
Rope: TYPE USING[ROPE, Text],
RopeInline: TYPE USING[InlineFlatten],
Stream: TYPE USING[Handle, InputOptions],
System: TYPE USING[GreenwichMeanTime],
UnsafeSTP;

NewSTPImpl: CEDAR PROGRAM
 IMPORTS ConvertUnsafe, Heap, RopeInline, UnsafeSTP
 EXPORTS NewSTP = {

Create: PUBLIC PROC RETURNS [stp: NewSTP.Handle] = TRUSTED {
 stp ← NEW[NewSTP.Object ← [UnsafeSTP.Create[]]];
 };

Destroy: PUBLIC PROC [stp: NewSTP.Handle] RETURNS [NewSTP.Handle] = TRUSTED {
 stp.stp ← UnsafeSTP.Destroy[stp.stp];
 FREE[@stp];
 RETURN[NIL];
 };

Open: PUBLIC PROC [stp: NewSTP.Handle, host: Rope.ROPE] RETURNS [herald: Rope.ROPE] = TRUSTED {
 flathost: Rope.Text ← RopeInline.InlineFlatten[host];
 h: LONG STRING ← UnsafeSTP.Open[stp.stp, LOOPHOLE[flathost]];
 herald ← ConvertUnsafe.ToRope[h];
 Heap.systemZone.FREE[@h];
 };

Close: PUBLIC PROC [stp: NewSTP.Handle] = TRUSTED {
 UnsafeSTP.Close[stp.stp];
 };

-- set credentials
Login: PUBLIC PROC [stp: NewSTP.Handle, name, password: Rope.ROPE] = TRUSTED {
 flatname: Rope.Text ← RopeInline.InlineFlatten[name];
 flatpass: Rope.Text ← RopeInline.InlineFlatten[password];
 UnsafeSTP.Login[stp.stp, LOOPHOLE[flatname], LOOPHOLE[flatpass]];
 };

Connect: PUBLIC PROC [stp: NewSTP.Handle, name, password: Rope.ROPE] = TRUSTED {
 flatname: Rope.Text ← RopeInline.InlineFlatten[name];
 flatpass: Rope.Text ← RopeInline.InlineFlatten[password];
 UnsafeSTP.Connect[stp.stp, LOOPHOLE[flatname], LOOPHOLE[flatpass]];
 };

-- set various properties
SetHost: PUBLIC PROC [stp: NewSTP.Handle, host: Rope.ROPE] = TRUSTED {
 flathost: Rope.Text ← RopeInline.InlineFlatten[host];
 UnsafeSTP.SetHost[stp.stp, LOOPHOLE[flathost]];
 };

SetDirectory: PUBLIC PROC [stp: NewSTP.Handle, directory: Rope.ROPE] = TRUSTED {
 flatdirectory: Rope.Text ← RopeInline.InlineFlatten[directory];
 UnsafeSTP.SetDirectory[stp.stp, LOOPHOLE[flatdirectory]];
 };

SetDesiredProperties: PUBLIC PROC [stp: NewSTP.Handle, props: NewSTP.DesiredProperties] = TRUSTED {
 UnsafeSTP.SetDesiredProperties[stp.stp, props];
 };

-- query various properties
IsOpen: PUBLIC PROC [stp: NewSTP.Handle] RETURNS [yes: BOOL] = TRUSTED {
 RETURN[UnsafeSTP.IsOpen[stp.stp]];
 };

GetFileInfo: PUBLIC PROC [stp: NewSTP.Handle] RETURNS [fi: NewSTP.FileInfo] = TRUSTED {
 u: UnsafeSTP.FileInfo ← UnsafeSTP.GetFileInfo[stp.stp];
 fi ← NEW[NewSTP.FileInfoObject];
 fi.directory ← ConvertUnsafe.ToRope[u.directory];
 fi.body ← ConvertUnsafe.ToRope[u.body];
 fi.version ← ConvertUnsafe.ToRope[u.version];
 fi.author ← ConvertUnsafe.ToRope[u.author];
 fi.create ← ConvertUnsafe.ToRope[u.create];
 fi.read ← ConvertUnsafe.ToRope[u.read];
 fi.write ← ConvertUnsafe.ToRope[u.write];
 fi.size ← u.size;
 fi.type ← u.type;
 };

GetProperty: PUBLIC PROC [stp: NewSTP.Handle, prop: NewSTP.ValidProperties]
 RETURNS [Rope.ROPE] = TRUSTED {
 s: LONG STRING ← UnsafeSTP.GetProperty[stp.stp, prop];
 RETURN[ConvertUnsafe.ToRope[s]];
 };

GetDesiredProperties: PUBLIC PROC [stp: NewSTP.Handle] RETURNS [props: NewSTP.DesiredProperties] = TRUSTED {
 RETURN[UnsafeSTP.GetDesiredProperties[stp.stp]];
 };

-- retrieve, store, etc.
CreateRemoteStream: PUBLIC PROC [
stp: NewSTP.Handle,
file: Rope.ROPE,
access: NewSTP.Access,
fileType: NewSTP.Type,
options: Stream.InputOptions,
creation: System.GreenwichMeanTime]
RETURNS[stream: Stream.Handle] = TRUSTED {
flatfile: Rope.Text ← RopeInline.InlineFlatten[file];
RETURN[UnsafeSTP.CreateRemoteStream[stp.stp, LOOPHOLE[flatfile], access,
 fileType, options, creation]];
};

NextFileName: PUBLIC PROC [remoteStream: Stream.Handle] RETURNS [file: Rope.ROPE] = TRUSTED {
 s: LONG STRING ← UnsafeSTP.NextFileName[remoteStream];
 file ← ConvertUnsafe.ToRope[s];
 Heap.systemZone.FREE[@s];
 };

Delete: PUBLIC PROC [ stp: NewSTP.Handle,
name: Rope.ROPE,
confirm: NewSTP.ConfirmProcType,
complete: NewSTP.CompletionProcType]
= TRUSTED {
 flatname: Rope.Text ← RopeInline.InlineFlatten[name];
UnsafeSTP.Delete[stp.stp, LOOPHOLE[flatname], confirm, complete];
};

Enumerate: PUBLIC PROC [stp: NewSTP.Handle, name: Rope.ROPE, proc: NewSTP.NoteFileProcType] = TRUSTED {
 flatname: Rope.Text ← RopeInline.InlineFlatten[name];
UnsafeSTP.Enumerate[stp.stp, LOOPHOLE[flatname], proc];
};

Rename: PUBLIC PROC [stp: NewSTP.Handle, old, new: Rope.ROPE] = TRUSTED {
 flatold: Rope.Text ← RopeInline.InlineFlatten[old];
flatnew: Rope.Text ← RopeInline.InlineFlatten[new];
UnsafeSTP.Rename[stp.stp, LOOPHOLE[flatold], LOOPHOLE[flatnew]];
};

Retrieve: PUBLIC PROC [
stp: NewSTP.Handle,
file: Rope.ROPE,
confirm: NewSTP.ConfirmProcType,
complete: NewSTP.CompletionProcType]
= TRUSTED {
flatfile: Rope.Text ← RopeInline.InlineFlatten[file];
UnsafeSTP.Retrieve[stp.stp, LOOPHOLE[flatfile], confirm, complete];
};

Store: PUBLIC PROC [
stp: NewSTP.Handle,
file: Rope.ROPE,
stream: Stream.Handle,
noteFile: NewSTP.NoteFileProcType,
fileType: NewSTP.Type,
creation: System.GreenwichMeanTime]
= TRUSTED {
flatfile: Rope.Text ← RopeInline.InlineFlatten[file];
UnsafeSTP.Store[stp.stp, LOOPHOLE[flatfile], stream, noteFile, fileType, creation];
};

}.