// StringDataTree.cpp version 1.18 created March 15, 2000 by Rick Wagner.
//
// Implementation file for StringTreeUtility.h
// Last modified April 9, 2000 by Rick Wagner.
// Copyright 2000 by Rick Wagner, all rights reserved.

#include <iostream.h>                               // For console IO.
#include <fstream.h>                                // For file IO.
#include <stdlib.h>                                 // For NULL.
#include <string.h>                                 // For strlen(), etc..
#include "StringTreeUtility.h"                      // For the class definitions.

// Person class implementation:

// Constructors:
Person::Person()
{
  _iAge = 0;
  _sfWeight = 0;
  _sFirstName = NULL;
  _sLastName = NULL;
  _sSSN = NULL;
}

Person::Person(int age, float weight, char* firstName, char* lastName, char* SSN)
{
  _iAge = age;
  _sfWeight = weight;
  _sFirstName = new char[strlen(firstName) + 1];
  strcpy(_sFirstName, firstName);
  _sLastName = new char[strlen(lastName) + 1];
  strcpy(_sLastName, lastName);
  _sSSN = new char[strlen(SSN) + 1];
  strcpy(_sSSN, SSN);
}

// Destructor:
Person::~Person()
{
  delete _sFirstName;
  delete _sLastName;
  delete _sSSN;
}

// Accessors:
int Person::getAge()
{
  return _iAge;
}

float Person::getWeight()
{
  return _sfWeight;
}

char* Person::getFirstName()
{
  return _sFirstName;
}

char* Person::getLastName()
{
  return _sLastName;
}

char* Person::getSSN()
{
  return _sSSN;
}

void Person::print()
{
  cout << setw(11) << _sSSN << " "
       << setw(17) << _sFirstName  << " "
       << setw(11) << _sLastName  << " "
       << setw(10) << _iAge  << " "
       << setw(10) << _sfWeight << endl;
}


// Rock class implementation:

Rock::Rock()
{
  _iID = 0;
  _sColor = NULL;
}

Rock::Rock(int iID, char* sColor)
{
  _iID = iID;
  _sColor = new char[strlen(sColor) + 1];
  strcpy(_sColor, sColor);
}

Rock::~Rock()
{
  delete [] _sColor;
}

int Rock::getID()
{
  return _iID;
}

char* Rock::getColor()
{
  return _sColor;
}

void Rock::print()
{
  cout << setw(6) << _iID << " " << _sColor << endl;
}

// * * General utility functions: * *

// Convert an integer to a C string:
void intToString(int iIn, char*& sOut)
{
  int i = 0;
  int iSize = 0;
  int iNegative = 1;
  char cDigit = 0;
  Stack<char> cStack;

  if (iIn >= 0)
  {
    iNegative = 0;
  }
  else
  {
    cout << "Negative input to intToString.";
  }

  if (iIn == 0)
  {
    sOut = new char[2];
    sOut[0] = '0';
    sOut[1] = '\0';
  }
  else
  {
    while (iIn != 0)
    {
      cDigit = iIn % 10 + 48;               // Shift by 48 to convert to ascii.
      cStack.push(cDigit);                  // Push the digit onto the stack.
      iIn /= 10;                            // Integer division.
    }
    iSize = cStack.size();
    if (iNegative)
    {
      sOut = new char[iSize + 2];
      sOut[0] = '-';
      for (i = 0; i < iSize; i++)
      {
        sOut[i + 1] = cStack.pop();
      }
      sOut[iSize + 1] = '\0';                // String terminator.
    }
    else
    {
      sOut = new char[iSize + 1];
      for (i = 0; i < iSize; i++)
      {
        sOut[i] = cStack.pop();
      }
      sOut[iSize] = '\0';                    // String terminator.
    }
  }
}
