C:/programs/SCPPNT/src/include/scppnt_error.cpp

00001 /*! \file sccpnt_error.cpp
00002  \brief Member function definitions for classes used to signal errors.
00003  
00004  Definitions of member functions for classes used in throwing
00005  exceptions indicating various types of run-time errors.
00006  */
00007 
00008 /*
00009 
00010  Simple C++ Numerical Toolkit (SCPPNT)
00011  http://www.smallwaters.com/software/cpp/scppnt.html
00012  This release updates original work contributed by 
00013  Brad Hanson (http://www.b-a-h.com/) in 2001.
00014 
00015  */
00016 
00017 #ifdef SCPPNT_NO_DIR_PREFIX
00018 #include "scppnt_error.h"
00019 #else
00020 #include "scppnt/scppnt_error.h"
00021 #endif
00022 
00023 #include <cstring> // for strlen and strncat
00024 #ifdef BOOST_NO_STDC_NAMESPACE
00025 namespace std
00026 { using ::strlen; using ::strncat;}
00027 #endif
00028 
00029 namespace SCPPNT
00030 {
00031   Exception::Exception()
00032   {
00033     message[0] = 0;
00034   }
00035 
00036   Exception::~Exception()
00037   {
00038   }
00039 
00040   /*
00041    Concatenate s to the end of the message string
00042    if there is sufficient space left.
00043    */
00044   void Exception::AppendToMessage(const char *s)
00045   {
00046     if (s == 0)
00047       return; // return if pointer is null
00048 
00049     int len = std::strlen(s);
00050     int currlen = std::strlen(message);
00051     int freechar = MaxChar() - currlen;
00052 
00053     if (freechar <= 0 || len == 0)
00054       return;
00055 
00056     if (len > freechar)
00057       len = freechar;
00058 
00059     std::strncat(message, s, len);
00060 
00061     message[len+currlen] = 0; // add terminating null character
00062   }
00063 
00064   LogicError::LogicError(const char *type, const char *mess, const char *func)
00065   {
00066 
00067     /* Create message explaining error */
00068 
00069     AppendToMessage(type);
00070 
00071     if (mess)
00072     {
00073       AppendToMessage(mess);
00074       AppendToMessage("\n");
00075     }
00076 
00077     if (func)
00078     {
00079       AppendToMessage("in function ");
00080       AppendToMessage(func);
00081       AppendToMessage("\n");
00082     }
00083   }
00084 
00085   LogicError::~LogicError()
00086   {
00087   }
00088 
00089   InvalidArgument::InvalidArgument(const char *mess, const char *func) :
00090     LogicError("Invalid argument: ", mess, func)
00091   {
00092   }
00093 
00094   RuntimeError::RuntimeError(const char *mess, const char *func)
00095   {
00096 
00097     /* Create message explaining error */
00098     if (mess)
00099     {
00100       AppendToMessage(mess);
00101       AppendToMessage("\n");
00102     }
00103 
00104     if (func)
00105     {
00106       AppendToMessage("in function ");
00107       AppendToMessage(func);
00108       AppendToMessage("\n");
00109     }
00110   }
00111 
00112   RuntimeError::~RuntimeError()
00113   {
00114   }
00115 
00116   BoundsError::BoundsError(const char *func) :
00117     RuntimeError("Attempted to access element outside valid bounds", func)
00118   {
00119   }
00120 
00121   BadDimension::BadDimension(const char *func) :
00122     RuntimeError("Argument has invalid dimension", func)
00123   {
00124   }
00125 
00126 } // namespace SCPPNT

Generated on Tue Dec 18 23:34:06 2007 for SCPPNT by  doxygen 1.5.4