File: texception.h

package info (click to toggle)
mp3check 0.8.7-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 680 kB
  • sloc: cpp: 11,577; makefile: 181; sh: 3
file content (135 lines) | stat: -rw-r--r-- 5,132 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*GPL*START*
 * 
 * texception - basic exceptions
 * 
 * Copyright (C) 1998 by Johannes Overmann <Johannes.Overmann@gmx.de>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * *GPL*END*/  

#ifndef _ngw_texception_h_
#define _ngw_texception_h_

extern "C" {
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
}

// history:
// 1999:
// 17:02 04 Jun derived from terror.h
// 2000:
// 11:10 09 Jul tbaseexception and texception merged, message() and name() added
// 00:50 09 Jul internal error added

#define TExceptionN(n) public: virtual const char *name()  const { return #n; }
#define TExceptionM(m) public: virtual const char *message() const { return m; }
#define TExceptionM1(m,a) public: virtual const char *message() const { char *buf; int result = asprintf(&buf, m, a); return result != -1 ? buf : "asprintf failure"; }
#define TExceptionM2(m,a,b) public: virtual const char *message() const { char *buf; int result = asprintf(&buf, m, a,b); return result != -1 ? buf : "asprintf failure"; }
#define TExceptionM3(m,a,b,c) public: virtual const char *message() const { char *buf; int result = asprintf(&buf, m, a,b,c); return result != -1 ? buf : "asprintf failure"; }
#define TExceptionM4(m,a,b,c,d) public: virtual const char *message() const { char *buf; int result = asprintf(&buf, m, a,b,c,d); return result != -1 ? buf : "asprintf failure"; }

// base class of all exceptions 
class TException {
   TExceptionN(TException);
   virtual ~TException() {}
   TExceptionM("(no message available)");
#if 0    
#ifndef __USE_GNU
   static void asprintf(char **strp, const char *format, ...) {
      va_list ap;
      va_start(ap, format);
      *strp = new char[1024];
      vsprintf(*strp, format, ap);
      va_end(ap);
   }
#endif
#if !(defined HAVE_STRDUP)
   static char *strdup(const char *str) { char *buf; vasprintf(&buf, "%s", str); return buf; }
#endif
#endif
};


// general exceptions, also base classes
class TIndexOutOfRangeException: public TException {
   TExceptionN(TIndexOutOfRangeException);
   TIndexOutOfRangeException(int lower_, int index_, int upper_): 
   lower(lower_), index(index_), upper(upper_) {}
   TExceptionM3("index %d not in [%d..%d]", index, lower, upper);
   int lower, index, upper;
};


class TZeroBasedIndexOutOfRangeException: public TIndexOutOfRangeException {
   TExceptionN(TZeroBasedIndexOutOfRangeException);
   TZeroBasedIndexOutOfRangeException(int index_, int total_num_): TIndexOutOfRangeException(0, index_, total_num_-1) {}
};


class TErrnoException: public TException {
   TExceptionN(TErrnoException);
   TErrnoException(int error = -1): err(error) { if(err < 0) err = errno; }
   const char *str() const { if(err >= 0) return strerror(err); else return "(no error)"; }
   int err;
   TExceptionM2("%s (errno #%d)", str(), err);
};


class TOperationErrnoException: public TErrnoException {
   TExceptionN(TOperationErrnoException);
   TOperationErrnoException(const char *operation_, int error = -1): TErrnoException(error), operation(operation_) {}
   const char *operation;
   TExceptionM3("%s: %s (errno #%d)", operation, str(), err);
};


class TNotFoundException: public TException {
   TExceptionN(TNotFoundException);
};


class TFileOperationErrnoException: public TErrnoException {
   TExceptionN(TFileOperationErrnoException);
   TFileOperationErrnoException(const char *filename_, const char *operation_, int err_ = -1):
   TErrnoException(err_), filename(strdup(filename_)), operation(strdup(operation_)) {}
//   virtual ~TFileOperationErrnoException() { free(filename); free(operation); }
   const char *filename;
   const char *operation;
   TExceptionM3("%s: %s (during %s)", filename, TErrnoException::message(), operation);
//   TExceptionM2("%s: %s (during )", filename, operation);
//   TExceptionM("toll");
};


class TInternalErrorException: public TException {
   TExceptionN(TInternalErrorException);
   TInternalErrorException(const char *error_ = "unspecific error"): error(strdup(error_)) {}
//   ~TInternalErrorException() { free(error); }
   char *error;
   TExceptionM1("internal error: %s", error);
};

class TNotInitializedException: public TInternalErrorException {
   TExceptionN(TNotInitializedException);
   TNotInitializedException(const char *type_name_): type_name(type_name_) {}
   TExceptionM1("internal_error: object of type '%s' is not initialized", type_name ? type_name : "<unknown>");
   const char *type_name;
};


#endif /* texception.h */