// Change History
// 03/18/04 AV Development: Add DRS_VectorVirtualPeriodCount, DRS_VectorVirtualPeriodDescr
// 01/08/04 AV Development: Call RecordMacro/RunMacro/StopMacro for appl. to fix #55
// 12/10/03 AV Development: Add ProcessCount and DRS_ClearProcessCache funcs
// 12/09/03 AV Development: Remove params from Process->Execute()
// 11/20/03 AV Development: BC6 conversion. Changes in DRS_RecList_Sort
// 11/19/03 AV Development: Remove development version of the DRS_FieldMaxSize func
// 11/19/03 AV Development: Merge Production version 12
// 11/05/03 AV Production: Add  DRS_FieldMaxSize( DRS_Field const *pField, uint * Size)
// 8/28/03 AV Development: Remove library inclusions
// 10/23/03 AV Development: Add Macro Def services

// DRS_CIF.CPP
// DLL C interface for access to DART functionality
#include <process.h>
//#include <windows>

#include "drs/drs_cif.h"

#include "retrieve/at_retrieve.h"

#include "drs/drs_appl.h"
#include "drs/drs_rec.h"
#include "drs/drs_rpt.h"
#include "drs/drs_formula.h"
#include "drs/drs_sort.h"
#include "drs/drs_srch.h"

// Module Data
//DRS_Application *pCurAppl;

static std::vector<DRS_Application *> Apps;
static DRS_Application * pSelectedApp = NULL;

static DRS_Application * GetSelectedApp()
{
return pSelectedApp;
}

static DRS_Application * SetSelectedApp(DRS_Application * app)
{
    DRS_Application * pOld = pSelectedApp;
    // PS fixed
    if (app)
      pSelectedApp = app;

return pOld;
}

static DRS_Application * PushApp(DRS_Application * app)
{
    Apps.push_back(app);
    DRS_Application * pOld = SetSelectedApp(app);
return pOld;    
}

