/*  $Id:$
 */

#include "cybernet.h"



/** {duh} **/
Junction::Junction() : Entity()
{
}


/** {duh} **/
Junction::~Junction()
{
}

/** {secret} **/
static int getJctKey(struct resbuf *rb, int *key)
{
  while (rb)
    {
      if (rb->restype == 1000 &&!strcmp(rb->resval.rstring, "NODE_#"))
        {
          rb = rb->rbnext;
          *key = atoi(rb->resval.rstring + 1);
          return 0;
        }
      rb = rb->rbnext;
    }
  return -1;
}

Selection * Junction::jcts = NULL;

/**
 ** Iz Acad baze pobere vse vozle in jih popreje registriramo z Register()
 ** Naredi hiter AVL seznam za uporabo v Find()
 **/
void Junction::Collect()
{
  struct resbuf x_jct = {NULL, 2};
  x_jct.resval.rstring = "X_JCT";
  struct resbuf xFilter = {&x_jct, 0};
  xFilter.resval.rstring = "INSERT";
  if (jcts)
    delete (jcts);
  jcts = new Selection();
  if (jcts != NULL)
    jcts->Select(&xFilter, getJctKey, apps);
}

/**
 ** Zbrise seznam vseh vozlov
 **/
void Junction::Throw()
{
  delete (jcts);
  jcts = NULL;
}


void Junction::ForAll(void (*func) (ads_name name))
{
  jcts->ForAll(func);
}


/**
 ** Poisce vozel v bazi z imenom /jctName/, ki je normalno Cybernet
 ** ime (J234). Ce ga najde vrne 0. Izbor iz baze je cashiran v izboru,
 ** zato lahko se veckratno iskanje po seznamu vozlov izvaja hitreje.
 ** Ob napaki vrne -1; Ce ni nasel vrne 1; Ce je OK vrne 0;
 **/
Junction::Find(const char *jctName)
{
  ads_name ent;

  if (!jcts)
    Collect();
  if(jcts->Find(atoi(jctName + 1), ent) != AVL_OK)
    return -1;

  Get(ent);
  return 0;
}

Junction::Get(ads_name name)
{
  Entity::Get(name);
  return getData();
}

                                         
/**
 ** Nastavi spremenljivke, ki so specificne za Junction.
 **/
int Junction::getData(void)
{
  Entity::Get();
  if (!isA("X_JCT", rb))
    return RTERROR;

  /* Kar predpostavimo, da je vse to ze notri */
  Num     = getXdata(rb, "NODE_#")->rstring;
  Zone    = getXdata(rb, "ZONE")->rlong;
  Elev    = getXdata(rb, "ELEV")->rreal;
  Elev    = getXdata(rb, "ELEV")->rreal;
  U_elev  = getXdata(rb, "U_ELEV")->rreal;
  Dmdad   = getXdata(rb, "DMDAD")->rreal;
  Dmdmd   = getXdata(rb, "DMDMD")->rreal;
  Dmdph   = getXdata(rb, "DMDPH")->rreal;
  Dmd1    = getXdata(rb, "DMD1")->rreal;
  Dmd2    = getXdata(rb, "DMD2")->rreal;
  Dmdf    = getXdata(rb, "DMDF")->rstring;
  Desc    = getXdata(rb, "DESC")->rstring;
  Text    = getXdata(rb, "TEXT")->rstring;

  /* Za Hyperwaft pa tega ne moremo reci */
  union ads_u_val *p;

  if ((p = getXdata(rb, "TZ")) != NULL)
    tempZone = p->rlong;
  else
    tempZone = 0;

  if ((p = getXdata(rb, "UCODE")) != NULL)
    unitCode = p->rstring;
  else
    unitCode = "";


  return RTNORM;
}



/**
 ** Nastavi spremenljivke, ki so specificne za Pipe.
 **/
int Pipe::getData(void)
{
  Get();
  if (!isA("X_PIPE", rb))
    {
      return RTERROR;
    }

  /* Kar predpostavimo, da je vse to ze notri */
  num     = getXdata(rb, "PIPE_#")->rlong;
  jct1    = getXdata(rb, "NODE1_#")->rstring;
  jct2    = getXdata(rb, "NODE2_#")->rstring;
  Diam    = getXdata(rb, "DIAM")->rreal;
  Len     = getXdata(rb, "LEN")->rreal;
  Lenud   = getXdata(rb, "LENUD")->rreal;
  CoefR   = getXdata(rb, "COEFR")->rreal;
  CoefML  = getXdata(rb, "COEFML")->rreal;
  CoefMLUD= getXdata(rb, "COEFMLUD")->rreal;
  Dsc     = getXdata(rb, "DESC")->rstring;
  Text_1  = getXdata(rb, "TEXT")->rstring;
  O_c     = getXdata(rb, "O_C")->rstring;


  /* Za Hyperwaft pa tega ne moremo reci */
  union ads_u_val *p;

  if ((p = getXdata(rb, "TZ")) != NULL)
    tempZone = p->rlong;
  else
    tempZone = 0;

  if ((p = getXdata(rb, "UCODE")) != NULL)
    unitCode = p->rstring;
  else
    unitCode = "";


  return RTNORM;
}

/**
 ** Omogoca izbor cevi
 **/
int Pipe::Pick()
{
  int status = Entity::Pick("Izberi cev:");
  if (status != RTNORM)
    return status;

  return getData();
}

/**
 ** Zapolni podatke o cevi in vrne RTNORM ce je vse OK
 **/
Pipe::GetPipe(ads_name name)
{
  Get(name);
  int status = getData();
  return status;
}



syntax highlighted by Code2HTML, v. 0.9.1