-- File: DBNamesImpl.mesa
-- last edited by:
-- Cattell, July 5, 1983 5:00 pm
-- Donahue, July 28, 1983 3:13 pm
-- Willie-sue, February 22, 1983 4:08 pm

DIRECTORY
DB,
DBEnvironment,
DBNames,
NutOps,
Rope,
ViewerOps;

DBNamesImpl: CEDAR PROGRAM
IMPORTS Rope, DB, DBEnvironment, NutOps

EXPORTS DBNames =

BEGIN OPEN Rope, DB;

Separator: PUBLIC CHAR ← '!;

Sep: ROPE = Rope.FromChar[Separator];

EntityToName: PUBLIC PROC[ e: Entity, seg: DB.Segment ← NIL ] RETURNS[ name: ROPE ] = {
IF NOT NutOps.IsSystemDomain[d: DomainOf[e]] THEN seg ← DB.SegmentOf[e];
name ← Rope.Cat[ DB.GetSegmentInfo[seg].filePath,
      Sep, NameOf[DomainOf[e]], Sep, NameOf[e] ]};
     
MakeName: PUBLIC PROC[seg: DB.Segment, d: Domain, eName: ROPE] RETURNS[name: ROPE] =
{ name ← Rope.Cat[GetSegmentInfo[seg].filePath, Sep, NameOf[d], Sep, eName]};

NameToEntity: PUBLIC PROC[name: ROPE, create: BOOLFALSE] RETURNS[e: Entity] = {
segName, domainName, entityName: ROPE;
domain: Domain;
segment: DB.Segment;
[segName, domainName, entityName] ← DecomposeName[name];
segment ← NutOps.AtomFromSegment[segName];
-- check to make sure that this is the right segment for the entity
IF NOT Rope.Equal[DB.GetSegmentInfo[segment].filePath, segName, FALSE]
THEN RETURN[NIL];
domain ← DeclareDomain[ domainName, segment, Version[OldOnly] !
    DBEnvironment.Error => CHECKED { domain ← NIL; CONTINUE } ];
e ← IF domain = NIL THEN NIL
ELSE FetchEntity[ domain, entityName, segment ];
IF e = NIL AND domain # NIL AND create THEN
e ← DeclareEntity[ domain, entityName ! DB.Error => CONTINUE ] };
   
SegmentOf: PUBLIC PROC[ name: ROPE ] RETURNS[ segment: DB.Segment ] = {
segNameLength: INT = Rope.Find[name, Sep];
segName: ROPE = Rope.Substr[name, 0, segNameLength];
RETURN[ NutOps.AtomFromSegment[ segName ] ] };

DecomposeName: PUBLIC PROC[ name: ROPE ] RETURNS[ segment, domain, entity: ROPE ] =
BEGIN
segNameLength: INT = Rope.Find[ name, Sep ];
domainNameLength: INT = Rope.Find[ name, Sep, segNameLength+1 ] - segNameLength - 1;
segment← Rope.Substr[ name, 0, segNameLength ];
domain← Rope.Substr[ name, segNameLength+1, domainNameLength ];
entity← Rope.Substr[ name, segNameLength+domainNameLength+2 ];
END;

END. . .