// file: $isip/class/sp/Component/cmp_05.cc
// version: $Id: cmp_05.cc 8236 2002-06-26 19:06:05Z gao $
//

// isip include files
//
#include "Component.h"

// method: setInputName
//
// arguments:
//  const String& name: (input) names of input
//
// return: a boolean value indicating status
//
// this method returns the input name
//
boolean Component::setInputName(const String& name_a) {

  // count the tokens of the input string
  //
  long count = name_a.countTokens(DELIM);
  input_names_d.setLength(count);
  input_offsets_d.setLength(count);
  
  // tokenize the input by spaces
  //
  long pos = 0;
  String temp;
  for (long index = 0; name_a.tokenize(temp, pos, DELIM); index++) {
    temp.trim();
    splitName(input_names_d(index), input_offsets_d(index), temp);
  }
  
  // exit gracefully
  //
  return true;
}

// method: setInputName
//
// arguments:
//  long index: (input) which input to set
//  const String& str: (input) name to set
//
// return: a boolean value indicating status
//
// this method sets the input name at position index
//
boolean Component::setInputName(long index_a, const String& str_a) {

  // check the name is not empty
  //
  if (str_a.length() == 0) {
    return Error::handle(name(), L"setInputName", Error::ARG,
			 __FILE__, __LINE__);
  }

  // check the range of the index, possibly increase vector sizes
  //
  if (index_a >= input_names_d.length()) {
    input_names_d.setLength(index_a + 1);
    input_offsets_d.setLength(index_a + 1);
  }

  // make sure the name has not already been set
  //
  if (input_names_d(index_a).length() != 0) {
    input_names_d(index_a).debug(L"existing name");
    return Error::handle(name(), L"setInputName", ERR, __FILE__, __LINE__);
  }

  // assign the data
  //
  input_offsets_d(0).assign(0);
  return input_names_d(index_a).assign(str_a);
}

// method: splitName
//
// arguments:
//  String& base_name: (output) name of output
//  Long& offset: (output) offset of output
//  const String& full_name: (input) name + offset string
//
// return: a boolean value indicating status
//
// this method splits the input name
//
boolean Component::splitName(String& base_name_a, Long& offset_a,
			   const String& full_name_a) const {

  offset_a = 0;

  long index0 = full_name_a.firstStr(L"(");
  long index1 = full_name_a.firstStr(L")", index0);

  if ((index0 > 0) && (index1 > 0)) {
    String num;
    full_name_a.substr(base_name_a, 0, index0);
    full_name_a.substr(num, index0 + 1, index1 - index0 - 1);

    base_name_a.trimLeft();
    num.trim();
    long n;
    num.get(n);
    offset_a = n;
  }

  // if one is present without the other
  //
  else if ((index0 > 0) ^ (index1 > 0)) {
    return Error::handle(name(), L"splitName", ERR, __FILE__, __LINE__);
  }
  else {
    base_name_a.assign(full_name_a);
  }

  // exit gracefully
  //
  return true;
}

// method: joinName
//
// arguments:
//  String& out: (output) joined string
//
// return: a boolean value indicating status
//
// this method joins together all data in the input_names_d and
// input_offets_d Vectors into a single string (the string input
// format for input names).
//
boolean Component::joinName(String& out_a) const {

  out_a.clear();

  for (long i = 0; i < input_names_d.length(); i++) {

    if (i > 0) {
      out_a.concat(DELIM);
    }

    out_a.concat(input_names_d(i));

    if (input_offsets_d(i) != (long)0) {
      out_a.concat(L"(");
      out_a.concat(input_offsets_d(i));
      out_a.concat(L")");
    }
  }
  return true;
}
