Qucs-core  0.0.19
exceptionstack.cpp
Go to the documentation of this file.
00001 /*
00002  * exceptionstack.cpp - exception stack class implementation
00003  *
00004  * Copyright (C) 2004 Stefan Jahn <stefan@lkcc.org>
00005  *
00006  * This is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2, or (at your option)
00009  * any later version.
00010  * 
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this package; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019  * Boston, MA 02110-1301, USA.  
00020  *
00021  * $Id$
00022  *
00023  */
00024 
00025 #if HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 
00033 #include "logging.h"
00034 #include "exception.h"
00035 #include "exceptionstack.h"
00036 
00037 using namespace qucs;
00038 
00039 // Global exception stack.
00040 exceptionstack qucs::estack;
00041 
00042 // Constructor creates an instance of the exception stack class.
00043 exceptionstack::exceptionstack () {
00044   root = NULL;
00045 }
00046 
00047 /* This copy constructor creates a instance of the exception stack
00048    class based on the given exception stack. */
00049 exceptionstack::exceptionstack (const exceptionstack & e) {
00050   exception * last, * prev = NULL;
00051   for (exception * next = e.root; next != NULL; next = next->getNext ()) {
00052     last = new exception (*next);
00053     if (prev != NULL)
00054       prev->setNext (last);
00055     else
00056       root = last;
00057     prev = last;
00058   }
00059 }
00060 
00061 // Destructor deletes an instance of the exception stack class.
00062 exceptionstack::~exceptionstack () {
00063   exception * next;
00064   while (root) {
00065     next = root->getNext ();
00066     delete root;
00067     root = next;
00068   }
00069 }
00070 
00071 // The function pushes a new exception onto the exception stack.
00072 void exceptionstack::push (exception * e) {
00073   e->setNext (root);
00074   root = e;
00075 }
00076 
00077 /* This function removes the top exception from the exception stack
00078    and returns the new top exception. */
00079 exception * exceptionstack::pop (void) {
00080   if (root != NULL) {
00081     exception * next = root->getNext ();
00082     delete root;
00083     root = next;
00084   }
00085   return root;
00086 }
00087 
00088 // The function returns the top exception.
00089 exception * exceptionstack::top (void) {
00090   return root;
00091 }
00092 
00093 /* This function prints the complete exception stack and removes each
00094    exception from the stack. */
00095 void exceptionstack::print (const char * prefix) {
00096   exception * next;
00097   if (root)
00098     logprint (LOG_ERROR, "%s%sexception stack\n",
00099               prefix ? prefix : "", prefix ? " " : "");
00100   while ((next = top ()) != NULL) {
00101     logprint (LOG_ERROR, "  %03d: %s\n", next->getCode (), next->getText ());
00102     pop ();
00103   }
00104 }