[ return ]

names.c




/*
 *  file : names.c
 *
 * [DEFINE DESCRIPTION = Hierarchical name object]
 *
 *  Name                Date        Description
 *  --------------      --------    -------------------------------------
 *  Andrew H. Fagg      08/10/92    Original
 *
 *   A hierarchical name constructor.  This data structure is 
 * designed for easy & efficient specification of names in a
 * hierarchical data structure.  The name of one element is
 * composed of the name of its parent and a local name, and
 * is printed as "<parent_name>.<local_name>".
 *   The name_class data structure maintains a copy of the string
 * that represents the local name and a pointer to the parent
 * name data structure.
 *
 */

#include "nsl_include.h"
#include "names.h"

/*
 * name_class::name_class(char* local_name, name_class* p)
 *
 *  <local_name> = the sub-name of the element.
 *  <p> = a pointer to the parent name (may be NULL).
 *
 *  Name class constructor.
 *
 */

name_class::name_class(char* local_name, name_class* p)
{
  name = new char[strlen(local_name) + 1];
  strcpy(name, local_name);
  parent = p;
}

/*
 * name_class::~name_class()
 *
 *  Name class destructor.
 *
 */

name_class::~name_class()
{
  delete name;
}

/*
 * name_class* name_class::get_parent()
 *
 *  Returns the parent of the name.
 *
 */

name_class* name_class::get_parent()
{
  return parent;
}

/*
 * char* name_class::get_local_name()
 *
 *  Returns only the local name string.
 *
 */

char* name_class::get_local_name()
{
  return name;
}

/*
 * ostream& operator<<(ostream& out, name_class* n)
 *
 * <out> = stream.
 * <n> = pointer to a name.
 *
 *  Allows for piping the name out to a stream.
 *  e.g. : cout << "the name is : " << name nl;
 *
 */

ostream& operator<<(ostream& out, name_class* n)
{
  if(n->get_parent() != NULL)
    {
      out << *n->get_parent();
      out << ".";
    }
  out << n->get_local_name();        
  return out;
}

/*
 * ostream& operator<<(ostream& out, name_class& n)
 *
 * <out> = stream.
 * <n> = a name.
 *
 *  Allows for piping the name out to a stream.
 *  e.g. : cout << "the name is : " << name nl;
 *
 */

ostream& operator<<(ostream& out, name_class& n)
{
  if(n.get_parent() != NULL)
    {
      out << *n.get_parent();
      out << ".";
    }
  out << n.get_local_name();        
  return out;
}


[ return ]