static void PopApp(DRS_Application * app)
{
    uint i, cnt = Apps.size();
    for(i = 0; i < cnt; ++i)
        if(Apps[i] == app)
            {
            Apps[i] = NULL;
            break;
            }
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Application
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_Initialize(char *sApplID, char *sIniFile)
{         //_export    _stdcall
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = NULL;

try
  {
    PushApp(new DRS_Application( sApplID, sIniFile) );
    app = GetSelectedApp();
  }
catch (AT_Error &err)
  {
    if( ! app )
      {
      //Create blank appl to hold exceptions and return it.
      PushApp(new DRS_Application( sApplID) );
      app = GetSelectedApp();
      app->RecordErrors( err);
      }
    retErr = ERC_any;
  }

  return retErr;
}

DLL_Erc _drs_call_c DRS_Terminate()
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();

try
  {
  //Close application
  PopApp(app);
  if( app) delete app;

  if(GetSelectedApp() == app)
    SetSelectedApp(NULL);        

  app = 0;
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Handle _drs_call_c DRS_ApplicationGet()
{
return (DLL_Handle)GetSelectedApp();
}

DLL_Handle _drs_call_c DRS_ApplicationSelect(DLL_Handle app)
{
return (DLL_Handle)SetSelectedApp((DRS_Application *)app);
}

//////////////////////////////////////////////////////////////////////////////////////////////
// SearchFields
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_SearchFieldCount( uint16 *iCount)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
  
try
  {
  AT_Assert( app, app, "Selected Application");
  *iCount = (uint16) app->SearchFieldCount();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_SearchFieldDescr( uint16 iIdx, DRS_Descriptor *pDescr)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pDescr = app->SearchField( (uint) iIdx).Descriptor();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_SearchFieldHide( uint16 iSearchField, bool * IsHide)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();

try
  {
  *IsHide = false;
  AT_Assert( app, app, "Selected Application");
  const DRS_SearchField & sField = app->SearchField(iSearchField);
  uint i, cnt = sField.DataFieldCount();
  for(i = 0; i < cnt; ++i)
    {
    const DRS_Field & field = sField.DataField(i);
    if(field.Hide())
        {
        *IsHide = true;
        break;
        }
    }
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_SearchFieldIndexIsSortable( uint16 iSearchField, uint16 iIndex, bool *pIsSortable)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();

try
  {
  AT_Assert( app, app, "Selected Application");
  //For now only the first&only index is searched, browsed or sorted.
  *pIsSortable = app->SearchField( (uint) iSearchField).IndexList().IndexInfo(iIndex).IsSortable();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_SearchFieldDataFieldCount( uint16 iSearchFieldIdx, uint16 *pCount)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pCount = (uint16) app->SearchField(iSearchFieldIdx).DataFieldCount();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_SearchFieldDataField( uint16 iSearchFieldIdx, uint16 iDataFieldIdx, DLL_HandlePtr pDataField)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pDataField = (DLL_Handle) & app->SearchField(iSearchFieldIdx).DataField(iDataFieldIdx);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}


//Search field indexes
DLL_Erc _drs_call_c DRS_SearchFieldIndexCount( uint16 iSearchField, uint16 *pCount)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pCount = (uint16) app->SearchField( (uint) iSearchField).IndexList().IndexCount();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_SearchFieldIndexOpen( uint16 iSearchField, uint16 iIndex, DLL_HandlePtr ppIndex)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");

  *ppIndex = (DLL_Handle) app->SearchField( (uint) iSearchField).IndexList().IndexOpen(iIndex);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_SearchFieldFieldMapID( uint16 iSearchField, int16 *pFieldMapID)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( NULL, NULL, "DRS_SearchFieldFieldMapID");
  *pFieldMapID = 0;
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}


//////////////////////////////////////////////////////////////////////////////////////////////
// FieldMaps
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_FieldMapCount(uint16 *pCount)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pCount = (uint16) app->FieldMapCount();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldMapGet(uint16 iFieldMap, DLL_HandlePtr ppFieldMap)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( NULL, NULL, "DRS_FieldMapGet");
  *ppFieldMap = 0;
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldMapEntryCount(DLL_Handle pFieldMap, uint16 *pEntryCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Assert( NULL, NULL, "DRS_FieldMapEntryCount");
  *pEntryCount = 0;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}
DLL_Erc _drs_call_c DRS_FieldMapStateCount(DLL_Handle pFieldMap, uint16 *pStateCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
	AT_Assert( NULL, NULL, "DRS_FieldMapStateCount");  
  *pStateCount = 0;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}
DLL_Erc _drs_call_c DRS_FieldMapStateDescr(DLL_Handle pFieldMap, uint16 iState, DRS_Descriptor *pDescr)
{
  DLL_Erc retErr = ERC_none;
try
  {
	  AT_Assert( NULL, NULL, "DRS_FieldMapStateDescr");  
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}
DLL_Erc _drs_call_c DRS_FieldMapEntryGet(DLL_Handle pFieldMap, uint16 iState, uint16 iEntry, byte *sEntry, uint16 iLen)
{
  DLL_Erc retErr = ERC_none;
try
  {
	  AT_Assert( NULL, NULL, "DRS_FieldMapEntryGet");  
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}
DLL_Erc _drs_call_c DRS_FieldMapStateEntryToMapEntry(DLL_Handle pFieldMap, uint16 iState, uint16 iEntry, uint16 *pMapEntry)
{
  DLL_Erc retErr = ERC_none;
try
  {
	  AT_Assert( NULL, NULL, "DRS_FieldMapStateEntryToMapEntry");  
  *pMapEntry = 0;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}


//////////////////////////////////////////////////////////////////////////////////////////////
// Data Fields
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_FieldCount( uint16 *pCount)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pCount = (uint16) app->FieldCount();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldGet( uint16 iField, DLL_HandlePtr pField)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();  
try
  {
  AT_Assert( app, app, "Selected Application");
  *pField = (DLL_Handle) &app->Field( (uint) iField);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldMaxSize( DLL_Handle pField, uint * Size)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_Field * field = (const DRS_Field *)pField;
  *Size = field->MaxSize();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();  
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldHide( DLL_Handle pField, bool * IsHide)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_Field * field = (const DRS_Field *)pField;
  *IsHide = field->Hide();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldApplID( DLL_Handle pField, uint * pApplID)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_Field * field = (const DRS_Field *)pField;
  *pApplID = field->ApplID();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldApportionGroup( DLL_Handle pField, byte * sDest)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_Field * field = (const DRS_Field *)pField;
  const byte * group = field->FieldOccurrGroup();
  if(group && sDest)
    strcpy((char*) sDest, (const char*) group);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldDescr( DLL_Handle pField, DRS_Descriptor *pDescr)
{
  DLL_Erc retErr = ERC_none;
try
  {
    assert(sizeof(DLL_Handle) == sizeof(DRS_Field*));

  const DRS_Field * field = (const DRS_Field *)pField;
  *pDescr = field->Descriptor();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldStateCount( DLL_Handle pField, uint16 *pCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_Field * field = (const DRS_Field *)pField;
  *pCount = field->StateCount();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldStateDescr( DLL_Handle pField, uint16 iState, DRS_Descriptor *pDescr)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_Field * field = (const DRS_Field *)pField;
  *pDescr = field->State((uint)iState);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldOccurrences( DLL_Handle pRecord, DLL_Handle pField, uint16 *pFieldOccCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_RecordSet * record = (DRS_RecordSet *)pRecord;
  const DRS_Field * field = (const DRS_Field *)pField;

  AT_Assert( record, DRS_RecordSet *, "Record");
  *pFieldOccCount = (uint16) field->FieldOccurrences( record);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldDataLen( DLL_Handle pRecord, DLL_Handle pField, uint16 iState, uint16 iOccurrence, uint16 *pFieldLen)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_RecordSet * record = (DRS_RecordSet *)pRecord;
  const DRS_Field * field = (const DRS_Field *)pField;

  AT_Assert( record, DRS_RecordSet *, "Record");

  *pFieldLen = (uint16) field->FieldLen( record, iState, iOccurrence);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_FieldDataGet( DLL_Handle pRecord, DLL_Handle pField, uint16 iState, uint16 iOccurrence, byte *sDest, uint16 iDestLen)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_RecordSet * record = (DRS_RecordSet *)pRecord;
  const DRS_Field * field = (const DRS_Field *)pField;

  AT_Assert( record, DRS_RecordSet *, "Record");

  field->FieldData( record, iState, iOccurrence, sDest, iDestLen);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Vectors
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_VectorCount( uint16 *pVectorCount)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pVectorCount = (uint16) app->VectorCount();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorDescr( uint16 iVectorID, DRS_Descriptor *pDescr)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pDescr = app->Vector( (uint) iVectorID).Descriptor();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorDepth( uint16 iVectorID, uint16 *pDepth)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pDepth = (uint16) app->Vector(iVectorID).Depth();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorDimDescr( uint16 iVectorID, uint16 iDim, DRS_Descriptor *pDescr)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pDescr = app->Vector( iVectorID).Dim(iDim).Descriptor();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorDimIsCollective( uint16 iVector, uint16 iDim, bool *pIsCollective)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pIsCollective = app->Vector((uint)iVector).Dim((uint)iDim).IsCollective();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorDimIsConstant( uint16 iVector, uint16 iDim, bool *pIsConstant)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pIsConstant =  app->Vector((uint)iVector).Dim((uint)iDim).IsConstant();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);

}

DLL_Erc _drs_call_c DRS_VectorDimMaxLength( uint16 iVector, uint16 iDim, uint * pMaxLen)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pMaxLen = app->Vector((uint)iVector).Dim((uint)iDim).MaxLength();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);

}

DLL_Erc _drs_call_c DRS_VectorNodeCount( uint16 iVectorID, uint16 iDepth, VectorAddress * addrVector, uint *pLen)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  //if get by occurrence
  AT_Assert( app, app, "Selected Application");
  *pLen = app->Vector(iVectorID).NodeCount( iDepth, addrVector);


  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorNodeDescr( uint16 iVectorID, uint16 iDepth, VectorAddress * addrVector, DRS_Descriptor *pDescr)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();  
try
  {
    /*
    //Todo:
    if a child elevel ius constant, store and get a dummy node.
    occurs when a const dim occurs before a var dim, i.e. Plan/Product.
    don'ty want to duplicate plans in tree.
    */
    AT_Assert( app, app, "Selected Application");
    *pDescr = app->Vector(iVectorID).NodeDescr( (uint32*) addrVector, iDepth);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorPeriodCount( uint16 iVectorID, bool isIndex, uint16 *pCount)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  if( isIndex)
    *pCount = (uint16) app->Vector(iVectorID).IndexPeriodCount();
  else
    *pCount = (uint16) app->Vector(iVectorID).PeriodCount();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorVirtualPeriodCount( uint16 iVectorID, bool isIndex, uint16 *pCount)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  if( isIndex)
    *pCount = (uint16) app->Vector(iVectorID).VirtualIndexPeriodCount();
  else
    *pCount = (uint16) app->Vector(iVectorID).VirtualPeriodCount();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorPeriodDescr( uint16 iVectorID, uint16 iPeriod, bool isIndex, DRS_Descriptor *pDescr)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  if( isIndex)
    *pDescr = app->Vector(iVectorID).IndexPeriod( iPeriod);
  else
    *pDescr = app->Vector(iVectorID).Period( iPeriod);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorVirtualPeriodDescr( uint16 iVectorID, uint16 iPeriod, bool isIndex, DRS_Descriptor *pDescr)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  if( isIndex)
    *pDescr = app->Vector(iVectorID).VirtualIndexPeriod( iPeriod);
  else
    *pDescr = app->Vector(iVectorID).VirtualPeriod( iPeriod);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorIndexPeriodToDataPeriod( uint16 iVectorID, uint16 iIndexPeriod, uint16 *pDataPeriod)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pDataPeriod = (uint16) app->Vector(iVectorID).IndexPeriodToDataPeriod( iIndexPeriod);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorStateCount( uint16 iVectorID, uint16 *pCount)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pCount = (uint16) app->Vector((uint)iVectorID).IndexStateCount( );
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorStateDescr( uint16 iVectorID, uint16 iState, DRS_Descriptor *pDescriptor)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pDescriptor = app->Vector(iVectorID).IndexStateDescr( (uint) iState);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorIndexOpen( uint16 iVectorID, VectorAddress *addrVector,
                                      uint16 iVectorState,
                                      uint16 iPeriod, DLL_HandlePtr ppIndex)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *ppIndex = (DLL_Handle) app->Vector(iVectorID).IndexOpen( (uint32*) addrVector, (uint)iVectorState, (uint)iPeriod, 0);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorFormulaCount( uint16 iVectorID, uint16 *pCount)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pCount = (uint16) app->Vector((uint)iVectorID).FormulaCount( );
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorFormulaDescr( uint16 iVectorID, uint16 iFormula, DRS_Descriptor *pDescriptor)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pDescriptor = app->Vector(iVectorID).Formula( (uint) iFormula).Descriptor();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorFormulaInfo( uint16 iVectorID, uint16 iFormula, DRS_Formula_Info * pFormulaInfo)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();  
try
  {
  AT_Assert( app, app, "Selected Application");
  pFormulaInfo->IsReport = app->Vector(iVectorID).Formula( (uint) iFormula).IsReport();
  pFormulaInfo->IsMutable = app->Vector(iVectorID).Formula( (uint) iFormula).IsMutable();
  pFormulaInfo->IsQuantile = app->Vector(iVectorID).Formula( (uint) iFormula).IsQuantile();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorFormulaIsReport( uint16 iVectorID, uint16 iFormula, bool *pIsit)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pIsit = app->Vector(iVectorID).Formula( (uint) iFormula).IsReport();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorFormulaIsMutable( uint16 iVectorID, uint16 iFormula, bool *pIsit)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pIsit = app->Vector(iVectorID).Formula( (uint) iFormula).IsMutable();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorFormulaIsSumable( uint16 iVectorID, uint16 iFormula, bool *pIsit)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pIsit = app->Vector(iVectorID).Formula( (uint) iFormula).IsSumable();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_VectorFormulaIsGroupable( uint16 iVectorID, uint16 iFormula, bool *pIsit)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pIsit = app->Vector(iVectorID).Formula( (uint) iFormula).IsGroupable();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

// Get vector data by VectorAddress; Returns Vector data and isAvail.
DLL_Erc _drs_call_c DRS_VectorDataGet( DLL_Handle pRecord, uint16 iVector, VectorAddress Address[], uint16 iFormula, DLL_Handle ppResults, bool *pIsAvail)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_RecordSet * record = (DRS_RecordSet *)pRecord;
  DRS_VectorDataElem ** results = (  DRS_VectorDataElem **)ppResults;
  record->VectorGet( (uint)iVector, (uint32*)Address, (uint)iFormula, results, pIsAvail);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_VectorMaxPrecision( uint16 iVectorID, uint *pMaxPrecision)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *pMaxPrecision = app->Vector( iVectorID).DataPrecision();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

//Results
DLL_Erc _drs_call_c DRS_ResultSetOpen( uint16 iVectorID, DLL_HandlePtr ppFormulaResults)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  uint iPeriodCount = app->Vector( iVectorID).PeriodCount();
  uint iPrecisionMult = app->Vector( iVectorID).DataPrecisionMult();

  DRS_VectorDataElem ** results = new DRS_VectorDataElem * [iPeriodCount];
  for( uint i = 0; i < iPeriodCount; ++i)
    results[i] = new DRS_VectorDataElem( 0, iPrecisionMult);
  *ppFormulaResults = (DLL_Handle)results;
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_ResultSetClose( uint16 iVectorID, DLL_Handle ppFormulaResults)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  uint iPeriodCount = app->Vector( iVectorID).PeriodCount();
  DRS_VectorDataElem ** results = (DRS_VectorDataElem **)ppFormulaResults;
  for( uint i = 0; i < iPeriodCount; ++i)
    delete results[i];

  delete [] results;
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_ResultRealGet( DLL_Handle ppFormulaResults, uint16 iItem, uint iPrecision, double *pRealResult)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_VectorDataElem ** results = (DRS_VectorDataElem **)ppFormulaResults;
  *pRealResult = results[iItem]->GetRealValueAtPrecision( iPrecision);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_ResultIntGet( DLL_Handle ppFormulaResults, uint16 iItem, int32 *pIntResult)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_VectorDataElem ** results = (DRS_VectorDataElem **)ppFormulaResults;
  *pIntResult = results[iItem]->GetIntValue();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

#if(0)
// Get vector by occurrence; Returns VectorIDs and Vector
//TO_DO NOTE Vectors may be located in different databases and hence have similar
//addresses. There4 MUST be queried by VectorID.
DLL_Erc _drs_call_c DRS_VectorOccurrences( DLL_Handle pRecord, uint32 *pCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_RecordSet * record = (DRS_RecordSet *)pRecord;
  AT_Assert(record, DRS_RecordSet, "Record");

  *pCount = record->VectorCount( (uint) iVectorID);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app) app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

// Get vector by occurrence; Returns VectorID, VectorAddress and VectorData
DLL_Erc _drs_call_c DRS_VectorDataOccurrenceGet( DLL_Handle pRecord, uint32 iVectorOcc, uint16 *pVectorID, VectorAddress Address[], uint32 pVector[])
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_RecordSet * record = (DRS_RecordSet *)pRecord;
  AT_Assert(record, DRS_RecordSet, "Record");

  record->RecordVectorGetN( (uint)iVectorIdx, iVectorOcc, pVector, Address, (uint)iVectorIdx))
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}
#endif

#if 0
//////////////////////////////////////////////////////////////////////////////////////////////
// TO_DO Publicize vectors and formulas
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_VectorGet( uint16 iVectorIdx, DLL_HandlePtr ppVector)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  *ppVector = (DLL_Handle)&app->Vector( iVectorIdx);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}
DLL_Erc _drs_call_c DRS_VectorFormulaGet( DRS_Vector const *pVector, uint iFormulaIdx, DLL_HandlePtr ppFormula)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_Vector * vector = (const DRS_Vector *)pVector;
  *ppFormula = (DLL_Handle)vector->Formula( iFormulaIdx);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}
DLL_Erc _drs_call_c DRS_VectorFormulaCount( DLL_Handle pVector, uint *pFormulaCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_Vector * vector = (const DRS_Vector *)pVector;
  *pFormulaCount = vector->FormulaCount();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}
DLL_Erc _drs_call_c DRS_VectorFormulaDataGet( DLL_Handle pFormula, DLL_Handle pRecord, VectorAddress pVectorAddress[], int32 pVectorData[], bool *pIsAvail)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_Formula * formula = (const DRS_Formula *)pFormula;
  const DRS_RecordSet * record = (const DRS_RecordSet *)pRecord;
  formula->GetVector( *record, pVectorAddress, pVectorData, pIsAvail);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}
#endif

//////////////////////////////////////////////////////////////////////////////////////////////
// VectorNodeOccurrences
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_VectorNodeOccurrencesGet( DLL_Handle pRecord,
                                              uint16 iVector,
                                              VectorAddress addr[],
                                              uint16 iDepth,
                                              uint16 isUnique,
                                              DLL_HandlePtr ppOccs)
{
  // isUnique currently unused.

  uint16 retErr = ERC_none;
  DRS_Application * app = NULL;

  try
    {
    DRS_RecordSet * record = (DRS_RecordSet *)pRecord;
    DRS_OccurrenceCollection *pOccs = new DRS_OccurrenceCollection;
    app = (DRS_Application *)record->GetApplication();

    app->Vector( (uint) iVector).NodeOccurrences(  record, (uint32*) addr, iDepth, !!isUnique, pOccs);
    *ppOccs = (DLL_Handle)pOccs;
    }
  catch( AT_Error &err)
    {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
    }
  return retErr;
}

DLL_Erc _drs_call_c DRS_VectorNodeOccurrenceCount(DLL_Handle pOccs, uint *iCount)
{
  uint16 retErr = ERC_none;

  try
    {
    const DRS_OccurrenceCollection * occs = (const DRS_OccurrenceCollection *)pOccs;
    *iCount = occs->size();
    }
  catch( AT_Error &err)
    {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
    }
  return retErr;
}

DLL_Erc _drs_call_c DRS_VectorNodeOccurrenceAddress(DLL_Handle pOccs, uint iOcc,
                                                VectorAddress *pAddress)
{
  uint16 retErr = ERC_none;

  try
    {
    const DRS_OccurrenceCollection * occs = (const DRS_OccurrenceCollection *)pOccs;
    *pAddress = (*occs)[iOcc];
    }
  catch( AT_Error &err)
    {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
    }
  return retErr;
}

DLL_Erc _drs_call_c DRS_VectorNodeOccurrencesClose(DLL_Handle pOccs)
{
  uint16 retErr = ERC_none;

  try
    {
    const DRS_OccurrenceCollection * occs = (const DRS_OccurrenceCollection *)pOccs;
    delete occs;
    }
  catch( AT_Error &err)
    {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
    }
  return retErr;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Indexes
//////////////////////////////////////////////////////////////////////////////////////////////

DLL_Erc _drs_call_c DRS_Index_HasCounts( DLL_Handle pIndex, bool *pHasCounts)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_Index * index = (DRS_Index *)pIndex;
  *pHasCounts = index->hasCounts();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }

  return(retErr);
}

DLL_Erc _drs_call_c DRS_Index_IsSeek( DLL_Handle pIndex, bool *pIsSeek)
{
  DLL_Erc retErr = ERC_none;
  try
    {
    DRS_Index * index = (DRS_Index *)pIndex;
    *pIsSeek =  index->IsSeekable();
    }
  catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_Index_IsText( DLL_Handle pIndex, bool *pIsText)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_Index * index = (DRS_Index *)pIndex;
  *pIsText =  index->IsText();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_Index_Search( DLL_Handle pIndex, byte *sSearchString, DLL_HandlePtr ppRecList)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_Index * index = (DRS_Index *)pIndex;
  AT_Assert( index, DRS_Index, "IndexPtr");

  *ppRecList = (DLL_Handle) ARL_CreateRecordList( index->Search(sSearchString));
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_Index_TermCount( DLL_Handle pIndex, uint32 *lCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_Index * index = (DRS_Index *)pIndex;
  *lCount = index->TermCountGet();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_Index_Close( DLL_Handle pIndex)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_Index * index = (DRS_Index *)pIndex;
  AT_Assert( index, AT_Index, "Index");
  delete ( index);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Index Iterators
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_IndexIter_New( DLL_Handle pIndex, DLL_HandlePtr ppIter)
{
  DLL_Erc retErr = ERC_none;
  AT_Index::Iter *p_iter = 0;
try
  {
  DRS_Index * index = (DRS_Index *)pIndex;
  assert( ppIter);
  *ppIter = 0;
  p_iter = ARL_CreateIndexIter(index->begin());
  *ppIter = (DLL_Handle) p_iter;
  }
catch (AT_Error &err)
  {
    if( p_iter) delete p_iter;
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_IndexIter_Close( DLL_Handle pIter)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Index::Iter * iter = (AT_Index::Iter *)pIter;
  assert( iter);
  ARL_DeleteIndexIter(iter);  // AV
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_IndexIter_Next( DLL_Handle pIter)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Index::Iter * iter = (AT_Index::Iter *)pIter;
  assert( iter);
  ++(*iter);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_IndexIter_Prev( DLL_Handle pIter)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Index::Iter * iter = (AT_Index::Iter *)pIter;
  assert( iter);
  --(*iter);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_IndexIter_Seek( DLL_Handle pIter, long lOffset)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Index::Iter * iter = (AT_Index::Iter *)pIter;
  assert( iter);
  *iter += lOffset;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_IndexIter_SeekTerm( DLL_Handle pIndex, DLL_Handle pIter, byte *sSeekString, uint32 *pRetIdx)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_Index * index = (DRS_Index *)pIndex;
  AT_Index::Iter * iter = (AT_Index::Iter *)pIter;

  AT_Assert( sSeekString, sSeekString, "Index term");

  *iter = index->SeekTerm( sSeekString);
  *pRetIdx = *iter - index->begin();

  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_IndexIter_TermGet( DLL_Handle pIndex, DLL_Handle pIter, byte *sDest, uint16 iDestLen, uint32 *pOccurrences)
{
//   1. Get iterator for index.
//   2. Get AT_Index_Term from iterator with operator*.
//   3. Get term from AT_Index_Term.
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_Index * index = (DRS_Index *)pIndex;
  AT_Index::Iter * iter = (AT_Index::Iter *)pIter;

  *sDest = 0;
  index->TermGet( *iter, sDest, iDestLen, pOccurrences);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Record Lists
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_RecList_Count( DLL_Handle pRecList, uint32 *cnt)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List * list = (AT_Record_List *)pRecList;
  AT_Assert( list, AT_Record_List, "Record list");
  *cnt = list->count();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecList_Close( DLL_Handle pRecList)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List * list = (AT_Record_List *)pRecList;
  AT_Assert( list, AT_Record_List, "Record list");
  // again, this should make no difference...
  //(*pRecList).AT_Record_List::~AT_Record_List();
  //operator delete (pRecList);
  ARL_DeleteRecordList(list);
  //delete ( pRecList);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

//...for building boolean connection logic
DLL_Erc _drs_call_c DRS_RecList_New( DLL_HandlePtr ppRecList)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");  
  *ppRecList = (DLL_Handle)app->RecList_New();
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecList_Invert( DLL_Handle pRecListDest)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Record_List * list = (AT_Record_List *)pRecListDest;
  AT_Assert( app, app, "Selected Application");
  list->invert(app->RecordCount());
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecList_Connect( DLL_Handle pRecListDest, DLL_Handle pRecListSource, uint16 iConnector)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List * dest = (AT_Record_List *)pRecListDest;
  AT_Record_List * src = (AT_Record_List *)pRecListSource;

  AT_Assert( dest, AT_Record_List, "Null param.");
  
  switch( (DRS_Connection) iConnector)
    {
    case DRS_Connection_AND:
                        *dest &= *src;
                        break;
    case DRS_Connection_OR:
                        *dest |= *src;
                        break;
    case DRS_Connection_WOUT:
                        *dest -= *src;
                        break;
    case DRS_Connection_XOR:
                        *dest ^= *src;
                        break;
    default:
                        throw AT_Bounds_Err(iConnector, (AT_String) "Connector", (uint32) iConnector);
    }
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecList_Add ( DLL_Handle pRecListSrc , uint32 idx, DLL_Handle pRecListDest)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List * src = (AT_Record_List *)pRecListSrc;
  AT_Record_List * dest = (AT_Record_List *)pRecListDest;

  assert( src);
  assert( dest);
  AT_Record_List::Iter iter = src->begin();

  //AT_Record_List::Iter iter(pRecList->begin());
  iter += idx;

  *dest += *iter;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecList_Remove (DLL_Handle pRecListSrc, uint32 idx, DLL_Handle pRecListDest)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List * src = (AT_Record_List *)pRecListSrc;
  AT_Record_List * dest = (AT_Record_List *)pRecListDest;

  assert( dest);
  AT_Record_List::Iter iter = src->begin();

  iter += idx;

  *dest -= *iter;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}


DLL_Erc _drs_call_c DRS_RecList_IsOneOf (DLL_Handle pRecListSrc, uint32 *pidx, DLL_Handle pRecListDest, bool *pIsOne)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List * src = (AT_Record_List *)pRecListSrc;
  AT_Record_List * dest = (AT_Record_List *)pRecListDest;

  assert( dest);

  if( src) //Get record number from source reclist?, else idx is abs rec no
    {
    AT_Record_List::Iter iter = src->begin();
    iter += *pidx;
    *pidx = *iter;
    }
  uint32 idx_dest;
  *pIsOne = dest->hasRecord( *pidx, &idx_dest);
  *pidx = idx_dest;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecList_AbsRecGet (DLL_Handle pRecListSrc, uint32 idx, uint32 *pAbsRecNo)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List * src = (AT_Record_List *)pRecListSrc;
  assert( src);

  AT_Record_List::Iter iter = src->begin();
  iter += idx;
  *pAbsRecNo = *iter;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecList_Copy( DLL_Handle pRecListSrc, DLL_HandlePtr ppRecListDest)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List * src = (AT_Record_List *)pRecListSrc;

  assert( ppRecListDest);

  *ppRecListDest = (DLL_Handle)ARL_CreateRecordList( *src);
  AT_Assert( *ppRecListDest, DLL_Handle, "record list");
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecList_Sort( DLL_Handle pRecListSrc, sortRec cif_ctxts[], uint16 iCount, HWND hParent, bool *pIsCancel)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List * src = (AT_Record_List *)pRecListSrc;
  AT_Assert( src, AT_Record_List, "record list");
  DRS_Sort_RecList SortRecList(src);

  //Create sort contexts
  std::vector<AT_Record_List::Sort_Context> Contexts(iCount);
  for( uint i = 0; i < iCount; i++)
    {
//    Contexts[i] = AT_Record_List::Sort_Context(*((AT_Index *)cif_ctxts[i].pIndex),
//                                               cif_ctxts[i].isAscending,
//                                               cif_ctxts[i].keepUnindexed,
//                                               cif_ctxts[i].isDuplicate);

    Contexts[i] = AT_Record_List::Sort_Context((AT_Index *)cif_ctxts[i].pIndex,
                                               cif_ctxts[i].isAscending,
                                               cif_ctxts[i].keepUnindexed,
                                               cif_ctxts[i].isDuplicate);

    }
  SortRecList.Sort( &Contexts.front(), (unsigned int) iCount, hParent, pIsCancel);
  //*pRecListSrc = (DLL_Handle)SortRecList.getBase();
  }

catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecList_TermCount( DLL_Handle pRecListSrc, DLL_Handle index, bool *pIsCancel, uint * iCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List * src = (AT_Record_List *)pRecListSrc;
  AT_Index * idx = (AT_Index *)index;
  AT_Assert( src, AT_Record_List, "record list");
  AT_Assert( idx, AT_Index, "index");

  // DRS_Progress prog((byte *)"Term Count");
  *iCount = src->term_count( *idx, NULL);
  //*pIsCancel = prog.IsCancel();
  *pIsCancel = false;
  }

catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

//////////////////////////////////////////////////////////////////////////////////////////////
// RecordList Iterators
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_RecListIter_New( DLL_Handle pRecList, DLL_HandlePtr ppRLIter)
{
  DLL_Erc retErr = ERC_none;
  AT_Record_List::Iter *p_iter = 0;
try
  {
  AT_Record_List * list = (AT_Record_List *)pRecList;
  assert( ppRLIter);
  //p_iter = new AT_Record_List::Iter( pRecList->begin());
  p_iter = ARL_CreateRecordListIter( list->begin());
  *ppRLIter = (DLL_Handle) p_iter;
  }
catch (AT_Error &err)
  {
    if( p_iter) delete p_iter;

    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecListIter_Close( DLL_Handle pRLIter)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List::Iter * iter = (AT_Record_List::Iter *)pRLIter;
  assert( iter);
  ARL_DeleteRecordListIter( iter);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();  
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecListIter_Next( DLL_Handle pRLIter)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List::Iter * iter = (AT_Record_List::Iter *)pRLIter;
  assert( iter);
  ++(*iter);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecListIter_Prev( DLL_Handle pRLIter)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List::Iter * iter = (AT_Record_List::Iter *)pRLIter;
  assert( iter);
  --(*iter);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecListIter_Seek( DLL_Handle pRLIter, long lOffset)
{
  DLL_Erc retErr = ERC_none;
try
  {
  AT_Record_List::Iter * iter = (AT_Record_List::Iter *)pRLIter;
  assert( iter);
  *iter += lOffset;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Records
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _drs_call_c DRS_RecordGet( DLL_Handle p_iter, DLL_HandlePtr ppRecord)
{
  DLL_Erc retErr = ERC_none;
  DRS_RecordSet *pRecord = 0;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Record_List::Iter * iter = (AT_Record_List::Iter *)p_iter;

  assert( iter);
  assert( ppRecord);
  AT_Assert( app, app, "Selected Application");

  *ppRecord = (DLL_Handle)new DRS_RecordSet(app, **iter);
  }
catch (AT_Error &err)
  {
    if( pRecord) delete pRecord;
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

DLL_Erc _drs_call_c DRS_RecordClose( DLL_Handle pRecord)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_RecordSet * record = (DRS_RecordSet *)pRecord;
  delete record;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return(retErr);
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Reports
//////////////////////////////////////////////////////////////////////////////////////////////

//Report definition, generation
DLL_Erc _drs_call_c DRS_ReportNew( DRS_Descriptor *pDescriptor, DLL_HandlePtr ppReport)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();  
try
  {
  AT_Assert( pDescriptor, DRS_Descriptor, "Bad handle");
  AT_Assert( app, app, "Selected Application");
  *ppReport = (DLL_Handle)new DRS_ReportTree( *app, *pDescriptor);
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ReportClose( DLL_Handle pReport)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_ReportTree * tree = (DRS_ReportTree *)pReport;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");
  delete tree;
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}
DLL_Erc _drs_call_c DRS_ReportAddVector( DLL_Handle pReport, uint16 iVectorID, VectorAddress VectorSelector[])
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_ReportTree * tree = (DRS_ReportTree *)pReport;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");
  tree->AddVector( (uint)iVectorID, (uint32*)VectorSelector);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}
DLL_Erc _drs_call_c DRS_ReportAddSubreport( DLL_Handle pReportTree, uint16 iSearchFieldID)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_ReportTree * tree = (DRS_ReportTree *)pReportTree;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");
  tree->AddSubreport( (uint)iSearchFieldID);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}
DLL_Erc _drs_call_c DRS_ReportGenerate( DLL_Handle pReportTree, DLL_Handle pRecordList, bool *pIsCancel)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_ReportTree * tree = (DRS_ReportTree *)pReportTree;
  AT_Record_List * list = (AT_Record_List *)pRecordList;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");

  *pIsCancel = false;
  tree->Report( list, pIsCancel);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

//Report retrieval
DLL_Erc _drs_call_c DRS_ReportTreeGetRoot( DLL_Handle pReportTree, DLL_HandlePtr ppNode)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_ReportTree * tree = (DRS_ReportTree *)pReportTree;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");

  *ppNode = (DLL_Handle)tree->GetRootNode();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}


DLL_Erc _drs_call_c DRS_ReportTreeDepth( DLL_Handle pReport, uint16 *pDepth)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_ReportTree * tree = (DRS_ReportTree *)pReport;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");
  *pDepth = (uint16) tree->Depth();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}
DLL_Erc _drs_call_c DRS_ReportTreeDimDescr( DLL_Handle pReportTree, uint16 iDim, DRS_Descriptor *pDescr)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_ReportTree * tree = (DRS_ReportTree *)pReportTree;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");
  *pDescr = tree->Dim( (uint) iDim).Descriptor();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ReportTreeNodeGet( DLL_Handle pReportTree, VectorAddress ReportAddress[], uint16 iDim, DLL_HandlePtr ppNode)
{
  DLL_Erc retErr = ERC_none;
try
  {
  DRS_ReportTree * tree = (DRS_ReportTree *)pReportTree;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");
  *ppNode = (DLL_Handle)&tree->Node( (uint) iDim, (uint32*) ReportAddress);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

//Report nodes
DLL_Erc _drs_call_c DRS_ReportNodeQualifierLen( DLL_Handle pReportNode, uint16 *pLen)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportNode * node = (const DRS_ReportNode *)pReportNode;
  AT_Assert( node, DRS_ReportNode, "Bad handle");
  *pLen = (uint16) node->FieldData()->Length();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}
DLL_Erc _drs_call_c DRS_ReportNodeQualifierGet( DLL_Handle pReportNode, byte *sDest, uint16 iDestLen)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportNode * node = (const DRS_ReportNode *)pReportNode;
  AT_Assert( node, DRS_ReportNode, "Bad handle");
  node->FieldData()->Get( sDest, (uint)iDestLen);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ReportNodeChildCount( DLL_Handle pReportNode, uint32 *pChildCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportNode * node = (const DRS_ReportNode *)pReportNode;
  AT_Assert( node, DRS_ReportNode, "Bad handle");
  *pChildCount = (uint32) node->ChildCount();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ReportNodeChildGet( DLL_Handle pReportNode, uint32 lChild, DLL_HandlePtr ppNode)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportNode * node = (const DRS_ReportNode *)pReportNode;

  AT_Assert( node, DRS_ReportNode, "Bad handle");
  *ppNode = (DLL_Handle)node->GetChild( lChild);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

//Report Vectors
DLL_Erc _drs_call_c DRS_ReportTreeVectorTreeCount( DLL_Handle pReportTree, uint32 *pCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportTree * tree = (const DRS_ReportTree *)pReportTree;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");
  *pCount = tree->GetRootNode()->VectorCount();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ReportTreeVectorTreeSelector( DLL_Handle pReportTree, uint iVectorTree, VectorAddress *addrSelector)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportTree * tree = (const DRS_ReportTree *)pReportTree;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");
  tree->Root().ReportVector(iVectorTree).SelectorGet( (uint32*) addrSelector);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}


DLL_Erc _drs_call_c DRS_ReportVectorTree2ApplVectorTree( DLL_Handle pReportTree, uint iReportVector, VectorAddress *pApplVectorID)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportTree * tree = (const DRS_ReportTree *)pReportTree;
  AT_Assert( tree, DRS_ReportTree, "Bad handle");
  *pApplVectorID = tree->GetRootNode()->ReportVector(iReportVector).Vector().VectorID();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ReportNodeVectorTreeGet( DLL_Handle pReportNode, uint16 iVectorIdx, DLL_HandlePtr ppVectorNode)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportNode * node = (const DRS_ReportNode *)pReportNode;
  AT_Assert( node, DRS_ReportNode, "Bad handle");
  *ppVectorNode = (DLL_Handle)node->ReportVector( (uint) iVectorIdx).GetRootNode();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ReportVectorNodeChildCount( DLL_Handle pVectorNode, uint *pCount)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportVectorTreeNode * node = (const DRS_ReportVectorTreeNode *)pVectorNode;
  AT_Assert( node, DRS_ReportVectorTreeNode, "Bad handle");
  *pCount = node->ChildCount();
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ReportVectorNodeChildGet( DLL_Handle pVectorNode, uint iApplVector, uint16 iDepth, uint iChildIdx, uint *pChildID, DLL_HandlePtr ppChildNode)
{
//iApplVector & iDepth: Needed for mapping return address to appl vector address

  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();

try
  {
  const DRS_ReportVectorTreeNode * node = (const DRS_ReportVectorTreeNode *)pVectorNode;

  AT_Assert( node, DRS_ReportVectorTreeNode, "Bad handle");

  if( ppChildNode)
    *ppChildNode = (DLL_Handle)node->GetChildAbs( iChildIdx);
  if( pChildID)
    {
    *pChildID = node->GetChildAbs( iChildIdx)->ID();

    //Bump down collective
    DRS_Vector const &v = app->Vector(iApplVector);

      if(  v.Dim(iDepth).IsCollective()
          && v.Dim(iDepth-1).IsMappedToChild()
          && (*pChildID >= v.Dim(iDepth-1).MappedToChild()))

              *pChildID = *pChildID - 1;
    }
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ReportVectorNodeChildGetByID( DLL_Handle pVectorNode, uint iChildID, DLL_HandlePtr ppChildNode)
{
//iApplVector & iDepth: Needed for mapping return address to appl vector address

  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportVectorTreeNode * node = (const DRS_ReportVectorTreeNode *)pVectorNode;
  AT_Assert( node, DRS_ReportVectorTreeNode, "Bad handle");

  if( ppChildNode)
    *ppChildNode = (DLL_Handle)node->GetChildNoRaise( iChildID);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ReportVectorNodeDataGet( DLL_Handle pReportVectorNode, DLL_Handle ppResults)
{
  DLL_Erc retErr = ERC_none;
try
  {
  const DRS_ReportVectorTreeNode * node = (const DRS_ReportVectorTreeNode *)pReportVectorNode;
  DRS_VectorDataElem ** results = (DRS_VectorDataElem **)ppResults;

  AT_Assert( node, DRS_ReportVectorTreeNode, "Bad handle");
  node->GetData( results);
  }
catch (AT_Error &err)
  {
    DRS_Application * app = GetSelectedApp();
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}


//////////////////////////////////////////////////////////////////////////////////////////////
// Exceptions
//////////////////////////////////////////////////////////////////////////////////////////////

DLL_Erc _drs_call_c DRS_ExceptionPop( byte *sDest, uint16 iDestLen)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  AT_Assert( app, app, "Selected Application");
  if( app && app->ExceptionCount())
    std::strncpy( (char*) sDest, app->ExceptionPop().c_str(), iDestLen );
  else retErr = ERC_any;
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}

DLL_Erc _drs_call_c DRS_ExceptionGet( uint i, byte *sDest, uint16 iDestLen)
{
  DLL_Erc retErr = ERC_none;
  DRS_Application * app = GetSelectedApp();
try
  {
  if( app)
    {
    uint cnt = app->ExceptionCount();
    if(i < cnt)
        std::strncpy( (char*) sDest, app->Exceptions()[i].c_str(), iDestLen );
    }
  else retErr = ERC_any;
  }
catch (AT_Error &err)
  {
    if( app)
      app->RecordErrors( err);
    retErr = ERC_any;
  }
  return( retErr);
}


uint16 _drs_call_c DRS_ExceptionCount()
{
  DRS_Application * app = GetSelectedApp();
  if( !app) return 0;
  return( app->ExceptionCount());
}

int16 _drs_call_c DRS_ExceptionPopLen()
{
  DRS_Application * app = GetSelectedApp();
  if( !app) return 0;
  return( app->ExceptionLen());
}

int16 _drs_call_c DRS_ExceptionGetLen(uint i)
{
  DRS_Application * app = GetSelectedApp();
  if( !app) return 0;

  byte buf[1000];
  DRS_ExceptionGet(i, buf, sizeof(buf));
  return( std::strlen( (const char*) buf));
}

