TDirToLoganBerry.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Doug Terry, June 18, 1987 2:40:38 pm PDT
Converts Finch TDir files into LoganBerry databases.
DIRECTORY
Rope USING [ROPE, Concat, Equal, Fetch, Find, Substr],
IO USING [PutF, STREAM, rope, Close, RIS, BreakProc, TAB, CR, EndOfStream, GetTokenRope, EndOf, GetLineRope],
FS USING [ComponentPositions, ExpandName, StreamOpen],
TextNode USING [Ref, StepForward, NodeRope],
NodeProps USING [GetProp],
PutGet USING [FromFile],
Commander USING [CommandProc, Register],
UserProfile USING [Token],
VoiceUtils USING [Registrize],
CommandTool USING [NextArgument];
TDirToLoganBerry: CEDAR PROGRAM
IMPORTS Rope, IO, FS, TextNode, NodeProps, PutGet, Commander, CommandTool, UserProfile, VoiceUtils
~ BEGIN
ROPE: TYPE = Rope.ROPE;
STREAM: TYPE = IO.STREAM;
Convert: PROC [tdir, schema: ROPE] RETURNS [] ~ {
s: STREAM;
fname, shortname, basename, logname: ROPE;
cp: FS.ComponentPositions;
root: TextNode.Ref;
File names
[fname, cp] ← FS.ExpandName[schema];
shortname ← Rope.Substr[base: fname, start: cp.base.start, len: cp.ver.start - cp.base.start];
basename ← Rope.Substr[base: fname, start: cp.base.start, len: cp.base.length];
logname ← Rope.Concat[Rope.Substr[base: fname, start: 0, len: cp.base.start+cp.base.length], ".lblog"];
Write log file
s ← FS.StreamOpen[fileName: logname, accessOptions: create];
root ← PutGet.FromFile[fileName: tdir];
FOR node: TextNode.Ref ← TextNode.StepForward[root], TextNode.StepForward[node] WHILE node#NIL DO
For each non-comment node, write it to the log file.
commentProp: REF BOOLNARROW[NodeProps.GetProp[node, $Comment]];
IF commentProp#NIL AND commentProp^ THEN LOOP;
ParseAndOutput[s, TextNode.NodeRope[node]];
ENDLOOP;
IO.PutF[s, "\377"];
IO.Close[s];
Write schema file
s ← FS.StreamOpen[fileName: fname, accessOptions: create];
IO.PutF[s, "-- %g\n", IO.rope[shortname]];
IO.PutF[s, "-- A LoganBerry schema file for a Finch personal telephone directory\n"];
IO.PutF[s, "-- Created by TDirToLoganBerry from %g\n", IO.rope[tdir]];
IO.PutF[s, "\n"];
IO.PutF[s, "Directory [Indigo]<Voice>Loganberry>Top>\n"];
IO.PutF[s, " %g\n", IO.rope[shortname]];
IO.PutF[s, "\n"];
IO.PutF[s, "Directory [Indigo]<Voice>Loganberry>%g>\n", IO.rope[basename]];
IO.PutF[s, " --> log 0 readwrite\n"];
IO.PutF[s, " %g\n", IO.rope[Rope.Concat[basename, ".lblog"]]];
IO.PutF[s, "\n"];
IO.PutF[s, " --> index \"name\" primary\n"];
IO.PutF[s, " %gName.lbindex\n", IO.rope[basename]];
IO.PutF[s, " --> index \"rname\" secondary\n"];
IO.PutF[s, " %gRname.lbindex\n", IO.rope[basename]];
IO.PutF[s, " --> index \"officenumber\" secondary\n"];
IO.PutF[s, " %gOfficePhone.lbindex\n", IO.rope[basename]];
IO.PutF[s, " --> index \"homenumber\" secondary\n"];
IO.PutF[s, " %gHomePhone.lbindex\n", IO.rope[basename]];
IO.Close[s];
};
ParseAndOutput: PROC [out: STREAM, r: ROPE] RETURNS [] ~ {
in: STREAM = IO.RIS[r];
field: ROPE;
rnameStart, rnameEnd: INT;
nameEnd: INT;
IF r = NIL THEN RETURN;
field ← ToTab[in];
IF field # NIL THEN { -- look for embedded rname
rnameStart ← Rope.Find[field, "<"];
IF rnameStart<0 THEN
WriteAttribute[out, "name", field]
ELSE {
FOR nameEnd ← rnameStart - 1, nameEnd - 1 DO -- get rid of blanks at end of name
IF Rope.Fetch[field, nameEnd] # ' THEN EXIT;
ENDLOOP;
WriteAttribute[out, "name", Rope.Substr[base: field, start: 0, len: nameEnd+1]];
rnameEnd Rope.Find[field, ">", rnameStart];
field ← VoiceUtils.Registrize[Rope.Substr[base: field, start: rnameStart+1, len: rnameEnd-rnameStart-1]];
WriteAttribute[out, "rname", field];
};
};
field ← ToTab[in];
IF field # NIL THEN
WriteAttribute[out, "officenumber", field];
field ← ToTab[in];
IF field # NIL THEN
WriteAttribute[out, "homenumber", field];
field ← ToTab[in];
IF field # NIL THEN
WriteAttribute[out, "remarks", field];
IO.PutF[out, "\n"];
};
ToTab: PROC[s: STREAM] RETURNS [field: ROPENIL] = {
ToTabProc: IO.BreakProc = {
RETURN[IF char=IO.TAB OR char=IO.CR THEN sepr ELSE other]; };
field ← s.GetTokenRope[ToTabProc ! IO.EndOfStream=> CONTINUE;].token;
IF field.Equal["*"] THEN field←NIL;
};
WriteAttribute: PROC [s: STREAM, type, value: ROPE] RETURNS [] ~ INLINE {
IO.PutF[s, "%g: %g\n", IO.rope[type], IO.rope[value]];
};
Doit: Commander.CommandProc = {
[cmd: Commander.Handle] RETURNS [result: REF ANY ← NIL, msg: ROPE ← NIL]
get file name from command line if present, otherwise use profile entry
tdir, schema: ROPE;
fname: ROPE;
cp: FS.ComponentPositions;
s: STREAM;
tdir ← CommandTool.NextArgument[cmd];
IF tdir = NIL THEN { -- code taken from FinchDirectoryImpl
tdir ← UserProfile.Token[key: "Finch.TelephoneDirectory"];
IF tdir = NIL THEN RETURN[msg: "usage: TDirToLoganBerry file.tdir fileTDir.lbdf"];
s ← IO.RIS[tdir];
UNTIL IO.EndOf[s] DO
tdir ← IO.GetLineRope[s];
IF NOT Rope.Equal[tdir,""] THEN EXIT;
ENDLOOP;
};
schema ← CommandTool.NextArgument[cmd];
IF schema = NIL THEN {
[fname, cp] ← FS.ExpandName[tdir];
schema ← Rope.Concat[Rope.Substr[base: fname, start: 0, len: cp.base.start+cp.base.length], "TDir.lbdf"];
};
Convert[tdir, schema];
};
Commander.Register[key: "TDirToLoganBerry", proc: Doit, doc: "convert a Finch TDir file into a LoganBerry database
usage: TDirToLoganBerry file.tdir fileTDir.lbdf"];
END.
Doug Terry, December 17, 1985 12:21:21 pm PST
changes to: TDirToLoganBerry, ~, Convert, Doit
Doug Terry, December 17, 1985 4:47:52 pm PST
changes to: TDirToLoganBerry, ~, Convert, Doit, END
Doug Terry, December 18, 1985 5:00:03 pm PST
changes to: Convert, ~, ParseAndOutput, ToTab, Doit, Doit, IMPORTS, DIRECTORY
Doug Terry, December 19, 1985 9:20:24 am PST
changes to: Doit, Convert, ParseAndOutput