File: SVFilesImpl.mesa
Last edited by: Eric Bier on January 2, 1985 5:22:31 pm PST
Copyright © 1984 by Xerox Corporation. All rights reserved.
Contents: Interfaces which make creating scenes look like opening files.
DIRECTORY
DisplayList3d,
Filein3d,
Fileout3d,
FileNames,
FS,
IO,
MessageWindow,
Rope,
SVArtwork,
SVError,
SVFiles,
SVTransforms,
SVSceneTypes,
TFI3d;
SVFilesImpl: PROGRAM
IMPORTS DisplayList3d, Filein3d, Fileout3d, FileNames, FS, IO, MessageWindow, Rope, SVArtwork, SVError, SVTransforms, TFI3d
EXPORTS SVFiles =
BEGIN
Scene: TYPE = SVSceneTypes.Scene;
SceneTable: TYPE = LIST OF Scene;
FindScene: PRIVATE PROC [name: Rope.ROPE, table: SceneTable] RETURNS [scene: Scene, success: BOOL] = {
success ← FALSE;
FOR list: SceneTable ← table, list.rest UNTIL list = NIL OR success DO
IF Rope.Equal[s1: name, s2: list.first.name, case: TRUE] THEN {
success ← TRUE;
scene ← list.first;
};
ENDLOOP;
};
AddSceneToTable: PRIVATE PROC [scene: Scene, table: SceneTable] RETURNS [newtable: SceneTable] = {
newtable ← CONS[scene, table];
};
OpenScene: PUBLIC PROC [picName: Rope.ROPE, wdirForAIS: Rope.ROPE] RETURNS [scene: Scene, success: BOOL] = {
Look for the pic file on the local disk. If there, read in the scene. If not, Tell the user "file not found" and return success = false.
f: IO.STREAM;
[f, success] ← OpenFile[picName];
IF NOT success THEN RETURN; -- OpenFile prints any error messages.
Now we must turn the text file into a scene.
[scene, success] ← MakeScene[f, picName, wdirForAIS]; -- MakeScene will print any error messages
};
OpenFile: PUBLIC PROC [picName: Rope.ROPE] RETURNS [f: IO.STREAM, success: BOOL] = {
success ← TRUE;
Two possiblilities
1) File doesn't exist. Print error message.
2) File does exist. File it in. Succeed.
f ← FS.StreamOpen[picName
! FS.Error => CHECKED {
IF error.group = user THEN {
success ← FALSE;
SVError.Append["Picture file problem: ", TRUE];
SVError.Append[error.explanation];
}
ELSE ERROR;
CONTINUE}];
}; -- end of OpenFile
MakeScene: PRIVATE PROC [f: IO.STREAM, picName: Rope.ROPE, wdirForAIS: Rope.ROPE] RETURNS [scene: Scene, success: BOOL] = {
errorPos: NAT;
errorRope, expected, actual: Rope.ROPE;
fileNotFound: BOOLFALSE;
allMapsFound: BOOLTRUE;
success ← TRUE;
scene ← DisplayList3d.CreateEmptyScene[picName];
SVError.Append["Reading scene: ",TRUE];
SVError.Append[scene.name];
SVError.Append[" from file: "];
SVError.Append[picName];
SVError.Append["..."];
Filein3d.FileinScene [scene, f, wdirForAIS
!TFI3d.RopeNotOnTop => {
success ← FALSE; errorPos ← position; expected ← notThere;
actual ← wasThere; CONTINUE};
SVArtwork.FileNotFound => {allMapsFound ← FALSE; CONTINUE}
];
IF NOT allMapsFound THEN {
errorRope ← IO.PutFR["Some AIS mapping file not found"];
SVError.Append[errorRope, TRUE, TRUE];
success ← FALSE;
RETURN
};
IF NOT success THEN {
errorRope ← IO.PutFR["At %g in %g expected '%g' not '%g'", [integer[errorPos]], [rope[picName]], [rope[expected]], [rope[actual]]];
SVError.Append[errorRope, TRUE, TRUE];
RETURN
}
ELSE SVError.Append["Done", FALSE, TRUE];
}; -- end of MakeScene
SaveScene: PUBLIC PROC [updatedScene: Scene, picName: Rope.ROPE] RETURNS [success: BOOL] = {
Check local disk to see that we have a scene with this name. If so, save it as a file on the local disk after confirmation. scene.dirty ← FALSE.
If not, SVError "File not found. Use Store." and return success = FALSE.
f: IO.STREAM;
success ← TRUE;
IF FileExists[picName] THEN {
f ← FS.StreamOpen[picName, $create
! FS.Error => CHECKED {
IF error.group = user THEN {
success ← FALSE;
SVError.Append["Pic file problem: ", TRUE];
SVError.Append[error.explanation];
SVError.Blink[];
CONTINUE}
ELSE ERROR}];
IF success THEN {
SVTransforms.TidyUpCoordSysTree[updatedScene.coordSysRoot];
SaveFile[updatedScene, f, picName];
};
}
ELSE {
SVError.Append["File not found. Use Store.", TRUE];
SVError.Blink[];
};
};
SaveFile: PRIVATE PROC [scene: Scene, f: IO.STREAM, picName: Rope.ROPE] = {
SVError.Append["Writing scene: ",TRUE];
SVError.Append[scene.name];
SVError.Append[" to file: "];
SVError.Append[picName];
SVError.Append["..."];
Fileout3d.FileoutScene [scene, f, picName];
scene.dirty ← FALSE;
SVError.Append["Done",FALSE,TRUE];
}; -- end of SaveFile
StoreScene: PUBLIC PROC [unNamedScene: Scene, newPicName: Rope.ROPE] RETURNS [success: BOOL] = {
Check the local disk for this name. Ask for confirmation to store to new (or old) file. Store if confirmed.
f: IO.STREAM;
fileAlreadyExists: BOOLFALSE;
confirmation: BOOLFALSE;
success ← FALSE;
IF FileExists[newPicName] THEN {
confirmation ← MessageWindow.Confirm["Confirm overwrite of existing pic file"];
IF confirmation THEN
f ← FS.StreamOpen[newPicName, $create]
ELSE RETURN;
}
ELSE f ← FS.StreamOpen[newPicName, $create];
SVError.Append["Writing scene: ",TRUE];
SVError.Append[newPicName];
SVError.Append[" to file: "];
SVError.Append[newPicName];
SVError.Append["..."];
SVTransforms.TidyUpCoordSysTree[unNamedScene.coordSysRoot];
Fileout3d.FileoutScene [unNamedScene, f, newPicName];
SVError.Append["Done", FALSE, TRUE];
success ← TRUE;
unNamedScene.dirty ← FALSE;
unNamedScene.name ← newPicName;
}; -- end of StoreScene
FileExists: PUBLIC PROC [fileName: Rope.ROPE] RETURNS [answer: BOOL] = {
answer ← TRUE;
[----, ----, ----, ----] ← FS.FileInfo[fileName
! FS.Error => {IF error.code = $unknownFile THEN {answer ← FALSE; CONTINUE}
ELSE ERROR}];
};
FilenameMinusExtension: PUBLIC PROC [wholeName: Rope.ROPE] RETURNS [firstPart: Rope.ROPE] = {
directory, shortName, shortNameMinusExtension: Rope.ROPE;
shortNameStream: IO.STREAM;
directory ← FileNames.Directory[wholeName];
shortName ← FileNames.GetShortName[wholeName, TRUE];
shortNameStream ← IO.RIS[shortName];
[shortNameMinusExtension, ----] ← IO.GetTokenRope[shortNameStream, PeriodBreakProc];
firstPart ← Rope.Concat[directory, shortNameMinusExtension];
};
PeriodBreakProc: SAFE PROC [char: CHAR] RETURNS [IO.CharClass] = TRUSTED {
SELECT char FROM
'.=> RETURN[sepr];
ENDCASE => RETURN[other];
};
END.