
<<CSImpl.mesa>>
<<Copyright © 1985 by Xerox Corporation.  All rights reserved.>>
<<last edit by Satterthwaite, May 12, 1986 11:44:32 am PDT>>

DIRECTORY
BasicTime: TYPE USING [GMT],
Convert: TYPE USING [CardFromRope, RopeFromCard],
CS: TYPE USING [],
FileNames: TYPE USING [Directory, GetShortName],
Rope: TYPE USING [Equal, Fetch, FromProc, Index, Length, ROPE, Substr],
SafeStorage: TYPE USING [GetSystemZone],
TimeStamp: TYPE USING [Stamp];

CSImpl: CEDAR PROGRAM
IMPORTS Convert, FileNames, Rope, SafeStorage
EXPORTS CS ~ {

<<variables>>
z: PUBLIC ZONE _ NIL;

<<startup>>
Init: PUBLIC PROC ~ {        -- call to make sure module is started
z _ SafeStorage.GetSystemZone[]};

<<rope utilities>>

RootName: PUBLIC PROC[name: Rope.ROPE] RETURNS[Rope.ROPE] ~ {
i: CARDINAL _ name.Length - 1;
WHILE i > 0 AND name.Fetch[i] ~= '. DO i _ i-1 ENDLOOP;
RETURN[IF i > 0 THEN name.Substr[0, i] ELSE name]};

ShortName: PUBLIC PROC[name: Rope.ROPE] RETURNS[Rope.ROPE] ~ {
RETURN[FileNames.GetShortName[name, TRUE]]};

PartialName: PUBLIC PROC[name: Rope.ROPE] RETURNS[BOOL] ~ {
RETURN[name.Length=0 OR name.Fetch[0] # '/]};

DirectoryPart: PUBLIC PROC[name: Rope.ROPE] RETURNS[Rope.ROPE] ~ {
RETURN[FileNames.Directory[name]]};

EndsIn: PUBLIC PROC[str: Rope.ROPE, suf: Rope.ROPE] RETURNS[BOOL] ~ {
strLength: CARDINAL ~ str.Index[0, "!"];
sufLength: CARDINAL ~ suf.Length;
RETURN[sufLength <= strLength AND
suf.Equal[str.Substr[strLength-sufLength, sufLength], FALSE]]
};


GMTFromRope: PUBLIC PROC[r: Rope.ROPE] RETURNS[BasicTime.GMT] ~ {
RETURN[LOOPHOLE[Convert.CardFromRope[r]]]};

RopeFromGMT: PUBLIC PROC[val: BasicTime.GMT] RETURNS[Rope.ROPE] ~ {
RETURN[Convert.RopeFromCard[LOOPHOLE[val]]]};


StampDigits: TYPE ~ [0 .. 4*TimeStamp.Stamp.SIZE);

StampFromRope: PUBLIC PROC[r: Rope.ROPE] RETURNS[TimeStamp.Stamp] ~ {
hex: PACKED ARRAY StampDigits OF [0..16);
FOR i: NAT IN StampDigits DO
c: CHAR ~ r.Fetch[i];
hex[i] _ (SELECT c FROM
IN ['0..'9] => (c.ORD-'0.ORD),
IN ['a..'f] => (c.ORD-'a.ORD)+10,
IN ['A..'F] => (c.ORD-'A.ORD)+10,
ENDCASE => ERROR);
ENDLOOP;
RETURN[LOOPHOLE[hex]]};

RopeFromStamp: PUBLIC PROC[stamp: TimeStamp.Stamp] RETURNS[Rope.ROPE] ~ {
hex: PACKED ARRAY StampDigits OF [0..16) ~ LOOPHOLE[stamp];
i: NAT _ 0;

PutChar: PROC RETURNS[CHAR] ~ {
d: [0..16) ~ hex[i];
i _ i+1;
RETURN[IF d<10 THEN VAL['0.ORD+d] ELSE VAL['a.ORD+(d-10)]]};

RETURN[Rope.FromProc[StampDigits.LAST-StampDigits.FIRST+1, PutChar]]};

}.

