//=========================================================================
// Summary  : 
// Filename : DeviceList.cc
// Author   : marsh
// Project  : 
// Revision : 1
// Created  : 2000/08/19
// Modified : 2000/08/19
//=========================================================================
// Description :
//=========================================================================

#include "System.h"
#include "Syslog.h"

#include "Device.h"
#include "DeviceList.h"

#include "AttributeParser.h"


DeviceList::DeviceList( )
     : ItemList( DEVICE_LIST_ID )
{

}

DeviceList::~DeviceList()
{
     ItemList::~ItemList();
}


Item *DeviceList::addItem( const char *name )
{
     Boolean debug = False;
     int index;

     dprintf("DeviceList -- Adding a new device of name %s", name );

     Device *newDevice = new Device( name );

     index = _itemList.add( (Item * *)&newDevice );

     dprintf("DeviceList -- Added new device with name %s", newDevice->name() );

     return newDevice;
}

int DeviceList::loadDevices( const char *filename )
{
     Boolean debug = False;
    

     const char *fullFilename = System::configurationFile( filename );


     dprintf("DeviceList::loadDevices -- "
		   "Starting load of device list ");
     Syslog::write("Using device config file %s\n", fullFilename );

     AttributeParser::reset();

     try {
	  
	  AttributeParser::parse( fullFilename, this ); 
     }
     catch(Exception e)
     {
	  Syslog::write("DeviceList::loadDevices -- AttributeParser threw: %s",
			e.msg );
	  return -1;
     }
     catch(...)
     {
	  Syslog::write("DeviceList::loadDevices -- AttributeParser threw an "
			"unknown exceptions");

	  return -1;
     }

     int bad = 0;
     if( (bad = verify()) ) {
	  Syslog::write("DeviceList::loadDevices -- %d devices didn't load "
			"properly -- exiting.", bad );
	  return -1;
     }


     debug = False;

     if( debug ) {
	  dprintf("Device list from %s", fullFilename );
	  dumpDeviceList();
     }
     
     return 0;
     

}

void DeviceList::dumpDeviceList()
{
     Device *current;

     for( int i = 0; i < _itemList.size(); i++ ) {
	  _itemList.get(i,(Item * *)&current);
	  Syslog::write("%d:\t%s\t%s\t%d",
			i, current->name(),
			current->port(), current->priority() );

     }
}



