// Change History:
// DART Vector Definition
// 12/03/03 AV Development: Code cleanup. const/none const

#include "drs/drs_vectdef.h"
//#include "drs_appl.h"
#include "drs/drs_formula.h"


// **************************************************************************
// DRS_VectorDef
// **************************************************************************
DRS_VectorDef::DRS_VectorDef(DRS_Application * appl, uint vectID, uint * a,
    uint f, uint p, bool all) :
    HasAddr(false), VectorId(0), AllowAll(all)
{
    const DRS_Vector *vect = SetVector(appl, vectID);
    SetAddress(vect, a);
    SetFormula(vect, f);
    SetPeriod(vect, p);
}

//--------------------------------------------------------------------------

DRS_VectorDef::DRS_VectorDef(DRS_Vector * vect, uint * a, uint f, uint p, bool all) :
    VectorId(0), AllowAll(all)
{
    SetVector(vect);
    SetAddress(vect, a);
    SetFormula(vect, f);
    SetPeriod(vect, p);
}

//--------------------------------------------------------------------------

const DRS_Vector * DRS_VectorDef::SetVector(DRS_Vector * vect)
{
    if(!vect)
        return NULL;
    VectorId = vect->VectorID();
return vect;
}

//--------------------------------------------------------------------------

const DRS_Vector * DRS_VectorDef::SetVector(DRS_Application * appl, uint vectId)
{
    if(vectId >= appl->VectorCount())
        return NULL;
    VectorId = vectId;
return &appl->Vector(VectorId);
}

//--------------------------------------------------------------------------

bool DRS_VectorDef::SetAddress(const DRS_Vector * vect, uint * a)
{
    if(!a || !vect)
        return false;
    HasAddr = false;
    uint i, d, n = vect->Depth();
    AddressDescr.clear();
    Descr desc;
    desc = vect->Descriptor();
    AddressDescr.push_back(desc);
    for(i = 0, d = 1; i < n; ++i, ++d)
        {
        if(AllowAll && a[i] == MAXUINT)
            {
            desc.Caption = "All";
            desc.Short = "All";
            desc.ID = "All";
            }
        else
            {
            if(AllowAll)
                vect->Dim(d).UnbumpDim(*vect, (VectorAddress *)a);
            desc = vect->NodeDescr((uint32*)a, d);
            if(AllowAll)
                vect->Dim(d).BumpDim(*vect, (VectorAddress *)a);
            HasAddr = true;
            }
        AddressDescr.push_back(desc);
        }
return true;
}

//--------------------------------------------------------------------------

bool DRS_VectorDef::SetPeriod(const DRS_Vector * vect, uint p)
{
    PeriodDescr.Clear();
    if(!vect)
        return false;
    if(p > vect->PeriodCount())
        return false;

    PeriodDescr = vect->Period(p);
return true;
}

//--------------------------------------------------------------------------

bool DRS_VectorDef::SetFormula(const DRS_Vector * vect, uint f)
{
    FormulaDescr.Clear();
    if(!vect)
        return false;
    if(f > vect->FormulaCount())
        return false;

    FormulaDescr = vect->Formula(f).Descriptor();
return true;
}

//--------------------------------------------------------------------------

uint DRS_VectorDef::GetDescr(byte * buf, uint sz, const byte * delim, bool IsLong) const
{
    if(!AddressDescr.size() || !buf || !sz)
        return 0;

    uint i, s = 0, n = AddressDescr.size();
    const byte * desc;
    for(i = 0; i < n; ++i)
        {
        desc = AddressDescr[i].Get(IsLong);
        if(!desc)
            return 0;
        s += std::sprintf((char*)&buf[s], "%s", desc);
        if(i < n - 1)
            s += std::sprintf((char*)&buf[s], "%s", delim);
        }

    if(FormulaDescr.MaxLen())
        {
        s += std::sprintf((char*)&buf[s], "%s", delim);
        desc = FormulaDescr.Get(IsLong);
        if(!desc)
            return 0;
        s += std::sprintf((char*)&buf[s], "%s", desc);
        }

    if(PeriodDescr.MaxLen())
        {
        s += std::sprintf((char*)&buf[s], "%s", delim);
        desc = PeriodDescr.Get(IsLong);
        if(!desc)
            return 0;
        s += std::sprintf((char*)&buf[s], "%s", desc);
        }

return s;
}

/****************************************************************************
 NOTES
 ****************************************************************************/
