summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/pwmexception.h
blob: 7f5a3a691d65befd0e40cf8e4d3a7ddb949cf499 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/***************************************************************************
 *                                                                         *
 *   copyright (C) 2003, 2004 by Michael Buesch                            *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
 * copyright (C) 2004 by Ulf Schenk
 * This file is originaly based on version 1.0.1 of pwmanager
 * and was modified to run on embedded devices that run microkde
 *
 * $Id$
 **************************************************************************/  

#ifndef __PWMEXCEPTION_H
#define __PWMEXCEPTION_H

//#include "globalstuff.h"

#include <iostream>
#include <string>
using std::string;
using std::cerr;
using std::cout;
using std::endl;

/* This is an internal function to reduce code-overhead
 * of the BUG(), WARN(), TOD0() and FiXME() macros. Please use
 * these macros instead of calling this function directly.
 */
void pwmFatal(const char *id,
	      const char *file,
	      int line);

/** Use PWM_ASSERT(condition) for debugging assertions.
  * "condition" is eaten up and replaced with a NOP
  * when debugging is disabled.
  *
  * PWM_ASSERT_NOEAT(condition) is the same as PWM_ASSERT(condition),
  * but it does _not_ eat up "condition" and ensures that
  * condition is always evaluated.
  */
#ifdef PWM_ASSERT
# undef PWM_ASSERT
#endif
#ifdef PWM_ASSERT_NOEAT
# undef PWM_ASSERT_NOEAT
#endif
#ifdef PWM_DEBUG
# define PWM_ASSERT(x)	do {							\
				if (unlikely(!(x))) {				\
					cerr << "PWM_ASSERT failed: (" << #x	\
					     << ") in " << __FILE__		\
					     << ":" << __LINE__			\
					     << endl;				\
				}						\
			} while (0)
# define PWM_ASSERT_NOEAT(x)	do { PWM_ASSERT(x); } while (0)
#else // PWM_DEBUG
# define PWM_ASSERT(x)		do { } while (0)
# define PWM_ASSERT_NOEAT(x)	do { if (x) ; } while (0)
#endif // PWM_DEBUG

/** Insert a BUG() into code paths which clearly show
  * a bug in the code and which should, under normal
  * circumstances, never execute.
  */
#ifdef BUG
# undef BUG
#endif
#define BUG()	do { pwmFatal("BUG", __FILE__, __LINE__); } while (0)

/** Use BUG_ON(condition) to print a bug-message if "condition"
  * is true. This is also enabled in non-debugging code.
  */
#ifdef BUG_ON
# undef BUG_ON
#endif
#define BUG_ON(x)	do { if (unlikely(x))	BUG(); } while (0)

/** Insert a WARN() into code-paths which should not
  * execute normally, but if they do it's non-fatal.
  */
#ifdef WARN
# undef WARN
#endif
#define WARN()	do { pwmFatal("badness", __FILE__, __LINE__); } while (0)

/** Same as BUG_ON() but prints a warning-message */
#ifdef WARN_ON
# undef WARN_ON
#endif
#define WARN_ON(x)	do { if (unlikely(x))	WARN(); } while (0)

/** Insert this into code which is incomplete */
#ifdef TODO
# undef TODO
#endif
#define TODO()	do { pwmFatal("TODO", __FILE__, __LINE__); } while (0)

/** Insert this into code which likely contains bugs */
#ifdef FIXME
# undef FIXME
#endif
#define FIXME()	do { pwmFatal("FIXME", __FILE__, __LINE__); } while (0)


/** PwM error codes */
enum PwMerror {
	e_success = 0,

	// file access errors
	e_filename,
	e_readFile,
	e_writeFile,
	e_openFile,
	e_accessFile, // permission error, etc...
	e_fileGeneric,
	e_alreadyOpen,

	// other file errors
	e_fileVer,
	e_fileFormat,		// format error
	e_unsupportedFormat,	// completely unsupported format
	e_setFilePointer,
	e_fileBackup,
	e_fileCorrupt,		// file data has correct format,
				// but is corrupt (checksum error, etc)

	// password errors
	e_wrongPw,
	e_getPw,
	e_weakPw,
	e_noPw,

	// action not implemented errors
	e_hashNotImpl,
	e_cryptNotImpl,

	// argument/parameter errors
	e_incompleteArg,
	e_invalidArg,

	// misc
	e_writeHeader,
	e_serializeDta,
	e_enc,
	e_entryExists,
	e_categoryExists,
	e_maxAllowedEntr,	// no more entries can be added.
	e_outOfMem,
	e_lock,			// error while (un)locking
	e_docNotSaved,		// doc wasn't saved to a file, yet.
	e_docIsEmpty,
	e_binEntry,
	e_normalEntry,
	e_syncError,

	e_generic
};

/** can be used for general exception faults */
class PwMException
{
public:
	enum exceptionId
	{
		EX_GENERIC = 0,
		EX_OPEN,
		EX_CLOSE,
		EX_READ,
		EX_WRITE,
		EX_LOAD_MODULE,
		EX_PARSE
	};

public:
	PwMException(exceptionId id = EX_GENERIC,
		     const char *message = "")
		{
			exId = id;
			exMsg = message;
		}

	exceptionId getId()
			{ return exId; }
	const char* getMessage()
			{ return exMsg; }

protected:
	/** ID of this exception */
	exceptionId exId;
	/** additional error-message for this exception */
	const char *exMsg;
};

void __printInfo(const string &msg);
void __printWarn(const string &msg);
void __printError(const string &msg);

#ifdef PWM_DEBUG
  void __printDebug(const string &msg);
# define printDebug(x)	__printDebug(x)
#else
# define printDebug(x)	do { } while (0)
#endif

#define printInfo(x)	__printInfo(x)
#define printWarn(x)	__printWarn(x)
#define printError(x)	__printError(x)

#include "globalstuff.h"
#endif // __PWMEXCEPTION_H