/*****************************************************************************
Copyright 1997 MBARI                                                      
******************************************************************************
Summary  : String Class
Filename : String.C
Author   : Michael B. Matthews                                                
Project  : New ROV                                                        
Version  : 1.0                                                          
Created  : 20.2.97                                                      
Modified : 17.3.98                                                             
Archived :                                                              
Notes    : 
Updates  : 17.3.98 - added string compare operators
******************************************************************************/

#include "String.h"


/******************************************************************************
Function : String::String                                              
Purpose  : String constructor						      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String::String(Int n)
{
  length = n;
  str = new char[n+1];
  str[0] = '\0';
}



/******************************************************************************
Function : String::String                                              
Purpose  : String constructor						      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String::String(const String& s)
{
  length = s.length;
  str = new char[length+1];
  strcpy(str,s.str);
}



/******************************************************************************
Function : String::String                                              
Purpose  : String constructor						      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String::String(const char* s)
{
  length = strlen(s);
  str = new char[length+1];
  strcpy(str,s);
}



/******************************************************************************
Function : String::operator=                                              
Purpose  : String assignment operator				      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String& String::operator=(const String& s)
{
  if (&s == this) return *this;
  length = s.length;
  delete str;
  str = new char[length+1];
  strcpy(str,s.str);
  return *this;
}



/******************************************************************************
Function : String::operator=                                              
Purpose  : String assignment operator				      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String& String::operator=(const char* s)
{
  length = strlen(s);
  delete str;
  str = new char[length+1];
  strcpy(str,s);
  return *this;
}



/******************************************************************************
Function : String::operator=                                              
Purpose  : String assignment operator				      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String& String::operator=(const char s)
{
  length = 1;
  delete str;
  str = new char[length+1];
  str[0] = s;
  str[1] = '\0';
  return *this;
}



/******************************************************************************
Function : String::operator+                                              
Purpose  : String catenation operator				      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String String::operator+(const String& s)
{
  String p = *this;
  p += s;
  return p;
}



/******************************************************************************
Function : String::operator+                                              
Purpose  : String catenation operator				      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String String::operator+(const char* s)
{
  String p = *this;
  p += s;
  return p;
}



/******************************************************************************
Function : String::operator+                                              
Purpose  : String catenation operator				      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String String::operator+(const char s)
{
  String p = *this;
  p += s;
  return p;
}



/******************************************************************************
Function : String::operator+=                                              
Purpose  : String catenation operator				      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String& String::operator+=(const String& s)
{
  length += s.length;
  char* p = new char[length+1];
  strcpy(p,str);
  strcat(p,s.str);
  delete str;
  str = p;
  return *this;
}
  


/******************************************************************************
Function : String::operator+=                                              
Purpose  : String catenation operator				      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String& String::operator+=(const char* s)
{
  length += strlen(s);
  char* p = new char[length+1];
  strcpy(p,str);
  strcat(p,s);
  delete str;
  str = p;
  return *this;
}
  

/******************************************************************************
Function : String::operator+=                                              
Purpose  : String catenation operator				      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String& String::operator+=(const char s)
{
  length++;
  char* p = new char[length+1];
  strcpy(p,str);
  delete str;
  str = p;
  str[length-1] = s;
  str[length] = '\0';
  return *this;
}
  

/******************************************************************************
Function : friend operator+                                              
Purpose  : String catenation operator				      
Inputs   :                		                                      
Outputs  : 		                                                      
******************************************************************************/

String operator+(const char* s, const String& string)
{
  String p = string;	
  String r(s);
  r += p;
  return r;
}



