Changeset 10791 in ntrip for trunk/BNC/newmat/myexcept.cpp


Ignore:
Timestamp:
Dec 3, 2025, 5:37:16 PM (4 weeks ago)
Author:
mervart
Message:

BNC Multifrequency and PPPAR Client (initial version)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BNC/newmat/myexcept.cpp

    r9434 r10791  
    2121
    2222#include "myexcept.h"                  // for exception handling
     23#include "newmat.h"                  // for exception handling
    2324
    2425#ifdef use_namespace
     
    5152
    5253
    53 unsigned long BaseException::Select;
    54 char* BaseException::what_error;
    55 int BaseException::SoFar;
    56 int BaseException::LastOne;
    57 
    58 BaseException::BaseException(const char* a_what)
    59 {
    60    Select++; SoFar = 0;
    61    if (!what_error)                   // make space for exception message
    62    {
    63       LastOne = 511;
    64       what_error = new char[512];
    65       if (!what_error)                // fail to make space
    66       {
    67          LastOne = 0;
    68          what_error = (char *)"No heap space for exception message\n";
    69       }
    70    }
    71    AddMessage("\n\nAn exception has been thrown\n");
    72    AddMessage(a_what);
    73    if (a_what) Tracer::AddTrace();
    74 }
    75 
    76 void BaseException::AddMessage(const char* a_what)
    77 {
    78    if (a_what)
    79    {
    80       int l = strlen(a_what); int r = LastOne - SoFar;
    81       if (l < r) { strcpy(what_error+SoFar, a_what); SoFar += l; }
    82       else if (r > 0)
    83       {
    84          strncpy(what_error+SoFar, a_what, r);
    85          what_error[LastOne] = 0;
    86          SoFar = LastOne;
    87       }
    88    }
    89 }
    90 
    91 void BaseException::AddInt(int value)
    92 {
    93    bool negative;
    94    if (value == 0) { AddMessage("0"); return; }
    95    else if (value < 0) { value = -value; negative = true; }
    96    else negative = false;
    97    int n = 0; int v = value;        // how many digits will we need?
    98    while (v > 0) { v /= 10; n++; }
    99    if (negative) n++;
    100    if (LastOne-SoFar < n) { AddMessage("***"); return; }
    101 
    102    SoFar += n; n = SoFar; what_error[n] = 0;
    103    while (value > 0)
    104    {
     54BaseException::BaseException(const char* a_what) {
     55  _what = new char[MAXSIZE];
     56  if (!_what) {                // fail to make space
     57    _what = (char *)"No heap space for exception message\n";
     58    _deleteMe = false;
     59  }
     60  else {
     61    _deleteMe = true;
     62  }
     63  AddMessage("\n\nAn exception has been thrown\n");
     64  AddMessage(a_what);
     65}
     66
     67BaseException::BaseException(const BaseException& oth) {
     68  _what = new char[MAXSIZE];
     69  if (!_what) {                // fail to make space
     70    _what = (char *)"No heap space for exception message\n";
     71    _deleteMe = false;
     72  }
     73  else {
     74    _deleteMe = true;
     75    strcpy(_what, oth._what);
     76  }
     77}
     78 
     79BaseException& BaseException::operator=(const BaseException& oth) {
     80  if (this == &oth) return *this;
     81  _what = new char[MAXSIZE];
     82  if (!_what) {                // fail to make space
     83    _what = (char *)"No heap space for exception message\n";
     84    _deleteMe = false;
     85  }
     86  else {
     87    _deleteMe = true;
     88    strcpy(_what, oth._what);
     89  }
     90  return *this;
     91}
     92 
     93BaseException::~BaseException() {
     94  if (_deleteMe) delete[] _what;
     95}
     96
     97void BaseException::AddMessage(const char* a_what) {
     98  if (a_what) {
     99    size_t nn = MAXSIZE - strlen(_what) - 1;
     100    strncat(_what, a_what, nn);
     101  }
     102}
     103
     104void BaseException::AddInt(int value) {
     105  bool negative = false;
     106   if (value == 0) {
     107     AddMessage("0");
     108     return;
     109   }
     110   else if (value < 0) {
     111     value = -value;
     112     negative = true;
     113   }
     114   int nn = (negative ? 1 : 0); // how many digits will we need?
     115   int vv = value;       
     116   while (vv > 0) { vv /= 10; ++nn; }
     117   if ((int)(MAXSIZE - strlen(_what)) < nn) {
     118     AddMessage("***");
     119     return;
     120   }
     121
     122   nn += strlen(_what);
     123   _what[nn] = 0;
     124   while (value > 0) {
    105125      int nv = value / 10; int rm = value - nv * 10;  value = nv;
    106       what_error[--n] = (char)(rm + '0');
    107    }
    108    if (negative) what_error[--n] = '-';
    109    return;
    110 }
    111 
    112 void Tracer::PrintTrace()
    113 {
    114    cout << "\n";
    115    for (Tracer* et = last; et; et=et->previous)
    116       cout << "  * " << et->entry << "\n";
    117 }
    118 
    119 void Tracer::AddTrace()
    120 {
    121    if (last)
    122    {
    123       BaseException::AddMessage("Trace: ");
    124       BaseException::AddMessage(last->entry);
    125       for (Tracer* et = last->previous; et; et=et->previous)
    126       {
    127          BaseException::AddMessage("; ");
    128          BaseException::AddMessage(et->entry);
    129       }
    130       BaseException::AddMessage(".\n");
    131    }
    132 }
     126      _what[--nn] = (char)(rm + '0');
     127   }
     128   if (negative) _what[--nn] = '-';
     129}
     130
     131 
     132void BaseException::MatrixDetails(const GeneralMatrix& A)
     133{
     134   MatrixBandWidth bw = A.bandwidth();
     135   int ubw = bw.upper_val; int lbw = bw.lower_val;
     136   AddMessage("MatrixType = ");
     137   AddMessage(A.Type().Value());
     138   AddMessage("  # Rows = "); BaseException::AddInt(A.Nrows());
     139   AddMessage("; # Cols = "); BaseException::AddInt(A.Ncols());
     140   if (lbw >=0)
     141   {
     142      AddMessage("; lower BW = ");
     143      AddInt(lbw);
     144   }
     145   if (ubw >=0)
     146   {
     147      AddMessage("; upper BW = ");
     148      AddInt(ubw);
     149   }
     150   AddMessage("\n");
     151}
     152
    133153
    134154#ifdef SimulateExceptions
     
    230250{
    231251   cout << "\n\nThere has been an exception with no handler - exiting";
    232    const char* what = BaseException::what();
    233    if (what) cout << what << "\n";
    234252   exit(1);
    235253}
     
    479497
    480498
     499unsigned long BaseException::Select;
    481500unsigned long Logic_error::Select;
    482501unsigned long Runtime_error::Select;
Note: See TracChangeset for help on using the changeset viewer.