Diffstat (limited to 'pwmanager/pwmanager/pwmexception.h') (more/less context) (show whitespace changes)
-rw-r--r-- | pwmanager/pwmanager/pwmexception.h | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/pwmexception.h b/pwmanager/pwmanager/pwmexception.h new file mode 100644 index 0000000..c8a8c0f --- a/dev/null +++ b/pwmanager/pwmanager/pwmexception.h | |||
@@ -0,0 +1,216 @@ | |||
1 | /*************************************************************************** | ||
2 | * * | ||
3 | * copyright (C) 2003, 2004 by Michael Buesch * | ||
4 | * email: mbuesch@freenet.de * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License version 2 * | ||
8 | * as published by the Free Software Foundation. * | ||
9 | * * | ||
10 | ***************************************************************************/ | ||
11 | |||
12 | /*************************************************************************** | ||
13 | * copyright (C) 2004 by Ulf Schenk | ||
14 | * This file is originaly based on version 1.0.1 of pwmanager | ||
15 | * and was modified to run on embedded devices that run microkde | ||
16 | * | ||
17 | * $Id$ | ||
18 | **************************************************************************/ | ||
19 | |||
20 | #ifndef __PWMEXCEPTION_H | ||
21 | #define __PWMEXCEPTION_H | ||
22 | |||
23 | #include "globalstuff.h" | ||
24 | |||
25 | #include <iostream> | ||
26 | #include <string> | ||
27 | using std::string; | ||
28 | using std::cerr; | ||
29 | using std::cout; | ||
30 | using std::endl; | ||
31 | |||
32 | /* This is an internal function to reduce code-overhead | ||
33 | * of the BUG(), WARN(), TOD0() and FiXME() macros. Please use | ||
34 | * these macros instead of calling this function directly. | ||
35 | */ | ||
36 | void pwmFatal(const char *id, | ||
37 | const char *file, | ||
38 | int line); | ||
39 | |||
40 | /** Use PWM_ASSERT(condition) for debugging assertions. | ||
41 | * "condition" is eaten up and replaced with a NOP | ||
42 | * when debugging is disabled. | ||
43 | * | ||
44 | * PWM_ASSERT_NOEAT(condition) is the same as PWM_ASSERT(condition), | ||
45 | * but it does _not_ eat up "condition" and ensures that | ||
46 | * condition is always evaluated. | ||
47 | */ | ||
48 | #ifdef PWM_ASSERT | ||
49 | # undef PWM_ASSERT | ||
50 | #endif | ||
51 | #ifdef PWM_ASSERT_NOEAT | ||
52 | # undef PWM_ASSERT_NOEAT | ||
53 | #endif | ||
54 | #ifdef PWM_DEBUG | ||
55 | # define PWM_ASSERT(x) do { \ | ||
56 | if (unlikely(!(x))) { \ | ||
57 | cerr << "PWM_ASSERT failed: (" << #x\ | ||
58 | << ") in " << __FILE__ \ | ||
59 | << ":" << __LINE__ \ | ||
60 | << endl; \ | ||
61 | } \ | ||
62 | } while (0) | ||
63 | # define PWM_ASSERT_NOEAT(x)do { PWM_ASSERT(x); } while (0) | ||
64 | #else // PWM_DEBUG | ||
65 | # define PWM_ASSERT(x) do { } while (0) | ||
66 | # define PWM_ASSERT_NOEAT(x)do { if (x) ; } while (0) | ||
67 | #endif // PWM_DEBUG | ||
68 | |||
69 | /** Insert a BUG() into code paths which clearly show | ||
70 | * a bug in the code and which should, under normal | ||
71 | * circumstances, never execute. | ||
72 | */ | ||
73 | #ifdef BUG | ||
74 | # undef BUG | ||
75 | #endif | ||
76 | #define BUG()do { pwmFatal("BUG", __FILE__, __LINE__); } while (0) | ||
77 | |||
78 | /** Use BUG_ON(condition) to print a bug-message if "condition" | ||
79 | * is true. This is also enabled in non-debugging code. | ||
80 | */ | ||
81 | #ifdef BUG_ON | ||
82 | # undef BUG_ON | ||
83 | #endif | ||
84 | #define BUG_ON(x) do { if (unlikely(x))BUG(); } while (0) | ||
85 | |||
86 | /** Insert a WARN() into code-paths which should not | ||
87 | * execute normally, but if they do it's non-fatal. | ||
88 | */ | ||
89 | #ifdef WARN | ||
90 | # undef WARN | ||
91 | #endif | ||
92 | #define WARN()do { pwmFatal("badness", __FILE__, __LINE__); } while (0) | ||
93 | |||
94 | /** Same as BUG_ON() but prints a warning-message */ | ||
95 | #ifdef WARN_ON | ||
96 | # undef WARN_ON | ||
97 | #endif | ||
98 | #define WARN_ON(x) do { if (unlikely(x))WARN(); } while (0) | ||
99 | |||
100 | /** Insert this into code which is incomplete */ | ||
101 | #ifdef TODO | ||
102 | # undef TODO | ||
103 | #endif | ||
104 | #define TODO()do { pwmFatal("TODO", __FILE__, __LINE__); } while (0) | ||
105 | |||
106 | /** Insert this into code which likely contains bugs */ | ||
107 | #ifdef FIXME | ||
108 | # undef FIXME | ||
109 | #endif | ||
110 | #define FIXME()do { pwmFatal("FIXME", __FILE__, __LINE__); } while (0) | ||
111 | |||
112 | |||
113 | /** PwM error codes */ | ||
114 | enum PwMerror { | ||
115 | e_success = 0, | ||
116 | |||
117 | // file access errors | ||
118 | e_filename, | ||
119 | e_readFile, | ||
120 | e_writeFile, | ||
121 | e_openFile, | ||
122 | e_accessFile, // permission error, etc... | ||
123 | e_fileGeneric, | ||
124 | e_alreadyOpen, | ||
125 | |||
126 | // other file errors | ||
127 | e_fileVer, | ||
128 | e_fileFormat, // format error | ||
129 | e_unsupportedFormat,// completely unsupported format | ||
130 | e_setFilePointer, | ||
131 | e_fileBackup, | ||
132 | e_fileCorrupt, // file data has correct format, | ||
133 | // but is corrupt (checksum error, etc) | ||
134 | |||
135 | // password errors | ||
136 | e_wrongPw, | ||
137 | e_getPw, | ||
138 | e_weakPw, | ||
139 | e_noPw, | ||
140 | |||
141 | // action not implemented errors | ||
142 | e_hashNotImpl, | ||
143 | e_cryptNotImpl, | ||
144 | |||
145 | // argument/parameter errors | ||
146 | e_incompleteArg, | ||
147 | e_invalidArg, | ||
148 | |||
149 | // misc | ||
150 | e_writeHeader, | ||
151 | e_serializeDta, | ||
152 | e_enc, | ||
153 | e_entryExists, | ||
154 | e_categoryExists, | ||
155 | e_maxAllowedEntr,// no more entries can be added. | ||
156 | e_outOfMem, | ||
157 | e_lock, // error while (un)locking | ||
158 | e_docNotSaved, // doc wasn't saved to a file, yet. | ||
159 | e_docIsEmpty, | ||
160 | e_binEntry, | ||
161 | e_normalEntry, | ||
162 | |||
163 | e_generic | ||
164 | }; | ||
165 | |||
166 | /** can be used for general exception faults */ | ||
167 | class PwMException | ||
168 | { | ||
169 | public: | ||
170 | enum exceptionId | ||
171 | { | ||
172 | EX_GENERIC = 0, | ||
173 | EX_OPEN, | ||
174 | EX_CLOSE, | ||
175 | EX_READ, | ||
176 | EX_WRITE, | ||
177 | EX_LOAD_MODULE, | ||
178 | EX_PARSE | ||
179 | }; | ||
180 | |||
181 | public: | ||
182 | PwMException(exceptionId id = EX_GENERIC, | ||
183 | const char *message = "") | ||
184 | { | ||
185 | exId = id; | ||
186 | exMsg = message; | ||
187 | } | ||
188 | |||
189 | exceptionId getId() | ||
190 | { return exId; } | ||
191 | const char* getMessage() | ||
192 | { return exMsg; } | ||
193 | |||
194 | protected: | ||
195 | /** ID of this exception */ | ||
196 | exceptionId exId; | ||
197 | /** additional error-message for this exception */ | ||
198 | const char *exMsg; | ||
199 | }; | ||
200 | |||
201 | void __printInfo(const string &msg); | ||
202 | void __printWarn(const string &msg); | ||
203 | void __printError(const string &msg); | ||
204 | |||
205 | #ifdef PWM_DEBUG | ||
206 | void __printDebug(const string &msg); | ||
207 | # define printDebug(x)__printDebug(x) | ||
208 | #else | ||
209 | # define printDebug(x)do { } while (0) | ||
210 | #endif | ||
211 | |||
212 | #define printInfo(x)__printInfo(x) | ||
213 | #define printWarn(x)__printWarn(x) | ||
214 | #define printError(x)__printError(x) | ||
215 | |||
216 | #endif // __PWMEXCEPTION_H | ||