// 
// except.cpp
// F.J. Alberti
// 27/04/2001
//
// (This file belongs to the SDK)
// 
// Modification history:
//
// FA   20/07/2001   Interface file except.h located in 'include' subdirectory
//                   Use _vsnprintf iff using Microsoft C
//                   Add constructor Exception::Exception(int32 code)
//

#include "include/except.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

//SCOL_BEGIN_NAMESPACE

// Modify this constant if you need a very long exception text
const uint kMaxExceptionTextLength = 256;


Exception::Exception(int32 code)
  : _code (code),
    _len  (0)
{
  _what = new char[1];
  _what[0] = '\0';
}


Exception::Exception(int32 code, const char* fmt, ...)
  : _code (code)
{
  static char buf[kMaxExceptionTextLength];

  va_list args;
  va_start(args, fmt);
#if defined(_MSC_VER)
  _vsnprintf(buf, kMaxExceptionTextLength, fmt, args);
#else
  vsprintf(buf, fmt, args);
#endif
  va_end(args);
  _len  = strlen(buf);
  _what = new char[_len+1];
  strcpy(_what, buf);
}


Exception::Exception(const Exception& exc)
  : _code(exc._code), 
    _len (exc._len)
{
  _what = new char[_len+1];
  strcpy(_what, exc._what);
}


Exception::~Exception()
{
  delete [] _what;
}


int32 Exception::code() const
{
  return _code;
}


const char* Exception::what() const
{
  return _what;
}


Exception& Exception::operator =(const Exception& exc)
{
  if (&exc != this) {
    _code = exc._code;
    delete [] _what;
    _what = new char[exc._len+1];
    strcpy(_what, exc._what);
  }
  return *this;
}

//SCOL_END_NAMESPACE