//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / mgmt module
//
//File: mgmtcommand.cpp
//
//Version: $Revision: 1.9 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/09/17 17:13:06 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Timo Kokkonen
//
//Description:
//      See corresponding header file.
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History:
//
#include "pf/exception.h"
#include "mgmtcommand.h"

//
//Function: constructor
//
//Description:
//      Creates mgmtCommand without list.
//
mgmtCommand :: mgmtCommand(const string &command_)
    : _command(command_),
      _nextParameter(0),
      _list()
{
    return;
}


//----------------------------------------------------------------------

//
//Function: Constructor
//
//Description:
//      Creates mgmtCommand with list.
//
mgmtCommand :: mgmtCommand(const string &command_,
                           const toveSwitch_ParameterList &list_)
    : _command(command_),
      _nextParameter(0),
      _list(list_)
{
    return;
}

//----------------------------------------------------------------------

//
//Function: Destructor
//
//Description:
//
//
mgmtCommand :: ~mgmtCommand(void)
{
    return;
}

//----------------------------------------------------------------------

//
//Function: operator=
//
//Description:
//      Copy command name and list.
//
const mgmtCommand &mgmtCommand :: operator=(const mgmtCommand &other_)
{
    if (this != &other_) // if not x = x situation
    {
        _command = other_._command;
    	_nextParameter = other_._nextParameter;
    	_list = other_._list;
    }
    return *this;
}

//----------------------------------------------------------------------

//
//Function: getCommandName
//
//Description:
//      Returns a command name.
//
string mgmtCommand :: getCommandName(void) const
{
    return _command;
}

//----------------------------------------------------------------------

//
//Function: getNextParameter
//
//Description:
//      Gives a next parameter from parameterlist.
//
void mgmtCommand :: getNextParameter(string &name_,
                                     string &value_,
                                     string &type_)
{
    if (_nextParameter >= _list.length())
    {
        throw pfOutOfRangeException(PF_EX_INFO);
    }

    name_ = _list[_nextParameter].id;
    value_ = _list[_nextParameter].value;
    type_ = _list[_nextParameter].type;

    ++_nextParameter;

    return;
}

void mgmtCommand :: getNextParameter(string &name_, string &value_)
{
    if (_nextParameter >= _list.length())
    {
        throw pfOutOfRangeException(PF_EX_INFO);
    }

    name_ = _list[_nextParameter].id;
    value_ = _list[_nextParameter].value;

    ++_nextParameter;

    return;
}

void mgmtCommand :: getNextParameter(string &value_)
{
    if (_nextParameter >= _list.length())
    {
        throw pfOutOfRangeException(PF_EX_INFO);
    }
    
    value_ = _list[_nextParameter].value;
    
    ++_nextParameter;

    return;
}

//----------------------------------------------------------------------

//
//Function: getParameterLength
//
//Description:
//      Returns parameterlist lengths.
//
pfUlong mgmtCommand :: getCommandLength(void) const
{
    pfUlong length = 0;

    length = _list.length();

    return length;
}

//----------------------------------------------------------------------

//
//Function: setCurrentTheFirst
//
//Description:
//      Sets next sending parameters value 0, means that next parameter is
//      the first of the list.
//
void mgmtCommand :: setCurrentTheFirst(void)
{
    _nextParameter = 0;

    return;
}

//----------------------------------------------------------------------

//
//Function: getList
//
//Description:
//      Returns parameterlist.
//
toveSwitch_ParameterList mgmtCommand :: getList(void) const
{
    return _list;
}

//----------------------------------------------------------------------

//
//Function: addParameter
//
//Description:
//      Adds a new parammeter into parameterlist.
//
void mgmtCommand :: addParameter(const string &name_,
                                 const string &value_,
                                 const string &type_)
{
    pfUlong length = _list.length();

    _list.length(length + 1);

    _list[length].id = CORBA_string_dup(name_.c_str());
    _list[length].value = CORBA_string_dup(value_.c_str());
    _list[length].type = CORBA_string_dup(type_.c_str());

    return;
}

//----------------------------------------------------------------------

//
//Function: addParameter
//
//Description:
//      Adds a new parammeter into parameterlist. Type field is now empty string.
//
void mgmtCommand :: addParameter(const string &name_, const string &value_)
{
    static string type("");
    addParameter(name_, value_, type);

    return;
}

//----------------------------------------------------------------------
