"Fossies" - the Fresh Open Source Software Archive

Member "qdiff-0.9.1/texception.h" (21 Oct 2008, 4940 Bytes) of package /linux/privat/old/qdiff-0.9.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*GPL*START*
    2  * 
    3  * texception - basic exceptions
    4  * 
    5  * Copyright (C) 1998 by Johannes Overmann <Johannes.Overmann@gmx.de>
    6  * 
    7  * This program is free software; you can redistribute it and/or modify
    8  * it under the terms of the GNU General Public License as published by
    9  * the Free Software Foundation; either version 2 of the License, or
   10  * (at your option) any later version.
   11  * 
   12  * This program is distributed in the hope that it will be useful,
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  * GNU General Public License for more details.
   16  * 
   17  * You should have received a copy of the GNU General Public License
   18  * along with this program; if not, write to the Free Software
   19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   20  * *GPL*END*/  
   21 
   22 #ifndef _ngw_texception_h_
   23 #define _ngw_texception_h_
   24 
   25 #include "config.h"
   26 
   27 extern "C" {
   28 #include <stdio.h>
   29 #include <stdarg.h>
   30 #include <string.h>
   31 #include <errno.h>
   32 }
   33 
   34 // history:
   35 // 1999:
   36 // 17:02 04 Jun derived from terror.h
   37 // 2000:
   38 // 11:10 09 Jul tbaseexception and texception merged, message() and name() added
   39 // 00:50 09 Jul internal error added
   40 
   41 #define TExceptionN(n) public: virtual const char *name()  const { return #n; }
   42 #define TExceptionM(m) public: virtual const char *message() const { return m; }
   43 #define TExceptionM1(m,a) public: virtual const char *message() const { char *buf; asprintf(&buf, m, a); return buf; }
   44 #define TExceptionM2(m,a,b) public: virtual const char *message() const { char *buf; asprintf(&buf, m, a,b); return buf; }
   45 #define TExceptionM3(m,a,b,c) public: virtual const char *message() const { char *buf; asprintf(&buf, m, a,b,c); return buf; }
   46 #define TExceptionM4(m,a,b,c,d) public: virtual const char *message() const { char *buf; asprintf(&buf, m, a,b,c,d); return buf; }
   47 
   48 // base class of all exceptions 
   49 class TException {
   50    TExceptionN(TException);
   51    virtual ~TException() {}
   52    TExceptionM("(no message available)");
   53 #ifndef __USE_GNU
   54    static void asprintf(char **strp, const char *format, ...) {
   55       va_list ap;
   56       va_start(ap, format);
   57       *strp = new char[1024];
   58       vsprintf(*strp, format, ap);
   59       va_end(ap);
   60    }
   61 #endif
   62 #if !(defined HAVE_STRDUP)
   63    static char *strdup(const char *str) { char *buf; vasprintf(&buf, "%s", str); return buf; }
   64 #endif
   65 };
   66 
   67 
   68 // general exceptions, also base classes
   69 class TIndexOutOfRangeException: public TException {
   70    TExceptionN(TIndexOutOfRangeException);
   71    TIndexOutOfRangeException(int lower_, int index_, int upper_): 
   72    lower(lower_), index(index_), upper(upper_) {}
   73    TExceptionM3("index %d not in [%d..%d]", index, lower, upper);
   74    int lower, index, upper;
   75 };
   76 
   77 
   78 class TZeroBasedIndexOutOfRangeException: public TIndexOutOfRangeException {
   79    TExceptionN(TZeroBasedIndexOutOfRangeException);
   80    TZeroBasedIndexOutOfRangeException(int index_, int total_num_): TIndexOutOfRangeException(0, index_, total_num_-1) {}
   81 };
   82 
   83 
   84 class TErrnoException: public TException {
   85    TExceptionN(TErrnoException);
   86    TErrnoException(int error = -1): err(error) { if(err < 0) err = errno; }
   87    const char *str() const { if(err >= 0) return strerror(err); else return "(no error)"; }
   88    int err;
   89    TExceptionM2("%s (errno #%d)", str(), err);
   90 };
   91 
   92 
   93 class TOperationErrnoException: public TErrnoException {
   94    TExceptionN(TOperationErrnoException);
   95    TOperationErrnoException(const char *operation_, int error = -1): TErrnoException(error), operation(operation_) {}
   96    const char *operation;
   97    TExceptionM3("%s: %s (errno #%d)", operation, str(), err);
   98 };
   99 
  100 
  101 class TNotFoundException: public TException {
  102    TExceptionN(TNotFoundException);
  103 };
  104 
  105 
  106 class TFileOperationErrnoException: public TErrnoException {
  107    TExceptionN(TFileOperationErrnoException);
  108    TFileOperationErrnoException(const char *filename_, const char *operation_, int err_ = -1):
  109    TErrnoException(err_), filename(strdup(filename_)), operation(strdup(operation_)) {}
  110 //   virtual ~TFileOperationErrnoException() { free(filename); free(operation); }
  111    const char *filename;
  112    const char *operation;
  113    TExceptionM3("%s: %s (during %s)", filename, TErrnoException::message(), operation);
  114 //   TExceptionM2("%s: %s (during )", filename, operation);
  115 //   TExceptionM("toll");
  116 };
  117 
  118 
  119 class TInternalErrorException: public TException {
  120    TExceptionN(TInternalErrorException);
  121    TInternalErrorException(const char *error_ = "unspecific error"): error(strdup(error_)) {}
  122 //   ~TInternalErrorException() { free(error); }
  123    char *error;
  124    TExceptionM1("internal error: %s", error);
  125 };
  126 
  127 class TNotInitializedException: public TInternalErrorException {
  128    TExceptionN(TNotInitializedException);
  129    TNotInitializedException(const char *type_name_): type_name(type_name_) {}
  130    TExceptionM1("internal_error: object of type '%s' is not initialized", type_name ? type_name : "<unknown>");
  131    const char *type_name;
  132 };
  133 
  134 
  135 #endif /* texception.h */